2

I am using the styled-components npm package and need to add the aria-haspopup attribute to my extended Button component. I have tried the following, but this doesn't add the attribute:

import Button from './button';

const StyledBtn = Button.attrs({
    'aria-haspopup': 'true',
}).extend``;
JoeTidee
  • 24,754
  • 25
  • 104
  • 149

2 Answers2

8

I found that the solution was:

New styled-components v4 syntax:

import Button from './button';

const StyledBtn = styled(Button).attrs({
    'aria-haspopup': 'true',
})``;

Old syntax:

import Button from './button';

const StyledBtn = Button.extend.attrs({
    'aria-haspopup': 'true',
})``;

https://www.styled-components.com/docs/api

https://medium.com/styled-components/styled-components-v4-new-final-finalest-for-real-final-final-psd-fa4d83398a77

Ming
  • 4,468
  • 1
  • 21
  • 21
JoeTidee
  • 24,754
  • 25
  • 104
  • 149
-1

The solution is:

import Button from './button';

const StyledBtn = Button.attrs(({ value }) => ({
  ariaHaspopup: value,
}));
Gilles-Antoine Nys
  • 1,481
  • 16
  • 21
  • I tested this in styled-components v4, and this is incorrect. It errors with `Button.attrs is not a function` – Ming Feb 28 '19 at 02:45