-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Refactor/toolbar button component to typescript #47750
Refactor/toolbar button component to typescript #47750
Conversation
…factor/toolbar-component-to-typescript
…factor/toolbar-component-to-typescript
Co-authored-by: Lena Morita <lena@jaguchi.com>
…thub.com/mike-day/gutenberg into refactor/toolbar-button-component-to-typescript
👋 Thanks for your first Pull Request and for helping build the future of Gutenberg and WordPress, @mike-day! In case you missed it, we'd love to have you join us in our Slack community, where we hold regularly weekly meetings open to anyone to coordinate with each other. If you want to learn more about WordPress development in general, check out the Core Handbook full of helpful information. |
@ciampo @mirka here is some initial work for |
Note that commit and file change history will be much smaller before this is merged — #47087 was pulled in to avoid duplicate work. |
…factor/toolbar-button-component-to-typescript
Hey @mike-day , we haven't taken a look at this PR as it's still marked as a draft — just let us know when and we'll take a look! |
/** | ||
* Additional props to be passed alongside props. | ||
*/ | ||
extraProps?: any; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not sure about the purpose of extraProps
— both props
and extraProps
are spread into Button
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed. The props passing seems fishy overall, especially with how props.foo
is used for some things. I have to guess that this code has degraded over time.
I'm also not entirely convinced that we should be accepting all Button props, since some of them are probably not appropriate in this specific toolbar context. But perhaps we can revisit this later when there is a more pressing need to improve these typings and APIs.
For posterity, I'll also note that I'm fine with resorting to an any
type here for the time being. Ideally it should be some variation of ButtonProps
, but that forces us to deal with the button
/anchor
polymorphism to an extent that I'd rather do it as a separate issue. I kind of feel like we should only allow "Button as a <button>
" inside toolbars, but even that decision requires some more investigation and effort!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This actually relates well to the work that @chad1008 is doing on CircularOptionPicker
's TypeScript refactor. For now, we've decided to use ButtonAsButtonProps
(so that we can avoid the anchor
case), but I'd definitely be interested in your opinion once the PR is ready for review
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
…factor/toolbar-button-component-to-typescript
Checks are running from a fresh pull of I opted to not rebase and leave commit history alone, but just let me know if I should clean that up (pulling in 47087 in the beginning of this work made commit history look more extensive than it really is). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good! This has uncovered some existing bugs and API issues, but those will be triaged and dealt with separately 👍
packages/components/src/toolbar/toolbar-button/toolbar-button-container.tsx
Outdated
Show resolved
Hide resolved
/** | ||
* Additional props to be passed alongside props. | ||
*/ | ||
extraProps?: any; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed. The props passing seems fishy overall, especially with how props.foo
is used for some things. I have to guess that this code has degraded over time.
I'm also not entirely convinced that we should be accepting all Button props, since some of them are probably not appropriate in this specific toolbar context. But perhaps we can revisit this later when there is a more pressing need to improve these typings and APIs.
For posterity, I'll also note that I'm fine with resorting to an any
type here for the time being. Ideally it should be some variation of ButtonProps
, but that forces us to deal with the button
/anchor
polymorphism to an extent that I'd rather do it as a separate issue. I kind of feel like we should only allow "Button as a <button>
" inside toolbars, but even that decision requires some more investigation and effort!
Hey @mike-day , do you still have capacity to work on this PR? |
Yes, apologies for the radio silence! I plan to jump back on this and work through comments tomorrow. |
…container.tsx Co-authored-by: Lena Morita <lena@jaguchi.com>
…ps://github.com/mike-day/gutenberg into refactor/toolbar-button-component-to-typescript
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great work, thank you!
I just went ahead and moved back the changelog into the current "Unreleased" section 👍
...props | ||
}, | ||
ref | ||
}: WordPressComponentProps< ToolbarButtonProps, typeof Button, false >, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe this is the first time that I see us using props from another React element (Button
) as the second argument for the WordPressComponentProps
— @mirka do you see any potential issue with this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good question. Considering this from several angles:
- Can Emotion handle polymorphism like this? — Yes. The Emotion
as
prop allowsReact.ElementType
, so it can potentially handleas={ Button }
if this was a polymorphic WordPressComponent. - Is it a good idea to allow something like
as={ Button }
? — In general, I don't like it because it's hard to predict what will happen. But it may make sense in some abstract components. - Should we prefer
ToolbarButtonProps & ButtonProps
syntax in cases like this? — No strong opinion, given that there are no serious downsides to theWordPressComponentProps
syntax. I like how we can just dotypeof Button
without importing theButtonProps
type though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good observations. Let's keep as-is, keep an eye for any unintended consequences, and otherwise consider it as another tool in our belt
Part of #35744.
To be merged after #47087.
What?
Convert the
ToolbarButton
component to TypeScript.Why?
This PR is needed as part of an effort to convert
Toolbar
and relatedToolbar
subcomponents to TypeScript.How?
By adding types to
ToolbarButton
andToolbarButtonContainer
.Testing Instructions
Image
)