Skip to content

Commit

Permalink
Merge branch 'main' into cookieyes
Browse files Browse the repository at this point in the history
  • Loading branch information
Dantemss authored Feb 20, 2025
2 parents f9bdf4d + 30babb1 commit 4056181
Show file tree
Hide file tree
Showing 10 changed files with 49 additions and 12 deletions.
5 changes: 3 additions & 2 deletions src/components/BodyPortal.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -235,10 +235,10 @@ describe('BodyPortal', () => {
`);
});

it('takes an id and testid', () => {
it('takes an id, testid and ariaLabel', () => {
render(
<BodyPortalSlotsContext.Provider value={['header', 'root']}>
<BodyPortal slot='header' tagName='header' id='orange' data-testid='blue'>
<BodyPortal slot='header' tagName='header' id='orange' data-testid='blue' ariaLabel='orange'>
Now you're thinking with portals
</BodyPortal>
</BodyPortalSlotsContext.Provider>,
Expand All @@ -248,6 +248,7 @@ describe('BodyPortal', () => {
expect(document.body).toMatchInlineSnapshot(`
<body>
<header
aria-label="orange"
data-portal-slot="header"
data-testid="blue"
id="orange"
Expand Down
9 changes: 7 additions & 2 deletions src/components/BodyPortal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@ export type BodyPortalProps = React.PropsWithChildren<{
tagName?: string;
id?: string;
'data-testid'?: string;
ariaLabel?: string;
}>;

export const BodyPortal = React.forwardRef<HTMLElement, BodyPortalProps>((
{ children, className, role, slot, tagName, id, ...props }, ref?: React.ForwardedRef<HTMLElement>
{ children, className, role, slot, tagName, id, ariaLabel, ...props }, ref?: React.ForwardedRef<HTMLElement>
) => {
const tag = tagName?.toUpperCase() ?? 'DIV';
const internalRef = React.useRef<HTMLElement>(document.createElement(tag));
Expand Down Expand Up @@ -62,6 +63,8 @@ export const BodyPortal = React.forwardRef<HTMLElement, BodyPortalProps>((

if (slot) { element.dataset.portalSlot = slot; }

if (ariaLabel) element.setAttribute('aria-label', ariaLabel);

document.body.insertBefore(element, getInsertBeforeTarget(bodyPortalOrderedRefs, slot));

return () => {
Expand All @@ -73,13 +76,15 @@ export const BodyPortal = React.forwardRef<HTMLElement, BodyPortalProps>((

if (role) { element.removeAttribute('role'); }

if (ariaLabel) { element.removeAttribute('aria-label'); }

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

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

if (testId) { delete element.dataset.testid; }
};
}, [bodyPortalOrderedRefs, className, id, role, slot, tag, testId]);
}, [bodyPortalOrderedRefs, className, id, role, slot, ariaLabel, tag, testId]);

return createPortal(children, internalRef.current);
});
5 changes: 5 additions & 0 deletions src/components/NavBar.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ describe('NavBar', () => {
expect(document.body).toMatchSnapshot();
});

it('sets the ariaLabel', () => {
render(<NavBar ariaLabel='test' maxWidth={128}>NavBar content</NavBar>, { container: root });
expect(document.body).toMatchSnapshot();
});

describe('with a logo', () => {
it('matches snapshot', () => {
render(<NavBar logo={true}>NavBar content</NavBar>, { container: root });
Expand Down
5 changes: 3 additions & 2 deletions src/components/NavBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,16 @@ type NavBarProps = React.PropsWithChildren<{
navMobileHeight?: number;
logo?: boolean | Logo;
justifyContent?: string;
ariaLabel?: string;
}>

export const NavBar = ({ logo = false, maxWidth, navDesktopHeight, navMobileHeight, justifyContent, ...props }: NavBarProps) => {
export const NavBar = ({ logo = false, maxWidth, navDesktopHeight, navMobileHeight, justifyContent, ariaLabel, ...props }: NavBarProps) => {
const logoIsObject = typeof logo === 'object';
const renderAnchor = logoIsObject && 'href' in logo;
const {alt = 'OpenStax Logo', ...anchorProps} = logoIsObject ? logo : {};
const logoComponent = logo ? <OpenstaxLogo alt={alt} /> : null;

return <BarWrapper tagName='nav' slot='nav' {...props}>
return <BarWrapper tagName='nav' ariaLabel={ariaLabel} slot='nav' {...props}>
<StyledNavBar
maxWidth={maxWidth}
navDesktopHeight={navDesktopHeight || Constants.navDesktopHeight}
Expand Down
2 changes: 1 addition & 1 deletion src/components/NavBarButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export const NavBarButton = styled(
"aria-label": ariaLabel,
...props
}: NavBarButtonProps) => (
<Button className={className} {...props}>
<Button className={className} aria-label={ariaLabel} {...props}>
{icon &&
(typeof icon === "string" ? (
<img aria-hidden="true" src={icon} alt="" />
Expand Down
6 changes: 3 additions & 3 deletions src/components/SidebarNav.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ const SidebarNavAndMain = () => {
<>
<GlobalStyle />
<Wrapper>
<StyledSidebarNav>
<StyledSidebarNav ariaLabel="Main navigation">
{({ setNavIsCollapsed, navIsCollapsed, isMobile }) => (
<NavItemsList
items={items}
Expand Down Expand Up @@ -227,7 +227,7 @@ const BodyPortalSidebarNavAndMain = () => {
<BodyPortalSlotsContext.Provider value={["sidebar", "nav", "main"]}>
<BodyPortalGlobalStyle />
<Wrapper>
<StyledBodyPortalSidebarNav navHeader={<NavBarLogo alt="logo" />}>
<StyledBodyPortalSidebarNav ariaLabel="Header navigation" navHeader={<NavBarLogo alt="logo" />}>
{({ setNavIsCollapsed, navIsCollapsed, isMobile }) => (
<NavItemsList
items={items}
Expand All @@ -237,7 +237,7 @@ const BodyPortalSidebarNavAndMain = () => {
/>
)}
</StyledBodyPortalSidebarNav>
<NavBar>
<NavBar ariaLabel="Main navigation">
<h1>Title</h1>
<InfoMenuButton label="Menu">
<PopoverContainer>
Expand Down
7 changes: 5 additions & 2 deletions src/components/SidebarNav/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ interface SidebarNavSharedProps {
mobileBreakpoint?: string;
isMobile?: boolean;
className?: string;
ariaLabel?: string;
}

export const SidebarNavBase = ({
Expand Down Expand Up @@ -157,7 +158,7 @@ export const SidebarNavBase = ({
};

export const SidebarNav = styled(
({ className, id, ...props }: SidebarNavSharedProps) => {
({ className, id, ariaLabel, ...props }: SidebarNavSharedProps) => {
const { isMobile, navIsCollapsed, setNavIsCollapsed } =
useSidebarNavProps(props);
const sidebarNavRef = React.useRef<HTMLElement>(null);
Expand All @@ -176,6 +177,7 @@ export const SidebarNav = styled(
id={id}
ref={sidebarNavRef}
data-testid="sidebarnav"
aria-label={ariaLabel}
className={classNames(className, {
collapsed: navIsCollapsed,
mobile: isMobile,
Expand All @@ -200,7 +202,7 @@ export const SidebarNav = styled(
`;

export const BodyPortalSidebarNav = styled(
({ className, id, ...props }: SidebarNavSharedProps) => {
({ className, id, ariaLabel, ...props }: SidebarNavSharedProps) => {
const { isMobile, navIsCollapsed, setNavIsCollapsed } =
useSidebarNavProps(props);

Expand All @@ -222,6 +224,7 @@ export const BodyPortalSidebarNav = styled(
tagName="nav"
slot="sidebar"
data-testid="sidebarnav"
ariaLabel={ariaLabel}
className={classNames(className, {
collapsed: navIsCollapsed,
mobile: isMobile,
Expand Down
19 changes: 19 additions & 0 deletions src/components/__snapshots__/NavBar.spec.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,25 @@ exports[`NavBar matches snapshot 1`] = `
</body>
`;

exports[`NavBar sets the ariaLabel 1`] = `
<body>
<main
id="root"
/>
<nav
aria-label="test"
class="sc-gsnTZi cnSXvF"
data-portal-slot="nav"
>
<div
class="sc-dkzDqf gQjsvL"
>
NavBar content
</div>
</nav>
</body>
`;

exports[`NavBar sets the maxWidth 1`] = `
<body>
<main
Expand Down
2 changes: 2 additions & 0 deletions src/components/__snapshots__/NavBarButton.spec.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

exports[`NavBarButton matches base64 icon snapshot 1`] = `
<button
aria-label="dots menu"
className="sc-bczRLJ dKgZvy"
data-rac=""
id="react-aria-5"
Expand Down Expand Up @@ -68,6 +69,7 @@ exports[`NavBarButton matches icon and text snapshot 1`] = `

exports[`NavBarButton matches svg icon snapshot 1`] = `
<button
aria-label="info"
className="sc-bczRLJ dKgZvy"
data-rac=""
id="react-aria-3"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ exports[`NavBarMenuButton matches snapshot 1`] = `
exports[`NavBarPopoverButton matches custom aria label snapshot 1`] = `
<button
aria-expanded={false}
aria-label="Custom label"
className="sc-bczRLJ dKgZvy"
data-rac=""
id="react-aria-5"
Expand Down

0 comments on commit 4056181

Please sign in to comment.