Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(react) Support for JSX Widgets in React #9278

Open
wants to merge 34 commits into
base: master
Choose a base branch
from

Conversation

chrisgervang
Copy link
Collaborator

@chrisgervang chrisgervang commented Dec 5, 2024

For #9056

Related #9304 #9309

Background

Change List

@felixpalmer felixpalmer mentioned this pull request Dec 6, 2024
36 tasks
Copy link
Collaborator

@felixpalmer felixpalmer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me, clean and not too much code :) I'm not that familiar with the React module so will leave the approval to @Pessimistress

modules/react/src/utils/use-widget.ts Show resolved Hide resolved
modules/react/src/index.ts Show resolved Hide resolved

this.props = {
...props,
transitionDuration: props.transitionDuration || 200,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wonder if it would be nice to support defaultProps for widgets to avoid this sort of code

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Widget is only an interface, so I think the best we can do is lead by example of least annoying code to maintain. I just changed the dev guide to use this.id = props.id ?? this.id to avoid needing to repeat the hard-coded values, which is a marked improvement over this.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could it be an abstract class instead?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's tempting but I'm more in favor of flexibility at this stage. I'm hesitant to abstract too early across widgets with there are so many ways to implement UI.

It's a bit of extra work to fill out the interface, but not much.

Maybe when we have a fuller library of examples exploring a breadth of use cases.. until then, there's nothing preventing an author from making their own abstract class.

modules/react/src/index.ts Show resolved Hide resolved
@@ -8,7 +8,7 @@ import type {Deck, Viewport, Widget, WidgetPlacement} from '@deck.gl/core';
import {render} from 'preact';
import {ButtonGroup, GroupedIconButton} from './components';

interface ZoomWidgetProps {
export interface ZoomWidgetProps {
Copy link
Collaborator Author

@chrisgervang chrisgervang Dec 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Pessimistress I'm noticing that the widget's onViewportChange isn't called on initialization. The user needs to interact with deck before a viewport is set.

Vanilla widgets work. Any idea what's different?

Copy link
Collaborator Author

@chrisgervang chrisgervang Dec 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The cause is this guard is hit before the viewports have been assigned to the widget. lastViewports was set on the first time props are set (when widgets: [] and it initialized the tooltip).
Screenshot 2024-12-12 at 5 18 07 PM

The goal of this code is to cache the current viewport so that the widget can use it as the starting point of its view modification.

The hook adds widgets in one-by-one: [] then [FirstWidget] then [FirstWidget, SecondWidget]

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess we could push this check down into each widget to implement since the manager isn't aware of how widgets store viewports, or we could add viewports: {} to class Widget and have the manager check that instead? Thoughts?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See proposed fix in #9303

@chrisgervang chrisgervang changed the title [WIP] React Widgets feat(react) Support for JSX Widgets in React Dec 19, 2024
@coveralls
Copy link

coveralls commented Jan 2, 2025

Coverage Status

coverage: 91.665% (-0.04%) from 91.705%
when pulling f144266 on chr/jsx-widgets
into 15a9344 on master.

output: {
children: [],
views: null,
layers: null
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding this test case since it was unclear that layers aren't always returning an empty array, regardless of input.

@@ -64,27 +65,40 @@ test('positionChildrenUnderViews#before initialization', t => {
test('positionChildrenUnderViews', t => {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

deck children are now wrapped in a DeckContext.Provider by default

Comment on lines +142 to +146
t.is(
layers && Array.isArray(layers) && layers.length,
0,
'Layers is reset to an empty array'
);
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This layers prop behavior is subtly different than pure-js and so I wanted to capture it in our tests. Widgets match this behavior as well.

onAfterRender: () => {
const {widgets, layers} = deck.props;
t.is(widgets && Array.isArray(widgets) && widgets.length, 1, 'Widgets remain set');
t.is(layers && Array.isArray(layers) && layers.length, 1, 'Layers remain set');
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See below, this works a little differently between react and pure-js. I believe this is intentional and just wasn't covered in our tests.

@@ -157,6 +157,7 @@ function DeckGLWithRef<ViewsT extends ViewOrViews = null>(
// Needs to be called both from initial mount, and when new props are received
const deckProps = useMemo(() => {
const forwardProps: DeckProps<ViewsT> = {
widgets: [],
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar to jsxProps.layers, the widgets array needs to reset to an empty array in the react component so that widgets unmount when the prop is removed.

This sets the behavior when a user goes from <DeckGL widget={[...]}/> to <DeckGL/>.

I believe unmounting in this case is the desired behavior. This behavior was noticeable in the deck.gl playground (which uses the react deck component) when removing the entire widgets section from the json string didn't remove them from the map.

Copy link
Collaborator

@felixpalmer felixpalmer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great job!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants