Skip to content

Commit b408d10

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 b408d10

File tree

7 files changed

+44
-14
lines changed

7 files changed

+44
-14
lines changed

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

Lines changed: 29 additions & 5 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(
@@ -242,8 +255,9 @@ export const JumpLinks: React.FunctionComponent<JumpLinksProps> = ({
242255
isExpanded && styles.modifiers.expanded,
243256
className
244257
)}
245-
aria-label={ariaLabel}
258+
aria-label={expandable || (label && alwaysShowLabel) ? null : 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={expandable || (label && alwaysShowLabel) ? null : 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/__tests__/__snapshots__/JumpLinks.test.tsx.snap

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
exports[`expandable vertical with subsection 1`] = `
44
<DocumentFragment>
55
<nav
6-
aria-label="Jump to section"
6+
aria-labelledby="pf-1754059205875vtill38mjx"
77
class="pf-v6-c-jump-links pf-m-vertical pf-m-expandable"
88
>
99
<div
@@ -17,11 +17,11 @@ exports[`expandable vertical with subsection 1`] = `
1717
>
1818
<button
1919
aria-expanded="false"
20-
aria-label="Toggle jump links"
2120
class="pf-v6-c-button pf-m-plain"
2221
data-ouia-component-id="OUIA-Generated-Button-plain-1"
2322
data-ouia-component-type="PF6/Button"
2423
data-ouia-safe="true"
24+
id="pf-1754059205875vtill38mjx"
2525
type="button"
2626
>
2727
<span
@@ -54,11 +54,13 @@ exports[`expandable vertical with subsection 1`] = `
5454
</div>
5555
<div
5656
class="pf-v6-c-jump-links__label"
57+
id="pf-1754059205875vtill38mjx"
5758
>
5859
Jump to section
5960
</div>
6061
</div>
6162
<ul
63+
aria-labelledby="pf-1754059205875vtill38mjx"
6264
class="pf-v6-c-jump-links__list"
6365
role="list"
6466
>
@@ -349,7 +351,7 @@ exports[`jumplinks centered 1`] = `
349351
exports[`jumplinks with label 1`] = `
350352
<DocumentFragment>
351353
<nav
352-
aria-label="Jump to section"
354+
aria-labelledby="pf-1754059205864jwyd6ijawzn"
353355
class="pf-v6-c-jump-links pf-m-center"
354356
>
355357
<div
@@ -360,11 +362,13 @@ exports[`jumplinks with label 1`] = `
360362
>
361363
<div
362364
class="pf-v6-c-jump-links__label"
365+
id="pf-1754059205864jwyd6ijawzn"
363366
>
364367
Jump to section
365368
</div>
366369
</div>
367370
<ul
371+
aria-labelledby="pf-1754059205864jwyd6ijawzn"
368372
class="pf-v6-c-jump-links__list"
369373
role="list"
370374
>
@@ -550,7 +554,7 @@ exports[`simple jumplinks 1`] = `
550554
exports[`vertical with label 1`] = `
551555
<DocumentFragment>
552556
<nav
553-
aria-label="Jump to section"
557+
aria-labelledby="pf-1754059205870og2bcrsz5jc"
554558
class="pf-v6-c-jump-links pf-m-vertical"
555559
>
556560
<div
@@ -561,11 +565,13 @@ exports[`vertical with label 1`] = `
561565
>
562566
<div
563567
class="pf-v6-c-jump-links__label"
568+
id="pf-1754059205870og2bcrsz5jc"
564569
>
565570
Jump to section
566571
</div>
567572
</div>
568573
<ul
574+
aria-labelledby="pf-1754059205870og2bcrsz5jc"
569575
class="pf-v6-c-jump-links__list"
570576
role="list"
571577
>

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)