0

I have problem with Navlink button's active class my code look like this:

<NavLink exact to="/"><Button>Page</Button></NavLink>

Somehow NavLink isActive isn't working. Only when I click on button it changes class to active, but it becomes not active again after I release the button.

Button styled-component:

import styled from 'styled-components';

const Button = styled.button`
  width: 50%;
  height:35px;
  background: white;
  color: #71C1A1;
  padding: 0;
  border:none;

   &:active {
      background: #71C1A1;
      color: white;
    }
`;

export default Button;

Maybe someone could help?

PrEto
  • 395
  • 2
  • 7
  • 23
  • active means when you click on the button, or select it with keyboard and press space or enter right? is'n't it expected behavior ? – Mojtaba Hn Feb 01 '19 at 14:10

1 Answers1

1

The standard class given by react router to the element <NavLink /> is active (you can control the given class with activeClassName="customClass").

You would therefore need to style a button wrapped in a link with class .active. Like this:

const Button = styled.button`

   /* ... your other styles */

  .active & {
      background: #71c1a1;
      color: white;
  }

`;
Daniel Sixl
  • 2,488
  • 2
  • 16
  • 29