npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

tw-lite

v1.0.0

Published

[![npm registry](https://img.shields.io/npm/v/tw-lite)](https://www.npmjs.com/package/tw-lite) [![npm bundle size](https://img.shields.io/bundlephobia/minzip/tw-lite)](https://bundlephobia.com/package/[email protected]) [![MIT License](https://img.shields.io/

Downloads

38

Readme

tw-lite

npm registry npm bundle size MIT License

This small library can be used to generate React styled components with TailwindCSS without relying on a CSS-in-JS library and Babel macros.

This library can replace a good chunk of the functionality of twin.macro.

Goals

Non-goals

  • No parsing of Tailwind config to validate class names
  • No support for custom syntax

Installation

npm install tw-lite
yarn add tw-lite
pnpm add tw-lite
bun add tw-lite

Usage

All the examples below expect the following import:

import tw from 'tw-lite'

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

const Input = tw("input")`border hover:border-black`

And clone and style existing components:

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

Transient props

You can define transient props that will be passed to the component, you can leverage these props to add dynamic styles:

type Props = {
  $isEnabled: boolean;
  $variant: "primary" | "secondary";
}

const Button = tw("button")<Props>`
  ${props => props.$variant === "primary" ? "bg-blue-500" : "bg-gray-500"}
  ${props => props.$isEnabled ? "cursor-pointer" : "cursor-not-allowed"}
`

<Button $isEnabled $variant="primary">Click me!</Button>
// this will render
<button class="bg-blue-500 cursor-pointer">Click me!</button>

Transient props should be prefixed with $ so they are not passed to HTML tags to avoid polluting the DOM with extra attributes. However, they are always forwarded to React components.

Good old CSS

Since tw-lite doesn't parse the Tailwind classes, it can also be used with any CSS class, for example regular CSS, CSS Modules or Sass classes.

// .btn and .btn-primary can be defined in a CSS or Sass stylesheet
const Button = tw("button")`btn btn-primary`
import styles from "./Button.module.css"

const Button = tw("button")`${styles.button} text-lg`

See the examples directory for more.

Compatibility with VSCode TailwindCSS extension

To get the best experience with the TailwindCSS Intellisense extension for VSCode, you can add the following to your settings.json:

{
  "tailwindCSS.experimental.classRegex": [
    "tw\\(.*?\\)`([^`]*)" // tw(Component)`...`
  ]
}

How it works

The tw function is a tagged template literal that takes a string of Tailwind classes and returns a component with those classes applied.

Unlike with twin.macro, you must configure Tailwind as usual to generate the final stylesheet. This utility will not parse your Tailwind config to generate styles.

This however has a key benefit as your bundle size will be smaller.

Since tw-lite doesn't depend on Babel macros, this means it can be used with any bundler, including Bun, Vite, and esbuild without any additional setup.