Skip to content

Commit

Permalink
fix: make menu, popover, select ssr friendly (#1660)
Browse files Browse the repository at this point in the history
* fix: make createportal ssr friendly

* refactor: change conditional to follow docs
  • Loading branch information
jinlee93 authored Jun 16, 2023
1 parent bbee463 commit eac8829
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 25 deletions.
12 changes: 8 additions & 4 deletions src/components/Menu/Menu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -159,10 +159,14 @@ const MenuItems = (props: MenuItemsProps) => {
...props,
...popperAttributes,
};

return (
<>{createPortal(<HeadlessMenu.Items {...optionProps} />, document.body)}</>
);
if (typeof document !== 'undefined') {
return (
<>
{createPortal(<HeadlessMenu.Items {...optionProps} />, document.body)}
</>
);
}
return null;
};

/**
Expand Down
36 changes: 19 additions & 17 deletions src/components/Popover/Popover.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -138,23 +138,25 @@ const PopoverContent = ({
};

const componentClassName = clsx(styles['popover-content'], className);

return (
<>
{createPortal(
<HeadlessPopover.Panel
{...allProps}
as="article"
className={componentClassName}
>
<PopoverContainer className={bodyClassName}>
{children as React.ReactNode}
</PopoverContainer>
</HeadlessPopover.Panel>,
document.body,
)}
</>
);
if (typeof document !== 'undefined') {
return (
<>
{createPortal(
<HeadlessPopover.Panel
{...allProps}
as="article"
className={componentClassName}
>
<PopoverContainer className={bodyClassName}>
{children as React.ReactNode}
</PopoverContainer>
</HeadlessPopover.Panel>,
document.body,
)}
</>
);
}
return null;
};

/**
Expand Down
10 changes: 6 additions & 4 deletions src/components/Select/Select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -247,10 +247,12 @@ const SelectOptions = function (props: PropsWithRenderProp<{ open: boolean }>) {
...popperAttributes,
...other,
};

return (
<>{createPortal(<Listbox.Options {...optionProps} />, document.body)}</>
);
if (typeof document !== 'undefined') {
return (
<>{createPortal(<Listbox.Options {...optionProps} />, document.body)}</>
);
}
return null;
};

type SelectOptionProps = {
Expand Down

0 comments on commit eac8829

Please sign in to comment.