Convert Figma logo to code with AI

tw-in-js logotwind

The smallest, fastest, most feature complete Tailwind-in-JS solution in existence.

3,765
100
3,765
27

Top Related Projects

A utility-first CSS framework for rapid UI development.

Visual primitives for the component age. Use the best bits of ES6 and CSS to style your apps without stress 💅

17,396

👩‍🎤 CSS-in-JS library designed for high performance style composition

[Not Actively Maintained] CSS-in-JS with near-zero runtime, SSR, multi-variant support, and a best-in-class developer experience.

Zero-runtime Stylesheets-in-TypeScript

Next generation utility-first CSS framework.

Quick Overview

Twind is a small, fast, and feature-complete CSS-in-JS library that uses Tailwind CSS as its styling language. It allows developers to use Tailwind's utility classes directly in JavaScript or TypeScript, providing a seamless integration of styles with components.

Pros

  • Zero-runtime CSS-in-JS with full Tailwind CSS support
  • Tiny bundle size (8kB) with no external dependencies
  • Excellent TypeScript support with autocompletion
  • Works with any framework or vanilla JavaScript

Cons

  • Learning curve for developers not familiar with Tailwind CSS
  • May require additional setup compared to traditional CSS approaches
  • Limited customization options compared to full CSS preprocessors
  • Potential performance impact for very large applications

Code Examples

  1. Basic usage with React:
import { tw } from 'twind'

function Button({ children }) {
  return (
    <button className={tw`bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded`}>
      {children}
    </button>
  )
}
  1. Dynamic styling:
import { tw } from 'twind'

function Alert({ type, message }) {
  const alertStyles = {
    success: 'bg-green-100 border-green-500 text-green-700',
    error: 'bg-red-100 border-red-500 text-red-700',
  }

  return (
    <div className={tw`border-l-4 p-4 ${alertStyles[type]}`}>
      {message}
    </div>
  )
}
  1. Using twind with vanilla JavaScript:
import { tw } from 'twind'

const button = document.createElement('button')
button.textContent = 'Click me'
button.className = tw`bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded`
document.body.appendChild(button)

Getting Started

  1. Install Twind:
npm install twind
  1. Set up Twind in your project:
import { setup } from 'twind'

setup()
  1. Use Twind in your components:
import { tw } from 'twind'

function MyComponent() {
  return <div className={tw`text-center p-4 bg-gray-100`}>Hello, Twind!</div>
}

Competitor Comparisons

A utility-first CSS framework for rapid UI development.

Pros of Tailwind CSS

  • Larger community and ecosystem, with more resources and third-party tools
  • More comprehensive documentation and official learning resources
  • Better browser support, especially for older versions

Cons of Tailwind CSS

  • Larger bundle size, which can impact initial load times
  • Requires a build step, making it less suitable for quick prototyping
  • More complex setup process, especially for non-Node.js environments

Code Comparison

Tailwind CSS:

<div class="bg-blue-500 text-white p-4 rounded-lg shadow-md">
  <h1 class="text-2xl font-bold mb-2">Hello, Tailwind!</h1>
  <p class="text-sm">This is a sample component.</p>
</div>

Twind:

import { tw } from 'twind'

const Component = () => (
  <div className={tw`bg-blue-500 text-white p-4 rounded-lg shadow-md`}>
    <h1 className={tw`text-2xl font-bold mb-2`}>Hello, Twind!</h1>
    <p className={tw`text-sm`}>This is a sample component.</p>
  </div>
)

Both libraries offer utility-first CSS approaches, but Twind focuses on runtime performance and ease of use in JavaScript environments, while Tailwind CSS provides a more traditional CSS framework experience with broader compatibility and community support.

Visual primitives for the component age. Use the best bits of ES6 and CSS to style your apps without stress 💅

Pros of styled-components

  • More mature and widely adopted ecosystem
  • Better support for dynamic styling based on props
  • Seamless integration with existing CSS workflows

Cons of styled-components

  • Larger bundle size compared to Twind
  • Steeper learning curve for developers new to CSS-in-JS
  • Potential performance overhead due to runtime style generation

Code Comparison

styled-components:

const Button = styled.button`
  background-color: ${props => props.primary ? 'blue' : 'white'};
  color: ${props => props.primary ? 'white' : 'blue'};
  padding: 10px 20px;
`;

Twind:

import { tw } from 'twind'

const Button = ({ primary }) => (
  <button className={tw`bg-${primary ? 'blue' : 'white'} text-${primary ? 'white' : 'blue'} p-2 px-4`}>
    Click me
  </button>
)

Summary

styled-components offers a more established ecosystem and powerful dynamic styling capabilities, but comes with a larger bundle size and potential performance trade-offs. Twind, on the other hand, provides a lightweight alternative with a focus on utility-first CSS and smaller bundle sizes, but may have a less extensive ecosystem and fewer advanced features for complex styling scenarios.

17,396

👩‍🎤 CSS-in-JS library designed for high performance style composition

Pros of Emotion

  • More mature and widely adopted in the React ecosystem
  • Offers a broader set of features, including server-side rendering and theming
  • Provides better TypeScript support out of the box

Cons of Emotion

  • Steeper learning curve due to its more complex API
  • Larger bundle size compared to Twind
  • Requires additional setup and configuration for optimal use

Code Comparison

Emotion:

import { css } from '@emotion/react'

const style = css`
  background-color: hotpink;
  &:hover {
    color: blue;
  }
`

Twind:

import { tw } from 'twind'

const style = tw`
  bg-pink-500
  hover:text-blue-500
`

Both Emotion and Twind are CSS-in-JS libraries, but they have different approaches. Emotion focuses on providing a powerful and flexible API for styling React components, while Twind aims to bring the utility-first approach of Tailwind CSS to JavaScript. Emotion's syntax is more similar to traditional CSS, whereas Twind uses a more concise, utility-based syntax. The choice between the two depends on your project requirements, team preferences, and existing ecosystem.

[Not Actively Maintained] CSS-in-JS with near-zero runtime, SSR, multi-variant support, and a best-in-class developer experience.

Pros of Stitches

  • Near-zero runtime, with most styles generated at build time
  • TypeScript-first approach with excellent type inference
  • Supports server-side rendering out of the box

Cons of Stitches

  • Steeper learning curve compared to utility-first approaches
  • Less flexibility for rapid prototyping and quick style changes
  • Smaller community and ecosystem compared to Twind

Code Comparison

Stitches:

import { styled } from '@stitches/react';

const Button = styled('button', {
  backgroundColor: 'gainsboro',
  borderRadius: '9999px',
  fontSize: '13px',
  padding: '10px 15px',
});

Twind:

import { tw } from 'twind';

const Button = ({ children }) => (
  <button className={tw`bg-gray-200 rounded-full text-sm py-2 px-4`}>
    {children}
  </button>
);

Both Stitches and Twind offer solutions for styling in JavaScript, but they take different approaches. Stitches focuses on a CSS-in-JS methodology with strong typing, while Twind provides a utility-first approach similar to Tailwind CSS. The choice between them depends on project requirements, team preferences, and performance considerations.

Zero-runtime Stylesheets-in-TypeScript

Pros of vanilla-extract

  • Type-safe CSS-in-JS with zero runtime
  • Supports static extraction of CSS
  • Allows for theme creation and sharing across projects

Cons of vanilla-extract

  • Steeper learning curve compared to utility-first approaches
  • Requires build-time processing
  • Less flexibility for rapid prototyping

Code Comparison

vanilla-extract:

import { style } from '@vanilla-extract/css';

export const button = style({
  backgroundColor: 'blue',
  color: 'white',
  padding: '10px 20px',
});

twind:

import { tw } from 'twind';

const button = tw`bg-blue-500 text-white px-5 py-2`;

Key Differences

  • vanilla-extract focuses on type-safe, zero-runtime CSS-in-JS
  • twind provides a utility-first approach similar to Tailwind CSS
  • vanilla-extract requires more setup but offers better type safety
  • twind allows for quicker prototyping and easier adoption for Tailwind users

Both libraries aim to improve the CSS authoring experience, but they take different approaches. vanilla-extract is better suited for larger projects requiring strict type safety, while twind excels in rapid development scenarios and projects transitioning from Tailwind CSS.

Next generation utility-first CSS framework.

Pros of Windicss

  • Faster compilation and smaller bundle size
  • Full compatibility with Tailwind CSS v2.0
  • Supports variant groups and important prefix

Cons of Windicss

  • Less active community and fewer resources compared to Twind
  • May require additional configuration for some frameworks

Code Comparison

Windicss:

<div class="bg-blue-500 hover:(bg-red-500 text-white)">
  Hover me
</div>

Twind:

import { tw } from 'twind'

const Button = () => (
  <div className={tw`bg-blue-500 hover:(bg-red-500 text-white)`}>
    Hover me
  </div>
)

Both Windicss and Twind offer utility-first CSS approaches, but they have different implementations. Windicss focuses on compile-time generation of CSS, while Twind is a runtime CSS-in-JS solution. Windicss provides better performance and smaller bundle sizes, making it suitable for larger projects. However, Twind offers more flexibility and easier integration with JavaScript frameworks.

The code comparison shows that both libraries support similar syntax for utility classes and variant groups. Windicss can be used directly in HTML, while Twind requires importing the library and using the tw function in JavaScript components.

Ultimately, the choice between Windicss and Twind depends on project requirements, performance needs, and developer preferences.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

Twind

The smallest, fastest, most feature complete Tailwind-in-JS solution in existence


MIT License Latest Release Documentation Github Discord CI Coverage Status

Twind is a small compiler that converts utility classes into CSS at runtime. The goal of this project is to unify the flexibility of CSS-in-JS with the carefully considered constraints of the Tailwind API.

Utility-first CSS without any build step right in the browser or any other environment like Node.js, deno, workers, ...


Nov 18, 2022: Twind v1 is now in stable release!

Check out the Migration Guide to upgrade or go to the v0.16 branch.


✨ Features

⚡️ No build step

Get all the benefits of Tailwind without the need for Tailwind, PostCSS, configuration, purging, or autoprefixing.

🚀 Framework agnostic

If your app uses HTML and JavaScript, it should work with Twind. This goes for server-rendered apps too.

😎 One low fixed cost

Twind ships the compiler, not the CSS. This means unlimited styles and variants for one low fixed cost.

Other features
  • 🌎 No bundler required: Usable via CDN
  • 🎨 Seamless integration with Tailwind
  • 🤝 Feature parity with Tailwind v3
  • 🎯 Extended variants, rules, and syntax
  • 🚓 Escape hatch for arbitrary CSS
  • 🤖 Built in support for conditional rule combining
  • 🧐 Improved readability with multiline styles and comments
  • ❄️ Optional hashing of class names ensuring no conflicts
  • 🔩 Flexible: configurable theme, rules and variants
  • 🔌 Language extension via presets
  • 🎩 No runtime overhead with static extraction
  • 🚅 Faster than most CSS-in-JS libraries
  • ⚡ Fully tree shakeable: Only take what you want
  • 🦾 Type Strong: Written in Typescript
  • and more!

📖 Documentation

The full documentation is available at twind.style.

💬 Community

For help, discussion about best practices, or any other conversation that would benefit from being searchable use Github Discussions.

To ask questions and discuss with other Twind users in real time use Discord Chat.

🧱 Contribute

See the Contributing Guide for information on how to contribute to this project.

🌸 Credits

💡 Inspiration

It would be untrue to suggest that the design here is totally original. Other than the founders' initial attempts at implementing such a module (oceanwind and beamwind) we are truly standing on the shoulders of giants.

  • Tailwind CSS: created a wonderfully thought out API on which the compiler's grammar was defined.
  • styled-components: implemented and popularized the advantages of doing CSS-in-JS.
  • htm: a JSX compiler that proved there is merit in doing runtime compilation of DSLs like JSX.
  • goober: an impossibly small yet efficient CSS-in-JS implementation that defines critical module features.
  • otion: the first CSS-in-JS solution specifically oriented around handling CSS in an atomic fashion.
  • clsx: a tiny utility for constructing class name strings conditionally.
  • style-vendorizer: essential CSS prefixing helpers in less than 1KB of JavaScript.
  • UnoCSS: for the configuration syntax.
  • CSSType: providing autocompletion and type checking for CSS properties and values.

🤝 Contributors

Thank you to all the people who have already contributed to twind!

🙏🏾 Sponsors

This project is kindly sponsored by Kenoxa GmbH who support @sastan to maintain this project as part of their open-source engagement.

COPILOT TRAVEL is partnering with @sastan to keep twind aligned with the latest Tailwind CSS releases.

Thank you to all our sponsors!

Sponsors

Please ask your company to also support this open source project by becoming a sponsor on opencollective or GitHub.

⚖️ License

The MIT license governs your use of Twind.

NPM DownloadsLast 30 Days