Skip to content

Commit

Permalink
Fixed SSR error in Menu (#16559)
Browse files Browse the repository at this point in the history
* fix: fixed SSR error

* fix: fixed error with render
  • Loading branch information
guidari authored Jun 17, 2024
1 parent c02f48c commit 6cbcba7
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions packages/react/src/components/Menu/Menu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { warning } from '../../internal/warning.js';

import { MenuContext, menuReducer } from './MenuContext';
import { useLayoutDirection } from '../LayoutDirection';
import { canUseDOM } from '../../internal/environment';

const spacing = 8; // distance to keep to window edges, in px

Expand Down Expand Up @@ -87,7 +88,7 @@ interface MenuProps extends React.HTMLAttributes<HTMLUListElement> {
/**
* Specify a DOM node where the Menu should be rendered in. Defaults to document.body.
*/
target?: HTMLElement;
target?: Element;

/**
* Specify the x position of the Menu. Either pass a single number or an array with two numbers describing your activator's boundaries ([x1, x2])
Expand Down Expand Up @@ -115,9 +116,8 @@ const Menu = forwardRef<HTMLUListElement, MenuProps>(function Menu(
open,
size = 'sm',
legacyAutoalign = 'true',
// TODO: #16004
// eslint-disable-next-line ssr-friendly/no-dom-globals-in-react-fc
target = document.body,
target = canUseDOM && document.body,
x = 0,
y = 0,
...rest
Expand Down Expand Up @@ -442,6 +442,10 @@ const Menu = forwardRef<HTMLUListElement, MenuProps>(function Menu(
</MenuContext.Provider>
);

if (!target) {
return rendered;
}

return isRoot ? (open && createPortal(rendered, target)) || null : rendered;
});

Expand Down

0 comments on commit 6cbcba7

Please sign in to comment.