-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(react) Support for JSX Widgets in React (#9278)
* Add DeckGLContext * chore(react) all children are provided a DeckContext by default * Add useWidget hook * chore(main) export react components for each widget * React widgets should warn when pure-js widgets are used * Widgets should be removed when JSX unmounts * chore(react) widgets should be reset when omitted in react * chore(react) widget prop warning shouldn't warn for empty array * chore(react) use consistent naming between react and purejs widgets * Should use @deck.gl/react for all widget imports --------- Signed-off-by: Chris Gervang <chris@gervang.com>
- Loading branch information
1 parent
6625885
commit 7d046e6
Showing
16 changed files
with
284 additions
and
40 deletions.
There are no files selected for viewing
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import {createContext} from 'react'; | ||
import type {EventManager} from 'mjolnir.js'; | ||
import type {Deck, DeckProps, Viewport, Widget} from '@deck.gl/core'; | ||
|
||
export type DeckGLContextValue = { | ||
viewport: Viewport; | ||
container: HTMLElement; | ||
eventManager: EventManager; | ||
onViewStateChange: DeckProps['onViewStateChange']; | ||
deck?: Deck<any>; | ||
widgets?: Widget[]; | ||
}; | ||
|
||
// @ts-ignore | ||
export const DeckGlContext = createContext<DeckGLContextValue>(); |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import {useContext, useMemo, useEffect} from 'react'; | ||
import {DeckGlContext} from './deckgl-context'; | ||
import {log, type Widget, _deepEqual as deepEqual} from '@deck.gl/core'; | ||
|
||
export function useWidget<T extends Widget, PropsT extends {}>( | ||
WidgetClass: {new (props: PropsT): T}, | ||
props: PropsT | ||
): T { | ||
const context = useContext(DeckGlContext); | ||
const {widgets, deck} = context; | ||
useEffect(() => { | ||
// warn if the user supplied a pure-js widget, since it will be ignored | ||
// NOTE: This effect runs once per widget. Context widgets and deck widget props are synced after first effect runs. | ||
const internalWidgets = deck?.props.widgets; | ||
if (widgets?.length && internalWidgets?.length && !deepEqual(internalWidgets, widgets, 1)) { | ||
log.warn('"widgets" prop will be ignored because React widgets are in use.')(); | ||
} | ||
|
||
return () => { | ||
// Remove widget from context when it is unmounted | ||
const index = widgets?.indexOf(widget); | ||
if (index && index !== -1) { | ||
widgets?.splice(index, 1); | ||
deck?.setProps({widgets}); | ||
} | ||
}; | ||
}, []); | ||
const widget = useMemo(() => new WidgetClass(props), [WidgetClass]); | ||
|
||
// Hook rebuilds widgets on every render: [] then [FirstWidget] then [FirstWidget, SecondWidget] | ||
widgets?.push(widget); | ||
widget.setProps(props); | ||
|
||
useEffect(() => { | ||
deck?.setProps({widgets}); | ||
}, [widgets]); | ||
|
||
return widget; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import {CompassWidget as _CompassWidget} from '@deck.gl/widgets'; | ||
import type {CompassWidgetProps} from '@deck.gl/widgets'; | ||
import {useWidget} from '../utils/use-widget'; | ||
|
||
export const CompassWidget = (props: CompassWidgetProps = {}) => { | ||
const widget = useWidget(_CompassWidget, props); | ||
return null; | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import {FullscreenWidget as _FullscreenWidget} from '@deck.gl/widgets'; | ||
import type {FullscreenWidgetProps} from '@deck.gl/widgets'; | ||
import {useWidget} from '../utils/use-widget'; | ||
|
||
export const FullscreenWidget = (props: FullscreenWidgetProps = {}) => { | ||
const widget = useWidget(_FullscreenWidget, props); | ||
return null; | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import {ZoomWidget as _ZoomWidget} from '@deck.gl/widgets'; | ||
import type {ZoomWidgetProps} from '@deck.gl/widgets'; | ||
import {useWidget} from '../utils/use-widget'; | ||
|
||
export const ZoomWidget = (props: ZoomWidgetProps = {}) => { | ||
const widget = useWidget(_ZoomWidget, props); | ||
return null; | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ | |
"outDir": "dist" | ||
}, | ||
"references": [ | ||
{"path": "../core"} | ||
{"path": "../core"}, | ||
{"path": "../widgets"} | ||
] | ||
} |
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
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
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
Oops, something went wrong.