|
5 | 5 |
|
6 | 6 | ## Installation
|
7 | 7 |
|
8 |
| -There are two parts to the installation of Pigment CSS - |
| 8 | +There are two parts to the installation and usage of Pigment CSS in an application. First one is the runtime package that is going to be part of an app's source code. And second one is the bundler package that will be part of the app's bundler config. |
9 | 9 |
|
10 |
| -### Install the runtime library |
11 |
| - |
12 |
| -If you are using Pigment CSS with React, you can directly install the package with React integration - |
| 10 | +### Runtime package |
13 | 11 |
|
14 | 12 | ```bash title="Terminal"
|
15 | 13 | npm i @pigment-css/react-new
|
16 | 14 | ```
|
17 | 15 |
|
18 |
| -```jsx title="Test" |
19 |
| -import os from 'os'; |
| 16 | +or when not using React |
| 17 | + |
| 18 | +```bash title="Terminal" |
| 19 | +npm i @pigment-css/core |
| 20 | +``` |
| 21 | + |
| 22 | +### Bundler package |
| 23 | + |
| 24 | +```bash title="Terminal" |
| 25 | +npm i --save-dev @pigment-css/plugins |
| 26 | +``` |
| 27 | + |
| 28 | +After the packages are installed, proceed to configure the app as below. |
| 29 | + |
| 30 | +## Setting up a Next.js app |
| 31 | + |
| 32 | +Open the `next.config.ts` or `next.config.mjs` file and add the following code to setup a basic Pigment CSS integration - |
| 33 | + |
| 34 | +```ts |
| 35 | +import withPigment from '@pigment-css/plugin/nextjs'; |
| 36 | + |
| 37 | +// App's Next.js config |
| 38 | +const nextConfig = {}; |
20 | 39 |
|
21 |
| -console.log(os); |
| 40 | +export default withPigment(nextConfig); |
22 | 41 | ```
|
| 42 | + |
| 43 | +Note: Pigment CSS with Next.js does not support Turbopack. We'll add the support once Turbopack is stable and has added some of the missing APIs that is present in Webpack right now and is needed by Pigment CSS. |
| 44 | + |
| 45 | +## Setting up a Vite based app |
| 46 | + |
| 47 | +Open the `vite.config.ts` file and add the `pigment` plugin - |
| 48 | + |
| 49 | +```ts |
| 50 | +import { defineConfig } from 'vite'; |
| 51 | +import pigment from '@pigment-css/plugin/vite'; |
| 52 | + |
| 53 | +export default defineConfig({ |
| 54 | + plugins: [react(), pigment()], |
| 55 | +}); |
| 56 | +``` |
| 57 | + |
| 58 | +## Usage |
| 59 | + |
| 60 | +### Non React usage |
| 61 | + |
| 62 | +```jsx |
| 63 | +import { css } from '@pigment-css/react'; |
| 64 | + |
| 65 | +const visuallyHidden = css({ |
| 66 | + border: 0, |
| 67 | + clip: 'rect(0 0 0 0)', |
| 68 | + height: '1px', |
| 69 | + margin: -1, |
| 70 | + overflow: 'hidden', |
| 71 | + padding: 0, |
| 72 | + position: 'absolute', |
| 73 | + whiteSpace: 'nowrap', |
| 74 | + width: '1px', |
| 75 | +}); |
| 76 | + |
| 77 | +// alternatively |
| 78 | +const visuallyHidden2 = css` |
| 79 | + border: 0; |
| 80 | + clip: rect(0 0 0 0); |
| 81 | + height: 1px; |
| 82 | + margin: -1px; |
| 83 | + overflow: hidden'; |
| 84 | + padding: 0; |
| 85 | + position: absolute; |
| 86 | + whiteSpace: nowrap; |
| 87 | + width: 1px; |
| 88 | +`; |
| 89 | + |
| 90 | +function App() { |
| 91 | + return <div {...visuallyHidden()}>I am invisible</div>; |
| 92 | +} |
| 93 | +``` |
| 94 | + |
| 95 | +Which API signature to use is totally on the requirements. Somethings are easier with the tagged-template literal signature like copy-pasting css from other source (Figma for example) but it requires editor integration. For example, [vscode-styled-components](https://marketplace.visualstudio.com/items?itemName=styled-components.vscode-styled-components) extension needs to be installed to get completions and syntax highlighting in VSCode. |
| 96 | + |
| 97 | +The css object API is more flexible as well as typesafe. Generic components with inbuilt variants can be authored with the CSS object API. |
| 98 | + |
| 99 | +### React components |
| 100 | + |
| 101 | +There's a `styled` export from the package that can be used to author React components directly using the same signature as `css`. |
| 102 | + |
| 103 | +```tsx |
| 104 | +import { styled } from '@pigment-css/react'; |
| 105 | + |
| 106 | +const Heading = styled('div')({ |
| 107 | + fontSize: '4rem', |
| 108 | + fontWeight: 'bold', |
| 109 | + padding: '10px 0px', |
| 110 | +}); |
| 111 | + |
| 112 | +// alternatively |
| 113 | +const Heading2 = styled('div')` |
| 114 | + font-size: 4rem; |
| 115 | + font-weight: bold; |
| 116 | + padding: 10px 0px; |
| 117 | +`; |
| 118 | + |
| 119 | +function App() { |
| 120 | + return <Heading>Hello</Heading>; |
| 121 | +} |
| 122 | +``` |
| 123 | + |
| 124 | +## Variants API |
| 125 | + |
| 126 | +Pigment CSS supports the class variance authority style API for both it `css` and `styled` functions. For example, a button component that accepts two props, `size` and `variant` can be authored and used like - |
| 127 | + |
| 128 | +```tsx |
| 129 | +const Button = styled('button')({ |
| 130 | + // base button styles |
| 131 | + display: 'inline-flex', |
| 132 | + alignItems: 'center', |
| 133 | + justifyContent: 'center', |
| 134 | + color: 'black', |
| 135 | + backgroundColor: 'white', |
| 136 | + // Variant styles |
| 137 | + variants: { |
| 138 | + size: { |
| 139 | + small: { |
| 140 | + padding: '2px 4px', |
| 141 | + }, |
| 142 | + medium: { |
| 143 | + padding: '5px 10px', |
| 144 | + }, |
| 145 | + large: { |
| 146 | + padding: '10px 12px', |
| 147 | + }, |
| 148 | + }, |
| 149 | + variant: { |
| 150 | + primary: { |
| 151 | + backgroundColor: 'blue', |
| 152 | + }, |
| 153 | + secondary: { |
| 154 | + backgroundColor: 'green', |
| 155 | + }, |
| 156 | + error: { |
| 157 | + backgroundColor: 'red', |
| 158 | + }, |
| 159 | + }, |
| 160 | + }, |
| 161 | + defaultVariants: { |
| 162 | + size: 'medium', |
| 163 | + variant: 'primary', |
| 164 | + }, |
| 165 | +}); |
| 166 | + |
| 167 | +// Usage |
| 168 | + |
| 169 | +function App() { |
| 170 | + return ( |
| 171 | + <> |
| 172 | + {/* Default values of size `medium` and variant `primary` will be used */} |
| 173 | + <Button>Default</Button> |
| 174 | + <Button size="small">Small</Button> |
| 175 | + <Button size="large">Large</Button> |
| 176 | + <Button variant="secondary">Secondary</Button> |
| 177 | + <Button variant="error">Error</Button> |
| 178 | + <Button size="small" variant="error"> |
| 179 | + Small Error |
| 180 | + </Button> |
| 181 | + </> |
| 182 | + ); |
| 183 | +} |
| 184 | +``` |
| 185 | + |
| 186 | +The same parameter is supported by the `css` API. Here's the usage - |
| 187 | + |
| 188 | +```tsx |
| 189 | +const buttonCss = css({ |
| 190 | + // base button styles |
| 191 | + display: 'inline-flex', |
| 192 | + alignItems: 'center', |
| 193 | + justifyContent: 'center', |
| 194 | + color: 'black', |
| 195 | + backgroundColor: 'white', |
| 196 | + // Variant styles |
| 197 | + variants: { |
| 198 | + size: { |
| 199 | + small: { |
| 200 | + padding: '2px 4px', |
| 201 | + }, |
| 202 | + medium: { |
| 203 | + padding: '5px 10px', |
| 204 | + }, |
| 205 | + large: { |
| 206 | + padding: '10px 12px', |
| 207 | + }, |
| 208 | + }, |
| 209 | + variant: { |
| 210 | + primary: { |
| 211 | + backgroundColor: 'blue', |
| 212 | + }, |
| 213 | + secondary: { |
| 214 | + backgroundColor: 'green', |
| 215 | + }, |
| 216 | + error: { |
| 217 | + backgroundColor: 'red', |
| 218 | + }, |
| 219 | + }, |
| 220 | + }, |
| 221 | + defaultVariants: { |
| 222 | + size: 'medium', |
| 223 | + variant: 'primary', |
| 224 | + }, |
| 225 | +}); |
| 226 | + |
| 227 | +// Usage |
| 228 | + |
| 229 | +function App() { |
| 230 | + return ( |
| 231 | + <> |
| 232 | + {/* Default values of size `medium` and variant `primary` will be used */} |
| 233 | + <button {...buttonCss()}>Default</button> |
| 234 | + <button {...buttonCss({ size: 'small' })}>Small</button> |
| 235 | + <button {...buttonCss({ size: 'large' })}>Large</button> |
| 236 | + <button {...buttonCss({ variant: 'secondary' })}>Secondary</button> |
| 237 | + <button {...buttonCss({ variant: 'error' })}>Error</button> |
| 238 | + <button {...buttonCss({ variant: 'error', size: 'small' })}>Small Error</button> |
| 239 | + </> |
| 240 | + ); |
| 241 | +} |
| 242 | +``` |
| 243 | + |
| 244 | +The runtime call to `buttonCss()` only determines which classes to apply based on the values passed. |
| 245 | + |
| 246 | +## Theming and CSS Variables |
| 247 | + |
| 248 | +To be added |
0 commit comments