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

Navlink to have an 'active' prop to support styling that is agnostic of RR #6390

Closed
Loque- opened this issue Oct 11, 2018 · 2 comments
Closed

Comments

@Loque-
Copy link

Loque- commented Oct 11, 2018

Hi all, thank you for your time, I think I have an interesting/useful feature request;

The request is to provide a property on NavLink, that allows CSS-in-JS libraries to style the component, based on the bool value of this property, perhaps it could be called active.

I appreciate there are solutions which are satisfactory for styling active NavLinks that support CSS-in-JS libs already using activeClassName, and, if I was building the React component in a 'normal' project, I would have no problem implementing this solution, however, this request has come through building components as part of a component library, that should be React Router agnostic, as it reduces dependencies and other obvious reasons.

In order to make the the component library agnostic of any router library, we use the HOC pattern, like so;

import React from 'react';
import styled from 'react-emotion';

const asAnchor = (AnchorType) => {
  const Anchor = props => (
    <AnchorType {...props}>{props.children}</AnchorType>
  );

  const StyledHoc = styled(Anchor)({
    color: 'blue',
    ':hover': {
      color: 'red',
    },
  }
));

  return StyledHoc;
};

export default asAnchor;

Implemented, with a solution that is specific to RR, it would look like;

import React from 'react';
import styled from 'react-emotion';

const asAnchor = (AnchorType) => {
  const Anchor = props => (
    <AnchorType activeClassName="active" {...props}>{props.children}</AnchorType>
  );

  const StyledHoc = styled(Anchor)({
    color: 'blue',
    ':hover': {
      color: 'red',
    },
    '&.active': {
      color: 'hotpink',
    },
  }
));

  return StyledHoc;
};

export default asAnchor;

What, I guess, I was hoping for, and expecting, was that NavLink would have some type of active prop on it, that would allow for this instead;

import React from 'react';
import styled from 'react-emotion';

const asAnchor = (AnchorType) => {
  const Anchor = props => (
    <AnchorType {...props}>{props.children}</AnchorType>
  );

  const StyledHoc = styled(Anchor)({
    ':hover': {
      color: 'red',
    },
  }, ({ active }) => ({
    color: active ? 'blue' : 'hotpink',
  }
));

  return StyledHoc;
};

export default asAnchor;

I created a small codesandbox that illustrates some of this;

https://codesandbox.io/s/2333oy601y

Do you have any thoughts/ideas on this? Maybe I missed something? I have posted in the Discord channel also and have mentioned it in the Emotion JS Slack channel to see if anyone else has some input.

Thanks so much for your time reading this.

@penx
Copy link
Contributor

penx commented Oct 19, 2018

Some background on the issue

govuk-react/govuk-react#423

And our current proposed solution

https://github.com/penx/as-nav-link

@stale stale bot added stale and removed stale labels Sep 10, 2019
@stale stale bot added stale and removed stale labels Nov 10, 2019
@stale stale bot added stale and removed stale labels Jan 9, 2020
@timdorr timdorr added the fresh label Jan 9, 2020
@remix-run remix-run deleted a comment from penx Jan 9, 2020
@remix-run remix-run deleted a comment from stale bot Jan 9, 2020
@remix-run remix-run deleted a comment from penx Jan 9, 2020
@remix-run remix-run deleted a comment from stale bot Jan 9, 2020
@remix-run remix-run deleted a comment from stale bot Jan 9, 2020
@remix-run remix-run deleted a comment from penx Jan 9, 2020
@chaance
Copy link
Collaborator

chaance commented Sep 4, 2021

I know this is a bit old at this point, so I'm sorry no one responded here sooner. I don't suspect we'll add a prop specifically for targeting Emotion's prop-based styling API, but my recommendation would be to use a selector based on the aria-current attribute, which is not React Router specific but should generally be used on active links anywhere.

const StyledNavLink = styled(NavLink)({
  padding: "5px",
  color: "blue",
  '&[aria-current="page"]': {
    color: "crimson"
  }
});

https://codesandbox.io/s/eager-hawking-l80qm?file=/src/App.js:168-301

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants