Skip to content

Commit 5cce7d0

Browse files
authored
feat: allow StatsGl style customization via prop and forward ref (#2030)
1 parent 1dfd9a0 commit 5cce7d0

File tree

1 file changed

+48
-30
lines changed

1 file changed

+48
-30
lines changed

src/core/StatsGl.tsx

+48-30
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,55 @@
1-
import * as React from 'react'
1+
import { ForwardRefComponent } from '../helpers/ts-utils'
22
import { addAfterEffect, useThree } from '@react-three/fiber'
3+
import * as React from 'react'
4+
35
import Stats from 'stats-gl'
46

57
type Props = Partial<Stats> & {
6-
showPanel?: number
7-
className?: string
8-
parent?: React.RefObject<HTMLElement>
8+
id?: string
9+
clearStatsGlStyle?: boolean
10+
showPanel?: number
11+
className?: string
12+
parent?: React.RefObject<HTMLElement>
13+
ref?: React.RefObject<HTMLElement>
914
}
1015

11-
export function StatsGl({ className, parent, ...props }: Props) {
12-
const gl: any = useThree((state) => state.gl)
13-
14-
const stats = React.useMemo(() => {
15-
const stats = new Stats({
16-
...props,
17-
})
18-
stats.init(gl)
19-
return stats
20-
}, [gl])
21-
22-
React.useEffect(() => {
23-
if (stats) {
24-
const node = (parent && parent.current) || document.body
25-
node?.appendChild(stats.dom)
26-
const classNames = (className ?? '').split(' ').filter((cls) => cls)
27-
if (classNames.length) stats.dom.classList.add(...classNames)
28-
const end = addAfterEffect(() => stats.update())
29-
return () => {
30-
if (classNames.length) stats.dom.classList.remove(...classNames)
31-
node?.removeChild(stats.dom)
32-
end()
33-
}
16+
export const StatsGl: ForwardRefComponent<Props, HTMLDivElement> = /* @__PURE__ */ React.forwardRef(
17+
({ className, parent, id, clearStatsGlStyle, ...props }: Props, fref) => {
18+
const gl: any = useThree((state) => state.gl)
19+
20+
const stats = React.useMemo(() => {
21+
const stats = new Stats({
22+
...props,
23+
})
24+
stats.init(gl)
25+
return stats
26+
}, [gl])
27+
28+
React.useImperativeHandle(fref, () => stats.dom)
29+
30+
React.useEffect(() => {
31+
if (stats) {
32+
const node = (parent && parent.current) || document.body
33+
node?.appendChild(stats.dom)
34+
stats.dom.querySelectorAll('canvas').forEach((canvas) => {
35+
canvas.style.removeProperty('position')
36+
})
37+
if (id) stats.dom.id = id
38+
if (clearStatsGlStyle) stats.dom.removeAttribute('style')
39+
stats.dom.removeAttribute('style')
40+
const classNames = (className ?? '').split(' ').filter((cls) => cls)
41+
if (classNames.length) stats.dom.classList.add(...classNames)
42+
const end = addAfterEffect(() => stats.update())
43+
return () => {
44+
if (classNames.length) stats.dom.classList.remove(...classNames)
45+
node?.removeChild(stats.dom)
46+
end()
47+
}
48+
}
49+
}, [parent, stats, className, id, clearStatsGlStyle])
50+
51+
return null
3452
}
35-
}, [parent, stats, className])
36-
return null
37-
}
53+
)
54+
55+
StatsGl.displayName = 'StatsGl'

0 commit comments

Comments
 (0)