Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[@mantine/core] Drawer: Add id for aria- attributes #3027

Merged
merged 1 commit into from Nov 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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