Skip to content

Commit a238182

Browse files
authoredOct 6, 2023
feat(Dropdown next): able to append the menu to any DOM element (#9652)
1 parent 19312a9 commit a238182

File tree

1 file changed

+22
-10
lines changed
  • packages/react-core/src/next/components/Dropdown

1 file changed

+22
-10
lines changed
 

‎packages/react-core/src/next/components/Dropdown/Dropdown.tsx

+22-10
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,14 @@ export interface DropdownProps extends MenuProps, OUIAProps {
3232
ouiaSafe?: boolean;
3333
/** z-index of the dropdown menu */
3434
zIndex?: number;
35+
/** The container to append the dropdown to. Defaults to 'inline'.
36+
* If your dropdown is being cut off you can append it to an element higher up the DOM tree.
37+
* Some examples:
38+
* appendTo="inline"
39+
* appendTo={() => document.body}
40+
* appendTo={document.getElementById('target')}
41+
*/
42+
appendTo?: HTMLElement | (() => HTMLElement) | 'inline';
3543
}
3644

3745
const DropdownBase: React.FunctionComponent<DropdownProps> = ({
@@ -48,6 +56,7 @@ const DropdownBase: React.FunctionComponent<DropdownProps> = ({
4856
ouiaId,
4957
ouiaSafe = true,
5058
zIndex = 9999,
59+
appendTo = 'inline',
5160
...props
5261
}: DropdownProps) => {
5362
const localMenuRef = React.useRef<HTMLDivElement>();
@@ -109,21 +118,24 @@ const DropdownBase: React.FunctionComponent<DropdownProps> = ({
109118
} as React.CSSProperties
110119
})}
111120
{...props}
121+
{...ouiaProps}
112122
>
113123
<MenuContent>{children}</MenuContent>
114124
</Menu>
115125
);
116-
return (
117-
<div ref={containerRef} {...ouiaProps}>
118-
<Popper
119-
trigger={toggle(toggleRef)}
120-
removeFindDomNode
121-
popper={menu}
122-
appendTo={containerRef.current || undefined}
123-
isVisible={isOpen}
124-
zIndex={zIndex}
125-
/>
126+
const popperProps = {
127+
trigger: toggle(toggleRef),
128+
removeFindDomNode: true,
129+
popper: menu,
130+
isVisible: isOpen,
131+
zIndex
132+
};
133+
return appendTo === 'inline' ? (
134+
<div ref={containerRef}>
135+
<Popper {...popperProps} appendTo={containerRef.current || undefined} />
126136
</div>
137+
) : (
138+
<Popper {...popperProps} appendTo={appendTo} />
127139
);
128140
};
129141

0 commit comments

Comments
 (0)