-
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
DropdownMenu: refactor to TypeScript #50187
Conversation
948180a
to
d87ca36
Compare
d87ca36
to
df2694d
Compare
Size Change: +1 B (0%) Total Size: 1.39 MB
ℹ️ View Unchanged
|
a2e6038
to
e994728
Compare
4e95457
to
b3b794c
Compare
{ /* There is an issue here with TS not recognizing the | ||
* `RenderProp` being passed. | ||
* @ts-expect-error */ } | ||
{ ( toggleProps ) => ( |
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.
Hey @diegohaz 👋 We're refactoring this component to TypeScript, and we're getting an error around the type of children
. We're passing a render prop, but for some reason it doesn't seem to work with the type that ToolbarItem
from reakit
is expecting. Do you have any ideas why this is happening?
Thanks 🙏
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.
What's the error? You might need to explicitly type the parameter (instead of relying on inference).
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 is the error:
Type '(toggleProps: ExtractHTMLAttributes<any>) => Element' is not assignable to type '((string | number | boolean | ReactElement<any, string | JSXElementConstructor<any>> | ReactFragment | ReactPortal) & (string | ... 5 more ... | RenderProp<...>)) | null | undefined'.
Type '(toggleProps: ExtractHTMLAttributes<any>) => Element' is not assignable to type 'string & RenderProp<ExtractHTMLAttributes<any>>'.
Type '(toggleProps: ExtractHTMLAttributes<any>) => Element' is not assignable to type 'string'.ts(2322)
What I don't understand is the initial string & ...
in the expected type. Do you have any ideas as to why? It seems that our render prop would otherwise be matching correctly the RenderProp
type
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.
(also, found this related comment about ToolbarItem
's children types #50262 (comment) )
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.
Hey @diegohaz , did you have time to take a look at 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.
Looks like it's coming from https://github.com/ariakit/ariakit/blob/3966597f6a24f601f83d0816231486b8a293faee/packages/reakit-utils/src/types.ts#L63
But I also don't understand why there's a string & ...
there. It should be possible to fix this in the ToolbarItem
component by providing own types for the children
prop instead of inheriting them directly from Reakit:
}: React.ComponentPropsWithoutRef< typeof BaseToolbarItem >, |
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.
Awesome, thanks for that @diegohaz. I'll put that on my list of things to follow up on!
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.
Thank you, @diegohaz . I'll circle back to you in case we understand where the string & ...
comes from in the resulting type
Flaky tests detected in 9b920e0. 🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/4928472878
|
There is very little reason to use an |
Thanks @andrewhayward |
@ciampo I've added the changes you and I discussed (related to this investigation) to allow Typing |
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.
Left a couple of comments, but overall the changes LGTM 🚀
Feel free to merge once pending comments are addressed (as an exception, let's not consider https://github.com/WordPress/gutenberg/pull/50187/files#r1185450867 blocking for this PR, we can open a follow-up once Diego comes back with an answer)
We should also rebase (or merge trunk) since this PR has stayed open for a while
b38f617
to
0f925c1
Compare
coauthored with @ciampo, who did most of the heavy lifting 💪
What?
Refactor
DropdownMenu
component to TypeScriptPart of #35744
Why?
The refactor to TypeScript has many benefits (auto-generated docs, static linting and error prevention, better IDE experience). See #35744 for more details
How?
Followed the steps in the TypeScript migration guide
Notes
One decision we had to make while typing the component's
toggleProps
prop was which set of props to import fromButton
. We opted forButtonAsButtonProps
because semantically we felt like abutton
made more sense than ananchor
. I looked at current implementations ofDropDownMenu
and didn't see any passing anhref
to the toggle so should be fine with existing usages.In my accessibility searches it didn't look like using a
button
for dropdown menus was a problem but I'd love a more experienced opinion... @andrewhayward do you have any thoughts? 😄Testing Instructions