An open API service indexing awesome lists of open source software.

https://github.com/hoangnhan2ka3/twg

๐Ÿชฅ A utility function for grouping TailwindCSS variants.
https://github.com/hoangnhan2ka3/twg

classnames clsx grouping-variants tailwind tailwind-variants tailwind-variants-grouping tailwindcss tailwindcss-plugin variants-grouping

Last synced: 4 months ago
JSON representation

๐Ÿชฅ A utility function for grouping TailwindCSS variants.

Awesome Lists containing this project

README

          


TWG Cover

twg


Tests Status
Repository stars
Bundle Size
Gzip Size
Total Downloads
Latest Release
License


Dependency Count
Tree Shakable Supports


๐Ÿชฅ A utility function for grouping Tailwind CSS variants.


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** |

---

## ๐Ÿ—๏ธ Features

- โœ… 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 `callee` name and `separator`.
- โœ… Compatible with wrappers like [`twMerge`](https://github.com/dcastil/tailwind-merge).
- โœ… "Base" support for Tailwind CSS IntelliSense (IDEs extension), as well as [Hover Preview](https://marketplace.visualstudio.com/items?itemName=bradlc.vscode-tailwindcss#hover-preview).
- โœ… Tree-shaking friendly.

---

## ๐Ÿ“ƒ Support

| Framework | Version |
| :------------ | :------ |
| `tailwindcss` | `3.x` |

> [!NOTE]
> Tailwind CSS `v4` is not supported. Because they removed [`content.transform`](https://v3.tailwindcss.com/docs/content-configuration#transforming-source-files) API.
> Currently, there is no way to intervene before Tailwind scans the classes.

---

## ๐Ÿ“Œ Table of contents

- [Quick intro](#-quick-intro)
- [Getting started](#-getting-started)
- [More on docs](#-docs)
- [Changelog](#-changelog)
- [Contributing](#-contributing)
- [Credits](#-credits)

---

## ๐Ÿšจ Quick intro

Example:

```jsx


Hello, World!

```

Output (html):

```jsx


Hello, World!

```

> [!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](../docs/usage.md) for more details.

---

## ๐Ÿš€ Getting started

**1. Install the package**

```bash
pnpm add twg
```

or

```bash
npm install twg
```

**2. Setup**

```js
// 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:

```js
transform: {
DEFAULT: transformer({
// Define options here, eg.:
callee: "cn"
})
}
```

See all [options](../docs/options.md#-default-version) and how to [custom options](../docs/options.md#-custom-options).

**3. Usage**

```jsx
import { twg } from "twg"
```

- If you need to override default `twg()` options, you need to use `createTwg()` function:

```js
import { createTwg } from "twg"

createTwg({ separator: "_" })(...inputs)
//...
```

See [custom `separator`](../docs/options.md#-custom-separator).

For more information, consider reading [custom options](../docs/options.md#-custom-options) โ†—๏ธ and [best practice](../docs/usage.md#best-practice-with-twmerge) โ†—๏ธ.

See [how to use](../docs/usage.md) on docs ๐Ÿ‘‡.

---

## ๐Ÿ“š Docs

- [Usage / Use cases](../docs/usage.md) โ†—๏ธ
- [Usage](../docs/usage.md#-usage) โ†—๏ธ
- [Basic usage](../docs/usage.md#-basic-usage) โ†—๏ธ
- [Complex as โ˜ ๏ธ usage](../docs/usage.md#-complex-as-%EF%B8%8F-usage) โ†—๏ธ
- [Use cases](../docs/usage.md#-use-cases) โ†—๏ธ
- [Conditionals](../docs/usage.md#-conditionals) โ†—๏ธ
- [Nesting callee functions](../docs/usage.md#-nesting-callee-functions) โ†—๏ธ
- [Combination](../docs/usage.md#-combination) โ†—๏ธ
- [Options](../docs/options.md) โ†—๏ธ
- [`transformer()` options](../docs/options.md#transformer-options) โ†—๏ธ
- [`createTwg()` options](../docs/options.md#createtwg-options) โ†—๏ธ
- [Custom options](../docs/options.md#-custom-options) โ†—๏ธ
- [Custom `callee`](../docs/options.md#-custom-callee) โ†—๏ธ
- [Custom `separator`](../docs/options.md#-custom-separator) โ†—๏ธ
- [Turn on `debug`](../docs/options.md#-turn-on-debug) โ†—๏ธ
- [API](../docs/api.md) โ†—๏ธ
- [Deeper explanation](../docs/introduction.md) โ†—๏ธ
- [What is `twg`?](../docs/introduction.md#-what-is-twg) โ†—๏ธ
- [Explanation](../docs/introduction.md#%EF%B8%8F-explanation) โ†—๏ธ
- [Trade-offs](../docs/introduction.md#-trade-offs) โ†—๏ธ

## ๐ŸŽ‰ Changelog

For full & latest update changelog, please refer to [CHANGELOG.md](../CHANGELOG.md).

## ๐Ÿ’Ž Contributing

### Bugs

`twg` now work for me but maybe not for you in some edges. Consider opening an [issue](https://github.com/hoangnhan2ka3/twg/issues) if you have any problem with it that I can fix it ASAP. Or a [pull request](https://github.com/hoangnhan2ka3/twg/pulls) is welcome too.

### Features

If you have any ideas, feel free to open a [feature request](https://github.com/hoangnhan2ka3/twg/issues/new/choose) template or make a [pull request](https://github.com/hoangnhan2ka3/twg/pulls) to share your ideas.

> For **Development** and more information on contributing please read [CONTRIBUTING.md](../CONTRIBUTING.md).

## ๐Ÿชช Credits

### References

- [easy-tailwind](https://github.com/Noriller/easy-tailwind) โ†—๏ธ
- [clsx](https://github.com/lukeed/clsx) โ†—๏ธ
- [tailwind-merge](https://github.com/dcastil/tailwind-merge) โ†—๏ธ
- [`content.transform` API](https://v3.tailwindcss.com/docs/content-configuration#transforming-source-files) โ†—๏ธ

### Project starts on

- August 15, 2024

### ๐Ÿ’ช Work with me

### Funding

[![Donate me](https://img.shields.io/static/v1?label=Donate%20me&message=โค๏ธ&style=social)](https://github.com/sponsors/hoangnhan2ka3)

---






Getting started




MIT ยฉ Nguyแป…n Hoร ng Nhรขn




Scroll to top