Skip to content

Commit 9838c07

Browse files
committed
fix(JumpLinks): Fix aria issues
Remove aria-label from toggle if there is visible text. Add aria-labelled by if there is visible text. Added aria-labels to examples.
1 parent 46b82e5 commit 9838c07

File tree

6 files changed

+33
-9
lines changed

6 files changed

+33
-9
lines changed

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

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import cssToggleDisplayVar from '@patternfly/react-tokens/dist/esm/c_jump_links_
77
import { Button } from '../Button';
88
import { JumpLinksItem, JumpLinksItemProps } from './JumpLinksItem';
99
import { JumpLinksList } from './JumpLinksList';
10-
import { canUseDOM, formatBreakpointMods } from '../../helpers/util';
10+
import { canUseDOM, formatBreakpointMods, getUniqueId } from '../../helpers/util';
1111

1212
export interface JumpLinksProps extends Omit<React.HTMLProps<HTMLElement>, 'label'> {
1313
/** Whether to center children. */
@@ -106,6 +106,17 @@ export const JumpLinks: React.FunctionComponent<JumpLinksProps> = ({
106106

107107
let scrollableElement: HTMLElement;
108108

109+
if (!label && !ariaLabel) {
110+
// eslint-disable-next-line no-console
111+
console.warn('For accessibility reasons, an aria-label should be specified on jump links if no label is provided');
112+
}
113+
if (!label && !toggleAriaLabel) {
114+
// eslint-disable-next-line no-console
115+
console.warn(
116+
'For accessibility reasons, a toggleAriaLabel should be specified on jump links if no label is provided'
117+
);
118+
}
119+
109120
const getScrollableElement = () => {
110121
if (scrollableRef) {
111122
if (scrollableRef instanceof HTMLElement) {
@@ -232,6 +243,8 @@ export const JumpLinks: React.FunctionComponent<JumpLinksProps> = ({
232243
return child;
233244
});
234245

246+
const id = getUniqueId();
247+
235248
return (
236249
<nav
237250
className={css(
@@ -244,6 +257,7 @@ export const JumpLinks: React.FunctionComponent<JumpLinksProps> = ({
244257
)}
245258
aria-label={ariaLabel}
246259
ref={navRef}
260+
aria-labelledby={expandable || (label && alwaysShowLabel) ? id : null}
247261
{...props}
248262
>
249263
<div className={styles.jumpLinksMain}>
@@ -253,21 +267,31 @@ export const JumpLinks: React.FunctionComponent<JumpLinksProps> = ({
253267
<Button
254268
variant="plain"
255269
onClick={() => setIsExpanded(!isExpanded)}
256-
aria-label={toggleAriaLabel}
270+
aria-label={label ? null : toggleAriaLabel}
257271
aria-expanded={isExpanded}
258272
icon={
259273
<span className={styles.jumpLinksToggleIcon}>
260274
<AngleRightIcon />
261275
</span>
262276
}
277+
id={id}
263278
>
264279
{label && label}
265280
</Button>
266281
</div>
267282
)}
268-
{label && alwaysShowLabel && <div className={css(styles.jumpLinksLabel)}>{label}</div>}
283+
{label && alwaysShowLabel && (
284+
<div className={css(styles.jumpLinksLabel)} id={id}>
285+
{label}
286+
</div>
287+
)}
269288
</div>
270-
<ul className={styles.jumpLinksList} role="list">
289+
<ul
290+
aria-label={ariaLabel}
291+
aria-labelledby={expandable || (label && alwaysShowLabel) ? id : null}
292+
className={styles.jumpLinksList}
293+
role="list"
294+
>
271295
{cloneChildren(children)}
272296
</ul>
273297
</div>

packages/react-core/src/components/JumpLinks/examples/JumpLinksBasic.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { JumpLinks, JumpLinksItem } from '@patternfly/react-core';
22

33
export const JumpLinksBasic: React.FunctionComponent = () => (
4-
<JumpLinks>
4+
<JumpLinks aria-label="Basic example">
55
<JumpLinksItem href="#">Inactive section</JumpLinksItem>
66
<JumpLinksItem href="#" isActive>
77
Active section

packages/react-core/src/components/JumpLinks/examples/JumpLinksExpandableVerticalWithSubsection.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { JumpLinks, JumpLinksItem, JumpLinksList } from '@patternfly/react-core';
22

33
export const JumpLinksExpandableVerticalWithSubsection: React.FunctionComponent = () => (
4-
<JumpLinks isVertical label="Jump to section" expandable={{ default: 'expandable' }}>
4+
<JumpLinks isVertical label="Jump to expandable vertical section" expandable={{ default: 'expandable' }}>
55
<JumpLinksItem href="#">Inactive section</JumpLinksItem>
66
<JumpLinksItem href="#">
77
Section with active subsection

packages/react-core/src/components/JumpLinks/examples/JumpLinksVertical.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { JumpLinks, JumpLinksItem } from '@patternfly/react-core';
22

33
export const JumpLinksVertical: React.FunctionComponent = () => (
4-
<JumpLinks isVertical>
4+
<JumpLinks isVertical aria-label="Vertical example">
55
<JumpLinksItem href="#">Inactive section</JumpLinksItem>
66
<JumpLinksItem href="#" isActive>
77
Active section

packages/react-core/src/components/JumpLinks/examples/JumpLinksVerticalWithLabel.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { JumpLinks, JumpLinksItem } from '@patternfly/react-core';
22

33
export const JumpLinksVerticalWithLabel: React.FunctionComponent = () => (
4-
<JumpLinks isVertical label="Jump to section">
4+
<JumpLinks isVertical label="Jump to vertical section">
55
<JumpLinksItem href="#">Inactive section</JumpLinksItem>
66
<JumpLinksItem href="#" isActive>
77
Active section

packages/react-core/src/components/JumpLinks/examples/JumpLinksWithCenteredList.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { JumpLinks, JumpLinksItem } from '@patternfly/react-core';
22

33
export const JumpLinksWithCenteredList: React.FunctionComponent = () => (
4-
<JumpLinks isCentered>
4+
<JumpLinks isCentered aria-label="Centered example">
55
<JumpLinksItem href="#">Inactive section</JumpLinksItem>
66
<JumpLinksItem href="#" isActive>
77
Active section

0 commit comments

Comments
 (0)