Skip to content

Commit

Permalink
Wrap the dropdown in a Portal (#153)
Browse files Browse the repository at this point in the history
  • Loading branch information
dalefukami authored Nov 9, 2023
1 parent 9009c9c commit ecffbd4
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 20 deletions.
7 changes: 5 additions & 2 deletions src/components/DropdownMenu/DropdownMenu.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ jest.mock('@radix-ui/react-dropdown-menu', () => ({
Arrow: (props: DropdownMenuArrowProps) => {
mockRadixArrow(props);
return <div data-testid="arrow">{props.children}</div>;
},
DropdownMenuPortal: (props: any) => {
return <div data-testid="portal">{props.children}</div>;
}
}));

Expand All @@ -70,11 +73,11 @@ describe('<DropdownMenu />', () => {
const root = container.firstChild;
const trigger = screen.getByTestId('trigger');
const content = screen.getByTestId('content');
const portal = screen.getByTestId('portal');

// Check that trigger and content are children of root
expect(root).toBe(screen.getByTestId('root'));
expect(trigger.parentElement).toBe(root);
expect(content.parentElement).toBe(root);
expect(content.parentElement).toBe(portal);

// Check that items are only rendered in the dropdown content
const items = container.getElementsByClassName('mock-item');
Expand Down
39 changes: 21 additions & 18 deletions src/components/DropdownMenu/DropdownMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import {
Root as RadixUIDropdownMenuRoot,
Trigger as RadixUIDropdownMenuTrigger,
Content as RadixUIDropdownMenuContent,
Item as RadixUIDropdownMenuItem
Item as RadixUIDropdownMenuItem,
DropdownMenuPortal as RadixDropdownMenuPortal
} from '@radix-ui/react-dropdown-menu';

import './DropdownMenu.scss';
Expand Down Expand Up @@ -70,23 +71,25 @@ export const DropdownMenu = forwardRef<HTMLDivElement, DropdownMenuProps>(
<RadixUIDropdownMenuTrigger className={classNames(className, 'zui-dropdown-trigger')}>
{triggerElement}
</RadixUIDropdownMenuTrigger>
<RadixUIDropdownMenuContent
className={classNames(menuClassName, 'zui-dropdown-menu')}
align={alignMenu}
side={side}
ref={ref}
>
{alignMenu === 'center' && showArrow && <RadixUIDropdownMenuArrow className="zui-dropdown-arrow" />}
{items.map(item => (
<RadixUIDropdownMenuItem
className={classNames('zui-dropdown-item', item.className)}
key={item.id}
onSelect={(event: Event) => item.onSelect(event)}
>
{item.label}
</RadixUIDropdownMenuItem>
))}
</RadixUIDropdownMenuContent>
<RadixDropdownMenuPortal>
<RadixUIDropdownMenuContent
className={classNames(menuClassName, 'zui-dropdown-menu')}
align={alignMenu}
side={side}
ref={ref}
>
{alignMenu === 'center' && showArrow && <RadixUIDropdownMenuArrow className="zui-dropdown-arrow" />}
{items.map(item => (
<RadixUIDropdownMenuItem
className={classNames('zui-dropdown-item', item.className)}
key={item.id}
onSelect={(event: Event) => item.onSelect(event)}
>
{item.label}
</RadixUIDropdownMenuItem>
))}
</RadixUIDropdownMenuContent>
</RadixDropdownMenuPortal>
</RadixUIDropdownMenuRoot>
);
}
Expand Down
3 changes: 3 additions & 0 deletions src/components/SelectInput/SelectInput.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ jest.mock('@radix-ui/react-dropdown-menu', () => ({
Item: (props: DropdownMenuItemProps) => {
mockRadixItem(props);
return <div className={'mock-item ' + props.className}>{props.children}</div>;
},
DropdownMenuPortal: (props: any) => {
return <div data-testid="portal">{props.children}</div>;
}
}));

Expand Down

0 comments on commit ecffbd4

Please sign in to comment.