|
1 | 1 | # Vite-Plugin-Hyperstyles |
2 | 2 |
|
3 | | -A Vite plugin that allows you to use styled hyperscript easily. |
| 3 | +A Vite plugin that allows you to easily style your hyperscript using |
| 4 | +module-scoped CSS. |
| 5 | + |
| 6 | +## Usage |
| 7 | + |
| 8 | +```css |
| 9 | +/* styles.modules.css */ |
| 10 | +.container { |
| 11 | + display: flex; |
| 12 | + flex-direction: column; |
| 13 | + align-items: center; |
| 14 | +} |
| 15 | + |
| 16 | +.title { |
| 17 | + color: blue; |
| 18 | + font-size: 20px; |
| 19 | +} |
| 20 | + |
| 21 | +.description { |
| 22 | + color: gray; |
| 23 | + font-size: 16px; |
| 24 | +} |
| 25 | +``` |
| 26 | + |
| 27 | +```typescript |
| 28 | +import h from "./styles.modules.css"; |
| 29 | + |
| 30 | +// Class name scopes are applied automatically |
| 31 | +const element = h("div.container", [ |
| 32 | + h("h1.title", "Hello, World!"), |
| 33 | + h( |
| 34 | + "p.description", |
| 35 | + { className: h.description }, |
| 36 | + "This is a styled hyperscript example.", |
| 37 | + ), |
| 38 | +]); |
| 39 | +``` |
| 40 | + |
| 41 | +This replaces the slightly more verbose syntax that can be used without the |
| 42 | +plugin: |
| 43 | + |
| 44 | +```typescript |
| 45 | +import hyper from "@macrostrat/hyper"; |
| 46 | +import styles from "./styles.modules.css"; |
| 47 | + |
| 48 | +const h = hyper.styled(styles); |
| 49 | +... |
| 50 | +``` |
| 51 | + |
| 52 | +## Configuration |
| 53 | + |
| 54 | +In your `vite.config.ts`, add the plugin: |
| 55 | + |
| 56 | +```typescript |
| 57 | +import { defineConfig } from "vite"; |
| 58 | +import hyperStyles from "@macrostrat/vite-plugin-hyperstyles"; |
| 59 | + |
| 60 | +export default defineConfig({ |
| 61 | + plugins: [hyperStyles()], |
| 62 | +}); |
| 63 | +``` |
| 64 | + |
| 65 | +If you're using Typescript, you'll want to add a type declaration for the |
| 66 | +enhanced hyperscript function to your TSConfig |
| 67 | + |
| 68 | +```json |
| 69 | +{ |
| 70 | + "compilerOptions": { |
| 71 | + // ... |
| 72 | + "types": ["@macrostrat/vite-plugin-hyperstyles/client", "vite/client"] |
| 73 | + // Note: order matters; ensure vite/client comes after this plugin |
| 74 | + } |
| 75 | +} |
| 76 | +``` |
0 commit comments