Skip to content

Commit

Permalink
feat(Dropdown next): able to append the menu to any DOM element (#9652)
Browse files Browse the repository at this point in the history
  • Loading branch information
adamviktora authored Oct 6, 2023
1 parent 19312a9 commit a238182
Showing 1 changed file with 22 additions and 10 deletions.
32 changes: 22 additions & 10 deletions packages/react-core/src/next/components/Dropdown/Dropdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ export interface DropdownProps extends MenuProps, OUIAProps {
ouiaSafe?: boolean;
/** z-index of the dropdown menu */
zIndex?: number;
/** The container to append the dropdown to. Defaults to 'inline'.
* If your dropdown is being cut off you can append it to an element higher up the DOM tree.
* Some examples:
* appendTo="inline"
* appendTo={() => document.body}
* appendTo={document.getElementById('target')}
*/
appendTo?: HTMLElement | (() => HTMLElement) | 'inline';
}

const DropdownBase: React.FunctionComponent<DropdownProps> = ({
Expand All @@ -48,6 +56,7 @@ const DropdownBase: React.FunctionComponent<DropdownProps> = ({
ouiaId,
ouiaSafe = true,
zIndex = 9999,
appendTo = 'inline',
...props
}: DropdownProps) => {
const localMenuRef = React.useRef<HTMLDivElement>();
Expand Down Expand Up @@ -109,21 +118,24 @@ const DropdownBase: React.FunctionComponent<DropdownProps> = ({
} as React.CSSProperties
})}
{...props}
{...ouiaProps}
>
<MenuContent>{children}</MenuContent>
</Menu>
);
return (
<div ref={containerRef} {...ouiaProps}>
<Popper
trigger={toggle(toggleRef)}
removeFindDomNode
popper={menu}
appendTo={containerRef.current || undefined}
isVisible={isOpen}
zIndex={zIndex}
/>
const popperProps = {
trigger: toggle(toggleRef),
removeFindDomNode: true,
popper: menu,
isVisible: isOpen,
zIndex
};
return appendTo === 'inline' ? (
<div ref={containerRef}>
<Popper {...popperProps} appendTo={containerRef.current || undefined} />
</div>
) : (
<Popper {...popperProps} appendTo={appendTo} />
);
};

Expand Down

0 comments on commit a238182

Please sign in to comment.