Tailwind-Styled-Component icon indicating copy to clipboard operation
Tailwind-Styled-Component copied to clipboard

Using arbitrary values

Open vladikopl01 opened this issue 2 years ago • 3 comments

Hello, it would be nice to have this in your library, or is it already there?

Example of using arbitrary variants to style elements with a custom selector:

import tw from 'tailwind-styled-components'

const Button = tw.button`
  bg-black
  [> i]:block
  [> span]:(text-blue-500 w-10)
  [div.container]:(grid m-8)
`

const Component = () => (
  <Button>
    <i>Icon</i>
    <span>Label</span>
    <div className="container">Container</div>
  </Button>
)

vladikopl01 avatar Apr 27 '22 18:04 vladikopl01

Or even in such structure to implement same logic:

import tw from 'tailwind-styled-components'

const Button = tw.button`
  bg-black

  i {
    block
  }

  span {
    text-blue-500
    w-10
  }

  div.container {
    grid
    m-8
  }
`

const Component = () => (
  <Button>
    <i>Icon</i>
    <span>Label</span>
    <div className="container">Container</div>
  </Button>
)

vladikopl01 avatar Apr 27 '22 18:04 vladikopl01

I believe the second syntax makes more sense and I absolutely love it. Would spare a lot of time and it's cleaner.

fernandame avatar Apr 28 '22 14:04 fernandame

Have you tried to use @tailwindcss/nesting ?

MathiasGilson avatar Sep 07 '22 00:09 MathiasGilson