simplestyle-js 4.3.2 → 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
@@ -14,6 +14,7 @@ A concise guide to the core `simplestyle-js` APIs, how they fit together, and ho
14
14
  - [Quick Start](#quick-start)
15
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
19
  - [Next.js](#nextjs)
19
20
  - [Astro](#astro)
@@ -51,10 +52,10 @@ yarn add simplestyle-js
51
52
 
52
53
  ## Quick Start
53
54
 
54
- ```tsx
55
+ ```jsx
55
56
  import createStyles from 'simplestyle-js';
56
57
 
57
- const { classes } = createStyles('Button', {
58
+ const { classes } = createStyles('Button', () => ({
58
59
  root: {
59
60
  '&:hover': { backgroundColor: '#2d6cdf' },
60
61
  '@media (max-width: 768px)': { padding: '10px 12px' },
@@ -63,7 +64,7 @@ const { classes } = createStyles('Button', {
63
64
  color: '#fff',
64
65
  padding: '12px 16px',
65
66
  },
66
- });
67
+ }));
67
68
 
68
69
  document.querySelector('button')?.classList.add(classes.root);
69
70
 
@@ -83,7 +84,7 @@ Rules support nested selectors via `&`, media queries, and `$className` back-ref
83
84
  - `updateSheet(updatedRules)` merges rules and updates the existing sheet (works when `flush` is `true` or a `registry` is provided). Returns `{ classes, stylesheet } | null`.
84
85
  - Example:
85
86
  ```ts
86
- const { classes, stylesheet } = createStyles('Nav', {
87
+ const { classes, stylesheet } = createStyles('Nav', () => ({
87
88
  wrapper: { display: 'flex', gap: 12 },
88
89
  link: {
89
90
  '&:hover': { textDecoration: 'underline' },
@@ -91,24 +92,29 @@ Rules support nested selectors via `&`, media queries, and `$className` back-ref
91
92
  '@media (max-width: 600px)': {
92
93
  wrapper: { flexDirection: 'column' },
93
94
  },
94
- }, { flush: false }); // do not write to the DOM automatically
95
+ }), { flush: false }); // do not write to the DOM automatically
95
96
  ```
96
97
 
97
- - `keyframes(ruleId, frames, options?)`
98
+ - `keyframes(ruleId, framesFnc, options?)`
98
99
  - Generates a unique animation name and accompanying `@keyframes` CSS.
99
100
  - Returns `{ keyframe: string; stylesheet: string; }`. Respects `flush` and `insertBefore/After` options.
100
101
 
101
- - `rawStyles(ruleId, rules, options?)`
102
+ - `rawStyles(ruleId, rulesFnc, options?)`
102
103
  - Writes rules without generating new class names. Keys must already be selectors (e.g., `html`, `body *`, `.app`).
103
104
  - Good for global resets or theme primitives. Respects `flush` and `registry`.
104
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
+
105
111
  - `makeCreateStyles(registry)`
106
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).
107
113
 
108
- - `makeRawStyles(registry)` *(from `simplestyle-js/createStyles`)*
114
+ - `makeRawStyles(registry)`
109
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).
110
116
 
111
- - `makeKeyframes(registry)` *(from `simplestyle-js/createStyles`)*
117
+ - `makeKeyframes(registry)`
112
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).
113
119
 
114
120
  - `setSeed(seed: number | null)`
@@ -131,6 +137,36 @@ Rules support nested selectors via `&`, media queries, and `$className` back-ref
131
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
132
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.
133
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
+
134
170
  ## SSR
135
171
 
136
172
  ### Next.js
@@ -140,7 +176,7 @@ Create your style registry and scoped CSS functions, then use the official Next.
140
176
  ```typescript
141
177
  // src/styleRegistry.ts
142
178
 
143
- import { makeCreateStyles, makeKeyframes, setSeed } from "simplestyle-js";
179
+ import { makeCssFuncs, setSeed } from "simplestyle-js";
144
180
  import { SimpleStyleRegistry } from "simplestyle-js/simpleStyleRegistry";
145
181
 
146
182
  // ensures deterministic creation of CSS classnames
@@ -148,8 +184,7 @@ setSeed(11223344);
148
184
 
149
185
  export const StyleRegistry = new SimpleStyleRegistry();
150
186
 
151
- export const createStyles = makeCreateStyles(StyleRegistry);
152
- export const keyframes = makeKeyframes(StyleRegistry);
187
+ export const { createStyles, keyframes } = makeCssFuncs({ registry: StyleRegistry });
153
188
  ```
154
189
 
155
190
  ```tsx
@@ -159,12 +194,12 @@ import { SimpleStyleProvider } from "simplestyle-js/next";
159
194
  import { createStyles, StyleRegistry } from "./styleRegistry";
160
195
 
161
196
  // start writing CSS!
162
- const { classes } = createStyles('RootLayoutStyles', {
197
+ const { classes } = createStyles('RootLayoutStyles', () => ({
163
198
  rootLayout: {
164
199
  backgroundColor: 'pink',
165
200
  padding: '1rem',
166
201
  },
167
- });
202
+ }));
168
203
 
169
204
  export default function RootLayout({
170
205
  children,
@@ -193,7 +228,7 @@ Create your style registry and scoped CSS functions, then use the official Astro
193
228
  ```typescript
194
229
  // src/styleRegistry.ts
195
230
 
196
- import { makeCreateStyles, makeKeyframes, setSeed } from "simplestyle-js";
231
+ import { makeCssFuncs, setSeed } from "simplestyle-js";
197
232
  import { SimpleStyleRegistry } from "simplestyle-js/simpleStyleRegistry";
198
233
 
199
234
  // ensures deterministic creation of CSS classnames
@@ -201,8 +236,7 @@ setSeed(11223344);
201
236
 
202
237
  export const StyleRegistry = new SimpleStyleRegistry();
203
238
 
204
- export const createStyles = makeCreateStyles(StyleRegistry);
205
- export const keyframes = makeKeyframes(StyleRegistry);
239
+ export { createStyles, keyframes } = makeCssFuncs({ registry: StyleRegistry });
206
240
  ```
207
241
 
208
242
  ```astro
@@ -216,7 +250,7 @@ import {
216
250
  rawStyles,
217
251
  } from "../styleRegistry";
218
252
 
219
- rawStyles("basic-css-reset", {
253
+ rawStyles("basic-css-reset", () => ({
220
254
  "*": {
221
255
  boxSizing: "border-box",
222
256
  },
@@ -225,10 +259,10 @@ rawStyles("basic-css-reset", {
225
259
  fontSize: "16px",
226
260
  padding: 0,
227
261
  },
228
- });
262
+ }));
229
263
 
230
264
  // make changes to me and I will hot reload!
231
- const { keyframe } = keyframes("HomePage", {
265
+ const { keyframe } = keyframes("HomePage", () => ({
232
266
  "0%": {
233
267
  backgroundColor: "#cc2222cc",
234
268
  },
@@ -241,9 +275,9 @@ const { keyframe } = keyframes("HomePage", {
241
275
  "100%": {
242
276
  backgroundColor: "#cc2222cc",
243
277
  },
244
- });
278
+ }));
245
279
 
246
- const { classes } = createStyles("HomePage", {
280
+ const { classes } = createStyles("HomePage", () => ({
247
281
  background: {
248
282
  alignItems: "center",
249
283
  animation: `${keyframe} 5s linear infinite`,
@@ -260,7 +294,7 @@ const { classes } = createStyles("HomePage", {
260
294
  color: "white",
261
295
  padding: "1rem",
262
296
  },
263
- });
297
+ }));
264
298
  ---
265
299
 
266
300
  <html lang="en">
@@ -295,8 +329,7 @@ The core APIs needed to make this work are:
295
329
 
296
330
  - `new SimpleStyleRegistry()` - creates a new StyleSheet registry where all of your styles will be accumulated
297
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
298
- - `makeCreateStyle(registry)` - returns a `createStyles()` function that is locked to your StyleSheet registry
299
- - `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
300
333
 
301
334
  ### SSR steps for most SSR / SSG frameworks
302
335
 
@@ -306,7 +339,7 @@ The core APIs needed to make this work are:
306
339
  For demonstration purposes, we'll locate this at our `src/` root, and name it `styleLib.js`
307
340
 
308
341
  ```javascript
309
- import { makeCreateStyles, makeKeyframes, makeRawStyles, setSeed } from "simplestyle-js";
342
+ import { makeCssFuncs, setSeed } from "simplestyle-js";
310
343
  import { SimpleStyleRegistry } from "simplestyle-js/simpleStyleRegistry";
311
344
 
312
345
  // set the className generation seed to ensure classNames are computed consistently
@@ -319,9 +352,7 @@ setSeed(1);
319
352
  export const StyleRegistry = new SimpleStyleRegistry();
320
353
 
321
354
  // export the style functions that will be locked to your registry
322
- export const createStyles = makeCreateStyles(StyleRegistry);
323
- export const keyframes = makeKeyframes(StyleRegistry);
324
- export const rawStyles = makeRawStyles(StyleRegistry);
355
+ export const { createStyles, keyframes, rawStyles } = makeCssFuncs({ registry: })
325
356
  ```
326
357
 
327
358
  #### 2. Render the generated styles in your HTML
@@ -355,7 +386,7 @@ export default function Layout({ children }) {
355
386
  import { createStyles } from '../styleLib.js';
356
387
 
357
388
  // create your styles
358
- const { classes } = createStyles({
389
+ const { classes } = createStyles('my-component', () => ({
359
390
  awesome: {
360
391
  backgroundColor: 'purple',
361
392
  fontSize: '2rem',
@@ -367,7 +398,7 @@ const { classes } = createStyles({
367
398
  textDecoration: 'underline',
368
399
  },
369
400
  },
370
- });
401
+ }));
371
402
 
372
403
  export function MyCoolComponent() {
373
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.2",
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
+ }