simplestyle-js 3.4.2-alpha.1 → 3.4.2-alpha.3
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/dist/cjs/createStyles.cjs +26 -22
- package/dist/cjs/createStyles.d.ts +4 -4
- package/dist/cjs/react/useCreateStyles.cjs +2 -2
- package/dist/cjs/react/useCreateStyles.d.ts +1 -1
- package/dist/cjs/simpleStyleRegistry.cjs +22 -11
- package/dist/cjs/simpleStyleRegistry.d.ts +9 -1
- package/dist/esm/createStyles.d.ts +4 -4
- package/dist/esm/createStyles.mjs +26 -22
- package/dist/esm/react/useCreateStyles.d.ts +1 -1
- package/dist/esm/react/useCreateStyles.mjs +2 -2
- package/dist/esm/simpleStyleRegistry.d.ts +9 -1
- package/dist/esm/simpleStyleRegistry.mjs +21 -5
- package/package.json +2 -2
|
@@ -42,7 +42,7 @@ function formatCSSRuleName(rule) {
|
|
|
42
42
|
function formatCSSRules(cssRules) {
|
|
43
43
|
return Object.entries(cssRules).reduce((prev, [cssProp, cssVal])=>`${prev}${formatCSSRuleName(cssProp)}:${String(cssVal)};`, '');
|
|
44
44
|
}
|
|
45
|
-
function execCreateStyles(rules, options, parentSelector, noGenerateClassName = false) {
|
|
45
|
+
function execCreateStyles(ruleId, rules, options, parentSelector, noGenerateClassName = false) {
|
|
46
46
|
const out = {};
|
|
47
47
|
let sheetBuffer = '';
|
|
48
48
|
let mediaQueriesBuffer = '';
|
|
@@ -58,7 +58,7 @@ function execCreateStyles(rules, options, parentSelector, noGenerateClassName =
|
|
|
58
58
|
if (typeof classNameRules !== 'object') throw new Error('Unable to map @media query because rules / props are an invalid type');
|
|
59
59
|
guardCloseRuleWrite();
|
|
60
60
|
mediaQueriesBuffer += `${classNameOrCSSRule}{`;
|
|
61
|
-
const { mediaQueriesBuffer: mediaQueriesOutput, sheetBuffer: regularOutput } = execCreateStyles(classNameRules, options, parentSelector);
|
|
61
|
+
const { mediaQueriesBuffer: mediaQueriesOutput, sheetBuffer: regularOutput } = execCreateStyles(ruleId, classNameRules, options, parentSelector);
|
|
62
62
|
mediaQueriesBuffer += regularOutput;
|
|
63
63
|
mediaQueriesBuffer += '}';
|
|
64
64
|
mediaQueriesBuffer += mediaQueriesOutput;
|
|
@@ -68,17 +68,17 @@ function execCreateStyles(rules, options, parentSelector, noGenerateClassName =
|
|
|
68
68
|
// format of { '& > span': { display: 'none' } } (or further nesting)
|
|
69
69
|
const replaced = classNameOrCSSRule.replaceAll('&', parentSelector);
|
|
70
70
|
for (const selector of replaced.split(/,\s*/)){
|
|
71
|
-
const { mediaQueriesBuffer: mediaQueriesOutput, sheetBuffer: regularOutput } = execCreateStyles(classNameRules, options, selector);
|
|
71
|
+
const { mediaQueriesBuffer: mediaQueriesOutput, sheetBuffer: regularOutput } = execCreateStyles(ruleId, classNameRules, options, selector);
|
|
72
72
|
sheetBuffer += regularOutput;
|
|
73
73
|
mediaQueriesBuffer += mediaQueriesOutput;
|
|
74
74
|
}
|
|
75
75
|
} else if (!parentSelector && typeof classNameRules === 'object') {
|
|
76
76
|
guardCloseRuleWrite();
|
|
77
|
-
const generated = noGenerateClassName ? classNameOrCSSRule : (0, _generateClassName.generateClassName)(classNameOrCSSRule);
|
|
77
|
+
const generated = noGenerateClassName ? classNameOrCSSRule : (0, _generateClassName.generateClassName)(`${ruleId}_${classNameOrCSSRule}`);
|
|
78
78
|
// @ts-expect-error - yes, we can index this object here, so be quiet
|
|
79
79
|
out[classNameOrCSSRule] = generated;
|
|
80
80
|
const generatedSelector = `${noGenerateClassName ? '' : '.'}${generated}`;
|
|
81
|
-
const { mediaQueriesBuffer: mediaQueriesOutput, sheetBuffer: regularOutput } = execCreateStyles(classNameRules, options, generatedSelector);
|
|
81
|
+
const { mediaQueriesBuffer: mediaQueriesOutput, sheetBuffer: regularOutput } = execCreateStyles(ruleId, classNameRules, options, generatedSelector);
|
|
82
82
|
sheetBuffer += regularOutput;
|
|
83
83
|
mediaQueriesBuffer += mediaQueriesOutput;
|
|
84
84
|
} else {
|
|
@@ -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,34 +144,34 @@ 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
|
-
const { sheetBuffer: sheetContents, mediaQueriesBuffer: mediaQueriesContents } = execCreateStyles(rules, coerced, null, true);
|
|
149
|
+
const { sheetBuffer: sheetContents, mediaQueriesBuffer: mediaQueriesContents } = execCreateStyles(ruleId, 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
|
-
const keyframeName = (0, _generateClassName.generateClassName)(
|
|
156
|
-
const { sheetBuffer: keyframesContents } = execCreateStyles(frames, coerced, null, true);
|
|
156
|
+
const keyframeName = (0, _generateClassName.generateClassName)(`${ruleId}_keyframes_`);
|
|
157
|
+
const { sheetBuffer: keyframesContents } = execCreateStyles(ruleId, 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
|
-
const { classes: out, sheetBuffer: sheetContents, mediaQueriesBuffer: mediaQueriesContents } = execCreateStyles(rules, coerced, null);
|
|
174
|
+
const { classes: out, sheetBuffer: sheetContents, mediaQueriesBuffer: mediaQueriesContents } = execCreateStyles(ruleId, rules, coerced, null);
|
|
174
175
|
const mergedContents = `${sheetContents}${mediaQueriesContents}`;
|
|
175
176
|
const replacedSheetContents = replaceBackReferences(out, mergedContents);
|
|
176
177
|
let sheet = null;
|
|
@@ -178,7 +179,7 @@ function createStyles(rules, options) {
|
|
|
178
179
|
const updateSheet = (updatedRules)=>{
|
|
179
180
|
if ((options?.flush || options?.registry) && sheet || !options?.flush) {
|
|
180
181
|
// We prefer the first set, and then we shallow merge
|
|
181
|
-
const { classes: updatedOut, sheetBuffer: updatedSheetContents, mediaQueriesBuffer: updatedMediaQueriesContents } = execCreateStyles((0, _deepmerge.default)(rules, updatedRules), {
|
|
182
|
+
const { classes: updatedOut, sheetBuffer: updatedSheetContents, mediaQueriesBuffer: updatedMediaQueriesContents } = execCreateStyles(ruleId, (0, _deepmerge.default)(rules, updatedRules), {
|
|
182
183
|
flush: false
|
|
183
184
|
}, null);
|
|
184
185
|
const updatedMergedContents = `${updatedSheetContents}${updatedMediaQueriesContents}`;
|
|
@@ -191,8 +192,11 @@ function createStyles(rules, options) {
|
|
|
191
192
|
}
|
|
192
193
|
return null;
|
|
193
194
|
};
|
|
194
|
-
if (!options?.registry && coerced.flush)
|
|
195
|
-
|
|
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.
|
|
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
|
-
|
|
22
|
-
|
|
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>) => {
|
|
@@ -13,7 +13,7 @@ function formatCSSRuleName(rule) {
|
|
|
13
13
|
function formatCSSRules(cssRules) {
|
|
14
14
|
return Object.entries(cssRules).reduce((prev, [cssProp, cssVal])=>`${prev}${formatCSSRuleName(cssProp)}:${String(cssVal)};`, '');
|
|
15
15
|
}
|
|
16
|
-
function execCreateStyles(rules, options, parentSelector, noGenerateClassName = false) {
|
|
16
|
+
function execCreateStyles(ruleId, rules, options, parentSelector, noGenerateClassName = false) {
|
|
17
17
|
const out = {};
|
|
18
18
|
let sheetBuffer = '';
|
|
19
19
|
let mediaQueriesBuffer = '';
|
|
@@ -29,7 +29,7 @@ function execCreateStyles(rules, options, parentSelector, noGenerateClassName =
|
|
|
29
29
|
if (typeof classNameRules !== 'object') throw new Error('Unable to map @media query because rules / props are an invalid type');
|
|
30
30
|
guardCloseRuleWrite();
|
|
31
31
|
mediaQueriesBuffer += `${classNameOrCSSRule}{`;
|
|
32
|
-
const { mediaQueriesBuffer: mediaQueriesOutput, sheetBuffer: regularOutput } = execCreateStyles(classNameRules, options, parentSelector);
|
|
32
|
+
const { mediaQueriesBuffer: mediaQueriesOutput, sheetBuffer: regularOutput } = execCreateStyles(ruleId, classNameRules, options, parentSelector);
|
|
33
33
|
mediaQueriesBuffer += regularOutput;
|
|
34
34
|
mediaQueriesBuffer += '}';
|
|
35
35
|
mediaQueriesBuffer += mediaQueriesOutput;
|
|
@@ -39,17 +39,17 @@ function execCreateStyles(rules, options, parentSelector, noGenerateClassName =
|
|
|
39
39
|
// format of { '& > span': { display: 'none' } } (or further nesting)
|
|
40
40
|
const replaced = classNameOrCSSRule.replaceAll('&', parentSelector);
|
|
41
41
|
for (const selector of replaced.split(/,\s*/)){
|
|
42
|
-
const { mediaQueriesBuffer: mediaQueriesOutput, sheetBuffer: regularOutput } = execCreateStyles(classNameRules, options, selector);
|
|
42
|
+
const { mediaQueriesBuffer: mediaQueriesOutput, sheetBuffer: regularOutput } = execCreateStyles(ruleId, classNameRules, options, selector);
|
|
43
43
|
sheetBuffer += regularOutput;
|
|
44
44
|
mediaQueriesBuffer += mediaQueriesOutput;
|
|
45
45
|
}
|
|
46
46
|
} else if (!parentSelector && typeof classNameRules === 'object') {
|
|
47
47
|
guardCloseRuleWrite();
|
|
48
|
-
const generated = noGenerateClassName ? classNameOrCSSRule : generateClassName(classNameOrCSSRule);
|
|
48
|
+
const generated = noGenerateClassName ? classNameOrCSSRule : generateClassName(`${ruleId}_${classNameOrCSSRule}`);
|
|
49
49
|
// @ts-expect-error - yes, we can index this object here, so be quiet
|
|
50
50
|
out[classNameOrCSSRule] = generated;
|
|
51
51
|
const generatedSelector = `${noGenerateClassName ? '' : '.'}${generated}`;
|
|
52
|
-
const { mediaQueriesBuffer: mediaQueriesOutput, sheetBuffer: regularOutput } = execCreateStyles(classNameRules, options, generatedSelector);
|
|
52
|
+
const { mediaQueriesBuffer: mediaQueriesOutput, sheetBuffer: regularOutput } = execCreateStyles(ruleId, classNameRules, options, generatedSelector);
|
|
53
53
|
sheetBuffer += regularOutput;
|
|
54
54
|
mediaQueriesBuffer += mediaQueriesOutput;
|
|
55
55
|
} else {
|
|
@@ -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,34 +116,34 @@ 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
|
-
const { sheetBuffer: sheetContents, mediaQueriesBuffer: mediaQueriesContents } = execCreateStyles(rules, coerced, null, true);
|
|
121
|
+
const { sheetBuffer: sheetContents, mediaQueriesBuffer: mediaQueriesContents } = execCreateStyles(ruleId, 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
|
-
const keyframeName = generateClassName(
|
|
128
|
-
const { sheetBuffer: keyframesContents } = execCreateStyles(frames, coerced, null, true);
|
|
128
|
+
const keyframeName = generateClassName(`${ruleId}_keyframes_`);
|
|
129
|
+
const { sheetBuffer: keyframesContents } = execCreateStyles(ruleId, 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
|
-
const { classes: out, sheetBuffer: sheetContents, mediaQueriesBuffer: mediaQueriesContents } = execCreateStyles(rules, coerced, null);
|
|
146
|
+
const { classes: out, sheetBuffer: sheetContents, mediaQueriesBuffer: mediaQueriesContents } = execCreateStyles(ruleId, rules, coerced, null);
|
|
146
147
|
const mergedContents = `${sheetContents}${mediaQueriesContents}`;
|
|
147
148
|
const replacedSheetContents = replaceBackReferences(out, mergedContents);
|
|
148
149
|
let sheet = null;
|
|
@@ -150,7 +151,7 @@ export default function createStyles(rules, options) {
|
|
|
150
151
|
const updateSheet = (updatedRules)=>{
|
|
151
152
|
if ((options?.flush || options?.registry) && sheet || !options?.flush) {
|
|
152
153
|
// We prefer the first set, and then we shallow merge
|
|
153
|
-
const { classes: updatedOut, sheetBuffer: updatedSheetContents, mediaQueriesBuffer: updatedMediaQueriesContents } = execCreateStyles(merge(rules, updatedRules), {
|
|
154
|
+
const { classes: updatedOut, sheetBuffer: updatedSheetContents, mediaQueriesBuffer: updatedMediaQueriesContents } = execCreateStyles(ruleId, merge(rules, updatedRules), {
|
|
154
155
|
flush: false
|
|
155
156
|
}, null);
|
|
156
157
|
const updatedMergedContents = `${updatedSheetContents}${updatedMediaQueriesContents}`;
|
|
@@ -163,8 +164,11 @@ export default function createStyles(rules, options) {
|
|
|
163
164
|
}
|
|
164
165
|
return null;
|
|
165
166
|
};
|
|
166
|
-
if (!options?.registry && coerced.flush)
|
|
167
|
-
|
|
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.
|
|
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
|
-
|
|
12
|
-
|
|
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.
|
|
3
|
+
"version": "3.4.2-alpha.3",
|
|
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.
|
|
43
|
+
"eslint-config-react-yas": "^6.0.11",
|
|
44
44
|
"husky": "^9.1.7",
|
|
45
45
|
"postcss": "^8.5.6",
|
|
46
46
|
"react": ">=18",
|