@@ -6,15 +6,17 @@ import * as DropdownMenuPrimitive from '@radix-ui/react-dropdown-menu';
6
6
/**
7
7
* WordPress dependencies
8
8
*/
9
- import { forwardRef } from '@wordpress/element' ;
9
+ import { forwardRef , createContext , useContext } from '@wordpress/element' ;
10
10
import { isRTL } from '@wordpress/i18n' ;
11
11
import { check , chevronRightSmall , lineSolid } from '@wordpress/icons' ;
12
12
import { SVG , Circle } from '@wordpress/primitives' ;
13
13
14
14
/**
15
15
* Internal dependencies
16
16
*/
17
+ import { useSlot } from '../slot-fill' ;
17
18
import Icon from '../icon' ;
19
+ import { SLOT_NAME as POPOVER_DEFAULT_SLOT_NAME } from '../popover' ;
18
20
import * as DropdownMenuStyled from './styles' ;
19
21
import type {
20
22
DropdownMenuProps ,
@@ -34,6 +36,12 @@ const SUB_MENU_OFFSET_SIDE = 12;
34
36
// Opposite amount of the top padding of the menu item
35
37
const SUB_MENU_OFFSET_ALIGN = - 8 ;
36
38
39
+ const DropdownMenuPrivateContext = createContext < {
40
+ portalContainer : HTMLElement | null ;
41
+ } > ( {
42
+ portalContainer : null ,
43
+ } ) ;
44
+
37
45
/**
38
46
* `DropdownMenu` displays a menu to the user (such as a set of actions
39
47
* or functions) triggered by a button.
@@ -53,6 +61,10 @@ export const DropdownMenu = ( {
53
61
children,
54
62
trigger,
55
63
} : DropdownMenuProps ) => {
64
+ // Render the portal in the default slot used by the legacy Popover component.
65
+ const slot = useSlot ( POPOVER_DEFAULT_SLOT_NAME ) ;
66
+ const portalContainer = slot . ref ?. current ;
67
+
56
68
return (
57
69
< DropdownMenuPrimitive . Root
58
70
defaultOpen = { defaultOpen }
@@ -64,15 +76,19 @@ export const DropdownMenu = ( {
64
76
< DropdownMenuPrimitive . Trigger asChild >
65
77
{ trigger }
66
78
</ DropdownMenuPrimitive . Trigger >
67
- < DropdownMenuPrimitive . Portal >
79
+ < DropdownMenuPrimitive . Portal container = { portalContainer } >
68
80
< DropdownMenuStyled . Content
69
81
side = { side }
70
82
align = { align }
71
83
sideOffset = { sideOffset }
72
84
alignOffset = { alignOffset }
73
85
loop = { true }
74
86
>
75
- { children }
87
+ < DropdownMenuPrivateContext . Provider
88
+ value = { { portalContainer } }
89
+ >
90
+ { children }
91
+ </ DropdownMenuPrivateContext . Provider >
76
92
</ DropdownMenuStyled . Content >
77
93
</ DropdownMenuPrimitive . Portal >
78
94
</ DropdownMenuPrimitive . Root >
@@ -118,6 +134,8 @@ export const DropdownSubMenu = ( {
118
134
children,
119
135
trigger,
120
136
} : DropdownSubMenuProps ) => {
137
+ const { portalContainer } = useContext ( DropdownMenuPrivateContext ) ;
138
+
121
139
return (
122
140
< DropdownMenuPrimitive . Sub
123
141
defaultOpen = { defaultOpen }
@@ -130,7 +148,7 @@ export const DropdownSubMenu = ( {
130
148
>
131
149
{ trigger }
132
150
</ DropdownMenuStyled . SubTrigger >
133
- < DropdownMenuPrimitive . Portal >
151
+ < DropdownMenuPrimitive . Portal container = { portalContainer } >
134
152
< DropdownMenuStyled . SubContent
135
153
loop
136
154
sideOffset = { SUB_MENU_OFFSET_SIDE }
0 commit comments