simplestyle-js 3.4.2-alpha.1 → 3.4.2-alpha.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.
@@ -116,18 +116,19 @@ function replaceBackReferences(out, sheetContents) {
116
116
  }
117
117
  return (0, _plugins.getPosthooks)().reduce((prev, hook)=>hook(prev), outputSheetContents);
118
118
  }
119
- function createSheet(sheetContents) {
119
+ function createSheet(ruleId, sheetContents) {
120
120
  const doc = globalThis.document;
121
121
  if (doc === undefined) return null;
122
122
  // eslint-disable-next-line @typescript-eslint/no-deprecated
123
123
  if (typeof doc?.head?.appendChild !== 'function' || typeof doc.createElement !== 'function') return null;
124
124
  const styleTag = doc.createElement('style');
125
+ styleTag.id = ruleId;
125
126
  styleTag.innerHTML = sheetContents;
126
127
  return styleTag;
127
128
  }
128
- function flushSheetContents(sheetContents, options) {
129
+ function flushSheetContents(ruleId, sheetContents, options) {
129
130
  // In case we're in come weird test environment that doesn't support JSDom
130
- const styleTag = createSheet(sheetContents);
131
+ const styleTag = createSheet(ruleId, sheetContents);
131
132
  if (styleTag) {
132
133
  if (options?.insertAfter && options.insertBefore) {
133
134
  throw new Error('Both insertAfter and insertBefore were provided. Please choose only one.');
@@ -143,32 +144,32 @@ function coerceCreateStylesOptions(options) {
143
144
  flush: options && typeof options.flush === 'boolean' ? options.flush : true
144
145
  };
145
146
  }
146
- function rawStyles(rules, options) {
147
+ function rawStyles(ruleId, rules, options) {
147
148
  const coerced = coerceCreateStylesOptions(options);
148
149
  const { sheetBuffer: sheetContents, mediaQueriesBuffer: mediaQueriesContents } = execCreateStyles(rules, coerced, null, true);
149
150
  const mergedContents = `${sheetContents}${mediaQueriesContents}`;
150
- if (coerced.flush) flushSheetContents(mergedContents, options);
151
+ if (coerced.flush) flushSheetContents(ruleId, mergedContents, options);
151
152
  return mergedContents;
152
153
  }
153
- function keyframes(frames, options) {
154
+ function keyframes(ruleId, frames, options) {
154
155
  const coerced = coerceCreateStylesOptions(options);
155
156
  const keyframeName = (0, _generateClassName.generateClassName)('keyframes_');
156
157
  const { sheetBuffer: keyframesContents } = execCreateStyles(frames, coerced, null, true);
157
158
  const sheetContents = `@keyframes ${keyframeName}{${keyframesContents}}`;
158
- if (coerced.flush) flushSheetContents(sheetContents);
159
+ if (coerced.flush) flushSheetContents(ruleId, sheetContents);
159
160
  return [
160
161
  keyframeName,
161
162
  sheetContents
162
163
  ];
163
164
  }
164
165
  function makeCreateStyle(registry) {
165
- return function wrappedCreateStyles(rules) {
166
- return createStyles(rules, {
166
+ return function wrappedCreateStyles(ruleId, rules) {
167
+ return createStyles(ruleId, rules, {
167
168
  registry
168
169
  });
169
170
  };
170
171
  }
171
- function createStyles(rules, options) {
172
+ function createStyles(ruleId, rules, options) {
172
173
  const coerced = coerceCreateStylesOptions(options);
173
174
  const { classes: out, sheetBuffer: sheetContents, mediaQueriesBuffer: mediaQueriesContents } = execCreateStyles(rules, coerced, null);
174
175
  const mergedContents = `${sheetContents}${mediaQueriesContents}`;
@@ -191,8 +192,11 @@ function createStyles(rules, options) {
191
192
  }
192
193
  return null;
193
194
  };
194
- if (!options?.registry && coerced.flush) sheet = flushSheetContents(replacedSheetContents, options);
195
- else if (options?.registry) options.registry.add(replacedSheetContents);
195
+ if (!options?.registry && coerced.flush) {
196
+ sheet = flushSheetContents(ruleId, replacedSheetContents, options);
197
+ } else if (options?.registry) {
198
+ options.registry.add(ruleId, replacedSheetContents);
199
+ }
196
200
  // Need this TS cast to get solid code assist from the consumption-side
197
201
  return {
198
202
  classes: out,
@@ -26,9 +26,9 @@ export type CreateStylesOptions = Partial<{
26
26
  */
27
27
  registry?: SimpleStyleRegistry;
28
28
  }>;
29
- export declare function rawStyles<T extends SimpleStyleRules, K extends keyof T, O extends Record<K, string>>(rules: T, options?: Partial<CreateStylesOptions>): string;
30
- export declare function keyframes<T extends Record<string, Properties>>(frames: T, options?: CreateStylesOptions): [string, string];
31
- export declare function makeCreateStyle(registry: SimpleStyleRegistry): <T extends SimpleStyleRules, K extends keyof T, O extends Record<K, string>>(rules: T) => {
29
+ export declare function rawStyles<T extends SimpleStyleRules, K extends keyof T, O extends Record<K, string>>(ruleId: string, rules: T, options?: Partial<CreateStylesOptions>): string;
30
+ export declare function keyframes<T extends Record<string, Properties>>(ruleId: string, frames: T, options?: CreateStylesOptions): [string, string];
31
+ export declare function makeCreateStyle(registry: SimpleStyleRegistry): <T extends SimpleStyleRules, K extends keyof T, O extends Record<K, string>>(ruleId: string, rules: T) => {
32
32
  classes: O;
33
33
  stylesheet: string;
34
34
  updateSheet: <T2 extends SimpleStyleRules, K2 extends keyof T2, O2 extends Record<K2, string>>(updatedRules: Partial<T2>) => {
@@ -36,7 +36,7 @@ export declare function makeCreateStyle(registry: SimpleStyleRegistry): <T exten
36
36
  stylesheet: string;
37
37
  } | null;
38
38
  };
39
- export default function createStyles<T extends SimpleStyleRules, K extends keyof T, O extends Record<K, string>>(rules: T, options?: Partial<CreateStylesOptions>): {
39
+ export default function createStyles<T extends SimpleStyleRules, K extends keyof T, O extends Record<K, string>>(ruleId: string, rules: T, options?: Partial<CreateStylesOptions>): {
40
40
  classes: O;
41
41
  stylesheet: string;
42
42
  updateSheet: <T2 extends SimpleStyleRules, K2 extends keyof T2, O2 extends Record<K2, string>>(updatedRules: Partial<T2>) => {
@@ -16,7 +16,7 @@ function _interop_require_default(obj) {
16
16
  default: obj
17
17
  };
18
18
  }
19
- function useCreateStyles(rules, options) {
19
+ function useCreateStyles(ruleId, rules, options) {
20
20
  // cache rules to compare later
21
21
  const [cachedRules, setCachedRules] = (0, _react.useState)(()=>rules);
22
22
  // memoize options but keep them live
@@ -28,7 +28,7 @@ function useCreateStyles(rules, options) {
28
28
  const didFirstWriteRef = (0, _react.useRef)(false);
29
29
  const styleTagRef = (0, _react.useRef)(typeof document === 'undefined' ? null : document.createElement('style'));
30
30
  // initialize styles
31
- const [styleState, setStyleState] = (0, _react.useState)(()=>(0, _createStyles.default)(rules, {
31
+ const [styleState, setStyleState] = (0, _react.useState)(()=>(0, _createStyles.default)(ruleId, rules, {
32
32
  ...cachedOptions,
33
33
  flush: false
34
34
  }));
@@ -1,3 +1,3 @@
1
1
  import { CreateStylesOptions } from '../createStyles.js';
2
2
  import { SimpleStyleRules } from '../types.js';
3
- export declare function useCreateStyles<T extends SimpleStyleRules, K extends keyof T, O extends Record<K, string>>(rules: T, options?: Partial<Omit<CreateStylesOptions, 'flush'>>): O;
3
+ export declare function useCreateStyles<T extends SimpleStyleRules, K extends keyof T, O extends Record<K, string>>(ruleId: string, rules: T, options?: Partial<Omit<CreateStylesOptions, 'flush'>>): O;
@@ -1,9 +1,4 @@
1
- /**
2
- * Acts as an accumulator for all
3
- * generated css that occurs via createStyles().
4
- *
5
- * React variant of this will come after the baseline one is created
6
- */ "use strict";
1
+ "use strict";
7
2
  Object.defineProperty(exports, "__esModule", {
8
3
  value: true
9
4
  });
@@ -13,13 +8,29 @@ Object.defineProperty(exports, "SimpleStyleRegistry", {
13
8
  return SimpleStyleRegistry;
14
9
  }
15
10
  });
11
+ /* eslint-disable unicorn/prefer-query-selector */ const doc = globalThis.document;
16
12
  class SimpleStyleRegistry {
17
- sheets = [];
18
- add(contents) {
19
- this.sheets.push(contents);
13
+ sheets = new Map();
14
+ add(ruleId, contents) {
15
+ if (this.sheets.has(ruleId) && doc) {
16
+ const tag = doc.getElementById(ruleId);
17
+ if (tag) {
18
+ tag.innerHTML = contents;
19
+ }
20
+ }
21
+ this.sheets.set(ruleId, contents);
20
22
  }
21
- getCSS() {
22
- return this.sheets.reduce((prev, contents)=>`${prev}
23
+ /**
24
+ * returns the bare CSS to be injected into a <style /> tag
25
+ */ getCSS() {
26
+ return this.sheets.values().reduce((prev, contents)=>`${prev}
23
27
  ${contents}`, '');
24
28
  }
29
+ /**
30
+ * returns an HTML string with various <style /> tags
31
+ * mapped to their internal ruleset IDs
32
+ */ getHTML() {
33
+ return this.sheets.entries().reduce((prev, [ruleId, contents])=>`${prev}
34
+ <style id="${ruleId}">${contents}</style>`, '');
35
+ }
25
36
  }
@@ -6,6 +6,14 @@
6
6
  */
7
7
  export declare class SimpleStyleRegistry {
8
8
  private sheets;
9
- add(contents: string): void;
9
+ add(ruleId: string, contents: string): void;
10
+ /**
11
+ * returns the bare CSS to be injected into a <style /> tag
12
+ */
10
13
  getCSS(): string;
14
+ /**
15
+ * returns an HTML string with various <style /> tags
16
+ * mapped to their internal ruleset IDs
17
+ */
18
+ getHTML(): string;
11
19
  }
@@ -26,9 +26,9 @@ export type CreateStylesOptions = Partial<{
26
26
  */
27
27
  registry?: SimpleStyleRegistry;
28
28
  }>;
29
- export declare function rawStyles<T extends SimpleStyleRules, K extends keyof T, O extends Record<K, string>>(rules: T, options?: Partial<CreateStylesOptions>): string;
30
- export declare function keyframes<T extends Record<string, Properties>>(frames: T, options?: CreateStylesOptions): [string, string];
31
- export declare function makeCreateStyle(registry: SimpleStyleRegistry): <T extends SimpleStyleRules, K extends keyof T, O extends Record<K, string>>(rules: T) => {
29
+ export declare function rawStyles<T extends SimpleStyleRules, K extends keyof T, O extends Record<K, string>>(ruleId: string, rules: T, options?: Partial<CreateStylesOptions>): string;
30
+ export declare function keyframes<T extends Record<string, Properties>>(ruleId: string, frames: T, options?: CreateStylesOptions): [string, string];
31
+ export declare function makeCreateStyle(registry: SimpleStyleRegistry): <T extends SimpleStyleRules, K extends keyof T, O extends Record<K, string>>(ruleId: string, rules: T) => {
32
32
  classes: O;
33
33
  stylesheet: string;
34
34
  updateSheet: <T2 extends SimpleStyleRules, K2 extends keyof T2, O2 extends Record<K2, string>>(updatedRules: Partial<T2>) => {
@@ -36,7 +36,7 @@ export declare function makeCreateStyle(registry: SimpleStyleRegistry): <T exten
36
36
  stylesheet: string;
37
37
  } | null;
38
38
  };
39
- export default function createStyles<T extends SimpleStyleRules, K extends keyof T, O extends Record<K, string>>(rules: T, options?: Partial<CreateStylesOptions>): {
39
+ export default function createStyles<T extends SimpleStyleRules, K extends keyof T, O extends Record<K, string>>(ruleId: string, rules: T, options?: Partial<CreateStylesOptions>): {
40
40
  classes: O;
41
41
  stylesheet: string;
42
42
  updateSheet: <T2 extends SimpleStyleRules, K2 extends keyof T2, O2 extends Record<K2, string>>(updatedRules: Partial<T2>) => {
@@ -87,18 +87,19 @@ function replaceBackReferences(out, sheetContents) {
87
87
  }
88
88
  return getPosthooks().reduce((prev, hook)=>hook(prev), outputSheetContents);
89
89
  }
90
- function createSheet(sheetContents) {
90
+ function createSheet(ruleId, sheetContents) {
91
91
  const doc = globalThis.document;
92
92
  if (doc === undefined) return null;
93
93
  // eslint-disable-next-line @typescript-eslint/no-deprecated
94
94
  if (typeof doc?.head?.appendChild !== 'function' || typeof doc.createElement !== 'function') return null;
95
95
  const styleTag = doc.createElement('style');
96
+ styleTag.id = ruleId;
96
97
  styleTag.innerHTML = sheetContents;
97
98
  return styleTag;
98
99
  }
99
- function flushSheetContents(sheetContents, options) {
100
+ function flushSheetContents(ruleId, sheetContents, options) {
100
101
  // In case we're in come weird test environment that doesn't support JSDom
101
- const styleTag = createSheet(sheetContents);
102
+ const styleTag = createSheet(ruleId, sheetContents);
102
103
  if (styleTag) {
103
104
  if (options?.insertAfter && options.insertBefore) {
104
105
  throw new Error('Both insertAfter and insertBefore were provided. Please choose only one.');
@@ -115,32 +116,32 @@ function coerceCreateStylesOptions(options) {
115
116
  };
116
117
  }
117
118
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
118
- export function rawStyles(rules, options) {
119
+ export function rawStyles(ruleId, rules, options) {
119
120
  const coerced = coerceCreateStylesOptions(options);
120
121
  const { sheetBuffer: sheetContents, mediaQueriesBuffer: mediaQueriesContents } = execCreateStyles(rules, coerced, null, true);
121
122
  const mergedContents = `${sheetContents}${mediaQueriesContents}`;
122
- if (coerced.flush) flushSheetContents(mergedContents, options);
123
+ if (coerced.flush) flushSheetContents(ruleId, mergedContents, options);
123
124
  return mergedContents;
124
125
  }
125
- export function keyframes(frames, options) {
126
+ export function keyframes(ruleId, frames, options) {
126
127
  const coerced = coerceCreateStylesOptions(options);
127
128
  const keyframeName = generateClassName('keyframes_');
128
129
  const { sheetBuffer: keyframesContents } = execCreateStyles(frames, coerced, null, true);
129
130
  const sheetContents = `@keyframes ${keyframeName}{${keyframesContents}}`;
130
- if (coerced.flush) flushSheetContents(sheetContents);
131
+ if (coerced.flush) flushSheetContents(ruleId, sheetContents);
131
132
  return [
132
133
  keyframeName,
133
134
  sheetContents
134
135
  ];
135
136
  }
136
137
  export function makeCreateStyle(registry) {
137
- return function wrappedCreateStyles(rules) {
138
- return createStyles(rules, {
138
+ return function wrappedCreateStyles(ruleId, rules) {
139
+ return createStyles(ruleId, rules, {
139
140
  registry
140
141
  });
141
142
  };
142
143
  }
143
- export default function createStyles(rules, options) {
144
+ export default function createStyles(ruleId, rules, options) {
144
145
  const coerced = coerceCreateStylesOptions(options);
145
146
  const { classes: out, sheetBuffer: sheetContents, mediaQueriesBuffer: mediaQueriesContents } = execCreateStyles(rules, coerced, null);
146
147
  const mergedContents = `${sheetContents}${mediaQueriesContents}`;
@@ -163,8 +164,11 @@ export default function createStyles(rules, options) {
163
164
  }
164
165
  return null;
165
166
  };
166
- if (!options?.registry && coerced.flush) sheet = flushSheetContents(replacedSheetContents, options);
167
- else if (options?.registry) options.registry.add(replacedSheetContents);
167
+ if (!options?.registry && coerced.flush) {
168
+ sheet = flushSheetContents(ruleId, replacedSheetContents, options);
169
+ } else if (options?.registry) {
170
+ options.registry.add(ruleId, replacedSheetContents);
171
+ }
168
172
  // Need this TS cast to get solid code assist from the consumption-side
169
173
  return {
170
174
  classes: out,
@@ -1,3 +1,3 @@
1
1
  import { CreateStylesOptions } from '../createStyles.js';
2
2
  import { SimpleStyleRules } from '../types.js';
3
- export declare function useCreateStyles<T extends SimpleStyleRules, K extends keyof T, O extends Record<K, string>>(rules: T, options?: Partial<Omit<CreateStylesOptions, 'flush'>>): O;
3
+ export declare function useCreateStyles<T extends SimpleStyleRules, K extends keyof T, O extends Record<K, string>>(ruleId: string, rules: T, options?: Partial<Omit<CreateStylesOptions, 'flush'>>): O;
@@ -1,7 +1,7 @@
1
1
  import { useEffect, useMemo, useRef, useState } from 'react';
2
2
  import createStyles from '../createStyles.mjs';
3
3
  import { deepEqual } from '../util/index.mjs';
4
- export function useCreateStyles(rules, options) {
4
+ export function useCreateStyles(ruleId, rules, options) {
5
5
  // cache rules to compare later
6
6
  const [cachedRules, setCachedRules] = useState(()=>rules);
7
7
  // memoize options but keep them live
@@ -13,7 +13,7 @@ export function useCreateStyles(rules, options) {
13
13
  const didFirstWriteRef = useRef(false);
14
14
  const styleTagRef = useRef(typeof document === 'undefined' ? null : document.createElement('style'));
15
15
  // initialize styles
16
- const [styleState, setStyleState] = useState(()=>createStyles(rules, {
16
+ const [styleState, setStyleState] = useState(()=>createStyles(ruleId, rules, {
17
17
  ...cachedOptions,
18
18
  flush: false
19
19
  }));
@@ -6,6 +6,14 @@
6
6
  */
7
7
  export declare class SimpleStyleRegistry {
8
8
  private sheets;
9
- add(contents: string): void;
9
+ add(ruleId: string, contents: string): void;
10
+ /**
11
+ * returns the bare CSS to be injected into a <style /> tag
12
+ */
10
13
  getCSS(): string;
14
+ /**
15
+ * returns an HTML string with various <style /> tags
16
+ * mapped to their internal ruleset IDs
17
+ */
18
+ getHTML(): string;
11
19
  }
@@ -1,15 +1,31 @@
1
+ /* eslint-disable unicorn/prefer-query-selector */ const doc = globalThis.document;
1
2
  /**
2
3
  * Acts as an accumulator for all
3
4
  * generated css that occurs via createStyles().
4
5
  *
5
6
  * React variant of this will come after the baseline one is created
6
7
  */ export class SimpleStyleRegistry {
7
- sheets = [];
8
- add(contents) {
9
- this.sheets.push(contents);
8
+ sheets = new Map();
9
+ add(ruleId, contents) {
10
+ if (this.sheets.has(ruleId) && doc) {
11
+ const tag = doc.getElementById(ruleId);
12
+ if (tag) {
13
+ tag.innerHTML = contents;
14
+ }
15
+ }
16
+ this.sheets.set(ruleId, contents);
10
17
  }
11
- getCSS() {
12
- return this.sheets.reduce((prev, contents)=>`${prev}
18
+ /**
19
+ * returns the bare CSS to be injected into a <style /> tag
20
+ */ getCSS() {
21
+ return this.sheets.values().reduce((prev, contents)=>`${prev}
13
22
  ${contents}`, '');
14
23
  }
24
+ /**
25
+ * returns an HTML string with various <style /> tags
26
+ * mapped to their internal ruleset IDs
27
+ */ getHTML() {
28
+ return this.sheets.entries().reduce((prev, [ruleId, contents])=>`${prev}
29
+ <style id="${ruleId}">${contents}</style>`, '');
30
+ }
15
31
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "simplestyle-js",
3
- "version": "3.4.2-alpha.1",
3
+ "version": "3.4.2-alpha.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": {
@@ -40,7 +40,7 @@
40
40
  "@vitejs/plugin-react": "^5.1.1",
41
41
  "autoprefixer": "^10.4.22",
42
42
  "coveralls": "^3.1.1",
43
- "eslint-config-react-yas": "^6.0.10",
43
+ "eslint-config-react-yas": "^6.0.11",
44
44
  "husky": "^9.1.7",
45
45
  "postcss": "^8.5.6",
46
46
  "react": ">=18",