Skip to content

Commit c312238

Browse files
feat(fuselage): improve Toastbar accessibility (#1880)
1 parent cba7214 commit c312238

File tree

5 files changed

+48
-8
lines changed

5 files changed

+48
-8
lines changed

.changeset/lucky-bottles-sleep.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@rocket.chat/fuselage': minor
3+
---
4+
5+
feat(fuselage): improve `Toastbar` accessibility

packages/fuselage-toastbar/src/ToastBar.spec.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ const topStartStyle = {
2929
describe('[fuselage-toastbar rendering]', () => {
3030
test('should display ToastBar on the top right of the screen by default', async () => {
3131
render(<TopEnd />);
32-
const toast = screen.queryByRole('alert');
32+
const toast = screen.queryByRole('status');
3333
const toastContainer = toast?.parentElement?.parentElement?.parentElement;
3434

3535
expect(toastContainer).toHaveStyle(topEndStyle);
@@ -38,7 +38,7 @@ describe('[fuselage-toastbar rendering]', () => {
3838
test('should display ToastBar on the top right of the screen', async () => {
3939
document.body.setAttribute('dir', 'ltr');
4040
render(<TopEnd />);
41-
const toast = screen.queryByRole('alert');
41+
const toast = screen.queryByRole('status');
4242
const toastContainer = toast?.parentElement?.parentElement?.parentElement;
4343

4444
expect(toastContainer).toHaveStyle(topEndStyle);
@@ -47,7 +47,7 @@ describe('[fuselage-toastbar rendering]', () => {
4747
test('should display ToastBar on the top left of the screen', async () => {
4848
document.body.setAttribute('dir', 'rtl');
4949
render(<TopEnd />);
50-
const toast = screen.queryByRole('alert');
50+
const toast = screen.queryByRole('status');
5151
const toastContainer = toast?.parentElement?.parentElement?.parentElement;
5252

5353
expect(toastContainer).toHaveStyle(topStartStyle);

packages/fuselage/src/components/ToastBar/ToastBar.spec.tsx

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { axe } from 'jest-axe';
33

44
import { render } from '../../testing';
55

6+
import ToastBar from './ToastBar';
67
import * as stories from './ToastBar.stories';
78

89
const testCases = Object.values(composeStories(stories)).map((Story) => [
@@ -29,3 +30,35 @@ describe('[ToastBar Rendering]', () => {
2930
},
3031
);
3132
});
33+
34+
describe('[ToastBar Roles]', () => {
35+
it('should use role="status" for `info` variant (polite announcement)', () => {
36+
const { getByRole } = render(
37+
<ToastBar variant='info'>Info message</ToastBar>,
38+
);
39+
expect(getByRole('status')).toBeInTheDocument();
40+
expect(getByRole('status')).toHaveTextContent('Info message');
41+
});
42+
43+
it('should use role="status" for `success` variant (polite announcement)', () => {
44+
const { getByRole } = render(
45+
<ToastBar variant='success'>Success message</ToastBar>,
46+
);
47+
expect(getByRole('status')).toBeInTheDocument();
48+
expect(getByRole('status')).toHaveTextContent('Success message');
49+
});
50+
51+
it('should use role="alert" for `error` variant (assertive announcement)', () => {
52+
const { getByRole } = render(
53+
<ToastBar variant='error'>Error message</ToastBar>,
54+
);
55+
expect(getByRole('alert')).toBeInTheDocument();
56+
expect(getByRole('alert')).toHaveTextContent('Error message');
57+
});
58+
59+
it('should default to role="status" when no variant is specified', () => {
60+
const { getByRole } = render(<ToastBar>Default message</ToastBar>);
61+
expect(getByRole('status')).toBeInTheDocument();
62+
expect(getByRole('status')).toHaveTextContent('Default message');
63+
});
64+
});

packages/fuselage/src/components/ToastBar/ToastBar.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ function ToastBar({
7070
const uniqueId = useId();
7171
const toastId = id || uniqueId;
7272

73+
const role = variant === 'error' ? 'alert' : 'status';
74+
7375
return (
7476
<Box
7577
className={[
@@ -86,7 +88,7 @@ function ToastBar({
8688
size='x20'
8789
name={iconName}
8890
/>
89-
<div role='alert' className='rcx-toastbar_content'>
91+
<div role={role} className='rcx-toastbar_content'>
9092
{children}
9193
</div>
9294
{onClose && (

packages/fuselage/src/components/ToastBar/__snapshots__/ToastBar.spec.tsx.snap

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ exports[`[ToastBar Rendering] renders Default without crashing 1`] = `
1717
</i>
1818
<div
1919
class="rcx-toastbar_content"
20-
role="alert"
20+
role="status"
2121
>
2222
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod
2323
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
@@ -81,7 +81,7 @@ exports[`[ToastBar Rendering] renders Success without crashing 1`] = `
8181
</i>
8282
<div
8383
class="rcx-toastbar_content"
84-
role="alert"
84+
role="status"
8585
>
8686
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod
8787
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
@@ -113,7 +113,7 @@ exports[`[ToastBar Rendering] renders TinyText without crashing 1`] = `
113113
</i>
114114
<div
115115
class="rcx-toastbar_content"
116-
role="alert"
116+
role="status"
117117
>
118118
Lorem ipsum dolor sit amet
119119
</div>
@@ -143,7 +143,7 @@ exports[`[ToastBar Rendering] renders WithCloseButton without crashing 1`] = `
143143
</i>
144144
<div
145145
class="rcx-toastbar_content"
146-
role="alert"
146+
role="status"
147147
>
148148
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod
149149
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,

0 commit comments

Comments
 (0)