Ecosyste.ms: Awesome

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

Awesome Lists | Featured Topics | Projects

https://github.com/joe-bell/cx

Concatenate your classes (with shortcuts) πŸ”—
https://github.com/joe-bell/cx

classnames css cx react typescript

Last synced: about 2 months ago
JSON representation

Concatenate your classes (with shortcuts) πŸ”—

Awesome Lists containing this project

README

        

> ⚠️ **This project has been deprecated in favour of [`cva`](https://github.com/joe-bell/cva)**


cx πŸ”—



Concatenate your classes (with shortcuts)



NPM Version


Types Included


Minizipped Size


MIT License


NPM Downloads


Follow @joebell_ on Twitter


**`cx`** is a tiny function that mimics and extends the popular [`classnames`][classnames] utility with **"shortcuts"** to re-use common classes (with TypeScript autocompletion)

> πŸ’‘ Works especially nicely with [Tailwind IntelliSense][tw]

## Setup

1. Install the package via your favourite package manager:

```sh
npm i @joebell/cx
```

2. Create a custom `cx` function for later use.

Common styles can be defined in the `shortcuts` and accessed by it's `"key"` for re-use later.

> ⚠️ **FYI**: At this point in time, your shortcuts must be a flat object with no nested keys.

```ts
// styles/index.ts
import { createCx } from "@joebell/cx";

const shortcuts = {
// Components
"c-container": ["block", "max-w-5xl", "mx-auto", "px-4"],
// Utilities
"u-border": ["border-dashed", "border-black"],
};

export const cx = createCx(shortcuts);
```

3. Import `cx` into your component or layout.

**For example, in React.js:**

```tsx
// pages/index.tsx
import { cx } from "../styles";

const Page: React.FC = ({ children }) => (
{children}
);
```

_returns…_

```html
…
```

> πŸ’‘ **Tip**: If you're having issues with [Tailwind IntelliSense][tw] in VSCode, try reloading the window

## Acknowledgements

Without these projects/people, this project wouldn't exist…

- [**classnames** - Jed Watson][classnames]

[classnames]: https://github.com/JedWatson/classnames
[tw]: https://github.com/tailwindlabs/tailwindcss-intellisense