simplestyle-js 5.2.0 → 5.2.2

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
@@ -95,6 +95,9 @@ Rules support nested selectors via `&`, media queries, and `$className` back-ref
95
95
  }), { flush: false }); // do not write to the DOM automatically
96
96
  ```
97
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
+
98
101
  - `keyframes(ruleId, framesFnc, options?)`
99
102
  - Generates a unique animation name and accompanying `@keyframes` CSS.
100
103
  - Returns `{ keyframe: string; stylesheet: string; }`. Respects `flush` and `insertBefore/After` options.
@@ -183,7 +186,7 @@ setSeed(11223344);
183
186
 
184
187
  export const StyleRegistry = new SimpleStyleRegistry();
185
188
 
186
- export const { createStyles, keyframes } = makeCssFuncs({ registry: StyleRegistry });
189
+ export const { createStyles, imports, keyframes, rawStyles } = makeCssFuncs({ registry: StyleRegistry });
187
190
  ```
188
191
 
189
192
  ```tsx
@@ -235,7 +238,7 @@ setSeed(11223344);
235
238
 
236
239
  export const StyleRegistry = new SimpleStyleRegistry();
237
240
 
238
- export { createStyles, keyframes } = makeCssFuncs({ registry: StyleRegistry });
241
+ export { createStyles, imports, keyframes, rawStyles } = makeCssFuncs({ registry: StyleRegistry });
239
242
  ```
240
243
 
241
244
  ```astro
@@ -351,7 +354,7 @@ setSeed(1);
351
354
  export const StyleRegistry = new SimpleStyleRegistry();
352
355
 
353
356
  // export the style functions that will be locked to your registry
354
- export const { createStyles, keyframes, rawStyles } = makeCssFuncs({ registry: })
357
+ export const { createStyles, imports, keyframes, rawStyles } = makeCssFuncs({ registry: })
355
358
  ```
356
359
 
357
360
  #### 2. Render the generated styles in your HTML
@@ -15,6 +15,9 @@ _export(exports, {
15
15
  get default () {
16
16
  return _default;
17
17
  },
18
+ get imports () {
19
+ return imports;
20
+ },
18
21
  get keyframes () {
19
22
  return keyframes;
20
23
  },
@@ -154,6 +157,26 @@ function coerceCreateStylesOptions(options) {
154
157
  flush: options && typeof options.flush === 'boolean' ? options.flush : true
155
158
  };
156
159
  }
160
+ function imports(ruleId, rulesFnc, options) {
161
+ const coerced = coerceCreateStylesOptions(options);
162
+ const importRuleId = `${ruleId}_imports`;
163
+ const rules = rulesFnc();
164
+ if (!Array.isArray(rules)) {
165
+ throw new Error('the import() function expects the value returned to be an array of @import strings');
166
+ }
167
+ let sheetBuffer = '';
168
+ for (const importRule of rules){
169
+ if (!importRule.startsWith('@import')) {
170
+ throw new Error(`found an invalid import string: ${importRule}`);
171
+ }
172
+ sheetBuffer += `${importRule}${importRule.endsWith(';') ? '' : ';'}`;
173
+ }
174
+ if (options?.registry) {
175
+ options.registry.add(importRuleId, sheetBuffer);
176
+ } else if (coerced.flush) {
177
+ flushSheetContents(importRuleId, sheetBuffer, options);
178
+ }
179
+ }
157
180
  function rawStyles(ruleId, rulesFnc, options) {
158
181
  const rawStylesId = `${ruleId}_raw`;
159
182
  const coerced = coerceCreateStylesOptions(options);
@@ -1,6 +1,5 @@
1
- import type { Properties } from 'csstype';
2
1
  import type { SimpleStyleRegistry } from './simpleStyleRegistry.js';
3
- import type { SimpleStyleRules } from './types.js';
2
+ import type { ImportStringType, Properties, SimpleStyleRules } from './types.js';
4
3
  export type CreateStylesOptions = Partial<{
5
4
  /**
6
5
  * If true, automatically renders generated styles
@@ -26,6 +25,7 @@ export type CreateStylesOptions = Partial<{
26
25
  */
27
26
  registry?: SimpleStyleRegistry;
28
27
  }>;
28
+ export declare function imports(ruleId: string, rulesFnc: () => ImportStringType[], options?: CreateStylesOptions): void;
29
29
  export declare function rawStyles<T extends SimpleStyleRules>(ruleId: string, rulesFnc: () => T, options?: Partial<CreateStylesOptions>): string;
30
30
  export declare function keyframes<T extends Record<string, Properties>>(ruleId: string, framesFnc: () => T, options?: CreateStylesOptions): {
31
31
  keyframe: string;
@@ -29,6 +29,7 @@ const _createStyles = /*#__PURE__*/ _interop_require_wildcard(require("./createS
29
29
  const _generateClassName = require("./generateClassName.cjs");
30
30
  _export_star(require("./makeStyles.cjs"), exports);
31
31
  const _plugins = require("./plugins.cjs");
32
+ _export_star(require("./types.cjs"), exports);
32
33
  function _export_star(from, to) {
33
34
  Object.keys(from).forEach(function(k) {
34
35
  if (k !== "default" && !Object.prototype.hasOwnProperty.call(to, k)) {
@@ -4,4 +4,4 @@ export { setSeed } from './generateClassName.js';
4
4
  export * from './makeStyles.js';
5
5
  export type { PosthookPlugin } from './plugins.js';
6
6
  export { registerPosthook } from './plugins.js';
7
- export type { SimpleStyleRules } from './types.js';
7
+ export * from './types.js';
@@ -28,8 +28,15 @@ function makeCssFuncs(opts) {
28
28
  registry: 'registry' in opts ? opts.registry : overrides?.registry
29
29
  });
30
30
  }
31
+ function wrappedImports(ruleId, rulesFnc, overrides) {
32
+ return (0, _createStyles.imports)(ruleId, ()=>rulesFnc('variables' in opts ? opts.variables : undefined), {
33
+ ...overrides,
34
+ registry: 'registry' in opts ? opts.registry : overrides?.registry
35
+ });
36
+ }
31
37
  return {
32
38
  createStyles: wrappedCreateStyles,
39
+ imports: wrappedImports,
33
40
  keyframes: wrappedCreateKeyframes,
34
41
  rawStyles: wrappedRawStyles
35
42
  };
@@ -1,7 +1,7 @@
1
1
  import type { Properties } from 'csstype';
2
2
  import { type CreateStylesOptions } from './createStyles.js';
3
3
  import type { SimpleStyleRegistry } from './simpleStyleRegistry.js';
4
- import type { SimpleStyleRules } from './types.js';
4
+ import type { ImportStringType, SimpleStyleRules } from './types.js';
5
5
  type MakeCssFuncsOpts<T extends object> = {} | {
6
6
  registry: SimpleStyleRegistry;
7
7
  } | {
@@ -26,6 +26,7 @@ export declare function makeCssFuncs<V extends object>(opts: MakeCssFuncsOpts<V>
26
26
  stylesheet: string;
27
27
  } | null;
28
28
  };
29
+ imports: (ruleId: string, rulesFnc: (vars?: V) => ImportStringType[], overrides?: CreateStylesOptions) => void;
29
30
  keyframes: <T extends Record<string, Properties>>(ruleId: string, rulesFnc: (vars?: V) => T, overrides?: CreateStylesOptions) => {
30
31
  keyframe: string;
31
32
  stylesheet: string;
@@ -4,3 +4,5 @@ export type SimpleStyleRules = {
4
4
  };
5
5
  export type RenderableSimpleStyleRules = SimpleStyleRules & Record<string, Properties[]>;
6
6
  export type HasProperty<T, K extends keyof T> = T extends Record<K, any> ? true : false;
7
+ export type ImportStringType = `@import ${string}`;
8
+ export type { Properties };
@@ -1,6 +1,5 @@
1
- import type { Properties } from 'csstype';
2
1
  import type { SimpleStyleRegistry } from './simpleStyleRegistry.js';
3
- import type { SimpleStyleRules } from './types.js';
2
+ import type { ImportStringType, Properties, SimpleStyleRules } from './types.js';
4
3
  export type CreateStylesOptions = Partial<{
5
4
  /**
6
5
  * If true, automatically renders generated styles
@@ -26,6 +25,7 @@ export type CreateStylesOptions = Partial<{
26
25
  */
27
26
  registry?: SimpleStyleRegistry;
28
27
  }>;
28
+ export declare function imports(ruleId: string, rulesFnc: () => ImportStringType[], options?: CreateStylesOptions): void;
29
29
  export declare function rawStyles<T extends SimpleStyleRules>(ruleId: string, rulesFnc: () => T, options?: Partial<CreateStylesOptions>): string;
30
30
  export declare function keyframes<T extends Record<string, Properties>>(ruleId: string, framesFnc: () => T, options?: CreateStylesOptions): {
31
31
  keyframe: string;
@@ -125,6 +125,26 @@ function coerceCreateStylesOptions(options) {
125
125
  flush: options && typeof options.flush === 'boolean' ? options.flush : true
126
126
  };
127
127
  }
128
+ export function imports(ruleId, rulesFnc, options) {
129
+ const coerced = coerceCreateStylesOptions(options);
130
+ const importRuleId = `${ruleId}_imports`;
131
+ const rules = rulesFnc();
132
+ if (!Array.isArray(rules)) {
133
+ throw new Error('the import() function expects the value returned to be an array of @import strings');
134
+ }
135
+ let sheetBuffer = '';
136
+ for (const importRule of rules){
137
+ if (!importRule.startsWith('@import')) {
138
+ throw new Error(`found an invalid import string: ${importRule}`);
139
+ }
140
+ sheetBuffer += `${importRule}${importRule.endsWith(';') ? '' : ';'}`;
141
+ }
142
+ if (options?.registry) {
143
+ options.registry.add(importRuleId, sheetBuffer);
144
+ } else if (coerced.flush) {
145
+ flushSheetContents(importRuleId, sheetBuffer, options);
146
+ }
147
+ }
128
148
  export function rawStyles(ruleId, rulesFnc, options) {
129
149
  const rawStylesId = `${ruleId}_raw`;
130
150
  const coerced = coerceCreateStylesOptions(options);
@@ -4,4 +4,4 @@ export { setSeed } from './generateClassName.js';
4
4
  export * from './makeStyles.js';
5
5
  export type { PosthookPlugin } from './plugins.js';
6
6
  export { registerPosthook } from './plugins.js';
7
- export type { SimpleStyleRules } from './types.js';
7
+ export * from './types.js';
@@ -2,3 +2,4 @@ export { default as createStyles, keyframes, rawStyles } from './createStyles.mj
2
2
  export { setSeed } from './generateClassName.mjs';
3
3
  export * from './makeStyles.mjs';
4
4
  export { registerPosthook } from './plugins.mjs';
5
+ export * from './types.mjs';
@@ -1,7 +1,7 @@
1
1
  import type { Properties } from 'csstype';
2
2
  import { type CreateStylesOptions } from './createStyles.js';
3
3
  import type { SimpleStyleRegistry } from './simpleStyleRegistry.js';
4
- import type { SimpleStyleRules } from './types.js';
4
+ import type { ImportStringType, SimpleStyleRules } from './types.js';
5
5
  type MakeCssFuncsOpts<T extends object> = {} | {
6
6
  registry: SimpleStyleRegistry;
7
7
  } | {
@@ -26,6 +26,7 @@ export declare function makeCssFuncs<V extends object>(opts: MakeCssFuncsOpts<V>
26
26
  stylesheet: string;
27
27
  } | null;
28
28
  };
29
+ imports: (ruleId: string, rulesFnc: (vars?: V) => ImportStringType[], overrides?: CreateStylesOptions) => void;
29
30
  keyframes: <T extends Record<string, Properties>>(ruleId: string, rulesFnc: (vars?: V) => T, overrides?: CreateStylesOptions) => {
30
31
  keyframe: string;
31
32
  stylesheet: string;
@@ -1,4 +1,4 @@
1
- import { createStyles, keyframes, rawStyles } from './createStyles.mjs';
1
+ import { createStyles, imports, keyframes, rawStyles } from './createStyles.mjs';
2
2
  /**
3
3
  * Creates all of your CSS functions, createStyles, keframes and rawStyles,
4
4
  * and scopes them all to your registry and variables definitions (both are optional).
@@ -24,8 +24,15 @@ import { createStyles, keyframes, rawStyles } from './createStyles.mjs';
24
24
  registry: 'registry' in opts ? opts.registry : overrides?.registry
25
25
  });
26
26
  }
27
+ function wrappedImports(ruleId, rulesFnc, overrides) {
28
+ return imports(ruleId, ()=>rulesFnc('variables' in opts ? opts.variables : undefined), {
29
+ ...overrides,
30
+ registry: 'registry' in opts ? opts.registry : overrides?.registry
31
+ });
32
+ }
27
33
  return {
28
34
  createStyles: wrappedCreateStyles,
35
+ imports: wrappedImports,
29
36
  keyframes: wrappedCreateKeyframes,
30
37
  rawStyles: wrappedRawStyles
31
38
  };
@@ -4,3 +4,5 @@ export type SimpleStyleRules = {
4
4
  };
5
5
  export type RenderableSimpleStyleRules = SimpleStyleRules & Record<string, Properties[]>;
6
6
  export type HasProperty<T, K extends keyof T> = T extends Record<K, any> ? true : false;
7
+ export type ImportStringType = `@import ${string}`;
8
+ export type { Properties };
@@ -1 +1,2 @@
1
+ // re-export this type so we don't run into portability issues here
1
2
  export { };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "simplestyle-js",
3
- "version": "5.2.0",
3
+ "version": "5.2.2",
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": {