simplestyle-js 5.5.1-alpha.114 → 6.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +171 -290
- package/package.json +17 -6
package/README.md
CHANGED
|
@@ -2,26 +2,16 @@
|
|
|
2
2
|
|
|
3
3
|
An ultra-tiny, neat, easy-to-use CSS-in-JS library with SSR support,
|
|
4
4
|
tiny bundle size and only *one runtime dependency*.
|
|
5
|
+
Framework agnostic by design — works with React, Vue, Svelte, Next.js, Astro, or no framework at all.
|
|
5
6
|
|
|
6
7
|
`6.48kB` / `2.69kB gzip` ([courtesy of bundlejs.com](https://bundlejs.com/?q=simplestyle-js))
|
|
7
8
|
|
|
8
|
-
# Simplestyle-js Reference
|
|
9
|
-
|
|
10
|
-
A concise guide to the core `simplestyle-js` APIs, how they fit together, and how to use them in browser and server-rendered apps (including Next.js).
|
|
11
|
-
|
|
12
9
|
## Table of Contents
|
|
13
10
|
- [Install](#install)
|
|
14
|
-
- [Quick Start](#quick-start)
|
|
11
|
+
- [Quick Start (Browser)](#quick-start-browser)
|
|
12
|
+
- [SSR / Static Extraction (Framework-Agnostic)](#ssr--static-extraction-framework-agnostic)
|
|
15
13
|
- [API Reference](#api-reference)
|
|
16
14
|
- [Patterns and Tips](#patterns-and-tips)
|
|
17
|
-
- [Creating reusable variables and / or design system-like tokens](#reusable-variables)
|
|
18
|
-
- [SSR](#ssr)
|
|
19
|
-
- [Next.js](#nextjs)
|
|
20
|
-
- [Astro](#astro)
|
|
21
|
-
- [SSR steps for most SSR / SSG frameworks](#ssr-steps-for-most-ssr--ssg-frameworks)
|
|
22
|
-
- [1. Set your seed, create a SimpleStyleRegistry and your style functions](#1-set-your-seed-create-a-simplestyleregistry-and-your-style-functions)
|
|
23
|
-
- [2. Render the generated styles in your HTML](#2-render-the-generated-styles-in-your-html)
|
|
24
|
-
- [3. Create your styles and have fun!](#3-create-your-styles-and-have-fun)
|
|
25
15
|
- [Creating a simplestyle-js plugin](#creating-a-simplestyle-js-plugin)
|
|
26
16
|
- [Plugin Example (Autoprefixer)](#plugin-example-autoprefixer)
|
|
27
17
|
- [License](#license)
|
|
@@ -50,349 +40,239 @@ pnpm add simplestyle-js
|
|
|
50
40
|
yarn add simplestyle-js
|
|
51
41
|
```
|
|
52
42
|
|
|
53
|
-
## Quick Start
|
|
43
|
+
## Quick Start (Runtime styles in the Browser)
|
|
44
|
+
|
|
45
|
+
Use the browser entrypoint for runtime CSS injection. This variant creates `<style />` tags in the browser and is the most traditional CSS‑in‑JS experience.
|
|
46
|
+
|
|
47
|
+
```tsx
|
|
48
|
+
import { makeCssFuncs } from 'simplestyle-js/browser';
|
|
49
|
+
|
|
50
|
+
const { createStyles, createKeyframes, createRawStyles } = makeCssFuncs();
|
|
54
51
|
|
|
55
|
-
|
|
56
|
-
|
|
52
|
+
const { keyframe } = createKeyframes('Pulse', () => ({
|
|
53
|
+
'0%': { opacity: 0.6, transform: 'scale(0.98)' },
|
|
54
|
+
'100%': { opacity: 1, transform: 'scale(1)' },
|
|
55
|
+
}));
|
|
56
|
+
|
|
57
|
+
createRawStyles('GlobalReset', () => ({
|
|
58
|
+
'*, *::before, *::after': { boxSizing: 'border-box' },
|
|
59
|
+
body: { margin: 0, fontFamily: 'system-ui, sans-serif' },
|
|
60
|
+
}));
|
|
57
61
|
|
|
58
62
|
const { classes } = createStyles('Button', () => ({
|
|
59
63
|
root: {
|
|
60
|
-
'&:hover': { backgroundColor: '#2d6cdf' },
|
|
61
|
-
'@media (max-width: 768px)': { padding: '10px 12px' },
|
|
62
64
|
backgroundColor: '#1f4aa8',
|
|
63
65
|
borderRadius: 6,
|
|
64
66
|
color: '#fff',
|
|
65
67
|
padding: '12px 16px',
|
|
68
|
+
animation: `${keyframe} 900ms ease-in-out infinite alternate`,
|
|
69
|
+
'&:hover': { backgroundColor: '#2d6cdf' },
|
|
70
|
+
'@media (max-width: 768px)': { padding: '10px 12px' },
|
|
66
71
|
},
|
|
67
72
|
}));
|
|
68
73
|
|
|
69
74
|
document.querySelector('button')?.classList.add(classes.root);
|
|
70
|
-
|
|
71
|
-
// or, in a React / JSX-like component
|
|
72
|
-
const Button = (props) => <button {...props} className={classes.root}>Awesome button</button>
|
|
73
75
|
```
|
|
74
76
|
|
|
75
|
-
Rules support nested selectors via `&`, media queries, and `$className` back
|
|
76
|
-
|
|
77
|
-
## API Reference
|
|
77
|
+
Rules support nested selectors via `&`, media queries, and `$className` back‑references to other generated classes.
|
|
78
78
|
|
|
79
|
-
|
|
80
|
-
- Builds a set of class names and CSS from a rules object. Returns `{ classes, stylesheet, updateSheet }`.
|
|
81
|
-
- `options.flush` (default `true`): injects a `<style>` tag into `document.head`.
|
|
82
|
-
- `options.insertBefore` / `insertAfter`: choose where the `<style>` tag is placed when flushing.
|
|
83
|
-
- `options.registry`: provide a `SimpleStyleRegistry` instance to **collect** CSS instead of touching the DOM (ideal for SSR).
|
|
84
|
-
- `updateSheet(updatedRules)` merges rules and updates the existing sheet (works when `flush` is `true` or a `registry` is provided). Returns `{ classes, stylesheet } | null`.
|
|
85
|
-
- Example:
|
|
86
|
-
```ts
|
|
87
|
-
const { classes, stylesheet } = createStyles('Nav', () => ({
|
|
88
|
-
wrapper: { display: 'flex', gap: 12 },
|
|
89
|
-
link: {
|
|
90
|
-
'&:hover': { textDecoration: 'underline' },
|
|
91
|
-
},
|
|
92
|
-
'@media (max-width: 600px)': {
|
|
93
|
-
wrapper: { flexDirection: 'column' },
|
|
94
|
-
},
|
|
95
|
-
}), { flush: false }); // do not write to the DOM automatically
|
|
96
|
-
```
|
|
97
|
-
|
|
98
|
-
- `imports(ruleId, importRulesFnc, options?)`
|
|
99
|
-
- Allow you to define one or more `@import()` rules for importing or referencing external stylesheets. **Note**: These imports will not be transformed or attempted to have their contents imported.
|
|
100
|
-
|
|
101
|
-
- `keyframes(ruleId, framesFnc, options?)`
|
|
102
|
-
- Generates a unique animation name and accompanying `@keyframes` CSS.
|
|
103
|
-
- Returns `{ keyframe: string; stylesheet: string; }`. Respects `flush` and `insertBefore/After` options.
|
|
104
|
-
|
|
105
|
-
- `rawStyles(ruleId, rulesFnc, options?)`
|
|
106
|
-
- Writes rules without generating new class names. Keys must already be selectors (e.g., `html`, `body *`, `.app`).
|
|
107
|
-
- Good for global resets or theme primitives. Respects `flush` and `registry`.
|
|
108
|
-
|
|
109
|
-
- `makeCssFuncs({ registry?, variables? })`
|
|
110
|
-
- Returns `createStyles`, `keyframes` and `rawStyles` functions for you to use that are bound to an optional `SimpleStyleRegistry` and an optional `variables` object.
|
|
111
|
-
- Use this variant if you want to bind all of the CSS functions to a set of styling tokens / variables that you want accessible and available wherever you write your CSS, complete with IDE intellisense.
|
|
112
|
-
- Use this as a convenience if you don't want to manually wire up each CSS function (listed below) to your registry.
|
|
113
|
-
|
|
114
|
-
- `makeCreateStyles(registry)`
|
|
115
|
-
- Convenience wrapper that returns a `createStyles` instance pre-bound to a `SimpleStyleRegistry`. Use this when you want every call to accumulate in a registry (especially useful for SSR / Server-side rendering).
|
|
116
|
-
|
|
117
|
-
- `makeRawStyles(registry)`
|
|
118
|
-
- Returns a `rawStyles` helper preconfigured with the provided registry; calls will auto-add to that registry instead of touching the DOM (same motivation as `makeCreateStyles` for SSR motivations).
|
|
119
|
-
|
|
120
|
-
- `makeKeyframes(registry)`
|
|
121
|
-
- Returns a `keyframes` helper preconfigured with the provided registry; calls will auto-add to that registry instead of touching the DOM (same motivation as `makeCreateStyles` for SSR motivations).
|
|
122
|
-
|
|
123
|
-
- `registerPosthook(fn: (sheet: string) => string)`
|
|
124
|
-
- Adds a transform that runs after CSS strings are generated but before they are flushed or stored. Use for autoprefixing, minification, or custom transforms. Hooks run in registration order.
|
|
125
|
-
|
|
126
|
-
- Types
|
|
127
|
-
- `SimpleStyleRules`: `{ [selectorOrKey: string]: Properties | SimpleStyleRules }` (recursive rule tree).
|
|
128
|
-
- `CreateStylesOptions`: shape of the options described above.
|
|
129
|
-
- `PosthookPlugin`: signature for `registerPosthook`.
|
|
79
|
+
## SSR / Static Extraction (Framework‑Agnostic)
|
|
130
80
|
|
|
131
|
-
|
|
81
|
+
The SSR workflow requires usage of the built in `ssjs` CLI tool.
|
|
82
|
+
This works by scanning your source files and generating a single `.css` file, containing all of the collected styles.
|
|
83
|
+
Your source code remains untouched and does not require any transformation or transpilation.
|
|
84
|
+
To leverage this, you **must** use the SSR entrypoint and place all styles in files named with the following format:
|
|
132
85
|
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
- **Media queries**: Top-level `@media` keys contain further rule objects.
|
|
137
|
-
- **DOM placement**: `insertBefore` and `insertAfter` let you control the exact placement for where `<style />` tags will be rendered (does not apply to SSR / Server-side rendering).br
|
|
138
|
-
- **Updating styles**: `updateSheet` merges the new rules and updates the existing `<style>` tag (or registry entry, if you're not letting `simplestyle-js` flush to the DOM automatically for you). It also returns `{ classes, stylesheet }` so you can re-use class names if needed.
|
|
86
|
+
```
|
|
87
|
+
*.styles.{js|cjs|mjs|ts|mts|cts|jsx|tsx}
|
|
88
|
+
```
|
|
139
89
|
|
|
140
|
-
|
|
90
|
+
Run the `ssjs` CLI to compile a single CSS file. This works with **Next.js, Astro, or any framework** (SSR or SSG) because the output is just plain CSS.
|
|
141
91
|
|
|
142
|
-
|
|
143
|
-
To do this, you would use the same API that's used to bind to a specific sheet registry, but specify the `variables` option:
|
|
92
|
+
### 1) Write styles in `*.styles.*` files
|
|
144
93
|
|
|
145
|
-
```
|
|
146
|
-
|
|
94
|
+
```ts
|
|
95
|
+
// src/components/button.styles.ts
|
|
96
|
+
import { makeCssFuncs } from 'simplestyle-js/ssr';
|
|
147
97
|
|
|
98
|
+
const { createStyles } = makeCssFuncs();
|
|
148
99
|
|
|
149
|
-
export const {
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
});
|
|
157
|
-
|
|
158
|
-
const { classes } = createStyles('my-component', vars => ({
|
|
159
|
-
card: {
|
|
160
|
-
// use all of your variables here.
|
|
161
|
-
// your IDE should provide code assistance to you
|
|
162
|
-
// to inform you of what variables are available
|
|
163
|
-
backgroundColor: vars.background.secondary,
|
|
100
|
+
export const { classes } = createStyles('Button', () => ({
|
|
101
|
+
root: {
|
|
102
|
+
backgroundColor: '#1f4aa8',
|
|
103
|
+
borderRadius: 6,
|
|
104
|
+
color: '#fff',
|
|
105
|
+
padding: '12px 16px',
|
|
106
|
+
'&:hover': { backgroundColor: '#2d6cdf' },
|
|
164
107
|
},
|
|
165
108
|
}));
|
|
166
109
|
```
|
|
167
110
|
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
Create your style registry and scoped CSS functions, then use the official Next.js integration here and wrap the `SimpleStyleProvider` around your `layout` file.
|
|
111
|
+
```tsx
|
|
112
|
+
// src/components/Button.tsx
|
|
113
|
+
import { classes } from './button.styles';
|
|
173
114
|
|
|
174
|
-
|
|
175
|
-
|
|
115
|
+
export function Button() {
|
|
116
|
+
return <button className={classes.root}>Click me</button>;
|
|
117
|
+
}
|
|
118
|
+
```
|
|
176
119
|
|
|
177
|
-
|
|
178
|
-
import { SimpleStyleRegistry } from "simplestyle-js/simpleStyleRegistry";
|
|
120
|
+
### 2) Compile with `ssjs`
|
|
179
121
|
|
|
180
|
-
|
|
122
|
+
Define **accurate `--entrypoints`** CLI flags for your code so the compiler can follow your import graph and discover all of your styles.
|
|
123
|
+
The resulting CSS is written in the same order as your source code / import tree.
|
|
181
124
|
|
|
182
|
-
|
|
125
|
+
```bash
|
|
126
|
+
bun ssjs --entrypoints src/app.tsx src/pages/**/*.tsx --outfile public/my-styles.css
|
|
127
|
+
```
|
|
128
|
+
```bash
|
|
129
|
+
npx ssjs --entrypoints src/app.tsx src/pages/**/*.tsx --outfile public/my-styles.css
|
|
130
|
+
```
|
|
131
|
+
```bash
|
|
132
|
+
pnpm ssjs --entrypoints src/app.tsx src/pages/**/*.tsx --outfile public/my-styles.css
|
|
133
|
+
```
|
|
134
|
+
```bash
|
|
135
|
+
yarn ssjs --entrypoints src/app.tsx src/pages/**/*.tsx --outfile public/my-styles.css
|
|
183
136
|
```
|
|
184
137
|
|
|
185
|
-
|
|
186
|
-
// src/app/layout.tsx
|
|
187
|
-
import type { Metadata } from "next";
|
|
188
|
-
import { SimpleStyleProvider } from "simplestyle-js/next";
|
|
189
|
-
import { createStyles, StyleRegistry } from "./styleRegistry";
|
|
190
|
-
|
|
191
|
-
// start writing CSS!
|
|
192
|
-
const { classes } = createStyles('RootLayoutStyles', () => ({
|
|
193
|
-
rootLayout: {
|
|
194
|
-
backgroundColor: 'pink',
|
|
195
|
-
padding: '1rem',
|
|
196
|
-
},
|
|
197
|
-
}));
|
|
138
|
+
Optional watch mode:
|
|
198
139
|
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
}: Readonly<{
|
|
202
|
-
children: React.ReactNode;
|
|
203
|
-
}>) {
|
|
204
|
-
return (
|
|
205
|
-
<html lang="en">
|
|
206
|
-
<body className={classes.rootLayout}>
|
|
207
|
-
<SimpleStyleProvider registry={StyleRegistry}>
|
|
208
|
-
{children}
|
|
209
|
-
</SimpleStyleProvider>
|
|
210
|
-
</body>
|
|
211
|
-
</html>
|
|
212
|
-
);
|
|
213
|
-
}
|
|
140
|
+
```bash
|
|
141
|
+
npx ssjs --entrypoints src/app.tsx src/pages/**/*.tsx --outfile public/my-styles.css --watch
|
|
214
142
|
```
|
|
215
143
|
|
|
216
|
-
|
|
144
|
+
### 3) Include the generated CSS
|
|
217
145
|
|
|
146
|
+
The output file is plain CSS. Include it the same way you would any stylesheet:
|
|
218
147
|
|
|
219
|
-
|
|
148
|
+
```html
|
|
149
|
+
<!-- SSR/SSG: add to your HTML head -->
|
|
150
|
+
<link rel="stylesheet" href="/my-styles.css" />
|
|
151
|
+
```
|
|
220
152
|
|
|
221
|
-
|
|
222
|
-
|
|
153
|
+
Or import it via your framework’s entry:
|
|
154
|
+
|
|
155
|
+
```ts
|
|
156
|
+
// e.g. Next.js or Astro entry files, or any UI application that uses a bundler
|
|
157
|
+
import '../public/my-styles.css';
|
|
158
|
+
```
|
|
223
159
|
|
|
224
|
-
|
|
225
|
-
// src/styleRegistry.ts
|
|
160
|
+
## API Reference
|
|
226
161
|
|
|
227
|
-
|
|
162
|
+
**Important:** always call `makeCssFuncs()` from the correct entrypoint:
|
|
228
163
|
|
|
229
|
-
|
|
230
|
-
|
|
164
|
+
- Browser runtime: `import { makeCssFuncs } from 'simplestyle-js/browser'`
|
|
165
|
+
- SSR: `import { makeCssFuncs } from 'simplestyle-js/ssr'`
|
|
231
166
|
|
|
232
|
-
|
|
233
|
-
---
|
|
234
|
-
import { SimpleStyleRegistry } from "simplestyle-js";
|
|
235
|
-
import SimpleStyleProvider from "simplestyle-js/astro/SimpleStyleProvider";
|
|
167
|
+
### `makeCssFuncs({ variables? }?)`
|
|
236
168
|
|
|
237
|
-
|
|
238
|
-
createStyles,
|
|
239
|
-
keyframes,
|
|
240
|
-
rawStyles,
|
|
241
|
-
} from "../styleRegistry";
|
|
169
|
+
Creates the CSS helpers and optionally binds your design tokens for typed access.
|
|
242
170
|
|
|
243
|
-
|
|
171
|
+
```ts
|
|
172
|
+
import { makeCssFuncs } from 'simplestyle-js/browser'; // or import the SSR variant from 'simplestyle-js/ssr';
|
|
244
173
|
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
},
|
|
249
|
-
"body, html": {
|
|
250
|
-
fontFamily: "sans-serif",
|
|
251
|
-
fontSize: "16px",
|
|
252
|
-
padding: 0,
|
|
174
|
+
const { createStyles } = makeCssFuncs({
|
|
175
|
+
variables: {
|
|
176
|
+
colors: { brand: '#1f4aa8' },
|
|
253
177
|
},
|
|
254
|
-
}), {
|
|
255
|
-
// provide the registry used for this `.astro` file here
|
|
256
|
-
registry,
|
|
257
178
|
});
|
|
258
179
|
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
},
|
|
264
|
-
"33%": {
|
|
265
|
-
backgroundColor: "#22cc22cc",
|
|
266
|
-
},
|
|
267
|
-
"66%": {
|
|
268
|
-
backgroundColor: "#2222cccc",
|
|
269
|
-
},
|
|
270
|
-
"100%": {
|
|
271
|
-
backgroundColor: "#cc2222cc",
|
|
272
|
-
},
|
|
273
|
-
}), {
|
|
274
|
-
// provide the registry used for this `.astro` file here
|
|
275
|
-
registry,
|
|
276
|
-
});
|
|
180
|
+
const { classes } = createStyles('Card', (vars) => ({
|
|
181
|
+
root: { backgroundColor: vars.colors.brand },
|
|
182
|
+
}));
|
|
183
|
+
```
|
|
277
184
|
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
},
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
185
|
+
### `createStyles(ruleId, rulesFn, options?)`
|
|
186
|
+
|
|
187
|
+
Builds class names + CSS.
|
|
188
|
+
Returns `{ classes, stylesheet, updateSheet }`.
|
|
189
|
+
Use the returned `classes` directly in your component code / HTML.
|
|
190
|
+
|
|
191
|
+
```ts
|
|
192
|
+
const { classes } = createStyles('Nav', () => ({
|
|
193
|
+
wrapper: { display: 'flex', gap: 12 },
|
|
194
|
+
link: { '&:hover': { textDecoration: 'underline' } },
|
|
195
|
+
'@media (max-width: 600px)': { wrapper: { flexDirection: 'column' } },
|
|
196
|
+
}));
|
|
197
|
+
```
|
|
198
|
+
|
|
199
|
+
Back‑reference example (use `$otherRule` to reference another generated class):
|
|
200
|
+
|
|
201
|
+
```ts
|
|
202
|
+
const { classes } = createStyles('Card', () => ({
|
|
203
|
+
title: { fontWeight: 700, marginBottom: 8 },
|
|
204
|
+
root: {
|
|
205
|
+
padding: 16,
|
|
206
|
+
borderRadius: 8,
|
|
207
|
+
backgroundColor: '#f6f7fb',
|
|
208
|
+
'& $title': { color: '#1f4aa8' },
|
|
294
209
|
},
|
|
295
|
-
})
|
|
296
|
-
// provide the registry used for this `.astro` file here
|
|
297
|
-
registry,
|
|
298
|
-
});
|
|
299
|
-
---
|
|
300
|
-
<!-- wrap your astro content here with the provider and give it the registry you created in your front matter -->
|
|
301
|
-
<SimpleStyleProvider registry={registry}>
|
|
302
|
-
<div class={classes.background}>
|
|
303
|
-
<div class={classes.content}>
|
|
304
|
-
<h1>Astro</h1>
|
|
305
|
-
<p>
|
|
306
|
-
Checkout the CSS class and animation applied to the body, which is
|
|
307
|
-
making my colors changes.
|
|
308
|
-
</p>
|
|
309
|
-
<p>Feel free to edit me and I'll hot reload!</p>
|
|
310
|
-
</div>
|
|
311
|
-
</div>
|
|
312
|
-
</SimpleStyleProvider>
|
|
210
|
+
}));
|
|
313
211
|
```
|
|
314
212
|
|
|
315
|
-
|
|
213
|
+
### `createKeyframes(ruleId, framesFn, options?)`
|
|
316
214
|
|
|
317
|
-
|
|
215
|
+
Generates a unique animation name and `@keyframes` CSS. Returns `{ keyframe, stylesheet }`.
|
|
318
216
|
|
|
319
|
-
|
|
320
|
-
|
|
217
|
+
```ts
|
|
218
|
+
const { keyframe } = createKeyframes('FadeIn', () => ({
|
|
219
|
+
'0%': { opacity: 0, transform: 'translateY(6px)' },
|
|
220
|
+
'100%': { opacity: 1, transform: 'translateY(0px)' },
|
|
221
|
+
}));
|
|
321
222
|
|
|
322
|
-
|
|
323
|
-
|
|
223
|
+
const { classes } = createStyles('Notice', () => ({
|
|
224
|
+
root: {
|
|
225
|
+
animation: `${keyframe} 320ms ease-out`,
|
|
226
|
+
},
|
|
227
|
+
}));
|
|
228
|
+
```
|
|
324
229
|
|
|
325
|
-
###
|
|
230
|
+
### `createRawStyles(ruleId, rulesFn, options?)`
|
|
326
231
|
|
|
327
|
-
|
|
232
|
+
Writes rules without generating class names. Keys must be selectors (`html`, `body *`, `.app`).
|
|
328
233
|
|
|
329
|
-
|
|
330
|
-
|
|
234
|
+
```ts
|
|
235
|
+
createRawStyles('GlobalReset', () => ({
|
|
236
|
+
'*, *::before, *::after': { boxSizing: 'border-box' },
|
|
237
|
+
'html, body': {
|
|
238
|
+
margin: 0,
|
|
239
|
+
padding: 0,
|
|
240
|
+
fontFamily: 'system-ui, sans-serif',
|
|
241
|
+
lineHeight: 1.4,
|
|
242
|
+
backgroundColor: '#fff',
|
|
243
|
+
color: '#111',
|
|
244
|
+
},
|
|
245
|
+
img: { maxWidth: '100%', display: 'block' },
|
|
246
|
+
button: { font: 'inherit' },
|
|
247
|
+
}));
|
|
248
|
+
```
|
|
331
249
|
|
|
332
|
-
|
|
333
|
-
import { makeCssFuncs, SimpleStyleRegistry } from "simplestyle-js";
|
|
250
|
+
### `createImports(ruleId, rulesFn, options?)`
|
|
334
251
|
|
|
335
|
-
|
|
336
|
-
export const StyleRegistry = new SimpleStyleRegistry();
|
|
252
|
+
Creates `@import` rules. The array items must already be valid `@import` strings.
|
|
337
253
|
|
|
338
|
-
|
|
339
|
-
|
|
254
|
+
```ts
|
|
255
|
+
createImports('Imports', () => [
|
|
256
|
+
'@import "https://unpkg.com/normalize.css/normalize.css"',
|
|
257
|
+
]);
|
|
340
258
|
```
|
|
341
259
|
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
**Note**: If you use Next.js, you would do this in your `layout.jsx` or `layout.tsx` file.
|
|
345
|
-
Additionally, if you're not using React or any other JSX-inspired framework, you can use
|
|
346
|
-
`StyleRegistry.getHTML()` to return an HTML string with all of the `<style />` tags you need,
|
|
347
|
-
or `StyleRegistry.getCSS()` to just return a single, concatenated CSS string.
|
|
348
|
-
|
|
349
|
-
```jsx
|
|
350
|
-
import { StyleRegistry } from '../styleLib.js';
|
|
351
|
-
|
|
352
|
-
export default function Layout({ children }) {
|
|
353
|
-
return (
|
|
354
|
-
<body lang="en">
|
|
355
|
-
{/* render your <style /> tags and set the IDs on them */}
|
|
356
|
-
{StyleRegistry.getRulesById().map(([id, css]) => (
|
|
357
|
-
<style id={id} key={id}>
|
|
358
|
-
{css}
|
|
359
|
-
</style>
|
|
360
|
-
))}
|
|
361
|
-
{children}
|
|
362
|
-
</body>
|
|
363
|
-
);
|
|
364
|
-
}
|
|
365
|
-
```
|
|
260
|
+
### `registerPosthook(fn: (sheet: string) => string)`
|
|
366
261
|
|
|
367
|
-
|
|
262
|
+
Registers a transform that runs after CSS strings are generated, but before they’re flushed or written.
|
|
368
263
|
|
|
369
|
-
|
|
370
|
-
import { createStyles } from '../styleLib.js';
|
|
264
|
+
### Types
|
|
371
265
|
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
backgroundColor: 'purple',
|
|
376
|
-
fontSize: '2rem',
|
|
377
|
-
padding: '2rem',
|
|
266
|
+
- `SimpleStyleRules`: `{ [selectorOrKey: string]: Properties | SimpleStyleRules }`
|
|
267
|
+
- `CreateStylesOptions`: options for flushing/placement in the browser runtime.
|
|
268
|
+
- `PosthookPlugin`: signature for `registerPosthook`.
|
|
378
269
|
|
|
379
|
-
|
|
380
|
-
fontStyle: 'italic',
|
|
381
|
-
fontWeight: 'bold',
|
|
382
|
-
textDecoration: 'underline',
|
|
383
|
-
},
|
|
384
|
-
},
|
|
385
|
-
}));
|
|
270
|
+
## Patterns and Tips
|
|
386
271
|
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
This is super <span>cool.</span>
|
|
392
|
-
</div>
|
|
393
|
-
);
|
|
394
|
-
}
|
|
395
|
-
```
|
|
272
|
+
- **Nested selectors**: `&` is replaced with the parent selector. Comma‑separated selectors are supported (e.g., `'&:hover, &:focus'`).
|
|
273
|
+
- **Back‑references**: Use `$otherRule` to reference another generated class in the same `createStyles` call (the referenced rule should appear earlier in the object).
|
|
274
|
+
- **Media queries**: Top‑level `@media` keys contain further rule objects.
|
|
275
|
+
- **Updating styles**: `updateSheet` merges new rules and updates the existing `<style>` tag (browser) or returns an updated sheet string (SSR extraction).
|
|
396
276
|
|
|
397
277
|
## Creating a simplestyle-js plugin
|
|
398
278
|
|
|
@@ -403,12 +283,13 @@ Do this if you want to integrate with `postcss`, `autoprefixer`, or any other CS
|
|
|
403
283
|
```ts
|
|
404
284
|
import autoprefixer from 'autoprefixer';
|
|
405
285
|
import postcss from 'postcss';
|
|
406
|
-
import { registerPosthook } from 'simplestyle-js';
|
|
286
|
+
import { registerPosthook } from 'simplestyle-js/browser';
|
|
407
287
|
|
|
408
288
|
registerPosthook(css => postcss([autoprefixer]).process(css, { from: undefined }).css);
|
|
409
289
|
```
|
|
410
290
|
|
|
411
|
-
Any future `createStyles`, `
|
|
291
|
+
Any future `createStyles`, `createRawStyles`, or `createKeyframes` calls will run through the posthook chain.
|
|
292
|
+
You can use the same API from `simplestyle-js/ssr` if you want the transform applied to extracted CSS as well.
|
|
412
293
|
|
|
413
294
|
## License
|
|
414
295
|
[MIT](https://en.wikipedia.org/wiki/MIT_License)
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "simplestyle-js",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "6.0.0",
|
|
4
4
|
"description": "An incredibly straightforward and simple CSS-in-JS solution with zero runtime dependencies, and out-of-the-box TypeScript support",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"bin": {
|
|
@@ -28,11 +28,22 @@
|
|
|
28
28
|
"test:watch": "bun run --bun vitest"
|
|
29
29
|
},
|
|
30
30
|
"keywords": [
|
|
31
|
-
"
|
|
32
|
-
"
|
|
33
|
-
"
|
|
34
|
-
"
|
|
35
|
-
"
|
|
31
|
+
"css-in-js",
|
|
32
|
+
"css",
|
|
33
|
+
"styling",
|
|
34
|
+
"style",
|
|
35
|
+
"styled",
|
|
36
|
+
"typescript",
|
|
37
|
+
"zero-runtime",
|
|
38
|
+
"runtime-less",
|
|
39
|
+
"atomic-css",
|
|
40
|
+
"design-system",
|
|
41
|
+
"theme",
|
|
42
|
+
"react",
|
|
43
|
+
"ssr",
|
|
44
|
+
"nextjs",
|
|
45
|
+
"astro",
|
|
46
|
+
"compiler"
|
|
36
47
|
],
|
|
37
48
|
"author": "Benjamin Duran <stratodyne@gmail.com>",
|
|
38
49
|
"license": "MIT",
|