Created
April 18, 2023 22:03
-
-
Save lojjic/96a5f24d2bfa8580b38b5850acf48d9c to your computer and use it in GitHub Desktop.
A wrapper for Drei's <Html> component that works around some issues when used with the Matterport R3F web component
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
import { useThree } from '@react-three/fiber'; | |
import React, { useEffect } from 'react'; | |
import { Html as DreiHtml } from '@react-three/drei'; | |
import type { HtmlProps } from '@react-three/drei/web/Html'; | |
export function Html(props: HtmlProps) { | |
const { gl } = useThree(); | |
const canvas = gl.domElement as HTMLCanvasElement; | |
// Drei's Html component sets pointerEvents:'none' on the canvas, which breaks MP | |
// events, so we must undo that. Note this will affect the `occlude` feature. | |
useEffect(() => { | |
if (canvas) { | |
canvas.style.pointerEvents = ''; | |
} | |
}, [canvas]); | |
// The default insertion point puts the HTML inside the shadow dom, which prevents | |
// CSS styles from the outer document from being applied, so we change the default | |
// to insert as a sibling of the web component. This can be overridden via the | |
// `portal` prop to use a more specific container. | |
const container = (canvas.getRootNode() as ShadowRoot).host.parentElement; | |
return container && <DreiHtml portal={{ current: container }} {...props} />; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment