Skip to content

Commit 740a27b

Browse files
authored
docs(css): add createCssContext (#847)
1 parent 7d28abf commit 740a27b

1 file changed

Lines changed: 50 additions & 1 deletion

File tree

docs/helpers/css.md

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ You can write CSS in JSX in a JavaScript template literal named `css`. The retur
88

99
```ts
1010
import { Hono } from 'hono'
11-
import { css, cx, keyframes, Style } from 'hono/css'
11+
import { css, cx, keyframes, Style, createCssContext } from 'hono/css'
1212
```
1313

1414
## `css` <Badge style="vertical-align: middle;" type="warning" text="Experimental" />
@@ -212,6 +212,55 @@ app.get('/', (c) => {
212212
})
213213
```
214214

215+
## `createCssContext` <Badge style="vertical-align: middle;" type="warning" text="Experimental" />
216+
217+
`createCssContext` creates CSS helper functions (`css`, `cx`, `keyframes`, `viewTransition`, `Style`) with a custom context. You can use it to customize the style element ID and the generated class names.
218+
219+
```ts
220+
import { createCssContext } from 'hono/css'
221+
222+
const { css, cx, keyframes, Style } = createCssContext({
223+
id: 'my-app',
224+
})
225+
```
226+
227+
### `classNameSlug`
228+
229+
By default, CSS class names are generated in the format `css-1234567890`. You can customize this by passing a `classNameSlug` function.
230+
231+
The function receives three arguments:
232+
233+
- `hash` - the default generated class name (e.g. `css-1234567890`)
234+
- `label` - extracted from a `/* comment */` at the start of the CSS template (empty string if none)
235+
- `css` - the minified CSS string
236+
237+
```ts
238+
const { css, Style } = createCssContext({
239+
id: 'my-styles',
240+
classNameSlug: (hash, label) => (label ? `h-${label}` : hash),
241+
})
242+
243+
const heroClass = css`
244+
/* hero-section */
245+
background: blue;
246+
`
247+
// Generated class name: "h-hero-section"
248+
```
249+
250+
### `onInvalidSlug`
251+
252+
If the `classNameSlug` function returns an invalid CSS class name, a warning is logged by default. You can customize this behavior with `onInvalidSlug`.
253+
254+
```ts
255+
const { css, Style } = createCssContext({
256+
id: 'my-styles',
257+
classNameSlug: (hash, label) => label || hash,
258+
onInvalidSlug: (slug) => {
259+
throw new Error(`Invalid CSS class name: ${slug}`)
260+
},
261+
})
262+
```
263+
215264
## Tips
216265

217266
If you use VS Code, you can use [vscode-styled-components](https://marketplace.visualstudio.com/items?itemName=styled-components.vscode-styled-components) for Syntax highlighting and IntelliSense for CSS tagged literals.

0 commit comments

Comments
 (0)