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

DropdownMenu v2: Render in the default Popover.Slot #51046

Merged
merged 5 commits into from
May 31, 2023

Conversation

ciampo
Copy link
Contributor

@ciampo ciampo commented May 29, 2023

Part of #50459

What?

Tweak the new DropdownMenu's behavior so that, instead of appending its contents in the document.body, it renders by default in the Popover.Slot slot (thus behaving the same way as the legacy DropdownMenu component).

Why?

This is to integrate the new component better with how the editor expects its popover-based components to function.

How?

  • Using the useSlot hook to get the reference to the actual DOM element
  • Using the Popover's default slot name
  • Using React Context to share the DOM element between the main dropdown menu and its submenus

Further considerations

We purposefully decided not to expose any new prop on DropdownMenu for now:

  • we may do so only if / when it will be clear that changing the location of the portal is an actual need
  • we are still investigating whether we'd like to expose a slotName prop (thus keeping the integration with SlotFill an implementation detail of the component), or a portalContainer prop (thus making the component more generic and reusable also for consumers that don't necessarily rely on SlotFill)

Testing Instructions

Open the Storybook example, and verify that the contents of the dropdown menu (including its submenus) all render inside the <div class="popover-slot" /> element.

@ciampo ciampo self-assigned this May 29, 2023
@ciampo ciampo changed the title DropdownMenu v2: integrate with SlotFill DropdownMenu v2: Render in the default Popover.Slot May 29, 2023
@ciampo ciampo force-pushed the feat/dropdown-v2-slotfill branch from ad7d8a8 to 551f91b Compare May 29, 2023 10:23
@ciampo ciampo requested review from mirka and chad1008 May 29, 2023 10:28
@ciampo ciampo added [Type] Enhancement A suggestion for improvement. [Package] Components /packages/components labels May 29, 2023
@ciampo ciampo marked this pull request as ready for review May 29, 2023 10:32
@ciampo ciampo requested a review from ajitbohra as a code owner May 29, 2023 10:32
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.

This is testing nicely for me, and the logic of ensuring the new DropdownMenu is rendered to the same popover slot as the legacy menu sounds good 👍

✅ Double-checked the Radix UI docs that the container passed to the portal is correct
✅ Tests nicely in Storybook with both the root dropdown menu and any sub menus, with the sub menus correctly picking up the portalContainer from context, and each of the dropdown menu levels being rendered as siblings within the popover-slot:

image

LGTM! ✨

Copy link
Member

@tyxla tyxla left a comment

Choose a reason for hiding this comment

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

It works well and the code looks great 👍 🙌

<DropdownMenuStyled.Content
side={ side }
align={ align }
sideOffset={ sideOffset }
alignOffset={ alignOffset }
loop={ true }
>
{ children }
<DropdownMenuPrivateContext.Provider
value={ { portalContainer } }
Copy link
Member

Choose a reason for hiding this comment

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

Any reason why we memoize the context value in #51097 but not here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not really. I worked on #51097 after working on this PR, and realized that memoizing the context's value could have been an improvement in rendering performance.

I'm going to merge this PR as-is, and then decide on whether we should memoize the whole context (or not) after rebasing #51097

@ciampo ciampo merged commit 244c2e5 into trunk May 31, 2023
@ciampo ciampo deleted the feat/dropdown-v2-slotfill branch May 31, 2023 08:01
@github-actions github-actions bot added this to the Gutenberg 16.0 milestone May 31, 2023
sethrubenstein pushed a commit to pewresearch/gutenberg that referenced this pull request Jul 13, 2023
* Add new slotName prop

* Add storybook example

* Use context to use same slot for submenu portal

* Use default popover slot instead of exposing a slotName prop, update Storybook

* CHANGELOG
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Package] Components /packages/components [Type] Enhancement A suggestion for improvement.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants