Skip to content

Commit

Permalink
bodyportal needs to take an id for accessiblilty use (aria-controls)
Browse files Browse the repository at this point in the history
  • Loading branch information
jivey committed Aug 29, 2024
1 parent ba03764 commit 53b3d1f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
6 changes: 4 additions & 2 deletions src/components/BodyPortal.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -179,13 +179,13 @@ describe('BodyPortal', () => {
`);
});

it('takes a ref', () => {
it('takes a ref and ids', () => {
const ref = { current: document.createElement("HEADER") };

render(
<BodyPortalSlotsContext.Provider value={['header', 'root']}>
<div>
<BodyPortal slot='header' tagName='header' externalRef={ref}>
<BodyPortal slot='header' tagName='header' externalRef={ref} id="orange" data-testid="blue">
Now you're thinking with portals
</BodyPortal>
</div>
Expand All @@ -197,6 +197,8 @@ describe('BodyPortal', () => {
<body>
<header
data-portal-slot="header"
data-testid="blue"
id="orange"
>
Now you're thinking with portals
</header>
Expand Down
7 changes: 6 additions & 1 deletion src/components/BodyPortal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,15 @@ const getInsertBeforeTarget = (bodyPortalSlots: string[], slot?: string) => {
}

export const BodyPortal = ({
children, className, role, slot, tagName, externalRef, ...props
children, className, role, slot, tagName, externalRef, id, ...props
}: React.PropsWithChildren<{
className?: string;
role?: string;
slot?: string;
tagName?: string;
externalRef?: React.MutableRefObject<HTMLElement>;
'data-testid'?: string;
id?: string;
}>) => {
const tag = tagName?.toUpperCase() ?? 'DIV';
const ref = externalRef ?? React.useRef<HTMLElement>(document.createElement(tag));
Expand All @@ -44,6 +45,8 @@ export const BodyPortal = ({

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

if (id) { element.id = id; }

if (role) { element.setAttribute('role', role); }

if (slot) { element.dataset.portalSlot = slot; }
Expand All @@ -63,6 +66,8 @@ export const BodyPortal = ({

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

if (id) { element.id = ''; }

if (props['data-testid']) { delete element.dataset.testid; }
};
}, [bodyPortalOrderedRefs, className, role, slot, tag]);
Expand Down

0 comments on commit 53b3d1f

Please sign in to comment.