Skip to content

Commit

Permalink
feat: working, but shifty, info card
Browse files Browse the repository at this point in the history
  • Loading branch information
haakonflatval-cognite committed Jul 28, 2023
1 parent b4ff1f4 commit 5c7c129
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 51 deletions.
48 changes: 28 additions & 20 deletions react-components/src/components/InfoCard/InfoCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<HTMLElement>;
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<HTMLDivElement>(null);
const mods = useAuxillaryDivContext();

const htmlRef = useRef<HTMLDivElement>(null);
const element = (
<div id={'infoCardContent'} key={'info'} ref={htmlRef} style={{ position: 'absolute' }}>
{children}
</div>
);

const [htmlTool, _setHtmlTool] = useState<HtmlOverlayTool>(new HtmlOverlayTool(viewer));
const htmlElement = (<div ref={divReference}> {card} </div>);

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 <></>;
};
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -47,9 +48,11 @@ export function RevealContainer({
return (
<SDKProvider sdk={sdk}>
<QueryClientProvider client={queryClient}>
<div style={{ width: '100%', height: '100%' }} ref={revealDomElementRef}>
{mountChildren()}
</div>
<AuxillaryDivProvider>
<div style={{ width: '100%', height: '100%' }} ref={revealDomElementRef}>
{mountChildren()}
</div>
</AuxillaryDivProvider>
{uiElements}
</QueryClientProvider>
</SDKProvider>
Expand Down
74 changes: 46 additions & 28 deletions react-components/stories/Reveal3DResources.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -123,31 +124,48 @@ export const Main: Story = {
assetFdmSpace: 'bark-corporation'
}
},
render: ({ resources, styling, fdmAssetMappingConfig }) => (
<RevealContainer
sdk={sdk}
color={new Color(0x4a4a4a)}
uiElements={<ReactQueryDevtools initialIsOpen={false} />}
viewerOptions={{
loadingIndicatorStyle: {
opacity: 1,
placement: 'topRight'
}
}}>
<Reveal3DResources
resources={resources}
styling={styling}
fdmAssetMappingConfig={fdmAssetMappingConfig}
/>
<CameraController
initialFitCamera={{
to: 'allModels'
}}
cameraControlsOptions={{
changeCameraTargetOnClick: true,
mouseWheelAction: 'zoomToCursor'
}}
/>
</RevealContainer>
)
render: ({ resources, styling, fdmAssetMappingConfig }) => {
const position = new Vector3(50, 30, 50);

return (
<RevealContainer
sdk={sdk}
color={new Color(0x4a4a4a)}
uiElements={<ReactQueryDevtools initialIsOpen={false} />}
viewerOptions={{
loadingIndicatorStyle: {
opacity: 1,
placement: 'topRight'
}
}}>
<Reveal3DResources
resources={resources}
styling={styling}
fdmAssetMappingConfig={fdmAssetMappingConfig}
/>
<InfoCard position={position}>
<p
style={{
backgroundColor: 'turquoise',
borderColor: 'black',
borderWidth: '10px',
borderStyle: 'solid',
maxWidth: '300px',
transform: 'translate(0px, calc(-100% - 50px))'
}}>
This label is stuck at position {position.toArray().join(',')}
</p>
</InfoCard>
<CameraController
initialFitCamera={{
to: 'allModels'
}}
cameraControlsOptions={{
changeCameraTargetOnClick: true,
mouseWheelAction: 'zoomToCursor'
}}
/>
</RevealContainer>
);
}
};

0 comments on commit 5c7c129

Please sign in to comment.