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

Support controlling open/closed state for Dropdown and DropdownMenu #54257

Merged
merged 10 commits into from
Sep 11, 2023

Conversation

ciampo
Copy link
Contributor

@ciampo ciampo commented Sep 7, 2023

What?

Add support to control the open state of Dropdown and DropdownMenu components

Why?

To give more choice to consumers of the components on how to use such components (see for example #54083)

How?

By adding open / onToggle / defaultOpen props, using the useControlledValue hook and refactoring as needed.

Testing Instructions

  • Tweak Storybook examples locally to use Dropdown and DropdownMenu in controlled mode (see d8379d9 and 2d35f36 to see the required changes)
  • Test the tweaked Storybook examples on your machine (npm run storybook:dev) and make sure that the components continue to work as expected

✍️ Dev note

The open/closed state of the Dropdown and DropdownMenu components can now be controlled by their consumers via the open, onToggle and defaultOpen props, allowing more flexibility and more advanced behaviors when using these components.

@ciampo
Copy link
Contributor Author

ciampo commented Sep 7, 2023

Unit tests are broken, so we;d need to look into that and see if this approach is still feasible without introducing breaking changes.

Comment on lines -79 to -86
useEffect(
() => () => {
if ( onToggle && isOpen ) {
onToggle( false );
}
},
[ onToggle, isOpen ]
);
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice one, this is the removal that I was missing from my experiment 🎉

Copy link
Contributor

@andrewserong andrewserong left a comment

Choose a reason for hiding this comment

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

Nice one, this PR unblocks #54083 for me!

Unit tests are broken, so we;d need to look into that and see if this approach is still feasible without introducing breaking changes.

I've pulled in most of the changes here into #54083 where the unit tests are passing. Are they failing in this PR because of the temporarily commented out close() call in closeIfFocusOutside()?

Edit: the unit tests are passing over in #54083, but the e2es are failing, so there's likely still a bit to fix up. Still, I'm feeling confident about the direction, now!

Edit 2: I think I found the source of the e2e failures on the other PR #54083 — turns out I needed to explicitly clear the state in a couple of places, and in one e2e wait for the menu to be open before firing a keyboard shortcut. So, all up, looking pretty good so far 🙂

@ciampo
Copy link
Contributor Author

ciampo commented Sep 8, 2023

Cool! In that case, we can close this PR as it served its purpose :)

@ciampo ciampo closed this Sep 8, 2023
@ciampo ciampo reopened this Sep 8, 2023
@ciampo
Copy link
Contributor Author

ciampo commented Sep 8, 2023

Just seen #54083 (comment) — I'm going to reopen, so that we can use this PR to make changes to the components and keep the rest of the change sin #54083

@ciampo ciampo self-assigned this Sep 8, 2023
@ciampo ciampo added the [Package] Components /packages/components label Sep 8, 2023
@ciampo ciampo changed the title Try: allow dropdown and dropdowmnenu open state to be controlled, try to keep only one dropdownmenu open at a time Support controlling open/closed state for Dropdown and DropdownMenu Sep 8, 2023
@ciampo ciampo force-pushed the feat/dropdown-controlled branch 2 times, most recently from 15b31be to 6ea9b0a Compare September 8, 2023 12:14
@ciampo ciampo force-pushed the feat/dropdown-controlled branch from 6ea9b0a to ab6e115 Compare September 8, 2023 12:16
@ciampo ciampo marked this pull request as ready for review September 8, 2023 12:25
@ciampo ciampo requested a review from ajitbohra as a code owner September 8, 2023 12:25
@ciampo ciampo added the [Type] Enhancement A suggestion for improvement. label Sep 8, 2023
@ciampo ciampo requested review from chad1008 and brookewp September 8, 2023 13:07
@@ -2,16 +2,13 @@

## Unreleased

### Breaking changes
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Moved this entry to the previous release section, since I realised that the corresponding PR was merged before August 31st

Copy link
Contributor

@andrewserong andrewserong left a comment

Choose a reason for hiding this comment

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

Thanks again for all the work here, this looks great to me!

✅ The structure of the new controlled props matches what I need to get #54083 working correctly
✅ Code looks good, and documentation changes read well to me
✅ Smoke tested the Dropdown and DropdownMenu components in Storybook and across usage in the post and site editors

LGTM! ✨

@ciampo ciampo merged commit d5ade48 into trunk Sep 11, 2023
53 of 55 checks passed
@ciampo ciampo deleted the feat/dropdown-controlled branch September 11, 2023 07:09
@github-actions github-actions bot added this to the Gutenberg 16.7 milestone Sep 11, 2023
@ciampo ciampo added the has dev note when dev note is done (for upcoming WordPress release) label Oct 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
has dev note when dev note is done (for upcoming WordPress release) [Package] Components /packages/components [Type] Enhancement A suggestion for improvement.
Projects
Status: Done 🎉
Development

Successfully merging this pull request may close these issues.

2 participants