simplestyle-js 4.3.1 → 4.3.3-beta.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 CHANGED
@@ -12,10 +12,13 @@ A concise guide to the core `simplestyle-js` APIs, how they fit together, and ho
12
12
  ## Table of Contents
13
13
  - [Install](#install)
14
14
  - [Quick Start](#quick-start)
15
- - [API Reference](#api-reference-from-srcindexts)
15
+ - [API Reference](#api-reference)
16
16
  - [Patterns and Tips](#patterns-and-tips)
17
+ - [Creating reusable variables and / or design system-like tokens](#reusable-variables)
17
18
  - [SSR](#ssr)
18
- - [SSR steps for most SSR / SSG frameworks (including Next.js)](#ssr-steps-for-most-ssr--ssg-frameworks)
19
+ - [Next.js](#nextjs)
20
+ - [Astro](#astro)
21
+ - [SSR steps for most SSR / SSG frameworks](#ssr-steps-for-most-ssr--ssg-frameworks)
19
22
  - [1. Set your seed, create a SimpleStyleRegistry and your style functions](#1-set-your-seed-create-a-simplestyleregistry-and-your-style-functions)
20
23
  - [2. Render the generated styles in your HTML](#2-render-the-generated-styles-in-your-html)
21
24
  - [3. Create your styles and have fun!](#3-create-your-styles-and-have-fun)
@@ -49,10 +52,10 @@ yarn add simplestyle-js
49
52
 
50
53
  ## Quick Start
51
54
 
52
- ```tsx
55
+ ```jsx
53
56
  import createStyles from 'simplestyle-js';
54
57
 
55
- const { classes } = createStyles('Button', {
58
+ const { classes } = createStyles('Button', () => ({
56
59
  root: {
57
60
  '&:hover': { backgroundColor: '#2d6cdf' },
58
61
  '@media (max-width: 768px)': { padding: '10px 12px' },
@@ -61,7 +64,7 @@ const { classes } = createStyles('Button', {
61
64
  color: '#fff',
62
65
  padding: '12px 16px',
63
66
  },
64
- });
67
+ }));
65
68
 
66
69
  document.querySelector('button')?.classList.add(classes.root);
67
70
 
@@ -81,7 +84,7 @@ Rules support nested selectors via `&`, media queries, and `$className` back-ref
81
84
  - `updateSheet(updatedRules)` merges rules and updates the existing sheet (works when `flush` is `true` or a `registry` is provided). Returns `{ classes, stylesheet } | null`.
82
85
  - Example:
83
86
  ```ts
84
- const { classes, stylesheet } = createStyles('Nav', {
87
+ const { classes, stylesheet } = createStyles('Nav', () => ({
85
88
  wrapper: { display: 'flex', gap: 12 },
86
89
  link: {
87
90
  '&:hover': { textDecoration: 'underline' },
@@ -89,24 +92,29 @@ Rules support nested selectors via `&`, media queries, and `$className` back-ref
89
92
  '@media (max-width: 600px)': {
90
93
  wrapper: { flexDirection: 'column' },
91
94
  },
92
- }, { flush: false }); // do not write to the DOM automatically
95
+ }), { flush: false }); // do not write to the DOM automatically
93
96
  ```
94
97
 
95
- - `keyframes(ruleId, frames, options?)`
98
+ - `keyframes(ruleId, framesFnc, options?)`
96
99
  - Generates a unique animation name and accompanying `@keyframes` CSS.
97
100
  - Returns `{ keyframe: string; stylesheet: string; }`. Respects `flush` and `insertBefore/After` options.
98
101
 
99
- - `rawStyles(ruleId, rules, options?)`
102
+ - `rawStyles(ruleId, rulesFnc, options?)`
100
103
  - Writes rules without generating new class names. Keys must already be selectors (e.g., `html`, `body *`, `.app`).
101
104
  - Good for global resets or theme primitives. Respects `flush` and `registry`.
102
105
 
106
+ - `makeCssFuncs({ registry?, variables? })`
107
+ - Returns `createStyles`, `keyframes` and `rawStyles` functions for you to use that are bound to an optional `SimpleStyleRegistry` and an optional `variables` object.
108
+ - 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.
109
+ - Use this as a convenience if you don't want to manually wire up each CSS function (listed below) to your registry.
110
+
103
111
  - `makeCreateStyles(registry)`
104
112
  - 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).
105
113
 
106
- - `makeRawStyles(registry)` *(from `simplestyle-js/createStyles`)*
114
+ - `makeRawStyles(registry)`
107
115
  - 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).
108
116
 
109
- - `makeKeyframes(registry)` *(from `simplestyle-js/createStyles`)*
117
+ - `makeKeyframes(registry)`
110
118
  - 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).
111
119
 
112
120
  - `setSeed(seed: number | null)`
@@ -129,18 +137,190 @@ Rules support nested selectors via `&`, media queries, and `$className` back-ref
129
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
130
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.
131
139
 
140
+ ## Reusable Variables
141
+
142
+ SimpleStyle provides an easy way to "bind" all of the CSS functions it exports to an object that contains your tokens, variables, etc.
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:
144
+
145
+ ```typescript
146
+ import { makeCssFuncs } from "simplestyle-js";
147
+ import { SimpleStyleRegistry } from "simplestyle-js/simpleStyleRegistry";
148
+
149
+
150
+ export const { createStyles, keyframes } = makeCssFuncs({
151
+ variables: {
152
+ background: {
153
+ primary: '#f1f1f3',
154
+ secondary: '#555',
155
+ },
156
+ },
157
+ });
158
+
159
+ const { classes } = createStyles('my-component', vars => ({
160
+ card: {
161
+ // use all of your variables here.
162
+ // your IDE should provide code assistance to you
163
+ // to inform you of what variables are available
164
+ backgroundColor: vars.background.secondary,
165
+ },
166
+ }));
167
+ ```
168
+
169
+
132
170
  ## SSR
133
171
 
134
172
  ### Next.js
135
173
 
136
- Use the official Next.js integration here and wrap the `SimpleStyleProvider` around your `layout` file.
137
- Check out this [Code Sanbox w/Next.js integration to see how it works](https://codesandbox.io/p/devbox/t3smf4).
174
+ Create your style registry and scoped CSS functions, then use the official Next.js integration here and wrap the `SimpleStyleProvider` around your `layout` file.
175
+
176
+ ```typescript
177
+ // src/styleRegistry.ts
178
+
179
+ import { makeCssFuncs, setSeed } from "simplestyle-js";
180
+ import { SimpleStyleRegistry } from "simplestyle-js/simpleStyleRegistry";
181
+
182
+ // ensures deterministic creation of CSS classnames
183
+ setSeed(11223344);
184
+
185
+ export const StyleRegistry = new SimpleStyleRegistry();
186
+
187
+ export const { createStyles, keyframes } = makeCssFuncs({ registry: StyleRegistry });
188
+ ```
189
+
190
+ ```tsx
191
+ // src/app/layout.tsx
192
+ import type { Metadata } from "next";
193
+ import { SimpleStyleProvider } from "simplestyle-js/next";
194
+ import { createStyles, StyleRegistry } from "./styleRegistry";
195
+
196
+ // start writing CSS!
197
+ const { classes } = createStyles('RootLayoutStyles', () => ({
198
+ rootLayout: {
199
+ backgroundColor: 'pink',
200
+ padding: '1rem',
201
+ },
202
+ }));
203
+
204
+ export default function RootLayout({
205
+ children,
206
+ }: Readonly<{
207
+ children: React.ReactNode;
208
+ }>) {
209
+ return (
210
+ <html lang="en">
211
+ <body className={classes.rootLayout}>
212
+ <SimpleStyleProvider registry={StyleRegistry}>
213
+ {children}
214
+ </SimpleStyleProvider>
215
+ </body>
216
+ </html>
217
+ );
218
+ }
219
+ ```
220
+
221
+ Check out this [Code Sandbox w/Next.js integration to see how it works](https://codesandbox.io/p/devbox/t3smf4).
138
222
 
139
223
 
140
224
  ### Astro
141
225
 
142
- Use the official Astro integration here and wrap your page's `<head />` with the `SimpleStyleProvider`.
143
- Check out this [Code Sanbox w/Astro integration to see how it works](https://codesandbox.io/p/devbox/mq9twt).
226
+ Create your style registry and scoped CSS functions, then use the official Astro integration here and wrap the `SimpleStyleProvider` around your page's `<head />` contents.
227
+
228
+ ```typescript
229
+ // src/styleRegistry.ts
230
+
231
+ import { makeCssFuncs, setSeed } from "simplestyle-js";
232
+ import { SimpleStyleRegistry } from "simplestyle-js/simpleStyleRegistry";
233
+
234
+ // ensures deterministic creation of CSS classnames
235
+ setSeed(11223344);
236
+
237
+ export const StyleRegistry = new SimpleStyleRegistry();
238
+
239
+ export { createStyles, keyframes } = makeCssFuncs({ registry: StyleRegistry });
240
+ ```
241
+
242
+ ```astro
243
+ ---
244
+ import SimpleStyleProvider from "simplestyle-js/astro/SimpleStyleProvider";
245
+
246
+ import {
247
+ StyleRegistry,
248
+ createStyles,
249
+ keyframes,
250
+ rawStyles,
251
+ } from "../styleRegistry";
252
+
253
+ rawStyles("basic-css-reset", () => ({
254
+ "*": {
255
+ boxSizing: "border-box",
256
+ },
257
+ "body, html": {
258
+ fontFamily: "sans-serif",
259
+ fontSize: "16px",
260
+ padding: 0,
261
+ },
262
+ }));
263
+
264
+ // make changes to me and I will hot reload!
265
+ const { keyframe } = keyframes("HomePage", () => ({
266
+ "0%": {
267
+ backgroundColor: "#cc2222cc",
268
+ },
269
+ "33%": {
270
+ backgroundColor: "#22cc22cc",
271
+ },
272
+ "66%": {
273
+ backgroundColor: "#2222cccc",
274
+ },
275
+ "100%": {
276
+ backgroundColor: "#cc2222cc",
277
+ },
278
+ }));
279
+
280
+ const { classes } = createStyles("HomePage", () => ({
281
+ background: {
282
+ alignItems: "center",
283
+ animation: `${keyframe} 5s linear infinite`,
284
+ display: "flex",
285
+ flexFlow: "column",
286
+ height: "90vh",
287
+ justifyContent: "center",
288
+ margin: "0 auto",
289
+ width: "90vw",
290
+ },
291
+ content: {
292
+ backgroundColor: "#00000033",
293
+ borderRadius: "4px",
294
+ color: "white",
295
+ padding: "1rem",
296
+ },
297
+ }));
298
+ ---
299
+
300
+ <html lang="en">
301
+ <head>
302
+ <SimpleStyleProvider registry={StyleRegistry}>
303
+ <meta charset="utf-8" />
304
+ <link rel="icon" type="image/svg+xml" href="/favicon.svg" />
305
+ <meta name="viewport" content="width=device-width" />
306
+ <meta name="generator" content={Astro.generator} />
307
+ <title>Astro</title>
308
+ </SimpleStyleProvider>
309
+ </head>
310
+ <body class={classes.background}>
311
+ <div class={classes.content}>
312
+ <h1>Astro</h1>
313
+ <p>
314
+ Checkout the CSS class and animation applied to the body, which is
315
+ making my colors changes.
316
+ </p>
317
+ <p>Feel free to edit me and I'll hot reload!</p>
318
+ </div>
319
+ </body>
320
+ </html>
321
+ ```
322
+
323
+ Check out this [Code Sandbox w/Astro integration to see how it works](https://codesandbox.io/p/devbox/mq9twt).
144
324
 
145
325
  **General SSR Concepts**
146
326
 
@@ -149,8 +329,7 @@ The core APIs needed to make this work are:
149
329
 
150
330
  - `new SimpleStyleRegistry()` - creates a new StyleSheet registry where all of your styles will be accumulated
151
331
  - `setSeed(number)` - ensures that classNames are deterministically computed and will be the same on the server and when they're rehydrated on the client
152
- - `makeCreateStyle(registry)` - returns a `createStyles()` function that is locked to your StyleSheet registry
153
- - `makeKeyframes(registry)` - returns a `keyframes()` function that is locaked to your StyleSheet registry
332
+ - `makeCssFuncs({ registry })` - returns `createStyles()`, `keyframes()` and `rawStyles()` functions that are locked to your StyleSheet registry
154
333
 
155
334
  ### SSR steps for most SSR / SSG frameworks
156
335
 
@@ -160,7 +339,7 @@ The core APIs needed to make this work are:
160
339
  For demonstration purposes, we'll locate this at our `src/` root, and name it `styleLib.js`
161
340
 
162
341
  ```javascript
163
- import { makeCreateStyles, makeKeyframes, makeRawStyles, setSeed } from "simplestyle-js";
342
+ import { makeCssFuncs, setSeed } from "simplestyle-js";
164
343
  import { SimpleStyleRegistry } from "simplestyle-js/simpleStyleRegistry";
165
344
 
166
345
  // set the className generation seed to ensure classNames are computed consistently
@@ -173,9 +352,7 @@ setSeed(1);
173
352
  export const StyleRegistry = new SimpleStyleRegistry();
174
353
 
175
354
  // export the style functions that will be locked to your registry
176
- export const createStyles = makeCreateStyles(StyleRegistry);
177
- export const keyframes = makeKeyframes(StyleRegistry);
178
- export const rawStyles = makeRawStyles(StyleRegistry);
355
+ export const { createStyles, keyframes, rawStyles } = makeCssFuncs({ registry: })
179
356
  ```
180
357
 
181
358
  #### 2. Render the generated styles in your HTML
@@ -209,7 +386,7 @@ export default function Layout({ children }) {
209
386
  import { createStyles } from '../styleLib.js';
210
387
 
211
388
  // create your styles
212
- const { classes } = createStyles({
389
+ const { classes } = createStyles('my-component', () => ({
213
390
  awesome: {
214
391
  backgroundColor: 'purple',
215
392
  fontSize: '2rem',
@@ -221,7 +398,7 @@ const { classes } = createStyles({
221
398
  textDecoration: 'underline',
222
399
  },
223
400
  },
224
- });
401
+ }));
225
402
 
226
403
  export function MyCoolComponent() {
227
404
  // use your styles here!
@@ -9,21 +9,15 @@ function _export(target, all) {
9
9
  });
10
10
  }
11
11
  _export(exports, {
12
- get default () {
12
+ get createStyles () {
13
13
  return createStyles;
14
14
  },
15
+ get default () {
16
+ return _default;
17
+ },
15
18
  get keyframes () {
16
19
  return keyframes;
17
20
  },
18
- get makeCreateStyles () {
19
- return makeCreateStyles;
20
- },
21
- get makeKeyframes () {
22
- return makeKeyframes;
23
- },
24
- get makeRawStyles () {
25
- return makeRawStyles;
26
- },
27
21
  get rawStyles () {
28
22
  return rawStyles;
29
23
  }
@@ -160,9 +154,10 @@ function coerceCreateStylesOptions(options) {
160
154
  flush: options && typeof options.flush === 'boolean' ? options.flush : true
161
155
  };
162
156
  }
163
- function rawStyles(ruleId, rules, options) {
157
+ function rawStyles(ruleId, rulesFnc, options) {
164
158
  const rawStylesId = `${ruleId}_raw`;
165
159
  const coerced = coerceCreateStylesOptions(options);
160
+ const rules = rulesFnc();
166
161
  const { sheetBuffer: sheetContents, mediaQueriesBuffer: mediaQueriesContents } = execCreateStyles(rawStylesId, rules, coerced, null, true);
167
162
  const mergedContents = `${sheetContents}${mediaQueriesContents}`;
168
163
  if (options?.registry) {
@@ -172,16 +167,10 @@ function rawStyles(ruleId, rules, options) {
172
167
  }
173
168
  return mergedContents;
174
169
  }
175
- function makeRawStyles(registry) {
176
- return function wrappedRawStyles(ruleId, rules) {
177
- return rawStyles(ruleId, rules, {
178
- registry
179
- });
180
- };
181
- }
182
- function keyframes(ruleId, frames, options) {
170
+ function keyframes(ruleId, framesFnc, options) {
183
171
  const coerced = coerceCreateStylesOptions(options);
184
172
  const keyframeId = (0, _generateClassName.generateClassName)(`${ruleId}_keyframes`);
173
+ const frames = framesFnc();
185
174
  const { sheetBuffer: keyframesContents } = execCreateStyles(keyframeId, frames, coerced, null, true);
186
175
  const stylesheet = `@keyframes ${keyframeId}{${keyframesContents}}`;
187
176
  if (options?.registry) {
@@ -194,30 +183,17 @@ function keyframes(ruleId, frames, options) {
194
183
  stylesheet
195
184
  };
196
185
  }
197
- function makeKeyframes(registry) {
198
- return function wrappedCreateKeyframes(ruleId, rules) {
199
- return keyframes(ruleId, rules, {
200
- registry
201
- });
202
- };
203
- }
204
- function makeCreateStyles(registry) {
205
- return function wrappedCreateStyles(ruleId, rules) {
206
- return createStyles(ruleId, rules, {
207
- registry
208
- });
209
- };
210
- }
211
- function createStyles(ruleId, rules, options) {
186
+ function createStyles(ruleId, rulesFnc, options) {
187
+ const rules = rulesFnc();
212
188
  const coerced = coerceCreateStylesOptions(options);
213
189
  const { classes: out, sheetBuffer: sheetContents, mediaQueriesBuffer: mediaQueriesContents } = execCreateStyles(ruleId, rules, coerced, null);
214
190
  const mergedContents = `${sheetContents}${mediaQueriesContents}`;
215
191
  const replacedSheetContents = replaceBackReferences(out, mergedContents);
216
192
  let sheet = null;
217
- const updateSheet = (updatedRules)=>{
193
+ const updateSheet = (updatedRulesFnc)=>{
218
194
  if (options?.flush || options?.registry || !options?.flush) {
219
195
  // We prefer the first set, and then we shallow merge
220
- const { classes: updatedOut, sheetBuffer: updatedSheetContents, mediaQueriesBuffer: updatedMediaQueriesContents } = execCreateStyles(ruleId, (0, _deepmerge.default)(rules, updatedRules), {
196
+ const { classes: updatedOut, sheetBuffer: updatedSheetContents, mediaQueriesBuffer: updatedMediaQueriesContents } = execCreateStyles(ruleId, (0, _deepmerge.default)(rules, updatedRulesFnc()), {
221
197
  flush: false
222
198
  }, null);
223
199
  const updatedMergedContents = `${updatedSheetContents}${updatedMediaQueriesContents}`;
@@ -245,3 +221,4 @@ function createStyles(ruleId, rules, options) {
245
221
  updateSheet
246
222
  };
247
223
  }
224
+ const _default = createStyles;
@@ -1,6 +1,6 @@
1
- import { Properties } from 'csstype';
2
- import { SimpleStyleRegistry } from './simpleStyleRegistry.js';
3
- import { SimpleStyleRules } from './types.js';
1
+ import type { Properties } from 'csstype';
2
+ import type { SimpleStyleRegistry } from './simpleStyleRegistry.js';
3
+ import type { SimpleStyleRules } from './types.js';
4
4
  export type CreateStylesOptions = Partial<{
5
5
  /**
6
6
  * If true, automatically renders generated styles
@@ -26,30 +26,18 @@ export type CreateStylesOptions = Partial<{
26
26
  */
27
27
  registry?: SimpleStyleRegistry;
28
28
  }>;
29
- export declare function rawStyles<T extends SimpleStyleRules>(ruleId: string, rules: T, options?: Partial<CreateStylesOptions>): string;
30
- export declare function makeRawStyles(registry: SimpleStyleRegistry): <T extends SimpleStyleRules>(ruleId: string, rules: T) => string;
31
- export declare function keyframes<T extends Record<string, Properties>>(ruleId: string, frames: T, options?: CreateStylesOptions): {
29
+ export declare function rawStyles<T extends SimpleStyleRules>(ruleId: string, rulesFnc: () => T, options?: Partial<CreateStylesOptions>): string;
30
+ export declare function keyframes<T extends Record<string, Properties>>(ruleId: string, framesFnc: () => T, options?: CreateStylesOptions): {
32
31
  keyframe: string;
33
32
  stylesheet: string;
34
33
  };
35
- export declare function makeKeyframes(registry: SimpleStyleRegistry): <T extends Record<string, Properties>>(ruleId: string, rules: T) => {
36
- keyframe: string;
37
- stylesheet: string;
38
- };
39
- export declare function makeCreateStyles(registry: SimpleStyleRegistry): <T extends SimpleStyleRules, K extends keyof T, O extends Record<K, string>>(ruleId: string, rules: T) => {
40
- classes: O;
41
- stylesheet: string;
42
- updateSheet: <T2 extends SimpleStyleRules>(updatedRules: Partial<T2>) => {
43
- classes: Record<string | number | keyof T2, string>;
44
- stylesheet: string;
45
- } | null;
46
- };
47
- export default function createStyles<T extends SimpleStyleRules, K extends keyof T, O extends Record<K, string>>(ruleId: string, rules: T, options?: Partial<CreateStylesOptions>): {
34
+ export declare function createStyles<T extends SimpleStyleRules, K extends keyof T, O extends Record<K, string>>(ruleId: string, rulesFnc: () => T, options?: Partial<CreateStylesOptions>): {
48
35
  classes: O;
49
36
  stylesheet: string;
50
- updateSheet: <T2 extends SimpleStyleRules>(updatedRules: Partial<T2>) => {
37
+ updateSheet: <T2 extends SimpleStyleRules>(updatedRulesFnc: () => Partial<T2>) => {
51
38
  classes: Record<string | number | keyof T2, string>;
52
39
  stylesheet: string;
53
40
  } | null;
54
41
  };
55
42
  export type CreateStylesArgs = Parameters<typeof createStyles>;
43
+ export default createStyles;
@@ -15,15 +15,6 @@ _export(exports, {
15
15
  get keyframes () {
16
16
  return _createStyles.keyframes;
17
17
  },
18
- get makeCreateStyles () {
19
- return _createStyles.makeCreateStyles;
20
- },
21
- get makeKeyframes () {
22
- return _createStyles.makeKeyframes;
23
- },
24
- get makeRawStyles () {
25
- return _createStyles.makeRawStyles;
26
- },
27
18
  get rawStyles () {
28
19
  return _createStyles.rawStyles;
29
20
  },
@@ -36,7 +27,21 @@ _export(exports, {
36
27
  });
37
28
  const _createStyles = /*#__PURE__*/ _interop_require_wildcard(require("./createStyles.cjs"));
38
29
  const _generateClassName = require("./generateClassName.cjs");
30
+ _export_star(require("./makeStyles.cjs"), exports);
39
31
  const _plugins = require("./plugins.cjs");
32
+ function _export_star(from, to) {
33
+ Object.keys(from).forEach(function(k) {
34
+ if (k !== "default" && !Object.prototype.hasOwnProperty.call(to, k)) {
35
+ Object.defineProperty(to, k, {
36
+ enumerable: true,
37
+ get: function() {
38
+ return from[k];
39
+ }
40
+ });
41
+ }
42
+ });
43
+ return from;
44
+ }
40
45
  function _getRequireWildcardCache(nodeInterop) {
41
46
  if (typeof WeakMap !== "function") return null;
42
47
  var cacheBabelInterop = new WeakMap();
@@ -1,6 +1,7 @@
1
1
  export type { CreateStylesArgs, CreateStylesOptions } from './createStyles.js';
2
- export { default as createStyles, keyframes, makeCreateStyles, makeKeyframes, makeRawStyles, rawStyles, } from './createStyles.js';
2
+ export { default as createStyles, keyframes, rawStyles, } from './createStyles.js';
3
3
  export { setSeed } from './generateClassName.js';
4
+ export * from './makeStyles.js';
4
5
  export type { PosthookPlugin } from './plugins.js';
5
6
  export { registerPosthook } from './plugins.js';
6
7
  export type { SimpleStyleRules } from './types.js';
@@ -0,0 +1,33 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ Object.defineProperty(exports, "makeCssFuncs", {
6
+ enumerable: true,
7
+ get: function() {
8
+ return makeCssFuncs;
9
+ }
10
+ });
11
+ const _createStyles = require("./createStyles.cjs");
12
+ function makeCssFuncs(opts) {
13
+ function wrappedCreateStyles(ruleId, rulesFnc) {
14
+ return (0, _createStyles.createStyles)(ruleId, ()=>rulesFnc('variables' in opts ? opts.variables : undefined), {
15
+ registry: 'registry' in opts ? opts.registry : undefined
16
+ });
17
+ }
18
+ function wrappedCreateKeyframes(ruleId, rulesFnc) {
19
+ return (0, _createStyles.keyframes)(ruleId, ()=>rulesFnc('variables' in opts ? opts.variables : undefined), {
20
+ registry: 'registry' in opts ? opts.registry : undefined
21
+ });
22
+ }
23
+ function wrappedRawStyles(ruleId, rulesFnc) {
24
+ return (0, _createStyles.rawStyles)(ruleId, ()=>rulesFnc('variables' in opts ? opts.variables : undefined), {
25
+ registry: 'registry' in opts ? opts.registry : undefined
26
+ });
27
+ }
28
+ return {
29
+ createStyles: wrappedCreateStyles,
30
+ keyframes: wrappedCreateKeyframes,
31
+ rawStyles: wrappedRawStyles
32
+ };
33
+ }
@@ -0,0 +1,34 @@
1
+ import type { Properties } from 'csstype';
2
+ import type { SimpleStyleRegistry } from './simpleStyleRegistry.js';
3
+ import type { SimpleStyleRules, SimpleStyleVariables } from './types.js';
4
+ type MakeCssFuncsOpts<T extends SimpleStyleVariables> = {} | {
5
+ registry: SimpleStyleRegistry;
6
+ } | {
7
+ variables: T;
8
+ } | {
9
+ registry: SimpleStyleRegistry;
10
+ variables: T;
11
+ };
12
+ /**
13
+ * Creates all of your CSS functions, createStyles, keframes and rawStyles,
14
+ * and scopes them all to your registry and variables definitions (both are optional).
15
+ * The variants of these functions differ slightly, in that
16
+ * they accept a function as the 2nd parameter, instead of the usual object.
17
+ * The function will be provided with your variables
18
+ */
19
+ export declare function makeCssFuncs<V extends SimpleStyleVariables>(opts: MakeCssFuncsOpts<V>): {
20
+ createStyles: <T extends SimpleStyleRules, K extends keyof T, O extends Record<K, string>>(ruleId: string, rulesFnc: (vars?: V) => T) => {
21
+ classes: O;
22
+ stylesheet: string;
23
+ updateSheet: <T2 extends SimpleStyleRules>(updatedRulesFnc: () => Partial<T2>) => {
24
+ classes: Record<string | number | keyof T2, string>;
25
+ stylesheet: string;
26
+ } | null;
27
+ };
28
+ keyframes: <T extends Record<string, Properties>>(ruleId: string, rulesFnc: (vars?: V) => T) => {
29
+ keyframe: string;
30
+ stylesheet: string;
31
+ };
32
+ rawStyles: <T extends SimpleStyleRules>(ruleId: string, rulesFnc: (vars?: V) => T) => string;
33
+ };
34
+ export {};
@@ -1,5 +1,14 @@
1
- import { Properties } from 'csstype';
1
+ import type { Properties } from 'csstype';
2
2
  export type SimpleStyleRules = {
3
3
  [key: string]: Properties | SimpleStyleRules;
4
4
  };
5
5
  export type RenderableSimpleStyleRules = SimpleStyleRules & Record<string, Properties[]>;
6
+ export type SimpleStyleVariables = {
7
+ [group: string]: {
8
+ [variable: string]: {
9
+ default: string | number;
10
+ [variants: string]: string | number;
11
+ };
12
+ };
13
+ };
14
+ export type HasProperty<T, K extends keyof T> = T extends Record<K, any> ? true : false;
@@ -1,6 +1,6 @@
1
- import { Properties } from 'csstype';
2
- import { SimpleStyleRegistry } from './simpleStyleRegistry.js';
3
- import { SimpleStyleRules } from './types.js';
1
+ import type { Properties } from 'csstype';
2
+ import type { SimpleStyleRegistry } from './simpleStyleRegistry.js';
3
+ import type { SimpleStyleRules } from './types.js';
4
4
  export type CreateStylesOptions = Partial<{
5
5
  /**
6
6
  * If true, automatically renders generated styles
@@ -26,30 +26,18 @@ export type CreateStylesOptions = Partial<{
26
26
  */
27
27
  registry?: SimpleStyleRegistry;
28
28
  }>;
29
- export declare function rawStyles<T extends SimpleStyleRules>(ruleId: string, rules: T, options?: Partial<CreateStylesOptions>): string;
30
- export declare function makeRawStyles(registry: SimpleStyleRegistry): <T extends SimpleStyleRules>(ruleId: string, rules: T) => string;
31
- export declare function keyframes<T extends Record<string, Properties>>(ruleId: string, frames: T, options?: CreateStylesOptions): {
29
+ export declare function rawStyles<T extends SimpleStyleRules>(ruleId: string, rulesFnc: () => T, options?: Partial<CreateStylesOptions>): string;
30
+ export declare function keyframes<T extends Record<string, Properties>>(ruleId: string, framesFnc: () => T, options?: CreateStylesOptions): {
32
31
  keyframe: string;
33
32
  stylesheet: string;
34
33
  };
35
- export declare function makeKeyframes(registry: SimpleStyleRegistry): <T extends Record<string, Properties>>(ruleId: string, rules: T) => {
36
- keyframe: string;
37
- stylesheet: string;
38
- };
39
- export declare function makeCreateStyles(registry: SimpleStyleRegistry): <T extends SimpleStyleRules, K extends keyof T, O extends Record<K, string>>(ruleId: string, rules: T) => {
40
- classes: O;
41
- stylesheet: string;
42
- updateSheet: <T2 extends SimpleStyleRules>(updatedRules: Partial<T2>) => {
43
- classes: Record<string | number | keyof T2, string>;
44
- stylesheet: string;
45
- } | null;
46
- };
47
- export default function createStyles<T extends SimpleStyleRules, K extends keyof T, O extends Record<K, string>>(ruleId: string, rules: T, options?: Partial<CreateStylesOptions>): {
34
+ export declare function createStyles<T extends SimpleStyleRules, K extends keyof T, O extends Record<K, string>>(ruleId: string, rulesFnc: () => T, options?: Partial<CreateStylesOptions>): {
48
35
  classes: O;
49
36
  stylesheet: string;
50
- updateSheet: <T2 extends SimpleStyleRules>(updatedRules: Partial<T2>) => {
37
+ updateSheet: <T2 extends SimpleStyleRules>(updatedRulesFnc: () => Partial<T2>) => {
51
38
  classes: Record<string | number | keyof T2, string>;
52
39
  stylesheet: string;
53
40
  } | null;
54
41
  };
55
42
  export type CreateStylesArgs = Parameters<typeof createStyles>;
43
+ export default createStyles;
@@ -125,9 +125,10 @@ function coerceCreateStylesOptions(options) {
125
125
  flush: options && typeof options.flush === 'boolean' ? options.flush : true
126
126
  };
127
127
  }
128
- export function rawStyles(ruleId, rules, options) {
128
+ export function rawStyles(ruleId, rulesFnc, options) {
129
129
  const rawStylesId = `${ruleId}_raw`;
130
130
  const coerced = coerceCreateStylesOptions(options);
131
+ const rules = rulesFnc();
131
132
  const { sheetBuffer: sheetContents, mediaQueriesBuffer: mediaQueriesContents } = execCreateStyles(rawStylesId, rules, coerced, null, true);
132
133
  const mergedContents = `${sheetContents}${mediaQueriesContents}`;
133
134
  if (options?.registry) {
@@ -137,16 +138,10 @@ export function rawStyles(ruleId, rules, options) {
137
138
  }
138
139
  return mergedContents;
139
140
  }
140
- export function makeRawStyles(registry) {
141
- return function wrappedRawStyles(ruleId, rules) {
142
- return rawStyles(ruleId, rules, {
143
- registry
144
- });
145
- };
146
- }
147
- export function keyframes(ruleId, frames, options) {
141
+ export function keyframes(ruleId, framesFnc, options) {
148
142
  const coerced = coerceCreateStylesOptions(options);
149
143
  const keyframeId = generateClassName(`${ruleId}_keyframes`);
144
+ const frames = framesFnc();
150
145
  const { sheetBuffer: keyframesContents } = execCreateStyles(keyframeId, frames, coerced, null, true);
151
146
  const stylesheet = `@keyframes ${keyframeId}{${keyframesContents}}`;
152
147
  if (options?.registry) {
@@ -159,30 +154,17 @@ export function keyframes(ruleId, frames, options) {
159
154
  stylesheet
160
155
  };
161
156
  }
162
- export function makeKeyframes(registry) {
163
- return function wrappedCreateKeyframes(ruleId, rules) {
164
- return keyframes(ruleId, rules, {
165
- registry
166
- });
167
- };
168
- }
169
- export function makeCreateStyles(registry) {
170
- return function wrappedCreateStyles(ruleId, rules) {
171
- return createStyles(ruleId, rules, {
172
- registry
173
- });
174
- };
175
- }
176
- export default function createStyles(ruleId, rules, options) {
157
+ export function createStyles(ruleId, rulesFnc, options) {
158
+ const rules = rulesFnc();
177
159
  const coerced = coerceCreateStylesOptions(options);
178
160
  const { classes: out, sheetBuffer: sheetContents, mediaQueriesBuffer: mediaQueriesContents } = execCreateStyles(ruleId, rules, coerced, null);
179
161
  const mergedContents = `${sheetContents}${mediaQueriesContents}`;
180
162
  const replacedSheetContents = replaceBackReferences(out, mergedContents);
181
163
  let sheet = null;
182
- const updateSheet = (updatedRules)=>{
164
+ const updateSheet = (updatedRulesFnc)=>{
183
165
  if (options?.flush || options?.registry || !options?.flush) {
184
166
  // We prefer the first set, and then we shallow merge
185
- const { classes: updatedOut, sheetBuffer: updatedSheetContents, mediaQueriesBuffer: updatedMediaQueriesContents } = execCreateStyles(ruleId, merge(rules, updatedRules), {
167
+ const { classes: updatedOut, sheetBuffer: updatedSheetContents, mediaQueriesBuffer: updatedMediaQueriesContents } = execCreateStyles(ruleId, merge(rules, updatedRulesFnc()), {
186
168
  flush: false
187
169
  }, null);
188
170
  const updatedMergedContents = `${updatedSheetContents}${updatedMediaQueriesContents}`;
@@ -210,3 +192,4 @@ export default function createStyles(ruleId, rules, options) {
210
192
  updateSheet
211
193
  };
212
194
  }
195
+ export default createStyles;
@@ -1,6 +1,7 @@
1
1
  export type { CreateStylesArgs, CreateStylesOptions } from './createStyles.js';
2
- export { default as createStyles, keyframes, makeCreateStyles, makeKeyframes, makeRawStyles, rawStyles, } from './createStyles.js';
2
+ export { default as createStyles, keyframes, rawStyles, } from './createStyles.js';
3
3
  export { setSeed } from './generateClassName.js';
4
+ export * from './makeStyles.js';
4
5
  export type { PosthookPlugin } from './plugins.js';
5
6
  export { registerPosthook } from './plugins.js';
6
7
  export type { SimpleStyleRules } from './types.js';
@@ -1,3 +1,4 @@
1
- export { default as createStyles, keyframes, makeCreateStyles, makeKeyframes, makeRawStyles, rawStyles } from './createStyles.mjs';
1
+ export { default as createStyles, keyframes, rawStyles } from './createStyles.mjs';
2
2
  export { setSeed } from './generateClassName.mjs';
3
+ export * from './makeStyles.mjs';
3
4
  export { registerPosthook } from './plugins.mjs';
@@ -0,0 +1,34 @@
1
+ import type { Properties } from 'csstype';
2
+ import type { SimpleStyleRegistry } from './simpleStyleRegistry.js';
3
+ import type { SimpleStyleRules, SimpleStyleVariables } from './types.js';
4
+ type MakeCssFuncsOpts<T extends SimpleStyleVariables> = {} | {
5
+ registry: SimpleStyleRegistry;
6
+ } | {
7
+ variables: T;
8
+ } | {
9
+ registry: SimpleStyleRegistry;
10
+ variables: T;
11
+ };
12
+ /**
13
+ * Creates all of your CSS functions, createStyles, keframes and rawStyles,
14
+ * and scopes them all to your registry and variables definitions (both are optional).
15
+ * The variants of these functions differ slightly, in that
16
+ * they accept a function as the 2nd parameter, instead of the usual object.
17
+ * The function will be provided with your variables
18
+ */
19
+ export declare function makeCssFuncs<V extends SimpleStyleVariables>(opts: MakeCssFuncsOpts<V>): {
20
+ createStyles: <T extends SimpleStyleRules, K extends keyof T, O extends Record<K, string>>(ruleId: string, rulesFnc: (vars?: V) => T) => {
21
+ classes: O;
22
+ stylesheet: string;
23
+ updateSheet: <T2 extends SimpleStyleRules>(updatedRulesFnc: () => Partial<T2>) => {
24
+ classes: Record<string | number | keyof T2, string>;
25
+ stylesheet: string;
26
+ } | null;
27
+ };
28
+ keyframes: <T extends Record<string, Properties>>(ruleId: string, rulesFnc: (vars?: V) => T) => {
29
+ keyframe: string;
30
+ stylesheet: string;
31
+ };
32
+ rawStyles: <T extends SimpleStyleRules>(ruleId: string, rulesFnc: (vars?: V) => T) => string;
33
+ };
34
+ export {};
@@ -0,0 +1,29 @@
1
+ import { createStyles, keyframes, rawStyles } from './createStyles.mjs';
2
+ /**
3
+ * Creates all of your CSS functions, createStyles, keframes and rawStyles,
4
+ * and scopes them all to your registry and variables definitions (both are optional).
5
+ * The variants of these functions differ slightly, in that
6
+ * they accept a function as the 2nd parameter, instead of the usual object.
7
+ * The function will be provided with your variables
8
+ */ export function makeCssFuncs(opts) {
9
+ function wrappedCreateStyles(ruleId, rulesFnc) {
10
+ return createStyles(ruleId, ()=>rulesFnc('variables' in opts ? opts.variables : undefined), {
11
+ registry: 'registry' in opts ? opts.registry : undefined
12
+ });
13
+ }
14
+ function wrappedCreateKeyframes(ruleId, rulesFnc) {
15
+ return keyframes(ruleId, ()=>rulesFnc('variables' in opts ? opts.variables : undefined), {
16
+ registry: 'registry' in opts ? opts.registry : undefined
17
+ });
18
+ }
19
+ function wrappedRawStyles(ruleId, rulesFnc) {
20
+ return rawStyles(ruleId, ()=>rulesFnc('variables' in opts ? opts.variables : undefined), {
21
+ registry: 'registry' in opts ? opts.registry : undefined
22
+ });
23
+ }
24
+ return {
25
+ createStyles: wrappedCreateStyles,
26
+ keyframes: wrappedCreateKeyframes,
27
+ rawStyles: wrappedRawStyles
28
+ };
29
+ }
@@ -1,5 +1,14 @@
1
- import { Properties } from 'csstype';
1
+ import type { Properties } from 'csstype';
2
2
  export type SimpleStyleRules = {
3
3
  [key: string]: Properties | SimpleStyleRules;
4
4
  };
5
5
  export type RenderableSimpleStyleRules = SimpleStyleRules & Record<string, Properties[]>;
6
+ export type SimpleStyleVariables = {
7
+ [group: string]: {
8
+ [variable: string]: {
9
+ default: string | number;
10
+ [variants: string]: string | number;
11
+ };
12
+ };
13
+ };
14
+ export type HasProperty<T, K extends keyof T> = T extends Record<K, any> ? true : false;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "simplestyle-js",
3
- "version": "4.3.1",
3
+ "version": "4.3.3-beta.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
  "repository": {
@@ -101,6 +101,16 @@
101
101
  "default": "./dist/esm/index.mjs"
102
102
  }
103
103
  },
104
+ "./makeStyles": {
105
+ "require": {
106
+ "types": "./dist/cjs/makeStyles.d.ts",
107
+ "default": "./dist/cjs/makeStyles.cjs"
108
+ },
109
+ "import": {
110
+ "types": "./dist/esm/makeStyles.d.ts",
111
+ "default": "./dist/esm/makeStyles.mjs"
112
+ }
113
+ },
104
114
  "./next/SimpleStyleProvider": {
105
115
  "require": {
106
116
  "types": "./dist/cjs/next/SimpleStyleProvider.d.ts",
@@ -186,5 +196,8 @@
186
196
  },
187
197
  "module": "./dist/esm/index.mjs",
188
198
  "types": "./dist/cjs/index.d.ts",
189
- "main": "./dist/cjs/index.cjs"
190
- }
199
+ "main": "./dist/cjs/index.cjs",
200
+ "trustedDependencies": [
201
+ "@swc/core"
202
+ ]
203
+ }