Skip to content

Commit dbb8268

Browse files
authored
fix(Loading): improve screenreader support (#11759)
* fix(Loading): improve screenreader support * fix(Loading): more screenreader tweaks * test(snapshot): update snapshots * test(Loading): update Loading tests * test(format): format files
1 parent b70bf79 commit dbb8268

File tree

8 files changed

+18
-49
lines changed

8 files changed

+18
-49
lines changed

packages/react/__tests__/__snapshots__/PublicAPI-test.js.snap

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3793,7 +3793,7 @@ Map {
37933793
"Loading" => Object {
37943794
"defaultProps": Object {
37953795
"active": true,
3796-
"description": "Active loading indicator",
3796+
"description": "loading",
37973797
"small": false,
37983798
"withOverlay": true,
37993799
},
@@ -3807,9 +3807,7 @@ Map {
38073807
"description": Object {
38083808
"type": "string",
38093809
},
3810-
"id": Object {
3811-
"type": "string",
3812-
},
3810+
"id": [Function],
38133811
"small": Object {
38143812
"type": "bool",
38153813
},

packages/react/src/components/InlineLoading/InlineLoading-story.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,7 @@ const props = () => ({
1818
['inactive', 'active', 'finished', 'error'],
1919
'active'
2020
),
21-
iconDescription: text(
22-
'Icon description (iconDescription)',
23-
'Active loading indicator'
24-
),
21+
iconDescription: text('Icon description (iconDescription)', 'loading'),
2522
description: text(
2623
'Loading progress description (description)',
2724
'Loading data...'

packages/react/src/components/InlineLoading/InlineLoading-test.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,13 @@ describe('InlineLoading', () => {
1919
it('should render a loader by default', () => {
2020
render(<InlineLoading />);
2121

22-
expect(
23-
screen.getByLabelText('Active loading indicator')
24-
).toBeInTheDocument();
22+
expect(screen.getByTitle('loading')).toBeInTheDocument();
2523
});
2624

2725
it('should render a loader if the status is inactive', () => {
2826
render(<InlineLoading status="inactive" />);
2927

30-
expect(
31-
screen.getByLabelText('Active loading indicator')
32-
).toBeInTheDocument();
28+
expect(screen.getByTitle('not loading')).toBeInTheDocument();
3329
});
3430

3531
it('should render the success state if status is finished', () => {

packages/react/src/components/InlineLoading/InlineLoading.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,11 @@ export default function InlineLoading({
2424
const prefix = usePrefix();
2525
const loadingClasses = classNames(`${prefix}--inline-loading`, className);
2626
const getLoading = () => {
27+
let iconLabel = iconDescription ? iconDescription : status;
2728
if (status === 'error') {
2829
return (
2930
<ErrorFilled className={`${prefix}--inline-loading--error`}>
30-
<title>{iconDescription}</title>
31+
<title>{iconLabel}</title>
3132
</ErrorFilled>
3233
);
3334
}
@@ -40,15 +41,18 @@ export default function InlineLoading({
4041
return (
4142
<CheckmarkFilled
4243
className={`${prefix}--inline-loading__checkmark-container`}>
43-
<title>{iconDescription}</title>
44+
<title>{iconLabel}</title>
4445
</CheckmarkFilled>
4546
);
4647
}
4748
if (status === 'inactive' || status === 'active') {
49+
if (!iconDescription) {
50+
iconLabel = status === 'active' ? 'loading' : 'not loading';
51+
}
4852
return (
4953
<Loading
5054
small
51-
description={iconDescription}
55+
description={iconLabel}
5256
withOverlay={false}
5357
active={status === 'active'}
5458
/>

packages/react/src/components/InlineLoading/next/InlineLoading.stories.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export default {
1717
export const _InlineLoading = () => (
1818
<InlineLoading
1919
status="active"
20-
iconDescription="Active loading indicator"
20+
iconDescription="Loading"
2121
description="Loading data..."
2222
/>
2323
);

packages/react/src/components/Loading/Loading-story.js

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const props = () => ({
1313
active: boolean('Active (active)', true),
1414
withOverlay: boolean('With overlay (withOverlay)', false),
1515
small: boolean('Small (small)', false),
16-
description: text('Description (description)', 'Active loading indicator'),
16+
description: text('Description (description)', 'Loading'),
1717
});
1818

1919
export default {
@@ -25,13 +25,3 @@ export default {
2525
export const Default = () => {
2626
return <Loading {...props()} className={'some-class'} />;
2727
};
28-
29-
Default.parameters = {
30-
info: {
31-
text: `
32-
Loading spinners are used when retrieving data or performing slow computations,
33-
and help to notify users that loading is underway. The 'active' property is true by default;
34-
set to false to end the animation.
35-
`,
36-
},
37-
};

packages/react/src/components/Loading/Loading-test.js

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,6 @@ describe('Loading', () => {
3636
const { container } = render(<Loading />);
3737
const liveRegion = container.querySelector('[aria-live]');
3838
expect(liveRegion).toBeInstanceOf(HTMLElement);
39-
40-
const id = liveRegion.getAttribute('aria-labelledby');
41-
expect(id).toBeDefined();
42-
43-
const label = document.getElementById(id);
44-
expect(label).toBeDefined();
45-
expect(typeof label.textContent).toBe('string');
4639
});
4740

4841
// https://www.w3.org/TR/WCAG21/#status-messages

packages/react/src/components/Loading/Loading.js

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,11 @@
77

88
import cx from 'classnames';
99
import PropTypes from 'prop-types';
10-
import React, { useRef } from 'react';
11-
import setupGetInstanceId from '../../tools/setupGetInstanceId';
10+
import React from 'react';
1211
import { usePrefix } from '../../internal/usePrefix';
13-
14-
const getInstanceId = setupGetInstanceId();
12+
import deprecate from '../../prop-types/deprecate';
1513

1614
function Loading({
17-
id,
1815
active,
1916
className: customClassName,
2017
withOverlay,
@@ -23,7 +20,6 @@ function Loading({
2320
...rest
2421
}) {
2522
const prefix = usePrefix();
26-
const { current: instanceId } = useRef(getInstanceId());
2723
const loadingClassName = cx(customClassName, {
2824
[`${prefix}--loading`]: true,
2925
[`${prefix}--loading--small`]: small,
@@ -33,18 +29,13 @@ function Loading({
3329
[`${prefix}--loading-overlay`]: true,
3430
[`${prefix}--loading-overlay--stop`]: !active,
3531
});
36-
const loadingId = id || `loading-id-${instanceId}`;
3732

3833
const loading = (
3934
<div
4035
{...rest}
4136
aria-atomic="true"
42-
aria-labelledby={loadingId}
4337
aria-live={active ? 'assertive' : 'off'}
4438
className={loadingClassName}>
45-
<label id={loadingId} className={`${prefix}--visually-hidden`}>
46-
{description}
47-
</label>
4839
<svg className={`${prefix}--loading__svg`} viewBox="0 0 100 100">
4940
<title>{description}</title>
5041
{small ? (
@@ -91,7 +82,7 @@ Loading.propTypes = {
9182
/**
9283
* Provide an `id` to uniquely identify the label
9384
*/
94-
id: PropTypes.string,
85+
id: deprecate(PropTypes.string, `\nThe prop \`id\` is no longer needed.`),
9586

9687
/**
9788
* Specify whether you would like the small variant of <Loading>
@@ -108,7 +99,7 @@ Loading.defaultProps = {
10899
active: true,
109100
withOverlay: true,
110101
small: false,
111-
description: 'Active loading indicator',
102+
description: 'loading',
112103
};
113104

114105
export default Loading;

0 commit comments

Comments
 (0)