Skip to content
This repository was archived by the owner on Apr 26, 2024. It is now read-only.

Commit cbeaff7

Browse files
AlmeroSteynjeresig
authored andcommitted
fix: remove aria-current from navLink when inactive (remix-run#5508)
* fix: remove aria-current from navLink when inactive * Add parens so order-of-operations is clear * Still allow ariaCurrent value to pass through * fix: convert used of aria-current to standard React aria-* pattern and add valid values
1 parent cb1d7b1 commit cbeaff7

File tree

2 files changed

+48
-4
lines changed

2 files changed

+48
-4
lines changed

packages/react-router-dom/modules/NavLink.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const NavLink = ({
1616
activeStyle,
1717
style,
1818
isActive: getIsActive,
19-
ariaCurrent,
19+
'aria-current': ariaCurrent,
2020
...rest
2121
}) => {
2222
const path = typeof to === 'object' ? to.pathname : to
@@ -38,7 +38,7 @@ const NavLink = ({
3838
to={to}
3939
className={isActive ? [ className, activeClassName ].filter(i => i).join(' ') : className}
4040
style={isActive ? { ...style, ...activeStyle } : style}
41-
aria-current={isActive && ariaCurrent}
41+
aria-current={isActive && ariaCurrent || null}
4242
{...rest}
4343
/>
4444
)
@@ -57,12 +57,12 @@ NavLink.propTypes = {
5757
activeStyle: PropTypes.object,
5858
style: PropTypes.object,
5959
isActive: PropTypes.func,
60-
ariaCurrent: PropTypes.oneOf(['page', 'step', 'location', 'true'])
60+
'aria-current': PropTypes.oneOf(['page', 'step', 'location', 'date', 'time', 'true'])
6161
}
6262

6363
NavLink.defaultProps = {
6464
activeClassName: 'active',
65-
ariaCurrent: 'true'
65+
'aria-current': 'true'
6666
}
6767

6868
export default NavLink

packages/react-router-dom/modules/__tests__/NavLink-test.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,28 @@ describe('NavLink', () => {
5151
expect(a.style.color).toBe(activeStyle.color)
5252
})
5353

54+
it('applies aria-current of true if no override value is given', () => {
55+
ReactDOM.render((
56+
<MemoryRouter initialEntries={['/pizza']}>
57+
<NavLink to='/pizza' activeClassName='selected'>Pizza!</NavLink>
58+
</MemoryRouter>
59+
), node)
60+
const a = node.getElementsByTagName('a')[0]
61+
expect(a.getAttribute('aria-current')).toEqual('true')
62+
})
63+
64+
it('applies the override aria-current value when given', () => {
65+
ReactDOM.render((
66+
<MemoryRouter initialEntries={['/pizza']}>
67+
<NavLink to='/pizza'
68+
activeClassName='selected'
69+
aria-current="page">Pizza!</NavLink>
70+
</MemoryRouter>
71+
), node)
72+
const a = node.getElementsByTagName('a')[0]
73+
expect(a.getAttribute('aria-current')).toEqual('page')
74+
})
75+
5476
it('it properly escapes path-to-regexp special characters', () => {
5577
ReactDOM.render((
5678
<MemoryRouter initialEntries={['/pizza (1)']}>
@@ -105,6 +127,28 @@ describe('NavLink', () => {
105127
const a = node.getElementsByTagName('a')[0]
106128
expect(a.style.color).toBe(defaultStyle.color)
107129
})
130+
131+
it('does not apply an aria-current value if no override value is given', () => {
132+
ReactDOM.render((
133+
<MemoryRouter initialEntries={['/pizza']}>
134+
<NavLink to='/salad'
135+
activeClassName='selected'
136+
aria-current="page">Pizza!</NavLink>
137+
</MemoryRouter>
138+
), node)
139+
const a = node.getElementsByTagName('a')[0]
140+
expect(a.getAttribute('aria-current')).toBeNull()
141+
})
142+
143+
it('does not apply an aria-current value if an override value is given', () => {
144+
ReactDOM.render((
145+
<MemoryRouter initialEntries={['/pizza']}>
146+
<NavLink to='/salad' activeClassName='selected'>Pizza!</NavLink>
147+
</MemoryRouter>
148+
), node)
149+
const a = node.getElementsByTagName('a')[0]
150+
expect(a.getAttribute('aria-current')).toBeNull()
151+
})
108152
})
109153

110154
describe('isActive', () => {

0 commit comments

Comments
 (0)