All Projects β†’ ben-rogerson β†’ Twin.macro

ben-rogerson / Twin.macro

Licence: mit
πŸ¦Ήβ€β™‚οΈ Twin blends the magic of Tailwind with the flexibility of css-in-js (emotion, styled-components, stitches and goober) at build time.

Programming Languages

javascript
184084 projects - #8 most used programming language
typescript
32286 projects

Projects that are alternatives of or similar to Twin.macro

tsstyled
A small, fast, and simple CSS-in-JS solution for React.
Stars: ✭ 52 (-98.99%)
Mutual labels:  styled-components, emotion, css-in-js, emotionjs, goober
twin.examples
Packed with examples for different frameworks, this repo helps you get started with twin a whole lot faster.
Stars: ✭ 320 (-93.77%)
Mutual labels:  styled-components, twin, emotionjs, tailwind, tailwindcss
Xwind
Tailwind CSS as a templating language in JS and CSS-in-JS
Stars: ✭ 249 (-95.15%)
Mutual labels:  babel, tailwindcss, css-in-js, emotion
Xstyled
A utility-first CSS-in-JS framework built for React. πŸ’…πŸ‘©β€πŸŽ€βš‘οΈ
Stars: ✭ 1,835 (-64.28%)
Mutual labels:  styled-components, tailwindcss, css-in-js, emotion
stitchwind
A bridge between Tailwind and Stitches
Stars: ✭ 33 (-99.36%)
Mutual labels:  css-in-js, stitches, tailwind, tailwindcss
Goober
πŸ₯œ goober, a less than 1KB πŸŽ‰ css-in-js alternative with a familiar API
Stars: ✭ 2,317 (-54.9%)
Mutual labels:  styled-components, css-in-js, emotion, goober
tailwind-color-alpha
Automatic alpha variants for your Tailwind CSS colors based on your opacity config
Stars: ✭ 21 (-99.59%)
Mutual labels:  tailwind, tailwindcss, tailwind-css
visage
Visage design system
Stars: ✭ 12 (-99.77%)
Mutual labels:  emotion, css-in-js, emotionjs
tailwind-ui-components
Free Tailwind CSS UI Components - Crafted for modern websites, landing pages and web apps. TailGrids Core is free and open-source so, feel free to use with your personal or commercial projects. If you would like to show your support and love, don't forget to give us a star 🌟
Stars: ✭ 49 (-99.05%)
Mutual labels:  tailwind, tailwindcss, tailwind-css
Next-JS-Landing-Page-Starter-Template
πŸš€ Free NextJS Landing Page Template written in Tailwind CSS 3 and TypeScript ⚑️ Made with developer experience first: Next.js 12 + TypeScript + ESLint + Prettier + Husky + Lint-Staged + VSCode + Netlify + PostCSS + Tailwind CSS
Stars: ✭ 521 (-89.86%)
Mutual labels:  tailwind, tailwindcss, tailwind-css
benefit
✨ Utility CSS-in-JS library that provides a set of low-level, configurable, ready-to-use styles
Stars: ✭ 51 (-99.01%)
Mutual labels:  emotion, css-in-js, tailwindcss
cra-tailwindcss
Integrate Tailwind CSS in a Create React App setup
Stars: ✭ 105 (-97.96%)
Mutual labels:  tailwind, tailwindcss, tailwind-css
vuejs-tailwindcss-portfolio
A simple multipage and responsive Vue.js & Tailwind CSS portfolio theme with dark mode.
Stars: ✭ 100 (-98.05%)
Mutual labels:  tailwind, tailwindcss, tailwind-css
Styled-Responsive-Media-Queries
Responsive Media Queries for Emotion styled or styled-components β€” Standard size from Chrome DevTools.
Stars: ✭ 33 (-99.36%)
Mutual labels:  styled-components, emotion, css-in-js
material-tailwind
@material-tailwind is an easy-to-use components library for Tailwind CSS and Material Design.
Stars: ✭ 861 (-83.24%)
Mutual labels:  tailwind, tailwindcss, tailwind-css
eslint-plugin-tailwind
ESLint rules for Tailwind CSS
Stars: ✭ 97 (-98.11%)
Mutual labels:  tailwind, tailwindcss, tailwind-css
tailwindcss-postcss-browsersync-boilerplate
Tailwind CSS + PostCSS + BrowserSync boilerplate
Stars: ✭ 28 (-99.45%)
Mutual labels:  tailwind, tailwindcss, tailwind-css
Babel Plugin Tailwind Components
Use Tailwind with any CSS-in-JS library
Stars: ✭ 320 (-93.77%)
Mutual labels:  babel, tailwindcss, css-in-js
Styled Ppx
Typed styled components in Reason, OCaml and ReScript
Stars: ✭ 236 (-95.41%)
Mutual labels:  styled-components, css-in-js, emotion
Postjss
Use the power of PostCSS in compiling with JSS
Stars: ✭ 40 (-99.22%)
Mutual labels:  babel, styled-components, css-in-js

twin logo

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

Total Downloads Latest Release Discord

Demo twin on CodeSandbox β†’


Style jsx elements using Tailwind classes:

import 'twin.macro'

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

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

import tw from 'twin.macro'

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

Or mix sass styles with the css import:

import tw, { css } from 'twin.macro'

const hoverStyles = css`
  &:hover {
    border-color: black;
    ${tw`text-black`}
  }
`
const Input = ({ hasHover }) => (
  <input css={[tw`border`, hasHover && hoverStyles]} />
)

Styled Components

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

import tw from 'twin.macro'

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

And clone and style existing components:

const PurpleInput = tw(Input)`border-purple-500`

Switch to the styled import to add conditional styling:

import tw, { styled } from 'twin.macro'

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

Or use backticks to mix with sass styles:

import tw, { styled } from 'twin.macro'

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

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:

import tw from 'twin.macro'

tw`text-sm md:text-lg`

// ↓ ↓ ↓ ↓ ↓ ↓

{
  fontSize: '0.875rem',
  '@media (min-width: 768px)': {
    fontSize: '1.125rem',
  },
}

Features

πŸ‘Œ Simple imports - Twin collapses imports from common styling libraries into a single import:

+ import tw, { styled, css } from 'twin.macro'
- import tw from 'twin.macro'
- import styled from '@emotion/styled'
- import css from '@emotion/react'

🐹 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

πŸ›Ž Helpful suggestions for mistypings - Twin chimes in with class and variant values from your Tailwind config:

βœ• ml-7 was not found

Try one of these classes:
ml-0 [0] / ml-1 [0.25rem] / ml-2 [0.5rem] / ml-3 [0.75rem] / ml-4 [1rem] / ml-5 [1.25rem] / ml-6 [1.5rem]
ml-8 [2rem] / ml-10 [2.5rem] / ml-12 [3rem] / ml-16 [4rem] / ml-20 [5rem] / ml-24 [6rem] / ml-32 [8rem]
ml-40 [10rem] / ml-48 [12rem] / ml-56 [14rem] / ml-64 [16rem] / ml-auto [auto] / ml-px [1px]

πŸ’‘ Works with the official tailwind vscode plugin - Avoid having to look up your classes with auto-completions straight from your Tailwind config - See setup instructions β†’

πŸš₯ Over 40 variants to prefix on your classes - The prefixes are β€œalways on” and available for your classes

  • Prefix with hocus: to style hover + focus at the same time
  • Style form field states with checked:, invalid: and required:
  • Stack up variants whenever you need them sm:hover:first:bg-black

Check out the full list of variants β†’

🍱 Apply variants to multiple classes at once with variant groups

import 'twin.macro'

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

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

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

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

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

πŸ‘‘ Add vanilla css that integrates with twins features

const setCssVariables = () => <div tw="--base-color[#C0FFEE]" />

const customGridProperties = () => <div tw="grid-area[1 / 1 / 4 / 2]" />

const vendorPrefixes = () => <div tw="-webkit-mask-image[url(mask.png)]" />

πŸ–ŒοΈ Use the theme import to add values from your tailwind config

import { css, theme } from 'twin.macro'

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

See more examples using the theme import β†’

πŸ’₯ Add !important to any class with a trailing or leading bang!

<div tw="hidden!" /> || <div tw="!hidden" />
// ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓
<div css={{ "display": "none !important" }} />

Add !important to multiple classes with bracket groups:

<div tw="(hidden ml-auto)!" />
// ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓
<div css={{ "display": "none !important", "marginLeft": "auto !important" }} />

Get started

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

App build tools and libraries

Advanced frameworks

Component libraries

πŸŽ‰ : Fresh example

Community

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

Discord

Resources

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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].