|
1 | | -**💛 You can help the author become a full-time open-source maintainer by [sponsoring him on GitHub](https://github.com/sponsors/egoist).** |
| 1 | +<h1 align="center">react-cva</h1> |
2 | 2 |
|
3 | | ---- |
| 3 | +<p align="center"> |
| 4 | + <a href="https://www.npmjs.com/package/react-cva"> |
| 5 | + <img alt="NPM Version" src="https://badgen.net/npm/v/react-cva" /> |
| 6 | + </a> |
| 7 | + <a href="https://www.npmjs.com/package/react-cva"> |
| 8 | + <img alt="Types Included" src="https://badgen.net/npm/types/react-cva" /> |
| 9 | + </a> |
| 10 | + <a href="https://bundlephobia.com/result?p=react-cva"> |
| 11 | + <img alt="Minizipped Size" src="https://img.shields.io/bundlephobia/minzip/react-cva" /> |
| 12 | + </a> |
| 13 | + <a href="https://www.npmjs.com/package/react-cva"> |
| 14 | + <img alt="NPM Downloads" src="https://badgen.net/npm/dm/react-cva" /> |
| 15 | + </a> |
| 16 | + <a href="https://twitter.com/NFS_21"> |
| 17 | + <img alt="Follow @NFS_21 on Twitter" src="https://img.shields.io/twitter/follow/NFS_21.svg?style=social&label=Follow" /> |
| 18 | + </a> |
| 19 | +</p> |
4 | 20 |
|
5 | | -# my-ts-lib |
| 21 | +## Introduction |
6 | 22 |
|
7 | | -[](https://npm.im/my-ts-lib) [](https://npm.im/my-ts-lib) |
| 23 | +this is a helper library for [cva](https://github.com/joe-bell/cva#readme) which this uses internally, for creating react components. |
| 24 | +for more information view [cva docs](https://github.com/joe-bell/cva#readme). |
| 25 | +## Installation |
8 | 26 |
|
9 | | -## Using this template |
| 27 | +```sh |
| 28 | +npm i react-cva |
| 29 | +``` |
| 30 | + |
| 31 | +## Examples |
| 32 | + |
| 33 | +<details> |
| 34 | + <summary>without ref forwarding</summary> |
| 35 | + |
| 36 | +```tsx |
| 37 | +import { styled } from "react-cva"; |
| 38 | + |
| 39 | +const Button = styled("button")("test", { |
| 40 | + variants: { |
| 41 | + margin: { 0: "m-0", 2: "m-2", 4: "m-4", 8: "m-8" }, |
| 42 | + padding: { 0: "p-0", 2: "p-2", 4: "p-4", 8: "p-8" }, |
| 43 | + }, |
| 44 | + defaultVariants: { |
| 45 | + margin: 0, |
| 46 | + padding: 0, |
| 47 | + }, |
| 48 | +}); |
| 49 | + |
| 50 | +const Render = () => { |
| 51 | + return ( |
| 52 | + <div> |
| 53 | + <Button>test</Button> |
| 54 | + </div> |
| 55 | + ); |
| 56 | +}; |
| 57 | + |
| 58 | +``` |
| 59 | + |
| 60 | +</details> |
| 61 | + |
| 62 | + |
| 63 | +<details> |
| 64 | + <summary>with ref forwarding</summary> |
| 65 | + |
| 66 | +```tsx |
| 67 | +import { styled } from "react-cva"; |
| 68 | +import { useRef } from "react"; |
| 69 | + |
| 70 | +const Button = styled("button", true)("test", { |
| 71 | + variants: { |
| 72 | + margin: { 0: "m-0", 2: "m-2", 4: "m-4", 8: "m-8" }, |
| 73 | + padding: { 0: "p-0", 2: "p-2", 4: "p-4", 8: "p-8" }, |
| 74 | + }, |
| 75 | + defaultVariants: { |
| 76 | + margin: 0, |
| 77 | + padding: 0, |
| 78 | + }, |
| 79 | +}); |
| 80 | + |
| 81 | +const Render = () => { |
| 82 | + const ref = useRef<HTMLButtonElement | null>(null); |
| 83 | + |
| 84 | + return ( |
| 85 | + <div> |
| 86 | + <Button ref={ref}>test</Button> |
| 87 | + </div> |
| 88 | + ); |
| 89 | +}; |
10 | 90 |
|
11 | | -- Search `my-ts-lib` and replace it with your custom package name. |
12 | | -- Search `egoist` and replace it with your name. |
| 91 | +``` |
| 92 | + |
| 93 | +</details> |
| 94 | + |
| 95 | +<details> |
| 96 | + <summary>with tailwind css</summary> |
| 97 | + |
| 98 | +```tsx |
| 99 | +import { styled } from "react-cva"; |
| 100 | + |
| 101 | +const Button = styled("button", true)("button", { |
| 102 | + variants: { |
| 103 | + intent: { |
| 104 | + primary: [ |
| 105 | + "bg-blue-500", |
| 106 | + "text-white", |
| 107 | + "border-transparent", |
| 108 | + "hover:bg-blue-600", |
| 109 | + ], |
| 110 | + secondary: [ |
| 111 | + "bg-white", |
| 112 | + "text-gray-800", |
| 113 | + "border-gray-400", |
| 114 | + "hover:bg-gray-100", |
| 115 | + ], |
| 116 | + }, |
| 117 | + size: { |
| 118 | + small: ["text-sm", "py-1", "px-2"], |
| 119 | + medium: ["text-base", "py-2", "px-4"], |
| 120 | + }, |
| 121 | + }, |
| 122 | + compoundVariants: [{ intent: "primary", size: "medium", class: "uppercase" }], |
| 123 | + defaultVariants: { |
| 124 | + intent: "primary", |
| 125 | + size: "medium", |
| 126 | + }, |
| 127 | +}); |
| 128 | + |
| 129 | +const Render = () => { |
| 130 | + return ( |
| 131 | + <div> |
| 132 | + <Button intent="primary">test</Button> |
| 133 | + </div> |
| 134 | + ); |
| 135 | +}; |
| 136 | + |
| 137 | +``` |
| 138 | + |
| 139 | +</details> |
| 140 | + |
| 141 | +<details> |
| 142 | + <summary>with css modules</summary> |
| 143 | + |
| 144 | +```tsx |
| 145 | +import { styled } from "react-cva"; |
| 146 | +import style from "./button.module.css"; |
| 147 | + |
| 148 | +const Button = styled("button", true)(style.base, { |
| 149 | + variants: { |
| 150 | + intent: { |
| 151 | + primary: style.primary, |
| 152 | + secondary: style.secondary, |
| 153 | + }, |
| 154 | + size: { |
| 155 | + small: style.small, |
| 156 | + medium: style.medium, |
| 157 | + }, |
| 158 | + }, |
| 159 | + compoundVariants: [ |
| 160 | + { intent: "primary", size: "medium", class: style.primaryMedium }, |
| 161 | + ], |
| 162 | + defaultVariants: { |
| 163 | + intent: "primary", |
| 164 | + size: "medium", |
| 165 | + }, |
| 166 | +}); |
| 167 | + |
| 168 | +const Render = () => { |
| 169 | + return ( |
| 170 | + <div> |
| 171 | + <Button>test</Button> |
| 172 | + </div> |
| 173 | + ); |
| 174 | +}; |
| 175 | + |
| 176 | +``` |
13 | 177 |
|
14 | | -Features: |
| 178 | +</details> |
15 | 179 |
|
16 | | -- Package manager [pnpm](https://pnpm.js.org/), safe and fast |
17 | | -- Release with [semantic-release](https://npm.im/semantic-release) |
18 | | -- Bundle with [tsup](https://github.com/egoist/tsup) |
19 | | -- Test with [vitest](https://vitest.dev) |
| 180 | +## API Reference |
20 | 181 |
|
21 | | -To skip CI (GitHub action), add `skip-ci` to commit message. To skip release, add `skip-release` to commit message. |
| 182 | +### `styled` |
22 | 183 |
|
23 | | -## Install |
| 184 | +Builds a `styled` component |
24 | 185 |
|
25 | | -```bash |
26 | | -npm i my-ts-lib |
| 186 | +```ts |
| 187 | +const Component = styled("div",true)("base", options); |
27 | 188 | ``` |
28 | 189 |
|
29 | | -## Sponsors |
| 190 | +#### Parameters |
30 | 191 |
|
31 | | -[](https://github.com/sponsors/egoist) |
| 192 | +1. `div`: tag type (`HtmlElement`) |
| 193 | +2. `true` should forward prop (`boolean` or `undefined`) |
| 194 | +3. `base`: the base class name (`string`, `string[]` or `null`) |
| 195 | +4. `options` _(optional)_ |
| 196 | + - `variants`: your variants schema |
| 197 | + - `compoundVariants`: variants based on a combination of previously defined variants |
| 198 | + - `defaultVariants`: set default values for previously defined variants |
32 | 199 |
|
33 | | -## License |
| 200 | +#### Returns |
34 | 201 |
|
35 | | -MIT © [EGOIST](https://github.com/sponsors/egoist) |
| 202 | +A JSX Element |
0 commit comments