diff --git a/src/components/BodyPortal.spec.tsx b/src/components/BodyPortal.spec.tsx index 7c4561c1e..f21873ae9 100644 --- a/src/components/BodyPortal.spec.tsx +++ b/src/components/BodyPortal.spec.tsx @@ -179,13 +179,13 @@ describe('BodyPortal', () => { `); }); - it('takes a ref', () => { + it('takes a ref and ids', () => { const ref = { current: document.createElement("HEADER") }; render(
- + Now you're thinking with portals
@@ -197,6 +197,8 @@ describe('BodyPortal', () => {
Now you're thinking with portals
diff --git a/src/components/BodyPortal.tsx b/src/components/BodyPortal.tsx index 4a93fd18e..07ed7e30a 100644 --- a/src/components/BodyPortal.tsx +++ b/src/components/BodyPortal.tsx @@ -22,7 +22,7 @@ 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; @@ -30,6 +30,7 @@ export const BodyPortal = ({ tagName?: string; externalRef?: React.MutableRefObject; 'data-testid'?: string; + id?: string; }>) => { const tag = tagName?.toUpperCase() ?? 'DIV'; const ref = externalRef ?? React.useRef(document.createElement(tag)); @@ -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; } @@ -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]);