Skip to content

Commit

Permalink
Allow BodyPortals to accept ref parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
Dantemss committed Aug 23, 2024
1 parent 78ef540 commit 7f4367c
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions src/components/BodyPortal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,25 @@ const getInsertBeforeTarget = (bodyPortalSlots: string[], slot?: string) => {
}

export const BodyPortal = ({
children, className, role, slot, tagName
}: React.PropsWithChildren<{className?: string; role?: string; slot?: string; tagName?: string}>) => {
children, className, ref, role, slot, tagName
}: React.PropsWithChildren<{
className?: string;
ref?: React.MutableRefObject<HTMLElement | null>;
role?: string;
slot?: string;
tagName?: string
}>) => {
const tag = tagName?.toUpperCase() ?? 'DIV';
const ref = React.useRef<HTMLElement>(document.createElement(tag));
if (ref.current.tagName !== tag) {
ref.current = document.createElement(tag);
const internalRef = React.useRef<HTMLElement>(document.createElement(tag));
if (internalRef.current.tagName !== tag) {
internalRef.current = document.createElement(tag);
}
if (ref) { ref.current = internalRef.current; }

const bodyPortalOrderedRefs = React.useContext(BodyPortalSlotsContext);

React.useLayoutEffect(() => {
const element = ref.current;
const element = internalRef.current;

if (className) { element.classList.add(...className.split(' ')); }

Expand All @@ -56,5 +63,5 @@ export const BodyPortal = ({
};
}, [bodyPortalOrderedRefs, className, role, slot, tag]);

return createPortal(children, ref.current);
return createPortal(children, internalRef.current);
};

0 comments on commit 7f4367c

Please sign in to comment.