-
Notifications
You must be signed in to change notification settings - Fork 717
/
Example.tsx
53 lines (46 loc) · 1.59 KB
/
Example.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/* eslint react-hooks/exhaustive-deps: 1 */
import * as React from 'react'
import * as THREE from 'three'
import type { Color } from '@react-three/fiber'
import { Text3D } from './Text3D'
import { Center } from './Center'
export type ExampleProps = {
/** Text font to use */
font: string
/** Text color */
color?: Color
/** Debug mode */
debug?: boolean
/** Text bevel size */
bevelSize?: number
} & React.ComponentProps<'group'>
export type ExampleApi = {
incr: (x?: number) => void
decr: (x?: number) => void
}
/**
* A simple counter example component. Click to increment, meta-click to decrement.
*/
export const Example = /* @__PURE__ */ React.forwardRef<ExampleApi, ExampleProps>(
({ font, color = '#cbcbcb', bevelSize = 0.04, debug = false, children, ...props }, fref) => {
const [counter, setCounter] = React.useState(0)
const incr = React.useCallback((x = 1) => setCounter(counter + x), [counter])
const decr = React.useCallback((x = 1) => setCounter(counter - x), [counter])
// ref-API
const api = React.useMemo<ExampleApi>(() => ({ incr, decr }), [incr, decr])
React.useImperativeHandle(fref, () => api, [api])
return (
<group {...props}>
<React.Suspense fallback={null}>
<Center top cacheKey={JSON.stringify({ counter, font })}>
<Text3D bevelEnabled bevelSize={bevelSize} font={font}>
{debug ? <meshNormalMaterial wireframe /> : <meshStandardMaterial color={color} />}
{counter}
</Text3D>
</Center>
</React.Suspense>
{children}
</group>
)
}
)