Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: remove aria-current from navLink when inactive #5508

Merged
merged 6 commits into from
Nov 13, 2017

Conversation

AlmeroSteyn
Copy link
Contributor

Partially closes #5507.

Refer to the following two aria-current pattern docs from Léonie Watson:
https://tink.uk/using-the-aria-current-attribute/
http://design-patterns.tink.uk/aria-current/

Both indicates the aria-current ARIA state only being rendered on the current element, instead of setting it to false.

I have changed the NavLink component to not render this attribute unless it should be set to true.

This change fixes one of the issues from #5507 , namely that only the active link is then read out by NVDA to be active, instead of all the NavLinks.

@AlmeroSteyn
Copy link
Contributor Author

@timdorr Nice change to pass all the allowed values through! Actually because it is a camelCase prop I did not even think it was part of the component interface, sorry!

Can I change it to accept the aria-current as per React standard? All aria-* props should be kebab-cased.

The following change would allow the prop to be passed as aria-current and should then get standard UI and tooling support.

What do you think?

/**
 * A <Link> wrapper that knows if it's "active" or not.
 */
const NavLink = ({
  to,
  exact,
  strict,
  location,
  activeClassName,
  className,
  activeStyle,
  style,
  isActive: getIsActive,
  'aria-current': ariaCurrent,
  ...rest
}) => (
  <Route
    path={typeof to === 'object' ? to.pathname : to}
    exact={exact}
    strict={strict}
    location={location}
    children={({ location, match }) => {
      const isActive = !!(getIsActive ? getIsActive(match, location) : match)

      return (
        <Link
          to={to}
          className={isActive ? [ className, activeClassName ].filter(i => i).join(' ') : className}
          style={isActive ? { ...style, ...activeStyle } : style}
          aria-current={isActive && ariaCurrent || null}
          {...rest}
        />
      )
    }}
  />
)

NavLink.propTypes = {
  to: Link.propTypes.to,
  exact: PropTypes.bool,
  strict: PropTypes.bool,
  location: PropTypes.object,
  activeClassName: PropTypes.string,
  className: PropTypes.string,
  activeStyle: PropTypes.object,
  style: PropTypes.object,
  isActive: PropTypes.func,
  'aria-current': PropTypes.oneOf(['page', 'step', 'location', 'true'])
}

NavLink.defaultProps = {
  activeClassName: 'active',
  'aria-current': 'true'
}

export default NavLink

@pshrmn
Copy link
Contributor

pshrmn commented Sep 12, 2017

@AlmeroSteyn It's your PR, you can make any changes that you'd like 😉.

I would recommend adding a couple of tests just to verify the behavior. One where the <NavLink> is active to verify that the rendered anchor's aria-current attribute is correct and another to verify that that attribute does not exist when the <NavLink> is not active. The <NavLink> tests are in this file. You should be able to just copy one of the other test cases and change the props/attribute checked in an expect call.

@AlmeroSteyn
Copy link
Contributor Author

@pshrmn Thank so much for the info! I saw @timdorr had pushed changes so was not sure if I should overwrite it :-)

Will get to this as soon as I have a spare moment.

@AlmeroSteyn
Copy link
Contributor Author

@pshrmn I changed the prop to kebab-case and added values date and time as well as they are also catered for according to the spec.

And added the tests.

Copy link
Contributor

@pshrmn pshrmn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems good to me.

For anyone interested, using aria-* in JSX is documented in the accessibility docs. While a <NavLink> isn't a native component, I think that it makes sense to make its ARIA prop consistent with <input>, et al.

@@ -50,12 +50,12 @@ NavLink.propTypes = {
activeStyle: PropTypes.object,
style: PropTypes.object,
isActive: PropTypes.func,
ariaCurrent: PropTypes.oneOf(['page', 'step', 'location', 'true'])
'aria-current': PropTypes.oneOf(['page', 'step', 'location', 'date', 'time', 'true'])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we should be worrying about all the possible values and just doing a string type here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The current standard confix for create-react-app includes the eslint-plugin-jsx-a11y rule jsx-a11y/aria-proptypes activated by default. It already validates all aria-* values which, in this case, shows the following warning message for this rule at compile time:

The value for aria-current must be a single token from the following: page,step,location,date,time,true,false

So I agree that this is redundant. Better to have such a dedicated library worry about these details.

Shall I remove it?

@timdorr
Copy link
Member

timdorr commented Sep 13, 2017

LGTM

@timdorr
Copy link
Member

timdorr commented Nov 13, 2017

Weird, had to do it twice... Something's up with Github.

Anyways, this should be good to go. Will merge when it goes green.

@timdorr timdorr merged commit da63a49 into remix-run:master Nov 13, 2017
@AlmeroSteyn
Copy link
Contributor Author

Thank you for merging this!

bors bot added a commit to mozilla/delivery-console that referenced this pull request Jun 6, 2018
179: Update dependency react-router-dom to v4.3.0 r=magopian a=renovate[bot]

This Pull Request updates dependency [react-router-dom](https://github.com/ReactTraining/react-router) from `v4.2.2` to `v4.3.0`



<details>
<summary>Release Notes</summary>

### [`v4.3.0`](https://github.com/ReactTraining/react-router/blob/master/CHANGELOG.md#v430httpsgithubcomReactTrainingreact-routercomparev420v430)
[Compare Source](remix-run/react-router@v4.3.0-rc.3...a27bc56)
> Jun 6, 2018

* Use the `pretty` option in generatePath ([#&#8203;6172] by @&#8203;sibelius)
* aria-current has incorrect value "true" ([#&#8203;6118] by @&#8203;brandonrninefive)
* Redirect with parameters ([#&#8203;5209] by @&#8203;dlindenkreuz)
* Fix with missing pathname: `<Link to="?foo=bar">` ([#&#8203;5489] by @&#8203;pshrmn)
* Escape NavLink path to allow special characters in path. ([#&#8203;5596] by @&#8203;esiegel)
* Expose `generatePath` ([#&#8203;5661] by @&#8203;rybon)
* Use named import of history module. ([#&#8203;5589] by @&#8203;RoboBurned)
* Hoist dependencies for smaller UMD builds ([#&#8203;5720] by @&#8203;pshrmn)
* Remove aria-current from navLink when inactive ([#&#8203;5508] by @&#8203;AlmeroSteyn)
* Add invariant for missing "to" property on `<Link>` ([#&#8203;5792] by @&#8203;selbekk)
* Use Prettier on the code ([e6f9017] by @&#8203;mjackson)
* Fix pathless route's match when parent is null ([#&#8203;5964] by @&#8203;pshrmn)
* Use history.createLocation in `<StaticRouter>` ([#&#8203;5722] by @&#8203;pshrmn)

[#&#8203;6172]: `remix-run/react-router#6172
[#&#8203;6118]: `remix-run/react-router#6118
[#&#8203;5209]: `remix-run/react-router#5209
[#&#8203;5489]: `remix-run/react-router#5489
[#&#8203;5596]: `remix-run/react-router#5596
[#&#8203;5661]: `remix-run/react-router#5661
[#&#8203;5589]: `remix-run/react-router#5589
[#&#8203;5720]: `remix-run/react-router#5720
[#&#8203;5508]: `remix-run/react-router#5508
[#&#8203;5792]: `remix-run/react-router#5792
[e6f9017]: remix-run/react-router@e6f9017
[#&#8203;5964]: `remix-run/react-router#5964
[#&#8203;5722]: `remix-run/react-router#5722

---

### [`v4.3.0-rc.3`](https://github.com/ReactTraining/react-router/releases/v4.3.0-rc.3)
[Compare Source](remix-run/react-router@v4.3.0-rc.2...v4.3.0-rc.3)
- Fix broken UMD builds.
- Add sideEffects: false for webpack tree shaking (#&#8203;6082 by @&#8203;taylorc93)

---

### [`v4.3.0-rc.2`](https://github.com/ReactTraining/react-router/releases/v4.3.0-rc.2)
[Compare Source](remix-run/react-router@v4.3.0-rc.1...v4.3.0-rc.2)
- Bump `hoist-non-react-statics` for React 16.3.
- Missing `generatePath` in `react-router-dom` package.

---

### [`v4.3.0-rc.1`](https://github.com/ReactTraining/react-router/releases/v4.3.0-rc.1)
[Compare Source](remix-run/react-router@v4.2.2...v4.3.0-rc.1)
> Mar 26, 2018

- Redirect with parameters ([#&#8203;5209] by @&#8203;dlindenkreuz)
- Fix with missing pathname: `<Link to="?foo=bar">` ([#&#8203;5489] by @&#8203;pshrmn)
- Escape NavLink path to allow special characters in path. ([#&#8203;5596] by @&#8203;esiegel)
- Expose `generatePath` ([#&#8203;5661] by @&#8203;rybon)
- Use named import of history module. ([#&#8203;5589] by @&#8203;RoboBurned)
- Hoist dependencies for smaller UMD builds ([#&#8203;5720] by @&#8203;pshrmn)
- Remove aria-current from navLink when inactive ([#&#8203;5508] by @&#8203;AlmeroSteyn)
- Add invariant for missing "to" property on `<Link>` ([#&#8203;5792] by @&#8203;selbekk)
- Use Prettier on the code ([e6f9017] by @&#8203;mjackson)
- Fix pathless route's match when parent is null ([#&#8203;5964] by @&#8203;pshrmn)
- Use history.createLocation in `<StaticRouter>` ([#&#8203;5722] by @&#8203;pshrmn)

[#&#8203;5209]: `remix-run/react-router#5209
[#&#8203;5489]: `remix-run/react-router#5489
[#&#8203;5596]: `remix-run/react-router#5596
[#&#8203;5661]: `remix-run/react-router#5661
[#&#8203;5589]: `remix-run/react-router#5589
[#&#8203;5720]: `remix-run/react-router#5720
[#&#8203;5508]: `remix-run/react-router#5508
[#&#8203;5792]: `remix-run/react-router#5792
[e6f9017]: remix-run/react-router@e6f9017
[#&#8203;5964]: `remix-run/react-router#5964
[#&#8203;5722]: `remix-run/react-router#5722

---

</details>




---

This PR has been generated by [Renovate Bot](https://renovatebot.com).

Co-authored-by: Renovate Bot <bot@renovateapp.com>
bors bot added a commit to mozilla/delivery-console that referenced this pull request Jun 6, 2018
178: Update dependency react-router to v4.3.0 r=magopian a=renovate[bot]

This Pull Request updates dependency [react-router](https://github.com/ReactTraining/react-router) from `v4.2.0` to `v4.3.0`



<details>
<summary>Release Notes</summary>

### [`v4.3.0`](https://github.com/ReactTraining/react-router/blob/master/CHANGELOG.md#v430httpsgithubcomReactTrainingreact-routercomparev420v430)
[Compare Source](remix-run/react-router@v4.3.0-rc.3...v4.3.0)
> Jun 6, 2018

* Use the `pretty` option in generatePath ([#&#8203;6172] by @&#8203;sibelius)
* aria-current has incorrect value "true" ([#&#8203;6118] by @&#8203;brandonrninefive)
* Redirect with parameters ([#&#8203;5209] by @&#8203;dlindenkreuz)
* Fix with missing pathname: `<Link to="?foo=bar">` ([#&#8203;5489] by @&#8203;pshrmn)
* Escape NavLink path to allow special characters in path. ([#&#8203;5596] by @&#8203;esiegel)
* Expose `generatePath` ([#&#8203;5661] by @&#8203;rybon)
* Use named import of history module. ([#&#8203;5589] by @&#8203;RoboBurned)
* Hoist dependencies for smaller UMD builds ([#&#8203;5720] by @&#8203;pshrmn)
* Remove aria-current from navLink when inactive ([#&#8203;5508] by @&#8203;AlmeroSteyn)
* Add invariant for missing "to" property on `<Link>` ([#&#8203;5792] by @&#8203;selbekk)
* Use Prettier on the code ([e6f9017] by @&#8203;mjackson)
* Fix pathless route's match when parent is null ([#&#8203;5964] by @&#8203;pshrmn)
* Use history.createLocation in `<StaticRouter>` ([#&#8203;5722] by @&#8203;pshrmn)

[#&#8203;6172]: `remix-run/react-router#6172
[#&#8203;6118]: `remix-run/react-router#6118
[#&#8203;5209]: `remix-run/react-router#5209
[#&#8203;5489]: `remix-run/react-router#5489
[#&#8203;5596]: `remix-run/react-router#5596
[#&#8203;5661]: `remix-run/react-router#5661
[#&#8203;5589]: `remix-run/react-router#5589
[#&#8203;5720]: `remix-run/react-router#5720
[#&#8203;5508]: `remix-run/react-router#5508
[#&#8203;5792]: `remix-run/react-router#5792
[e6f9017]: remix-run/react-router@e6f9017
[#&#8203;5964]: `remix-run/react-router#5964
[#&#8203;5722]: `remix-run/react-router#5722

---

### [`v4.3.0-rc.3`](https://github.com/ReactTraining/react-router/releases/v4.3.0-rc.3)
[Compare Source](remix-run/react-router@v4.3.0-rc.2...v4.3.0-rc.3)
- Fix broken UMD builds.
- Add sideEffects: false for webpack tree shaking (#&#8203;6082 by @&#8203;taylorc93)

---

### [`v4.3.0-rc.2`](https://github.com/ReactTraining/react-router/releases/v4.3.0-rc.2)
[Compare Source](remix-run/react-router@v4.3.0-rc.1...v4.3.0-rc.2)
- Bump `hoist-non-react-statics` for React 16.3.
- Missing `generatePath` in `react-router-dom` package.

---

### [`v4.3.0-rc.1`](https://github.com/ReactTraining/react-router/releases/v4.3.0-rc.1)
[Compare Source](remix-run/react-router@v4.2.0...v4.3.0-rc.1)
> Mar 26, 2018

- Redirect with parameters ([#&#8203;5209] by @&#8203;dlindenkreuz)
- Fix with missing pathname: `<Link to="?foo=bar">` ([#&#8203;5489] by @&#8203;pshrmn)
- Escape NavLink path to allow special characters in path. ([#&#8203;5596] by @&#8203;esiegel)
- Expose `generatePath` ([#&#8203;5661] by @&#8203;rybon)
- Use named import of history module. ([#&#8203;5589] by @&#8203;RoboBurned)
- Hoist dependencies for smaller UMD builds ([#&#8203;5720] by @&#8203;pshrmn)
- Remove aria-current from navLink when inactive ([#&#8203;5508] by @&#8203;AlmeroSteyn)
- Add invariant for missing "to" property on `<Link>` ([#&#8203;5792] by @&#8203;selbekk)
- Use Prettier on the code ([e6f9017] by @&#8203;mjackson)
- Fix pathless route's match when parent is null ([#&#8203;5964] by @&#8203;pshrmn)
- Use history.createLocation in `<StaticRouter>` ([#&#8203;5722] by @&#8203;pshrmn)

[#&#8203;5209]: `remix-run/react-router#5209
[#&#8203;5489]: `remix-run/react-router#5489
[#&#8203;5596]: `remix-run/react-router#5596
[#&#8203;5661]: `remix-run/react-router#5661
[#&#8203;5589]: `remix-run/react-router#5589
[#&#8203;5720]: `remix-run/react-router#5720
[#&#8203;5508]: `remix-run/react-router#5508
[#&#8203;5792]: `remix-run/react-router#5792
[e6f9017]: remix-run/react-router@e6f9017
[#&#8203;5964]: `remix-run/react-router#5964
[#&#8203;5722]: `remix-run/react-router#5722

---

</details>




---

This PR has been generated by [Renovate Bot](https://renovatebot.com).

Co-authored-by: Renovate Bot <bot@renovateapp.com>
jeresig pushed a commit to Khan/react-router that referenced this pull request Aug 29, 2018
* 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
@lock lock bot locked as resolved and limited conversation to collaborators Jan 18, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

NavLink reporting current status incorrectly to NVDA screen reader
3 participants