Skip to content

chore(widgets): Join Widget and WidgetImpl, clean up widgets - #9576

Merged
ibgreen merged 10 commits into
masterfrom
ib/widget-refactor
Apr 22, 2025
Merged

ibgreen merged 10 commits into
masterfrom
ib/widget-refactor

Conversation

@ibgreen

@ibgreen ibgreen commented Apr 11, 2025

Copy link
Copy Markdown
Collaborator

Closes #

Background

Change List

  • Make Widget into a class, consolidate with WidgetImpl
  • Update and simplify widgets

@ibgreen
ibgreen force-pushed the ib/widget-refactor branch 2 times, most recently from 6735418 to 6a57d9f Compare April 17, 2025 16:36
@ibgreen
ibgreen force-pushed the ib/widget-refactor branch from 884a0db to d0a8ea0 Compare April 17, 2025 20:59

@chrisgervang chrisgervang left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This was a lot of work! Thank you. I like this direction a lot. My comments are about like tweaks we could make.

I like how this reduces boilerplate while also not adding opinions to the way the widget UI gets implemented (users are still free to use the framework of their choice).

Aside from the abstract class, the change list includes:

  • Adds references to widgetManager and deck
  • Adds onCreateRootElement so that by default each widget now mounts an HTML element
  • Modifies onAdd to be called after the element is mounted, which is useful for setting up DOM-dependent features.
  • Add updateHTML to trigger UI redraws, when necessary.
  • Using onRenderHTML in all widgets to handle reactive html updates

These changes all make sense to me. The most major change IMO is that all widgets now mount an element by default. I think all mounted UI in practice anyways, right @Pessimistress? And this method can be overridden if necessary.

Comment on lines +36 to +37
...Widget.defaultProps,
position: [0, 0],

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Suggested change
...Widget.defaultProps,
position: [0, 0],
id: 'info',
position: [0, 0],

Add id and rely on base class to spread defaults

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

It is a reasonable suggestion, but consider the typing approach I've chosen which is

  • To type as Required<Props>, which means that all props must be supplied
  • The widget implementor won't forget to add defaults when they add new props.
  • this.props naturally gets typed as Required<Props>, no more ugly this.props.xyz ?? 0 type code in the widgets, they can rely on props being initialized even if the actual Props were options

The downside is of course that this does require the base props to be supplied as well. But I feel it is a reasonable trade-off.

Any other solution I can think of requires fairly ugly typescript machinery that I think would detract more than this simple base class default spread, but maybe I have overlooked something better?

FWIW, this is the approach I used in the luma.gl v9 API and it has stood up well so far.

Comment on lines +41 to +42
static defaultProps: Required<FullscreenWidgetProps> = {
id: 'fullscreen',

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Suggested change
static defaultProps: Required<FullscreenWidgetProps> = {
id: 'fullscreen',
static defaultProps: Required<FullscreenWidgetProps> = {
...Widget.defaultProps,
id: 'fullscreen',

Do we need to consistently spread this in each widget, or is this taken care of in Widget's constructor? I noticed CompassWidget spreads Widget.defaultProps into it's defaults

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Edit: Actually, I propose we spread these base default props in Widget

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

This what I am commenting on above. I think playing around with the code could be helpful if you wanted to explore other approaches, in my experiments this change would trigger even more changes around the code.

Comment on lines +315 to -316
// const rowOffsetLeft = x[j] - rowSize[0] / 2;
// eslint-disable-next-line @typescript-eslint/no-shadow
const rowOffsetLeft = x[j] - rowSize[0] / 2;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

If rowOffsetLeft was unused we could probably delete it.

Comment thread modules/core/src/lib/widget.ts Resolved
Comment thread modules/core/src/lib/widget.ts Resolved
Comment thread modules/widgets/src/theme-widget.tsx Resolved
currentTime: number;

static defaultProps: Required<TimelineWidgetProps> = {
...Widget.defaultProps,

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Suggested change
...Widget.defaultProps,

style: {},
className: undefined!,
domain: [0, 100],
placement: 'bottom-left' as const,

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Suggested change
placement: 'bottom-left' as const,
placement: 'bottom-left',

They could place this anywhere. Does it matter if as const is on the default props if that's the case?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

This is a typescript thing. If I don't add some kind of annotation, bottom-left gets typed as string and doesn't match the WidgetPlacement type.

Comment thread modules/widgets/src/zoom-widget.tsx Resolved
};
class TestWidget extends Widget<TestWidgetProps> {
static defaultProps: Required<TestWidgetProps> = {
...Widget.defaultProps,

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Suggested change
...Widget.defaultProps,

props: CompassWidgetProps;
export class CompassWidget extends Widget<CompassWidgetProps> {
static defaultProps: Required<CompassWidgetProps> = {
...Widget.defaultProps,

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Suggested change
...Widget.defaultProps,

@coveralls

coveralls commented Apr 22, 2025 •

Copy link
Copy Markdown

Coverage Status

coverage: 91.632% (+0.1%) from 91.49%
when pulling cbd5d45 on ib/widget-refactor
into 00465d0 on master.

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.

3 participants