Twin

Twin blends the magic of Tailwind with the flexibility of css-in-js (emotio...

README

Twin examples Twin examples

The magic of Tailwind with the flexibility of css-in-js.

Total Downloads Latest Release Discord

🌟 New: Twin v3 now includes full Tailwind plugin support and more
Release notes →



Style jsx elements using Tailwind classes:

  1. ``` js
  2. import 'twin.macro'

  3. const Input = () => <input tw="border hover:border-black" />
  4. ```

Nest Twin’s tw import within a css prop to add conditional styles:

  1. ``` js
  2. import tw from 'twin.macro'

  3. const Input = ({ hasHover }) => (
  4.   <input css={[tw`border`, hasHover && tw`hover:border-black`]} />
  5. )
  6. ```

Or mix sass styles with the css import:

  1. ``` js
  2. import tw, { css } from 'twin.macro'

  3. const hoverStyles = css`
  4.   &:hover {
  5.     border-color: black;
  6.     ${tw`text-black`}
  7.   }
  8. `
  9. const Input = ({ hasHover }) => (
  10.   <input css={[tw`border`, hasHover && hoverStyles]} />
  11. )
  12. ```

Styled Components


You can also use the tw import to create and style new components:

  1. ``` js
  2. import tw from 'twin.macro'

  3. const Input = tw.input`border hover:border-black`
  4. ```

And clone and style existing components:

  1. ``` js
  2. const PurpleInput = tw(Input)`border-purple-500`
  3. ```

Switch to the styled import to add conditional styling:

  1. ``` js
  2. import tw, { styled } from 'twin.macro'

  3. const StyledInput = styled.input(({ hasBorder }) => [
  4.   `color: black;`,
  5.   hasBorder && tw`border-purple-500`,
  6. ])
  7. const Input = () => <StyledInput hasBorder />
  8. ```

Or use backticks to mix with sass styles:

  1. ``` js
  2. import tw, { styled } from 'twin.macro'

  3. const StyledInput = styled.input`
  4.   color: black;
  5.   ${({ hasBorder }) => hasBorder && tw`border-purple-500`}
  6. `
  7. const Input = () => <StyledInput hasBorder />
  8. ```

How it works


When babel runs over your javascript or typescript files at compile time, twin grabs your classes and converts them into css objects.
These css objects are then passed into your chosen css-in-js library without the need for an extra client-side bundle:

  1. ``` js
  2. import tw from 'twin.macro'

  3. tw`text-sm md:text-lg`

  4. // ↓ ↓ ↓ ↓ ↓ ↓

  5. {
  6.   fontSize: '0.875rem',
  7.   '@media (min-width: 768px)': {
  8.     fontSize: '1.125rem',
  9.   },
  10. }
  11. ```

Features


👌 Simple imports - Twin collapses imports from common styling libraries into a single import:

  1. ```diff
  2. - import styled from '@emotion/styled'
  3. - import css from '@emotion/react'
  4. + import { styled, css } from 'twin.macro'
  5. ```

🐹 Adds no size to your build - Twin converts the classes you’ve used into css objects using Babel and then compiles away, leaving no runtime code

🍱 Apply variants to multiple classes at once with variant groups

  1. ``` js
  2. import 'twin.macro'

  3. const interactionStyles = () => (
  4.   <div tw="hover:(text-black underline) focus:(text-blue-500 underline)" />
  5. )

  6. const mediaStyles = () => <div tw="sm:(w-4 mt-3) lg:(w-8 mt-6)" />

  7. const pseudoElementStyles = () => <div tw="before:(block w-10 h-10 bg-black)" />

  8. const stackedVariants = () => <div tw="sm:hover:(bg-black text-white)" />

  9. const groupsInGroups = () => <div tw="sm:(bg-black hover:(bg-white w-10))" />
  10. ```

🛎 Helpful suggestions for mistypings - Twin chimes in with class and variant values from your Tailwind config:

  1. ``` sh
  2. ml-1.25 was not found

  3. Try one of these classes:

  4. - ml-1.5 > 0.375rem
  5. - ml-1 > 0.25rem
  6. - ml-10 > 2.5rem
  7. ```

🖌️ Use the theme import to add values from your tailwind config

  1. ``` js
  2. import { css, theme } from 'twin.macro'

  3. const Input = () => <input css={css({ color: theme`colors.purple.500` })} />
  4. ```

See more examples using the theme import →

💡 Works with the official tailwind vscode plugin - Avoid having to look up your classes with auto-completions straight from your Tailwind config - setup instructions →

💥 Add !important to any class with a trailing or leading bang!

  1. ``` js
  2. <div tw="hidden!" /> ||
    >
  3. // ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓
  4. <div css={{ "display": "none !important" }} />
  5. ```

Add !important to multiple classes with bracket groups:

  1. ``` js
  2. <div tw="(hidden ml-auto)!" />
  3. // ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓
  4. <div css={{ "display": "none !important", "marginLeft": "auto !important" }} />
  5. ```

Get started


Twin works with many modern stacks - take a look at these examples to get started:

App build tools and libraries


- **Parcel**
[styled-components](https://github.com/ben-rogerson/twin.examples/tree/master/react-styled-components) / [emotion](https://github.com/ben-rogerson/twin.examples/tree/master/react-emotion) / [emotion (ts)](https://github.com/ben-rogerson/twin.examples/tree/master/react-emotion-typescript)- **Webpack**
[styled-components (ts)](https://github.com/ben-rogerson/twin.examples/tree/master/webpack-styled-components-typescript) / [emotion (ts)](https://github.com/ben-rogerson/twin.examples/tree/master/webpack-emotion-typescript)- **Preact**
[styled-components](https://github.com/ben-rogerson/twin.examples/tree/master/preact-styled-components) / [emotion](https://github.com/ben-rogerson/twin.examples/tree/master/preact-emotion) / [goober](https://github.com/ben-rogerson/twin.examples/tree/master/preact-goober)- **Create React App**
[styled-components](https://github.com/ben-rogerson/twin.examples/tree/master/cra-styled-components) / [emotion](https://github.com/ben-rogerson/twin.examples/tree/master/cra-emotion)- **Vite**
[styled-components (ts)](https://github.com/ben-rogerson/twin.examples/tree/master/vite-styled-components-typescript) / [emotion (ts)](https://github.com/ben-rogerson/twin.examples/tree/master/vite-emotion-typescript)

Advanced frameworks


- **Gatsby**
[styled-components](https://github.com/ben-rogerson/twin.examples/tree/master/gatsby-styled-components) / [emotion](https://github.com/ben-rogerson/twin.examples/tree/master/gatsby-emotion)- **Next.js**
[styled-components](https://github.com/ben-rogerson/twin.examples/tree/master/next-styled-components) / [styled-components (ts)](https://github.com/ben-rogerson/twin.examples/tree/master/next-styled-components-typescript) / [emotion](https://github.com/ben-rogerson/twin.examples/tree/master/next-emotion) / [emotion (ts)](https://github.com/ben-rogerson/twin.examples/tree/master/next-emotion-typescript) / [stitches (ts)](https://github.com/ben-rogerson/twin.examples/tree/master/next-stitches-typescript)- **Blitz.js**
[emotion (ts)](https://github.com/ben-rogerson/twin.examples/tree/master/blitz-emotion-typescript)

Component libraries


- **Storybook**
[styled-components (ts)](https://github.com/ben-rogerson/twin.examples/tree/master/storybook-styled-components-typescript) / [emotion](https://github.com/ben-rogerson/twin.examples/tree/master/storybook-emotion)- **yarn/npm workspaces + Next.js + shared ui components**
[styled-components](https://github.com/ben-rogerson/twin.examples/tree/master/component-library-styled-components)- **Yarn workspaces + Rollup**
[emotion](https://github.com/ben-rogerson/twin.examples/tree/master/component-library-emotion)
- [HeadlessUI (ts)](https://github.com/ben-rogerson/twin.examples/tree/master/headlessui-typescript)

Community


Drop into our Discord server for announcements, help and styling chat.

Discord

Resources


- 🔥 Docs: The prop styling guide - A must-read guide to level up on prop styling
- 🔥 Docs: The styled component guide - A must-read guide on getting productive with styled components
- Docs: Options - Learn about the features you can tweak via the twin config
- Plugin: babel-plugin-twin - Use the tw and css props without adding an import
- Example: Advanced theming - Add custom theming the right way using css variables
- Example: React + Tailwind breakpoint syncing - Sync your tailwind.config.js breakpoints with react
- Helpers: Twin VSCode snippets - For devs who want to type less
- Plugins: VSCode plugins - VScode plugins that work with twin
- Article: "Why I Love Tailwind" by Max Stoiber - Max (inventor of styled-components) shares his thoughts on twin

Special thanks


This project stemmed from babel-plugin-tailwind-components so a big shout out goes to Brad Cornes for the amazing work he produced. Styling with tailwind.macro has been such a pleasure.