-
Notifications
You must be signed in to change notification settings - Fork 309
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0667685
commit 60f0cea
Showing
5 changed files
with
115 additions
and
25 deletions.
There are no files selected for viewing
69 changes: 69 additions & 0 deletions
69
packages/mask/dashboard/components/SetupFrame/ParentSize.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
// Modified version of | ||
// https://github.com/splinetool/react-spline/blob/main/src/ParentSize.tsx | ||
import { debounce } from 'lodash-es' | ||
import { forwardRef, useEffect, useMemo, useRef, useState } from 'react' | ||
|
||
interface ParentSizeProps { | ||
children: ( | ||
args: { | ||
ref: HTMLDivElement | null | ||
resize: (state: ParentSizeState) => void | ||
} & ParentSizeState, | ||
) => React.ReactNode | ||
} | ||
|
||
type ParentSizeState = { | ||
width: number | ||
height: number | ||
top: number | ||
left: number | ||
} | ||
|
||
const defaultParentSizeStyles = { width: '100%', height: '100%' } | ||
|
||
export default forwardRef<HTMLDivElement, ParentSizeProps>(function ParentSize({ children }: ParentSizeProps, ref) { | ||
const target = useRef<HTMLDivElement | null>(null) | ||
const animationFrameID = useRef(0) | ||
|
||
const [state, setState] = useState<ParentSizeState>({ | ||
width: 0, | ||
height: 0, | ||
top: 0, | ||
left: 0, | ||
}) | ||
|
||
const resize = useMemo(() => debounce(setState, 50), []) | ||
|
||
useEffect(() => { | ||
const observer = new ResizeObserver((entries) => { | ||
entries.forEach((entry) => { | ||
const { left, top, width, height } = entry?.contentRect ?? {} | ||
animationFrameID.current = window.requestAnimationFrame(() => { | ||
resize({ width, height, top, left }) | ||
}) | ||
}) | ||
}) | ||
if (target.current) observer.observe(target.current) | ||
|
||
return () => { | ||
window.cancelAnimationFrame(animationFrameID.current) | ||
observer.disconnect() | ||
resize.cancel() | ||
} | ||
}, [resize]) | ||
|
||
return ( | ||
<div | ||
style={defaultParentSizeStyles} | ||
ref={(r) => { | ||
typeof ref === 'function' ? ref(r) : ref !== null && (ref.current = r) | ||
target.current = r | ||
}}> | ||
{children({ | ||
...state, | ||
ref: target.current, | ||
resize, | ||
})} | ||
</div> | ||
) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { useEffect, useRef, useState, forwardRef } from 'react' | ||
import { Application } from '@splinetool/runtime' | ||
import ParentSize from './ParentSize.js' | ||
|
||
export interface SplineProps { | ||
scene: string | ||
onLoad?: (e: Application) => void | ||
} | ||
|
||
const Spline = forwardRef<HTMLDivElement, SplineProps>(({ scene, onLoad }, ref) => { | ||
const canvasRef = useRef<HTMLCanvasElement>(null) | ||
const [isLoading, setIsLoading] = useState(true) | ||
|
||
useEffect(() => { | ||
setIsLoading(true) | ||
let disposed = false | ||
let speApp: Application | ||
if (canvasRef.current) { | ||
speApp = new Application(canvasRef.current) | ||
async function init() { | ||
await speApp.load(scene) | ||
if (disposed) return | ||
setIsLoading(false) | ||
onLoad?.(speApp) | ||
} | ||
init() | ||
} | ||
return () => { | ||
disposed = true | ||
speApp.dispose() | ||
} | ||
}, [scene]) | ||
|
||
return ( | ||
<ParentSize ref={ref}> | ||
{() => <canvas ref={canvasRef} style={{ display: isLoading ? 'none' : 'block' }} />} | ||
</ParentSize> | ||
) | ||
}) | ||
|
||
export default Spline |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.