Skip to content

Commit

Permalink
DropdownMenu v2: Render in the default Popover.Slot (WordPress#51046)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
ciampo authored and sethrubenstein committed Jul 13, 2023
1 parent 851a07b commit de6e39b
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 6 deletions.
1 change: 1 addition & 0 deletions packages/components/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
### Experimental

- `DropdownMenu` v2: Tweak styles ([#50967](https://github.com/WordPress/gutenberg/pull/50967)).
- `DropdownMenu` v2: Render in the default `Popover.Slot` ([#51046](https://github.com/WordPress/gutenberg/pull/51046)).

## 25.0.0 (2023-05-24)

Expand Down
26 changes: 22 additions & 4 deletions packages/components/src/dropdown-menu-v2/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@ import * as DropdownMenuPrimitive from '@radix-ui/react-dropdown-menu';
/**
* WordPress dependencies
*/
import { forwardRef } from '@wordpress/element';
import { forwardRef, createContext, useContext } from '@wordpress/element';
import { isRTL } from '@wordpress/i18n';
import { check, chevronRightSmall, lineSolid } from '@wordpress/icons';
import { SVG, Circle } from '@wordpress/primitives';

/**
* Internal dependencies
*/
import { useSlot } from '../slot-fill';
import Icon from '../icon';
import { SLOT_NAME as POPOVER_DEFAULT_SLOT_NAME } from '../popover';
import * as DropdownMenuStyled from './styles';
import type {
DropdownMenuProps,
Expand All @@ -34,6 +36,12 @@ const SUB_MENU_OFFSET_SIDE = 12;
// Opposite amount of the top padding of the menu item
const SUB_MENU_OFFSET_ALIGN = -8;

const DropdownMenuPrivateContext = createContext< {
portalContainer: HTMLElement | null;
} >( {
portalContainer: null,
} );

/**
* `DropdownMenu` displays a menu to the user (such as a set of actions
* or functions) triggered by a button.
Expand All @@ -53,6 +61,10 @@ export const DropdownMenu = ( {
children,
trigger,
}: DropdownMenuProps ) => {
// Render the portal in the default slot used by the legacy Popover component.
const slot = useSlot( POPOVER_DEFAULT_SLOT_NAME );
const portalContainer = slot.ref?.current;

return (
<DropdownMenuPrimitive.Root
defaultOpen={ defaultOpen }
Expand All @@ -64,15 +76,19 @@ export const DropdownMenu = ( {
<DropdownMenuPrimitive.Trigger asChild>
{ trigger }
</DropdownMenuPrimitive.Trigger>
<DropdownMenuPrimitive.Portal>
<DropdownMenuPrimitive.Portal container={ portalContainer }>
<DropdownMenuStyled.Content
side={ side }
align={ align }
sideOffset={ sideOffset }
alignOffset={ alignOffset }
loop={ true }
>
{ children }
<DropdownMenuPrivateContext.Provider
value={ { portalContainer } }
>
{ children }
</DropdownMenuPrivateContext.Provider>
</DropdownMenuStyled.Content>
</DropdownMenuPrimitive.Portal>
</DropdownMenuPrimitive.Root>
Expand Down Expand Up @@ -118,6 +134,8 @@ export const DropdownSubMenu = ( {
children,
trigger,
}: DropdownSubMenuProps ) => {
const { portalContainer } = useContext( DropdownMenuPrivateContext );

return (
<DropdownMenuPrimitive.Sub
defaultOpen={ defaultOpen }
Expand All @@ -130,7 +148,7 @@ export const DropdownSubMenu = ( {
>
{ trigger }
</DropdownMenuStyled.SubTrigger>
<DropdownMenuPrimitive.Portal>
<DropdownMenuPrimitive.Portal container={ portalContainer }>
<DropdownMenuStyled.SubContent
loop
sideOffset={ SUB_MENU_OFFSET_SIDE }
Expand Down
8 changes: 7 additions & 1 deletion packages/components/src/dropdown-menu-v2/stories/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import {
DropdownSubMenuTrigger,
} from '..';
import Button from '../../button';
import Popover from '../../popover';
import { Provider as SlotFillProvider } from '../../slot-fill';

/**
* WordPress dependencies
Expand Down Expand Up @@ -127,7 +129,11 @@ const RadioItemsGroup = () => {
};

const Template: ComponentStory< typeof DropdownMenu > = ( props ) => (
<DropdownMenu { ...props } />
<SlotFillProvider>
<DropdownMenu { ...props } />
{ /* @ts-expect-error Slot is not currently typed on Popover */ }
<Popover.Slot />
</SlotFillProvider>
);
export const Default = Template.bind( {} );
Default.args = {
Expand Down
2 changes: 1 addition & 1 deletion packages/components/src/popover/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ import { overlayMiddlewares } from './overlay-middlewares';
*
* @type {string}
*/
const SLOT_NAME = 'Popover';
export const SLOT_NAME = 'Popover';

// An SVG displaying a triangle facing down, filled with a solid
// color and bordered in such a way to create an arrow-like effect.
Expand Down

0 comments on commit de6e39b

Please sign in to comment.