Skip to content

Commit 112216e

Browse files
improve a11y
1 parent fb25d76 commit 112216e

File tree

4 files changed

+81
-22
lines changed

4 files changed

+81
-22
lines changed

.github/workflows/main.yml

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,6 @@ on:
33
pull_request:
44
workflow_call:
55
jobs:
6-
lint:
7-
name: Lint
8-
runs-on: ubuntu-latest
9-
steps:
10-
- name: Check out project
11-
uses: actions/checkout@v4
12-
13-
- name: Set up project
14-
uses: ./.github/actions/setup-project
15-
with:
16-
skip-build: true
17-
18-
- uses: actions/cache@v4
19-
name: Cache files proccesed by ESLint
20-
with:
21-
path: .eslintcache
22-
key: ${{ runner.os }}-eslint-cache
23-
24-
- name: Run linter
25-
run: yarn lint:all
26-
276
build:
287
name: Build
298
runs-on: ubuntu-latest

packages/react-core/src/components/Panel/Panel.tsx

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,22 @@ export interface PanelProps extends React.HTMLProps<HTMLDivElement> {
1111
variant?: 'raised' | 'bordered' | 'secondary';
1212
/** Flag to add scrollable styling to the panel */
1313
isScrollable?: boolean;
14+
/** Enable better a11y */
15+
a11y?: any;
1416
/** @hide Forwarded ref */
1517
innerRef?: React.Ref<any>;
1618
}
1719

20+
import { getA11y } from './utils';
21+
export enum a11yEnum {
22+
/** Adds aria-live="off" to the panel */
23+
off = 'off',
24+
/** Adds aria-live="assertive" to the panel */
25+
assertive = 'assertive',
26+
/** Adds aria-live="polite" to the panel */
27+
polite = 'polite'
28+
}
29+
1830
const PanelBase: React.FunctionComponent<PanelProps> = ({
1931
className,
2032
children,
@@ -31,12 +43,17 @@ const PanelBase: React.FunctionComponent<PanelProps> = ({
3143
className
3244
)}
3345
ref={innerRef}
46+
aria-live={convertGetA11yForAriaLive(getA11y(props)) as any}
3447
{...props}
3548
>
3649
{children}
3750
</div>
3851
);
3952

53+
function convertGetA11yForAriaLive(response: string) {
54+
return a11yEnum[response as 'off'];
55+
}
56+
4057
export const Panel = React.forwardRef((props: PanelProps, ref: React.Ref<any>) => (
4158
<PanelBase innerRef={ref} {...props} />
4259
));

packages/react-core/src/components/Panel/__tests__/Panel.test.tsx

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as React from 'react';
2-
import { render, screen } from '@testing-library/react';
2+
import { fireEvent, render, screen } from '@testing-library/react';
33
import { Panel } from '../Panel';
44
import { PanelMain } from '../PanelMain';
55
import { PanelMainBody } from '../PanelMainBody';
@@ -104,3 +104,40 @@ test('Matches the snapshot', () => {
104104
const { asFragment } = render(<Panel>Test</Panel>);
105105
expect(asFragment()).toMatchSnapshot();
106106
});
107+
108+
test('Renders correctly', () => {
109+
const { container } = render(<Panel a11y={{ announce: 'assertive' }}>Content</Panel>);
110+
expect(container.querySelector('div')?.attributes['aria-live']).toBe('assertive');
111+
});
112+
113+
test('Renders correctly', () => {
114+
const { container } = render(<Panel a11y={{ announce: 'polite' }}>Content</Panel>);
115+
expect(container.querySelector('div')?.attributes['aria-live']).toBe('polite');
116+
});
117+
118+
test('Renders correctly', () => {
119+
const { container } = render(<Panel a11y={{ announce: 'off' }}>Content</Panel>);
120+
expect(container.querySelector('div')?.attributes['aria-live']).toBe('off');
121+
});
122+
123+
test('Renders correctly', () => {
124+
const { container } = render(<Panel>Content</Panel>);
125+
expect(container.querySelector('div')?.attributes['aria-live']).toBe('polite');
126+
});
127+
128+
test('Renders correctly', () => {
129+
const { container } = render(<Panel a11y={{ announce: 'assertive' }}>Content</Panel>);
130+
fireEvent.click(container.querySelector('div') as Element);
131+
expect(container.querySelector('div')?.attributes['aria-live']).toBe('assertive');
132+
});
133+
134+
test('Renders correctly', () => {
135+
const { container } = render(<Panel a11y={{ announce: 'polite' }}>Content</Panel>);
136+
fireEvent.click(container.querySelector('div') as Element);
137+
expect(container.querySelector('div')?.attributes['aria-live']).toBe('polite');
138+
});
139+
140+
test('Renders correctly', () => {
141+
const { container } = render(<Panel a11y={{ announce: 'off' }}>Content</Panel>);
142+
expect(container.querySelector('div')?.attributes['aria-live']).toBe('off');
143+
});
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
var ariaLive = 'off';
2+
3+
import { PanelProps } from './Panel';
4+
5+
export function getAnnounce(props: PanelProps) {
6+
if (props.a11y) {
7+
if (props.a11y.announce === 'assertive') {
8+
return 'assertive';
9+
} else if (props.a11y.announce === 'off') {
10+
return 'off';
11+
} else {
12+
return 'polite';
13+
}
14+
} else {
15+
return 'polite';
16+
}
17+
}
18+
19+
export function getA11y(props: any) {
20+
if (props.a11y) {
21+
ariaLive = getAnnounce(props);
22+
} else {
23+
ariaLive = 'polite';
24+
}
25+
return ariaLive;
26+
}

0 commit comments

Comments
 (0)