Skip to content

Commit

Permalink
[@mantine/core] Drawer: Add missing aria-describedby and `aria-labe…
Browse files Browse the repository at this point in the history
…lledby` attributes to the root element (#3027)
  • Loading branch information
noahsb committed Nov 23, 2022
1 parent 58a7c7a commit 51a0299
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
6 changes: 6 additions & 0 deletions src/mantine-core/src/Drawer/Drawer.test.tsx
Expand Up @@ -36,6 +36,12 @@ describe('@mantine/core/Drawer', () => {
expect(screen.getByText('test-title')).toBeInTheDocument();
});

it('uses the provided id prop on the title and body', () => {
const { container } = render(<Drawer {...defaultProps} id="my-drawer" />);
expect(container.querySelectorAll('#my-drawer-title')).toHaveLength(1);
expect(container.querySelectorAll('#my-drawer-body')).toHaveLength(1);
});

it('allows to hide close button with withCloseButton={false} prop', () => {
const { container: withCloseButton } = render(<Drawer {...defaultProps} />);
const { container: withoutCloseButton } = render(
Expand Down
23 changes: 19 additions & 4 deletions src/mantine-core/src/Drawer/Drawer.tsx
@@ -1,5 +1,5 @@
import React, { useEffect } from 'react';
import { useScrollLock, useFocusTrap, useFocusReturn } from '@mantine/hooks';
import { useScrollLock, useFocusTrap, useFocusReturn, useId } from '@mantine/hooks';
import {
DefaultProps,
MantineNumberSize,
Expand Down Expand Up @@ -86,6 +86,9 @@ export interface DrawerProps
/** Close button aria-label */
closeButtonLabel?: string;

/** id base, used to generate ids to connect drawer title and body with aria- attributes, defaults to random id */
id?: string;

/** Target element or selector where drawer portal should be rendered */
target?: HTMLElement | string;

Expand Down Expand Up @@ -149,6 +152,7 @@ export function Drawer(props: DrawerProps) {
children,
withOverlay,
shadow,
id,
padding,
title,
withCloseButton,
Expand All @@ -162,6 +166,9 @@ export function Drawer(props: DrawerProps) {
withFocusReturn,
...others
} = useComponentDefaultProps('Drawer', defaultProps, props);
const baseId = useId(id);
const titleId = `${baseId}-title`;
const bodyId = `${baseId}-body`;

const { classes, cx, theme } = useStyles(
{ size, position, zIndex, withOverlay },
Expand Down Expand Up @@ -216,7 +223,14 @@ export function Drawer(props: DrawerProps) {
}}
>
{(transitionStyles) => (
<Box className={cx(classes.root, className)} role="dialog" aria-modal {...others}>
<Box
className={cx(classes.root, className)}
role="dialog"
aria-modal
aria-labelledby={titleId}
aria-describedby={bodyId}
{...others}
>
<Paper<'div'>
className={cx(classes.drawer, className)}
ref={focusTrapRef}
Expand All @@ -235,7 +249,7 @@ export function Drawer(props: DrawerProps) {
>
{(title || withCloseButton) && (
<div className={classes.header}>
<Text className={classes.title} unstyled={unstyled}>
<Text id={titleId} className={classes.title} unstyled={unstyled}>
{title}
</Text>

Expand All @@ -250,7 +264,8 @@ export function Drawer(props: DrawerProps) {
)}
</div>
)}
{children}

<div id={bodyId}>{children}</div>
</Paper>

{withOverlay && (
Expand Down

0 comments on commit 51a0299

Please sign in to comment.