babel-plugin-tailwind-components icon indicating copy to clipboard operation
babel-plugin-tailwind-components copied to clipboard

Pass variables to the tagged template literal

Open markbrouch opened this issue 6 years ago • 3 comments

I'm trying to pass a variable from Styled Components to my tailwind tagged template literal, but can't figure out how to do it. This doesn't work:

const Button = styled.button`
  ${p => tw`bg-${p.color}`}
`

markbrouch avatar Sep 29 '19 19:09 markbrouch

Having the same issue with something like:

const StyledCard = tw.div`
  w-full
  flex-grow 
  lg:w-1/2 
  bg-pink-300
  ${props => (props.noPadding ? `p-0` : `p-8`)}
`

eelkeh avatar Sep 30 '19 13:09 eelkeh

I believe you need to use the css helper to do interpolations: https://www.styled-components.com/docs/api#css

So something like this ...

  const StyledLink = styled.a`
    ${tw`no-underline text-black-two`}
    ${({ navigation }) =>
      navigation &&
      css`
        &:hover,
        &:focus {
          ${tw`text-white-three`}
          transition: color 0.3s ease;
        }
      `}
  `

NikkiJonesR avatar Sep 30 '19 21:09 NikkiJonesR

Using typescript this doesnt seem to work:

const Button = tw.button<ButtonProps>`
  bg-${props => props.bg || "primary"} 
  hover:bg-${props => props.bg || "primary"}-dark
  text-${props => props.color || "white"} 
  border-none font-bold font-mono py-2 px-4 rounded
`;

pyrbin avatar Oct 08 '19 03:10 pyrbin