CSS Hooks
Documentation
Source on GitHub

Quickstart: No framework

1 Create the project

shell
npm create vite@latest css-hooks-playground -- --template vanilla-ts cd css-hooks-playground

2 Install CSS Hooks

shell
npm install @css-hooks/core@next csstype

3 Create the styling module

src/css.ts
import { createHooksSystem } from "@css-hooks/core"; import type { PropertiesHyphen } from "csstype"; const { createHooks, mergeStyles } = createHooksSystem<PropertiesHyphen<string>>(); export const { on, styleSheet } = createHooks("&:hover"); /** Converts a style object to a string. */ export function styleObjectToString(style: PropertiesHyphen<string>) { return Object.entries(style) .filter(([, value]) => value !== undefined) .map(([property, value]) => `${property}: ${value}`) .join("; "); } export { mergeStyles };

4 Create a button

Replace the contents of src/main.ts with the following:

src/main.ts
document.querySelector("#app")!.innerHTML = "<button>Hover me</button>";

5 Render the stylesheet

src/main.ts
+import { styleSheet } from "./css"; + +const style = document.createElement("style"); +style.textContent = styleSheet(); +document.head.append(style); + document.querySelector("#app")!.innerHTML = "<button>Hover me</button>";

6 Create a hover effect

src/main.ts
-import { styleSheet } from "./css"; +import { on, styleObjectToString, styleSheet } from "./css"; const style = document.createElement("style"); style.textContent = styleSheet(); document.head.append(style); document.querySelector("#app")!.innerHTML = "<button>Hover me</button>"; + +document + .querySelector("button")! + .setAttribute("style", styleObjectToString(on("&:hover", { color: "red" })));

7 View the result

shell
npm run dev

By default, the development server is accessible at http://localhost:5173.

Next steps

Continue to Hooks and conditions to declare more hooks, then read Applying styles for composition patterns.