A more elegant way of writing Tailwind classes. Never need to repeating the same variants over and over again.
| 📍 Entry point | 📦 Bundle | 📦 Gzip |
|---|---|---|
twg |
732 B | 413 B |
twg/transform |
3676 B | 1497 B |
- Support for normal strings.
- Support for multiple objects parsing.
- Support for nesting multiple objects, arrays, and itself functions.
- Support for (multiple) conditional classes, objects, and arrays.
- Customizable
calleename andseparator. - Compatible with wrappers like
twMerge. - "Base" support for Tailwind CSS IntelliSense (IDEs extension), as well as Hover Preview.
- Tree-shaking friendly.
| Framework | Version |
|---|---|
tailwindcss |
3.x |
Note
Tailwind CSS v4 is not supported. Because they removed content.transform API.
Currently, there is no way to intervene before Tailwind scans the classes.
Example:
<div
className={twg("size-92 relative grid place-items-center", {
before: "absolute inset-0 bg-red-500",
"aria-expanded": "bg-red-500 text-yellow-500"
})}
>
Hello, World!
</div>Output (html):
<div class="size-92 relative grid place-items-center before:absolute before:inset-0 before:bg-red-500 aria-expanded:bg-red-500 aria-expanded:text-yellow-500">
Hello, World!
</div>Tip
Simply open an Object, put the variant as key, and classes you want to map to that variant as each value.
See usage / use cases for more details.
1. Install the package
pnpm add twgor
npm install twg2. Setup
// tailwind.config.ts
import { type Config } from "tailwindcss"
import { transformer } from "twg/transform"
export default {
content: {
files: [
"./src/app/**/*.{ts,tsx}",
"./src/components/**/*.{ts,tsx}",
"./src/content/**/*.mdx"
], // Move your old `content` to `content.files` like this
transform: {
DEFAULT: transformer() // Put `transformer()` here
}
},
// ...
} satisfies Config-
If you need to override default
transformer()options:transform: { DEFAULT: transformer({ // Define options here, eg.: callee: "cn" }) }
See all options and how to custom options.
3. Usage
import { twg } from "twg"-
If you need to override default
twg()options, you need to usecreateTwg()function:import { createTwg } from "twg" createTwg({ separator: "_" })(...inputs) //...
See custom
separator.
For more information, consider reading custom options
See how to use in docs.
For full & latest update changelog, please refer to CHANGELOG.md.
twg now work for me but maybe not for you in some edges. Consider opening an issue if you have any problem with it that I can fix it ASAP. Or a pull request is welcome too.
If you have any ideas, feel free to open a feature request template or make a pull request to share your ideas.
For Development and more information on contributing please read CONTRIBUTING.md.
- August 15, 2024
https://www.linkedin.com/in/hoangnhan2ka3/
