Skip to content

Commit

Permalink
fix: route onClick, ... event handlers through uikit so uikit is awar…
Browse files Browse the repository at this point in the history
…e of them and can compute the ancestorsHaveListeners flag correctly
  • Loading branch information
bbohlender committed Nov 5, 2024
1 parent 193b9ce commit b5fe6bd
Show file tree
Hide file tree
Showing 82 changed files with 986 additions and 926 deletions.
2 changes: 1 addition & 1 deletion docs/getting-started/components-and-properties.md
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,7 @@ All Components support [all R3F event handlers](https://docs.pmnd.rs/react-three

## Ref

Each component exposes the `ComponentInternals` when using a `ref`. The component internals provide you with access to
Each component exposes internal information when using a `ref`. For instance, the container component exposes internals of the type `ContainerRef`. The component internals provide you with access to

| Property | Explanation |
| ------------------- | --------------------------------------------------------------------------------------------------------------- |
Expand Down
5 changes: 2 additions & 3 deletions examples/uikit/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,10 @@ import {
SuspendingImage,
Input,
FontFamilyProvider,
ComponentInternals,
ImageProperties,
Video,
useMeasureText,
InputInternals,
ImageRef,
} from '@react-three/uikit'
import { Texture } from 'three'
import { Skeleton } from '../../../packages/kits/default/src/skeleton.js'
Expand All @@ -31,7 +30,7 @@ export default function App() {
const s = useMemo(() => signal(5), [])
const x = useMemo(() => signal<string | undefined>('red'), [])
const t = useMemo(() => signal('X X\nX X'), [])
const ref = useRef<ComponentInternals<ImageProperties>>(null)
const ref = useRef<ImageRef>(null)
const [input, setInput] = useState<InputInternals | null>(null)
const videoRef = useRef<HTMLVideoElement | undefined>()
const [videoel, setVideoEl] = useState<HTMLVideoElement | undefined>()
Expand Down
2 changes: 1 addition & 1 deletion examples/vanilla/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ setTimeout(() => {
const c = new Content({ flexShrink: 0, height: 100, backgroundColor: 'black' })
const loader = new GLTFLoader()
loader.load('example.glb', (gltf) => c.add(gltf.scene))
const del = new Delete({ width: 100, flexShrink: 0 })
const del = new Delete({ onClick: () => {}, width: 100, flexShrink: 0 })
const svg = new Svg({ src: 'example.svg', width: 100, height: 100, flexShrink: 0 })
const text = new Text('Hello World', { fontSize: 40, flexShrink: 0 })
const a = new Container({ flexShrink: 0, alignSelf: 'stretch', flexGrow: 1, backgroundColor: 'blue' })
Expand Down
4 changes: 2 additions & 2 deletions packages/icons/lucide/react/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ async function main() {
const svg = raw.toString()
const code = `
/* eslint-disable no-shadow-restricted-names */
import { Icon, ComponentInternals, IconProperties } from "@react-three/uikit";
import { Icon, IconRef, IconProperties } from "@react-three/uikit";
import { forwardRef } from "react";
const text = \`${svg}\`;
export const ${name}Icon = /*@__PURE__*/ forwardRef<ComponentInternals<IconProperties>, Omit<IconProperties, "text" | "svgWidth" | "svgHeight">>((props, ref) => {
export const ${name}Icon = /*@__PURE__*/ forwardRef<IconRef, Omit<IconProperties, "text" | "svgWidth" | "svgHeight">>((props, ref) => {
return <Icon {...props} ref={ref} text={text} svgWidth={24} svgHeight={24} />
})
export const ${name} = ${name}Icon
Expand Down
4 changes: 2 additions & 2 deletions packages/kits/apfel/src/button.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ComponentInternals, Container, ContainerProperties, DefaultProperties } from '@react-three/uikit'
import { Container, ContainerProperties, ContainerRef, DefaultProperties } from '@react-three/uikit'
import React, { ReactNode, RefAttributes, forwardRef } from 'react'
import { colors } from './theme.js'

Expand Down Expand Up @@ -61,7 +61,7 @@ export type ButtonProperties = ContainerProperties & {
disabled?: boolean
}

export const Button: (props: ButtonProperties & RefAttributes<ComponentInternals>) => ReactNode = forwardRef(
export const Button: (props: ButtonProperties & RefAttributes<ContainerRef>) => ReactNode = forwardRef(
({ children, size = 'md', variant = 'rect', platter, selected, disabled, ...props }, ref) => {
const { borderRadius, fontSize, height, padding, iconSize } =
typeof size === 'number' ? getAribtrarySize(size) : sizes[size]
Expand Down
4 changes: 2 additions & 2 deletions packages/kits/apfel/src/card.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { ComponentInternals, Container, ContainerProperties, DefaultProperties } from '@react-three/uikit'
import { ContainerRef, Container, ContainerProperties, DefaultProperties } from '@react-three/uikit'
import React, { ReactNode, RefAttributes, forwardRef } from 'react'
import { GlassMaterial, colors } from './theme.js'

export type CardProperties = ContainerProperties

export const Card: (props: CardProperties & RefAttributes<ComponentInternals>) => ReactNode = forwardRef(
export const Card: (props: CardProperties & RefAttributes<ContainerRef>) => ReactNode = forwardRef(
({ children, ...props }, ref) => {
return (
<Container
Expand Down
4 changes: 2 additions & 2 deletions packages/kits/apfel/src/checkbox.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ComponentInternals, Container, ContainerProperties } from '@react-three/uikit'
import { ContainerRef, Container, ContainerProperties } from '@react-three/uikit'
import { Check } from '@react-three/uikit-lucide'
import React, { ReactNode, RefAttributes, forwardRef, useState } from 'react'
import { colors } from './theme.js'
Expand All @@ -10,7 +10,7 @@ type CheckboxProperties = ContainerProperties & {
onSelectedChange?(value: boolean): void
}

export const Checkbox: (props: CheckboxProperties & RefAttributes<ComponentInternals>) => ReactNode = forwardRef(
export const Checkbox: (props: CheckboxProperties & RefAttributes<ContainerRef>) => ReactNode = forwardRef(
({ selected, disabled = false, defaultSelected, onSelectedChange, ...props }, ref) => {
const [internalValue, setInternalValue] = useState(defaultSelected ?? false)
const value = selected != null ? selected : internalValue
Expand Down
4 changes: 2 additions & 2 deletions packages/kits/apfel/src/input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
Text,
InputInternals,
InputProperties as BaseInputProperties,
ComponentInternals,
ContainerRef,
} from '@react-three/uikit'
import React, { ReactNode, RefAttributes, forwardRef, useMemo, useState } from 'react'

Expand All @@ -16,7 +16,7 @@ export type InputProperties = BaseInputProperties & { placeholder?: string; vari

//TODO: increase hitbox size (by increasing the size of the InputImpl)

export const Input: (props: InputProperties & RefAttributes<ComponentInternals>) => ReactNode = forwardRef(
export const Input: (props: InputProperties & RefAttributes<ContainerRef>) => ReactNode = forwardRef(
(
{
variant = 'rect',
Expand Down
6 changes: 3 additions & 3 deletions packages/kits/apfel/src/list.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ComponentInternals, Container, ContainerProperties, DefaultProperties } from '@react-three/uikit'
import { ContainerRef, Container, ContainerProperties, DefaultProperties } from '@react-three/uikit'
import React, { ReactNode, RefAttributes, createContext, forwardRef, useContext } from 'react'
import { colors } from './theme.js'

Expand All @@ -10,7 +10,7 @@ type ListProperties = ContainerProperties & {
type?: Type
}

export const List: (props: ListProperties & RefAttributes<ComponentInternals>) => ReactNode = forwardRef(
export const List: (props: ListProperties & RefAttributes<ContainerRef>) => ReactNode = forwardRef(
({ type = 'plain', ...props }, ref) => {
return (
<ListContext.Provider value={type}>
Expand All @@ -29,7 +29,7 @@ export type ListItemProperties = ContainerProperties & {
isLast?: boolean
}

export const ListItem: (props: ListItemProperties & RefAttributes<ComponentInternals>) => ReactNode = forwardRef(
export const ListItem: (props: ListItemProperties & RefAttributes<ContainerRef>) => ReactNode = forwardRef(
({ children, subtitle, selected, leadingAccessory, trailingAccessory, isFirst, isLast, ...props }, ref) => {
const type = useContext(ListContext)

Expand Down
4 changes: 2 additions & 2 deletions packages/kits/apfel/src/loading.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ComponentInternals, Container, ContainerProperties } from '@react-three/uikit'
import { ContainerRef, Container, ContainerProperties } from '@react-three/uikit'
import { useFrame } from '@react-three/fiber'
import React, { ReactNode, RefAttributes, forwardRef, useMemo } from 'react'
import { signal } from '@preact/signals-core'
Expand All @@ -18,7 +18,7 @@ export type LoadingProperties = ContainerProperties & {
size?: Size
}

export const Loading: (props: LoadingProperties & RefAttributes<ComponentInternals>) => ReactNode = forwardRef(
export const Loading: (props: LoadingProperties & RefAttributes<ContainerRef>) => ReactNode = forwardRef(
({ size = 'md', ...props }, ref) => {
const pillOpacities = useMemo(() => new Array(PILL_AMOUNT).fill(undefined).map(() => signal(0)), [])

Expand Down
4 changes: 2 additions & 2 deletions packages/kits/apfel/src/progress.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { ComponentInternals, Container, ContainerProperties } from '@react-three/uikit'
import { ContainerRef, Container, ContainerProperties } from '@react-three/uikit'
import React, { ReactNode, RefAttributes, forwardRef } from 'react'
import { colors } from './theme.js'

export type ProgressProperties = ContainerProperties & {
value?: number
}

export const Progress: (props: ProgressProperties & RefAttributes<ComponentInternals>) => ReactNode = forwardRef(
export const Progress: (props: ProgressProperties & RefAttributes<ContainerRef>) => ReactNode = forwardRef(
({ value = 0, ...props }, ref) => {
return (
<Container
Expand Down
6 changes: 3 additions & 3 deletions packages/kits/apfel/src/slider.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ThreeEvent } from '@react-three/fiber'
import type { EventHandlers } from '@react-three/fiber/dist/declarations/src/core/events'
import { ComponentInternals, Container, ContainerProperties, DefaultProperties } from '@react-three/uikit'
import { Container, ContainerProperties, ContainerRef, DefaultProperties } from '@react-three/uikit'
import React, { ReactNode, RefAttributes, forwardRef, useImperativeHandle, useMemo, useRef, useState } from 'react'
import { Vector3 } from 'three'
import { clamp } from 'three/src/math/MathUtils.js'
Expand Down Expand Up @@ -29,7 +29,7 @@ export type SliderProperties = ContainerProperties & {
icon?: ReactNode
}

export const Slider: (props: SliderProperties & RefAttributes<ComponentInternals>) => ReactNode = forwardRef(
export const Slider: (props: SliderProperties & RefAttributes<ContainerRef>) => ReactNode = forwardRef(
(
{
value: providedValue,
Expand All @@ -47,7 +47,7 @@ export const Slider: (props: SliderProperties & RefAttributes<ComponentInternals
) => {
const [uncontrolled, setUncontrolled] = useState(defaultValue)
const value = providedValue ?? uncontrolled ?? 50
const internalRef = useRef<ComponentInternals<ContainerProperties>>(null)
const internalRef = useRef<ContainerRef>(null)
const onChange = useRef(onValueChange)
onChange.current = onValueChange
const hasProvidedValue = providedValue != null
Expand Down
6 changes: 3 additions & 3 deletions packages/kits/apfel/src/tab-bar.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ComponentInternals, Container, ContainerProperties, DefaultProperties } from '@react-three/uikit'
import { ContainerRef, Container, ContainerProperties, DefaultProperties } from '@react-three/uikit'
import React, {
ReactNode,
RefAttributes,
Expand Down Expand Up @@ -28,7 +28,7 @@ export type TabBarProperties = ContainerProperties & {
onValueChange?(value: string): void
}

export const TabBar: (props: TabBarProperties & RefAttributes<ComponentInternals>) => ReactNode = forwardRef(
export const TabBar: (props: TabBarProperties & RefAttributes<ContainerRef>) => ReactNode = forwardRef(
({ value: valueProp, defaultValue, onValueChange, ...props }, ref) => {
const [internalValue, setInternalValue] = useState<string | undefined>(defaultValue)
const value = valueProp !== undefined ? valueProp : internalValue
Expand Down Expand Up @@ -82,7 +82,7 @@ export type TabBarItemProperties = ContainerProperties & {
icon: ReactNode
}

export const TabBarItem: (props: TabBarItemProperties & RefAttributes<ComponentInternals>) => ReactNode = forwardRef(
export const TabBarItem: (props: TabBarItemProperties & RefAttributes<ContainerRef>) => ReactNode = forwardRef(
({ value: tabValue, children, icon, ...props }, ref) => {
const { isExpanded, value, setValue } = useContext(TabBarContext)!
const isSelected = value === tabValue
Expand Down
6 changes: 3 additions & 3 deletions packages/kits/apfel/src/tabs.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ComponentInternals, Container, ContainerProperties, DefaultProperties } from '@react-three/uikit'
import { Container, ContainerProperties, ContainerRef, DefaultProperties } from '@react-three/uikit'
import React, {
ReactNode,
RefAttributes,
Expand Down Expand Up @@ -26,7 +26,7 @@ export type TabsProperties = ContainerProperties & {
disabled?: boolean
}

export const Tabs: (props: TabsProperties & RefAttributes<ComponentInternals>) => ReactNode = forwardRef(
export const Tabs: (props: TabsProperties & RefAttributes<ContainerRef>) => ReactNode = forwardRef(
({ value, defaultValue, onValueChange, disabled, ...props }, ref) => {
const [internalValue, setInternalValue] = useState<string | undefined>(defaultValue)
const currentValue = value != null ? value : internalValue
Expand Down Expand Up @@ -74,7 +74,7 @@ type TabsButtonProperties = ContainerProperties & {
disabled?: boolean
}

export const TabsButton: (props: TabsButtonProperties & RefAttributes<ComponentInternals>) => ReactNode = forwardRef(
export const TabsButton: (props: TabsButtonProperties & RefAttributes<ContainerRef>) => ReactNode = forwardRef(
({ children, value, disabled, ...props }, ref) => {
const { value: currentValue, onValueChange, disabled: tabsDisabled } = useContext(TabsContext) as TabsContext

Expand Down
13 changes: 7 additions & 6 deletions packages/kits/default/src/accordion.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { ReactNode, RefAttributes, createContext, forwardRef, useContext, useState } from 'react'
import { ComponentInternals, Container, ContainerProperties, DefaultProperties } from '@react-three/uikit'
import { ContainerRef, Container, ContainerProperties, DefaultProperties } from '@react-three/uikit'
import { ChevronDown } from '@react-three/uikit-lucide'

const AccordionContext = createContext<[string | undefined, (value: string | undefined) => void]>(null as any)
Expand All @@ -18,8 +18,8 @@ const AccordionItemContext = createContext<string>('')

export type AccordionItemProperties = ContainerProperties & { value: string }

export const AccordionItem: (props: RefAttributes<ComponentInternals> & AccordionItemProperties) => ReactNode =
forwardRef(({ children, ...props }, ref) => {
export const AccordionItem: (props: RefAttributes<ContainerRef> & AccordionItemProperties) => ReactNode = forwardRef(
({ children, ...props }, ref) => {
const [value, setValue] = useContext(AccordionContext)
const isSelected = props.value === value
return (
Expand All @@ -34,11 +34,12 @@ export const AccordionItem: (props: RefAttributes<ComponentInternals> & Accordio
<AccordionItemContext.Provider value={props.value}>{children}</AccordionItemContext.Provider>
</Container>
)
})
},
)

export type AccordionTriggerProperties = ContainerProperties

export const AccordionTrigger: (props: RefAttributes<ComponentInternals> & AccordionTriggerProperties) => ReactNode =
export const AccordionTrigger: (props: RefAttributes<ContainerRef> & AccordionTriggerProperties) => ReactNode =
forwardRef(({ children, ...props }, ref) => {
const itemValue = useContext(AccordionItemContext)
const [value] = useContext(AccordionContext)
Expand All @@ -62,7 +63,7 @@ export const AccordionTrigger: (props: RefAttributes<ComponentInternals> & Accor

export type AccordionContentProperties = ContainerProperties

export const AccordionContent: (props: RefAttributes<ComponentInternals> & AccordionContentProperties) => ReactNode =
export const AccordionContent: (props: RefAttributes<ContainerRef> & AccordionContentProperties) => ReactNode =
forwardRef(({ children, ...props }, ref) => {
const itemValue = useContext(AccordionItemContext)
const [value] = useContext(AccordionContext)
Expand Down
Loading

0 comments on commit b5fe6bd

Please sign in to comment.