Quickstart: Preact
1 Create the project
shellnpm create vite@latest css-hooks-playground -- --template preact-ts cd css-hooks-playground
2 Install CSS Hooks
shellnpm install @css-hooks/preact@next
3 Create the styling module
src/css.tsimport { createHooks, mergeStyles } from "@css-hooks/preact"; export const { on, styleSheet } = createHooks("&:hover"); export { mergeStyles };
4 Render the stylesheet
src/main.tsximport { render } from 'preact' -import './index.css' import { App } from './app.tsx' +import { styleSheet } from './css' -render(<App />, document.getElementById('app')!) +render( + <> + <style dangerouslySetInnerHTML={{ __html: styleSheet() }} /> + <App /> + </>, + document.getElementById('app')!, +)
5 Create a hover effect
Replace the contents of src/app.tsx with the following:
src/app.tsximport { on } from "./css"; export function App() { return <button style={on("&:hover", { color: "red" })}>Hover me</button>; }
6 View the result
shellnpm 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.