Skip to content

Commit

Permalink
DeckWidget Component Wrapper
Browse files Browse the repository at this point in the history
Signed-off-by: Chris Gervang <chris@gervang.com>
  • Loading branch information
chrisgervang committed Aug 30, 2024
1 parent 59c48df commit 9d3aabf
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 31 deletions.
92 changes: 61 additions & 31 deletions examples/get-started/react/widgets/app.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, {useState} from 'react';
import React, {useState, forwardRef, useImperativeHandle, useMemo, useRef} from 'react';
import {createRoot} from 'react-dom/client';
import DeckGL, {GeoJsonLayer, ArcLayer} from 'deck.gl';
import {
Expand Down Expand Up @@ -29,40 +29,66 @@ const INITIAL_VIEW_STATE = {
pitch: 30
};

function Root() {
const [widgetContainer, setWidgetContainer] = useState();
function useWidget(props = {}) {
const [container, setContainer] = useState(null);

class ReactWidget {
constructor(props) {
this.id = props.id || 'react';
this.placement = props.placement || 'top-left';
this.viewId = props.viewId;
this.props = props;
}

onAdd() {
const el = document.createElement('div');
setWidgetContainer(el);
return el;
}

onRemove() {
setWidgetContainer(undefined);
}

setProps(props) {
this.props = props;
}
constructor(props) {

Check failure on line 36 in examples/get-started/react/widgets/app.jsx

View workflow job for this annotation

GitHub Actions / test-node

'props' is already declared in the upper scope on line 32 column 20
this.id = props.id || 'react';
this.placement = props.placement || 'top-left';
this.viewId = props.viewId;
this.props = props;
}

onAdd() {
const el = document.createElement('div');
// Defer state update to avoid conflicts with rendering
requestAnimationFrame(() => setContainer(el));

Check failure on line 46 in examples/get-started/react/widgets/app.jsx

View workflow job for this annotation

GitHub Actions / test-node

'requestAnimationFrame' is not defined
return el;
}

onRemove() {
requestAnimationFrame(() => setContainer(null));

Check failure on line 51 in examples/get-started/react/widgets/app.jsx

View workflow job for this annotation

GitHub Actions / test-node

'requestAnimationFrame' is not defined
}

setProps(props) {

Check failure on line 54 in examples/get-started/react/widgets/app.jsx

View workflow job for this annotation

GitHub Actions / test-node

'props' is already declared in the upper scope on line 32 column 20
this.props = props;
this.placement = props.placement || this.placement;
this.viewId = props.viewId || this.viewId;
}
}

const reactWidget = new ReactWidget({});
const widget = useMemo(() => new ReactWidget(props), [props]);

return {
widget,
container
};
}

function DeckWidgetWithRef(props, ref) {
const { widget, container } = useWidget(props);

useImperativeHandle(ref, () => widget, [widget]);

return container ? createPortal(props.children, container) : null;
}

const DeckWidget = forwardRef(DeckWidgetWithRef);

function Root() {
const [placement, setPlacement] = useState('top-left');
const infoWidget = useWidget({id: 'hook'});
const buttonWidget = useRef(null);
console.log(infoWidget, buttonWidget)

Check warning on line 83 in examples/get-started/react/widgets/app.jsx

View workflow job for this annotation

GitHub Actions / test-node

Unexpected console statement

Check failure on line 83 in examples/get-started/react/widgets/app.jsx

View workflow job for this annotation

GitHub Actions / test-node

'console' is not defined

const onClick = () => {
// eslint-disable-next-line
alert('React widget!');
// alert('React widget!');
setPlacement('top-right');
};

const widget = (
const infoWidgetEl = (
<div className="deck-widget" style={widgetTheme}>
<div className="deck-widget-button">
<button className="deck-widget-icon-button" onClick={onClick}>
Expand All @@ -74,15 +100,19 @@ function Root() {

return (
<>
{widgetContainer && createPortal(widget, widgetContainer)}
{infoWidget.container && createPortal(infoWidgetEl, infoWidget.container)}
<DeckWidget ref={buttonWidget} id="component" placement={placement}>
{infoWidgetEl}
</DeckWidget>
<DeckGL
controller={true}
initialViewState={INITIAL_VIEW_STATE}
widgets={[
new ZoomWidget({style: widgetTheme}),
new CompassWidget({style: widgetTheme}),
new FullscreenWidget({style: widgetTheme}),
reactWidget
// new ZoomWidget({style: widgetTheme}),
// new CompassWidget({style: widgetTheme}),
// new FullscreenWidget({style: widgetTheme}),
infoWidget.widget,
buttonWidget.current
]}
layers={[
new GeoJsonLayer({
Expand Down
2 changes: 2 additions & 0 deletions modules/core/src/lib/widget-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,8 @@ export class WidgetManager {
}

for (let widget of nextWidgets) {
if (!widget) continue;
console.log(widget)
const oldWidget = oldWidgetMap[widget.id];
if (!oldWidget) {
// Widget is new
Expand Down

0 comments on commit 9d3aabf

Please sign in to comment.