Repository navigation
chore(widgets): Join Widget and WidgetImpl, clean up widgets - #9576
Conversation
6735418 to
6a57d9f
Compare
884a0db to
d0a8ea0
Compare
chrisgervang
left a comment
There was a problem hiding this comment.
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
widgetManageranddeck - Adds
onCreateRootElementso that by default each widget now mounts an HTML element - Modifies
onAddto be called after the element is mounted, which is useful for setting up DOM-dependent features. - Add
updateHTMLto trigger UI redraws, when necessary. - Using
onRenderHTMLin 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.
| ...Widget.defaultProps, | ||
| position: [0, 0], |
There was a problem hiding this comment.
| ...Widget.defaultProps, | |
| position: [0, 0], | |
| id: 'info', | |
| position: [0, 0], |
Add id and rely on base class to spread defaults
There was a problem hiding this comment.
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.propsnaturally gets typed asRequired<Props>, no more uglythis.props.xyz ?? 0type 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.
| static defaultProps: Required<FullscreenWidgetProps> = { | ||
| id: 'fullscreen', |
There was a problem hiding this comment.
| 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
There was a problem hiding this comment.
Edit: Actually, I propose we spread these base default props in Widget
There was a problem hiding this comment.
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.
| // const rowOffsetLeft = x[j] - rowSize[0] / 2; | ||
| // eslint-disable-next-line @typescript-eslint/no-shadow | ||
| const rowOffsetLeft = x[j] - rowSize[0] / 2; |
There was a problem hiding this comment.
If rowOffsetLeft was unused we could probably delete it.
| currentTime: number; | ||
|
|
||
| static defaultProps: Required<TimelineWidgetProps> = { | ||
| ...Widget.defaultProps, |
There was a problem hiding this comment.
| ...Widget.defaultProps, |
| style: {}, | ||
| className: undefined!, | ||
| domain: [0, 100], | ||
| placement: 'bottom-left' as const, |
There was a problem hiding this comment.
| 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?
There was a problem hiding this comment.
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.
| }; | ||
| class TestWidget extends Widget<TestWidgetProps> { | ||
| static defaultProps: Required<TestWidgetProps> = { | ||
| ...Widget.defaultProps, |
There was a problem hiding this comment.
| ...Widget.defaultProps, |
| props: CompassWidgetProps; | ||
| export class CompassWidget extends Widget<CompassWidgetProps> { | ||
| static defaultProps: Required<CompassWidgetProps> = { | ||
| ...Widget.defaultProps, |
There was a problem hiding this comment.
| ...Widget.defaultProps, |
Co-authored-by: Chris Gervang <[email protected]>
Co-authored-by: Chris Gervang <[email protected]>
Closes #
Background
Change List