Skip to content

Commit d0d01bf

Browse files
joebuonoJoe Buono
andauthored
ui-react(fix): Add accessible label to Alert dismiss button (#2303)
* add dismissButtonLabel to types * add dismissButtonLabel prop * wrote tests for default and custom cases * update docs * Create late-papayas-run.md * Update late-papayas-run.md * update alert props table Co-authored-by: Joe Buono <[email protected]>
1 parent 4163eee commit d0d01bf

File tree

9 files changed

+83
-7
lines changed

9 files changed

+83
-7
lines changed

.changeset/late-papayas-run.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
"@aws-amplify/ui-react": patch
3+
---
4+
5+
ui-react(fix): Adds an accessible label for the Alert's dismiss button, which is configurable via the `dismissButtonLabel` prop
6+
7+
<Alert dismissButtonLabel="Custom dismiss button label" isDismissible>
8+
Configure a custom aria label for the dismiss button
9+
</Alert>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { Alert } from '@aws-amplify/ui-react';
2+
3+
export const DismissButtonLabelExample = () => {
4+
return (
5+
<Alert dismissButtonLabel="Custom dismiss button label" isDismissible>
6+
Configure a custom aria label for the dismiss button
7+
</Alert>
8+
);
9+
};
Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
export { DefaultAlertExample } from './DefaultAlertExample';
2-
export { AlertVariationsExample } from './AlertVariationsExample';
31
export { AlertHeadingExample } from './AlertHeadingExample';
42
export { AlertIconExample } from './AlertIconExample';
5-
export { DismissibleAlertExample } from './DismissibleAlertExample';
6-
export { OnDismissAlertExample } from './OnDismissAlertExample';
73
export { AlertStylePropsExample } from './AlertStylePropsExample';
4+
export { AlertVariationsExample } from './AlertVariationsExample';
85
export { AlertThemeExample } from './AlertThemeExample';
6+
export { DefaultAlertExample } from './DefaultAlertExample';
7+
export { DismissButtonLabelExample } from './DismissButtonLabelExample';
8+
export { DismissibleAlertExample } from './DismissibleAlertExample';
9+
export { OnDismissAlertExample } from './OnDismissAlertExample';

docs/src/pages/[platform]/components/alert/props-table.mdx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,16 @@ string
3939
<TableCell className="props-table__tr-description">Additional CSS class name for component</TableCell>
4040
</TableRow>
4141

42+
<TableRow>
43+
<TableCell className="props-table__tr-name">dismissButtonLabel</TableCell>
44+
<TableCell>
45+
```jsx
46+
string
47+
```
48+
</TableCell>
49+
<TableCell className="props-table__tr-description">Configures the accessible label for the Alert's dismiss button.</TableCell>
50+
</TableRow>
51+
4252
<TableRow>
4353
<TableCell className="props-table__tr-name">hasIcon</TableCell>
4454
<TableCell>

docs/src/pages/[platform]/components/alert/react.mdx

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
OnDismissAlertExample,
1313
AlertStylePropsExample,
1414
AlertThemeExample,
15+
DismissButtonLabelExample,
1516
} from './examples';
1617

1718
## Demo
@@ -290,4 +291,19 @@ For Amplify UI, the `alert` ARIA role is not built-in because it depends on your
290291
However, if you're just displaying some information that isn't critical or time sensitive, then we would not recommend passing in the `alert` role.
291292

292293
For more information, check out the [MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Roles/Alert_Role).
293-
294+
295+
#### Custom aria label
296+
297+
You can configure a custom `aria-label` for the dismiss button using the `dismissButtonLabel` prop (defaults to `'Dismiss alert'`).
298+
299+
<Example>
300+
<DismissButtonLabelExample />
301+
302+
<ExampleCode>
303+
304+
```jsx file=./examples/DismissButtonLabelExample.tsx
305+
306+
```
307+
308+
</ExampleCode>
309+
</Example>

packages/react/src/primitives/Alert/Alert.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as React from 'react';
22
import classNames from 'classnames';
33

4-
import { ComponentClassNames } from '../shared/constants';
4+
import { ComponentClassNames, ComponentText } from '../shared/constants';
55
import { classNameModifier } from '../shared/utils';
66
import { AlertProps, Primitive } from '../types';
77
import { View } from '../View';
@@ -16,6 +16,7 @@ const AlertPrimitive: Primitive<AlertProps, typeof Flex> = (
1616
buttonRef,
1717
children,
1818
className,
19+
dismissButtonLabel = ComponentText.Alert.dismissButtonLabel,
1920
hasIcon = true,
2021
heading,
2122
isDismissible = false,
@@ -56,6 +57,7 @@ const AlertPrimitive: Primitive<AlertProps, typeof Flex> = (
5657
</View>
5758
{isDismissible && (
5859
<Button
60+
ariaLabel={dismissButtonLabel}
5961
variation="link"
6062
className={ComponentClassNames.AlertDismiss}
6163
onClick={dismissAlert}

packages/react/src/primitives/Alert/__tests__/Alert.test.tsx

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { render, screen } from '@testing-library/react';
22
import * as React from 'react';
33

44
import { Alert } from '../Alert';
5-
import { ComponentClassNames } from '../../shared/constants';
5+
import { ComponentClassNames, ComponentText } from '../../shared/constants';
66
import { ComponentPropsToStylePropsMap } from '../../types';
77
import kebabCase from 'lodash/kebabCase';
88

@@ -111,6 +111,26 @@ describe('Alert: ', () => {
111111
expect(isDismissible.childElementCount).toBe(2);
112112
});
113113

114+
it('can configure an accessible label for the dismiss button', async () => {
115+
const customDismissButtonLabel = 'Testing 123';
116+
render(
117+
<div>
118+
<Alert isDismissible>Default dismiss button label</Alert>
119+
<Alert isDismissible dismissButtonLabel={customDismissButtonLabel}>
120+
Custom dismiss button label
121+
</Alert>
122+
</div>
123+
);
124+
125+
const [defaultLabel, customLabel] = await screen.queryAllByRole('button');
126+
expect(defaultLabel.getAttribute('aria-label')).toBe(
127+
ComponentText.Alert.dismissButtonLabel
128+
);
129+
expect(customLabel.getAttribute('aria-label')).toBe(
130+
customDismissButtonLabel
131+
);
132+
});
133+
114134
it('can apply styling via props', async () => {
115135
render(
116136
<Alert backgroundColor="white" fontStyle="italic" testId="alertId">

packages/react/src/primitives/shared/constants.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,9 @@ export const ComponentClassObject = {
517517

518518
// For internal use, no need to export
519519
export const ComponentText = {
520+
Alert: {
521+
dismissButtonLabel: 'Dismiss alert',
522+
},
520523
Collection: {
521524
searchButtonLabel: 'Search',
522525
},

packages/react/src/primitives/types/alert.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ export interface AlertProps extends FlexProps {
1717
*/
1818
isDismissible?: boolean;
1919

20+
/**
21+
* @description
22+
* Configures the accessible label for the Alert's dismiss button.
23+
*/
24+
dismissButtonLabel?: string;
25+
2026
/**
2127
* @description
2228
* The onDismiss callback will be called when the user dismisses (closes) the Alert.

0 commit comments

Comments
 (0)