Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement react-use-measure #21

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@
},
"homepage": "https://github.com/pmndrs/react-three-csg#readme",
"dependencies": {
"mitt": "^3.0.0"
"mitt": "^3.0.0",
"react-merge-refs": "^2.1.1"
},
"peerDependencies": {
"@react-three/fiber": ">=8.0.0",
Expand Down
36 changes: 17 additions & 19 deletions src/Canvas.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React, { useEffect, useRef } from 'react'
import type { Options as ResizeOptions } from 'react-use-measure'
import useMeasure, { Options as ResizeOptions } from 'react-use-measure'
import { mergeRefs } from 'react-merge-refs'
import { Canvas as CanvasImpl, RenderProps } from '@react-three/fiber'
import { EVENTS } from './events'

Expand All @@ -25,7 +26,9 @@ function isRefObject<T>(ref: any): ref is React.MutableRefObject<T> {

export function Canvas({ eventSource, worker, fallback, style, className, id, ...props }: CanvasProps) {
const [shouldFallback, setFallback] = React.useState(false)
const [measureRef, bounds] = useMeasure(props.resize)
const canvasRef = useRef<HTMLCanvasElement>(null!)
const mergedRef = mergeRefs([canvasRef, measureRef])
const hasTransferredToOffscreen = useRef(false)

useEffect(() => {
Expand Down Expand Up @@ -110,25 +113,20 @@ export function Canvas({ eventSource, worker, fallback, style, className, id, ..
{ passive }
)
})

const handleResize = () => {
worker.postMessage({
type: 'resize',
payload: {
width: currentEventSource.clientWidth,
height: currentEventSource.clientHeight,
top: currentEventSource.offsetTop,
left: currentEventSource.offsetLeft,
},
})
}

window.addEventListener('resize', handleResize)
return () => {
window.removeEventListener('resize', handleResize)
}
}, [worker])

useEffect(() => {
worker.postMessage({
type: 'resize',
payload: {
width: bounds.width,
height: bounds.height,
top: bounds.top,
left: bounds.left,
},
})
}, [bounds])

useEffect(() => {
if (!worker) return
worker.postMessage({ type: 'props', payload: props })
Expand All @@ -143,7 +141,7 @@ export function Canvas({ eventSource, worker, fallback, style, className, id, ..
id={id}
className={className}
style={{ position: 'relative', width: '100%', height: '100%', overflow: 'hidden', display: 'block', ...style }}
ref={canvasRef}
ref={mergedRef}
/>
)
}