diff --git a/react-components/src/components/InfoCard/InfoCard.tsx b/react-components/src/components/InfoCard/InfoCard.tsx index f5dcaaa2256..5a6e1996ea8 100644 --- a/react-components/src/components/InfoCard/InfoCard.tsx +++ b/react-components/src/components/InfoCard/InfoCard.tsx @@ -3,42 +3,50 @@ */ import React, { JSX, useState, useEffect, useRef } from 'react'; - import { Vector3 } from 'three'; import { useReveal } from '../RevealContainer/RevealContext'; import { HtmlOverlayTool } from '@cognite/reveal/tools'; +import { useAuxillaryDivContext } from '../RevealContainer/AuxillaryDivProvider'; -import styled from 'styled-components'; +export type InfoCardElementMapping = { + ref: React.RefObject; + position: Vector3; +}; export type InfoCardProps = { - card: JSX.Element; position: Vector3; + children: React.ReactNode; }; -export const InfoCard = ({ - card, - position -}: InfoCardProps): JSX.Element => { +export const InfoCard = ({ position, children }: InfoCardProps): JSX.Element => { const viewer = useReveal(); - const divReference = useRef(null); + const mods = useAuxillaryDivContext(); + + const htmlRef = useRef(null); + const element = ( +
+ {children} +
+ ); + const [htmlTool, _setHtmlTool] = useState(new HtmlOverlayTool(viewer)); - const htmlElement = (
{card}
); useEffect(() => { - if (divReference.current === null) return; - const innerElement = divReference.current; - - htmlTool.add(innerElement, position); + mods.addElement(element); + return () => mods.removeElement(element); + }, []); - return () => htmlTool.remove(innerElement); - }, [card, htmlTool, divReference.current]); + useEffect(() => { + if (htmlRef.current === null) { + return; + } - return htmlElement; -} + htmlTool.clear(); + htmlTool.add(htmlRef.current, position); + }, [htmlTool, children, htmlRef.current]); -export const FloatingDiv = styled.div` - position: absolute; -` + return <>; +}; diff --git a/react-components/src/components/RevealContainer/RevealContainer.tsx b/react-components/src/components/RevealContainer/RevealContainer.tsx index ea0229cf6db..9d5c2695cf4 100644 --- a/react-components/src/components/RevealContainer/RevealContainer.tsx +++ b/react-components/src/components/RevealContainer/RevealContainer.tsx @@ -9,6 +9,7 @@ import { type Color } from 'three'; import { ModelsLoadingStateContext } from '../Reveal3DResources/ModelsLoadingContext'; import { SDKProvider } from './SDKProvider'; import { QueryClientProvider, QueryClient } from '@tanstack/react-query'; +import { AuxillaryDivContext, AuxillaryDivProvider } from './AuxillaryDivProvider'; type RevealContainerProps = { color?: Color; @@ -47,9 +48,11 @@ export function RevealContainer({ return ( -
- {mountChildren()} -
+ +
+ {mountChildren()} +
+
{uiElements}
diff --git a/react-components/stories/Reveal3DResources.stories.tsx b/react-components/stories/Reveal3DResources.stories.tsx index 019f2dcc04e..1b3860bbefc 100644 --- a/react-components/stories/Reveal3DResources.stories.tsx +++ b/react-components/stories/Reveal3DResources.stories.tsx @@ -3,10 +3,11 @@ */ import type { Meta, StoryObj } from '@storybook/react'; import { Reveal3DResources, RevealContainer } from '../src'; -import { Color, Matrix4 } from 'three'; +import { Color, Matrix4, Vector3 } from 'three'; import { CameraController } from '../src/'; import { createSdkByUrlToken } from './utilities/createSdkByUrlToken'; import { ReactQueryDevtools } from '@tanstack/react-query-devtools'; +import { InfoCard } from '../src/components/InfoCard/InfoCard'; const meta = { title: 'Example/Reveal3DResources', @@ -123,31 +124,48 @@ export const Main: Story = { assetFdmSpace: 'bark-corporation' } }, - render: ({ resources, styling, fdmAssetMappingConfig }) => ( - } - viewerOptions={{ - loadingIndicatorStyle: { - opacity: 1, - placement: 'topRight' - } - }}> - - - - ) + render: ({ resources, styling, fdmAssetMappingConfig }) => { + const position = new Vector3(50, 30, 50); + + return ( + } + viewerOptions={{ + loadingIndicatorStyle: { + opacity: 1, + placement: 'topRight' + } + }}> + + +

+ This label is stuck at position {position.toArray().join(',')} +

+
+ +
+ ); + } };