simplestyle-js 3.3.0 → 3.3.1-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/.coveralls.yml +2 -0
- package/.editorconfig +7 -0
- package/.prettierrc +7 -0
- package/.tool-versions +3 -0
- package/.travis.yml +8 -0
- package/.vscode/launch.json +36 -0
- package/.vscode/settings.json +3 -0
- package/CHANGELOG.md +265 -0
- package/README.md +4 -1
- package/eslint.config.js +3 -0
- package/package.json +134 -34
- package/setup.sh +4 -0
- package/src/createStyles.ts +251 -0
- package/src/generateClassName.ts +43 -0
- package/src/index.ts +6 -0
- package/src/numToAlpha.ts +5 -0
- package/src/plugins.ts +11 -0
- package/src/react/index.ts +1 -0
- package/src/react/useCreateStyles.ts +58 -0
- package/src/types.ts +7 -0
- package/src/util/deepEqual.ts +20 -0
- package/src/util/index.ts +1 -0
- package/test/createStyles.spec.ts +330 -0
- package/test/deepEqual.spec.ts +97 -0
- package/test/generateClassName.spec.ts +48 -0
- package/test/keyframes.spec.ts +19 -0
- package/test/plugins.spec.ts +43 -0
- package/test/react/useCreateStyles.spec.tsx +65 -0
- package/test/updateStyles.spec.ts +41 -0
- package/tsconfig.build.json +8 -0
- package/tsconfig.json +37 -0
- package/vite.config.ts +9 -0
- package/createStyles.d.ts +0 -36
- package/createStyles.js +0 -192
- package/createStyles.js.map +0 -7
- package/generateClassName.d.ts +0 -3
- package/generateClassName.js +0 -47
- package/generateClassName.js.map +0 -7
- package/index.cjs.js +0 -285
- package/index.d.ts +0 -6
- package/index.js +0 -11
- package/index.js.map +0 -7
- package/numToAlpha.d.ts +0 -1
- package/numToAlpha.js +0 -8
- package/numToAlpha.js.map +0 -7
- package/plugins.d.ts +0 -3
- package/plugins.js +0 -12
- package/plugins.js.map +0 -7
- package/react/index.cjs.js +0 -299
- package/react/index.d.ts +0 -1
- package/react/index.js +0 -2
- package/react/index.js.map +0 -7
- package/react/package.json +0 -6
- package/react/useCreateStyles.d.ts +0 -5
- package/react/useCreateStyles.js +0 -40
- package/react/useCreateStyles.js.map +0 -7
- package/types.d.ts +0 -7
- package/types.js +0 -1
- package/types.js.map +0 -7
- package/util/deepEqual.d.ts +0 -1
- package/util/deepEqual.js +0 -22
- package/util/deepEqual.js.map +0 -7
- package/util/index.cjs.js +0 -46
- package/util/index.d.ts +0 -1
- package/util/index.js +0 -2
- package/util/index.js.map +0 -7
- package/util/package.json +0 -6
package/vite.config.ts
ADDED
package/createStyles.d.ts
DELETED
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
import { Properties } from 'csstype';
|
|
2
|
-
import { SimpleStyleRules } from './types';
|
|
3
|
-
export type CreateStylesOptions = Partial<{
|
|
4
|
-
/**
|
|
5
|
-
* If true, automatically renders generated styles
|
|
6
|
-
* to the DOM in an injected <style /> tag
|
|
7
|
-
*/
|
|
8
|
-
flush: boolean;
|
|
9
|
-
/**
|
|
10
|
-
* If set, along with flush: true,
|
|
11
|
-
* will render the injected <style /> after this element
|
|
12
|
-
*/
|
|
13
|
-
insertAfter?: HTMLElement;
|
|
14
|
-
/**
|
|
15
|
-
* If set, along with flush: true,
|
|
16
|
-
* will render the injects <style /> before this element
|
|
17
|
-
*/
|
|
18
|
-
insertBefore?: HTMLElement;
|
|
19
|
-
}>;
|
|
20
|
-
export declare function rawStyles<T extends SimpleStyleRules, K extends keyof T, O extends {
|
|
21
|
-
[key in K]: string;
|
|
22
|
-
}>(rules: T, options?: Partial<CreateStylesOptions>): string;
|
|
23
|
-
export declare function keyframes<T extends {
|
|
24
|
-
[increment: string]: Properties;
|
|
25
|
-
}>(frames: T, options?: CreateStylesOptions): [string, string];
|
|
26
|
-
export default function createStyles<T extends SimpleStyleRules, K extends keyof T, O extends {
|
|
27
|
-
[classKey in K]: string;
|
|
28
|
-
}>(rules: T, options?: Partial<CreateStylesOptions>): {
|
|
29
|
-
classes: O;
|
|
30
|
-
stylesheet: string;
|
|
31
|
-
updateSheet: <T2 extends SimpleStyleRules, K2 extends keyof T2, O2 extends { [classKey in K2]: string; }>(updatedRules: Partial<T2>) => {
|
|
32
|
-
classes: string | number | keyof T2 extends infer T extends keyof T_1 ? { [classKey_1 in T]: string; } : never;
|
|
33
|
-
stylesheet: string;
|
|
34
|
-
} | null;
|
|
35
|
-
};
|
|
36
|
-
export type CreateStylesArgs = Parameters<typeof createStyles>;
|
package/createStyles.js
DELETED
|
@@ -1,192 +0,0 @@
|
|
|
1
|
-
import merge from "deepmerge";
|
|
2
|
-
import { generateClassName } from "./generateClassName";
|
|
3
|
-
import { getPosthooks } from "./plugins";
|
|
4
|
-
function isNestedSelector(r) {
|
|
5
|
-
return /&/g.test(r);
|
|
6
|
-
}
|
|
7
|
-
function isMedia(r) {
|
|
8
|
-
return r.toLowerCase().startsWith("@media");
|
|
9
|
-
}
|
|
10
|
-
function formatCSSRuleName(rule) {
|
|
11
|
-
return rule.replace(/([A-Z])/g, (p1) => `-${p1.toLowerCase()}`);
|
|
12
|
-
}
|
|
13
|
-
function formatCSSRules(cssRules) {
|
|
14
|
-
return Object.entries(cssRules).reduce(
|
|
15
|
-
(prev, [cssProp, cssVal]) => `${prev}${formatCSSRuleName(cssProp)}:${cssVal};`,
|
|
16
|
-
""
|
|
17
|
-
);
|
|
18
|
-
}
|
|
19
|
-
function execCreateStyles(rules, options, parentSelector, noGenerateClassName = false) {
|
|
20
|
-
const out = {};
|
|
21
|
-
let sheetBuffer = "";
|
|
22
|
-
let mediaQueriesBuffer = "";
|
|
23
|
-
const styleEntries = Object.entries(rules);
|
|
24
|
-
let ruleWriteOpen = false;
|
|
25
|
-
const guardCloseRuleWrite = () => {
|
|
26
|
-
if (ruleWriteOpen)
|
|
27
|
-
sheetBuffer += "}";
|
|
28
|
-
ruleWriteOpen = false;
|
|
29
|
-
};
|
|
30
|
-
for (const [classNameOrCSSRule, classNameRules] of styleEntries) {
|
|
31
|
-
if (isMedia(classNameOrCSSRule)) {
|
|
32
|
-
if (typeof classNameRules !== "object")
|
|
33
|
-
throw new Error("Unable to map @media query because rules / props are an invalid type");
|
|
34
|
-
guardCloseRuleWrite();
|
|
35
|
-
mediaQueriesBuffer += `${classNameOrCSSRule}{`;
|
|
36
|
-
const { mediaQueriesBuffer: mediaQueriesOutput, sheetBuffer: regularOutput } = execCreateStyles(
|
|
37
|
-
classNameRules,
|
|
38
|
-
options,
|
|
39
|
-
parentSelector
|
|
40
|
-
);
|
|
41
|
-
mediaQueriesBuffer += regularOutput;
|
|
42
|
-
mediaQueriesBuffer += "}";
|
|
43
|
-
mediaQueriesBuffer += mediaQueriesOutput;
|
|
44
|
-
} else if (isNestedSelector(classNameOrCSSRule)) {
|
|
45
|
-
if (!parentSelector)
|
|
46
|
-
throw new Error("Unable to generate nested rule because parentSelector is missing");
|
|
47
|
-
guardCloseRuleWrite();
|
|
48
|
-
const replaced = classNameOrCSSRule.replace(/&/g, parentSelector);
|
|
49
|
-
for (const selector of replaced.split(/,\s*/)) {
|
|
50
|
-
const { mediaQueriesBuffer: mediaQueriesOutput, sheetBuffer: regularOutput } = execCreateStyles(
|
|
51
|
-
classNameRules,
|
|
52
|
-
options,
|
|
53
|
-
selector
|
|
54
|
-
);
|
|
55
|
-
sheetBuffer += regularOutput;
|
|
56
|
-
mediaQueriesBuffer += mediaQueriesOutput;
|
|
57
|
-
}
|
|
58
|
-
} else if (!parentSelector && typeof classNameRules === "object") {
|
|
59
|
-
guardCloseRuleWrite();
|
|
60
|
-
const generated = noGenerateClassName ? classNameOrCSSRule : generateClassName(classNameOrCSSRule);
|
|
61
|
-
out[classNameOrCSSRule] = generated;
|
|
62
|
-
const generatedSelector = `${noGenerateClassName ? "" : "."}${generated}`;
|
|
63
|
-
const { mediaQueriesBuffer: mediaQueriesOutput, sheetBuffer: regularOutput } = execCreateStyles(
|
|
64
|
-
classNameRules,
|
|
65
|
-
options,
|
|
66
|
-
generatedSelector
|
|
67
|
-
);
|
|
68
|
-
sheetBuffer += regularOutput;
|
|
69
|
-
mediaQueriesBuffer += mediaQueriesOutput;
|
|
70
|
-
} else {
|
|
71
|
-
if (!parentSelector)
|
|
72
|
-
throw new Error("Unable to write css props because parent selector is null");
|
|
73
|
-
if (!ruleWriteOpen) {
|
|
74
|
-
sheetBuffer += `${parentSelector}{${formatCSSRules({ [classNameOrCSSRule]: classNameRules })}`;
|
|
75
|
-
ruleWriteOpen = true;
|
|
76
|
-
} else
|
|
77
|
-
sheetBuffer += formatCSSRules({ [classNameOrCSSRule]: classNameRules });
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
guardCloseRuleWrite();
|
|
81
|
-
return {
|
|
82
|
-
classes: out,
|
|
83
|
-
sheetBuffer,
|
|
84
|
-
mediaQueriesBuffer
|
|
85
|
-
};
|
|
86
|
-
}
|
|
87
|
-
function replaceBackReferences(out, sheetContents) {
|
|
88
|
-
let outputSheetContents = sheetContents;
|
|
89
|
-
const toReplace = [];
|
|
90
|
-
const toReplaceRegex = /\$\w([a-zA-Z0-9_-]+)?/gm;
|
|
91
|
-
let matches = toReplaceRegex.exec(outputSheetContents);
|
|
92
|
-
while (matches) {
|
|
93
|
-
toReplace.push(matches[0].valueOf());
|
|
94
|
-
matches = toReplaceRegex.exec(outputSheetContents);
|
|
95
|
-
}
|
|
96
|
-
for (const r of toReplace) {
|
|
97
|
-
outputSheetContents = outputSheetContents.replace(r, `.${out[r.substring(1)]}`);
|
|
98
|
-
}
|
|
99
|
-
return getPosthooks().reduce((prev, hook) => hook(prev), outputSheetContents);
|
|
100
|
-
}
|
|
101
|
-
function createSheet(sheetContents) {
|
|
102
|
-
var _a;
|
|
103
|
-
if (typeof document === "undefined")
|
|
104
|
-
return null;
|
|
105
|
-
if (typeof ((_a = document == null ? void 0 : document.head) == null ? void 0 : _a.appendChild) !== "function" || typeof (document == null ? void 0 : document.createElement) !== "function")
|
|
106
|
-
return null;
|
|
107
|
-
const styleTag = document.createElement("style");
|
|
108
|
-
styleTag.innerHTML = sheetContents;
|
|
109
|
-
return styleTag;
|
|
110
|
-
}
|
|
111
|
-
function flushSheetContents(sheetContents, options) {
|
|
112
|
-
var _a, _b;
|
|
113
|
-
const styleTag = createSheet(sheetContents);
|
|
114
|
-
if (styleTag) {
|
|
115
|
-
if ((options == null ? void 0 : options.insertAfter) && (options == null ? void 0 : options.insertBefore)) {
|
|
116
|
-
throw new Error("Both insertAfter and insertBefore were provided. Please choose only one.");
|
|
117
|
-
}
|
|
118
|
-
if ((_a = options == null ? void 0 : options.insertAfter) == null ? void 0 : _a.after)
|
|
119
|
-
options.insertAfter.after(styleTag);
|
|
120
|
-
else if ((_b = options == null ? void 0 : options.insertBefore) == null ? void 0 : _b.before)
|
|
121
|
-
options.insertBefore.before(styleTag);
|
|
122
|
-
else
|
|
123
|
-
document.head.appendChild(styleTag);
|
|
124
|
-
}
|
|
125
|
-
return styleTag;
|
|
126
|
-
}
|
|
127
|
-
function coerceCreateStylesOptions(options) {
|
|
128
|
-
return {
|
|
129
|
-
flush: options && typeof options.flush === "boolean" ? options.flush : true
|
|
130
|
-
};
|
|
131
|
-
}
|
|
132
|
-
function rawStyles(rules, options) {
|
|
133
|
-
const coerced = coerceCreateStylesOptions(options);
|
|
134
|
-
const { sheetBuffer: sheetContents, mediaQueriesBuffer: mediaQueriesContents } = execCreateStyles(
|
|
135
|
-
rules,
|
|
136
|
-
coerced,
|
|
137
|
-
null,
|
|
138
|
-
true
|
|
139
|
-
);
|
|
140
|
-
const mergedContents = `${sheetContents}${mediaQueriesContents}`;
|
|
141
|
-
if (coerced.flush)
|
|
142
|
-
flushSheetContents(mergedContents, options);
|
|
143
|
-
return mergedContents;
|
|
144
|
-
}
|
|
145
|
-
function keyframes(frames, options) {
|
|
146
|
-
const coerced = coerceCreateStylesOptions(options);
|
|
147
|
-
const keyframeName = generateClassName("keyframes_");
|
|
148
|
-
const { sheetBuffer: keyframesContents } = execCreateStyles(frames, coerced, null, true);
|
|
149
|
-
const sheetContents = `@keyframes ${keyframeName}{${keyframesContents}}`;
|
|
150
|
-
if (coerced.flush)
|
|
151
|
-
flushSheetContents(sheetContents);
|
|
152
|
-
return [keyframeName, sheetContents];
|
|
153
|
-
}
|
|
154
|
-
function createStyles(rules, options) {
|
|
155
|
-
const coerced = coerceCreateStylesOptions(options);
|
|
156
|
-
const {
|
|
157
|
-
classes: out,
|
|
158
|
-
sheetBuffer: sheetContents,
|
|
159
|
-
mediaQueriesBuffer: mediaQueriesContents
|
|
160
|
-
} = execCreateStyles(rules, coerced, null);
|
|
161
|
-
const mergedContents = `${sheetContents}${mediaQueriesContents}`;
|
|
162
|
-
const replacedSheetContents = replaceBackReferences(out, mergedContents);
|
|
163
|
-
let sheet = null;
|
|
164
|
-
const updateSheet = (updatedRules) => {
|
|
165
|
-
if (((options == null ? void 0 : options.flush) && sheet || !(options == null ? void 0 : options.flush)) && updatedRules) {
|
|
166
|
-
const {
|
|
167
|
-
classes: updatedOut,
|
|
168
|
-
sheetBuffer: updatedSheetContents,
|
|
169
|
-
mediaQueriesBuffer: updatedMediaQueriesContents
|
|
170
|
-
} = execCreateStyles(merge(rules, updatedRules), { flush: false }, null);
|
|
171
|
-
const updatedMergedContents = `${updatedSheetContents}${updatedMediaQueriesContents}`;
|
|
172
|
-
const updatedReplacedSheetContents = replaceBackReferences(out, updatedMergedContents);
|
|
173
|
-
if (sheet)
|
|
174
|
-
sheet.innerHTML = updatedReplacedSheetContents;
|
|
175
|
-
return { classes: updatedOut, stylesheet: updatedSheetContents };
|
|
176
|
-
}
|
|
177
|
-
return null;
|
|
178
|
-
};
|
|
179
|
-
if (coerced.flush)
|
|
180
|
-
sheet = flushSheetContents(replacedSheetContents, options);
|
|
181
|
-
return {
|
|
182
|
-
classes: out,
|
|
183
|
-
stylesheet: replacedSheetContents,
|
|
184
|
-
updateSheet
|
|
185
|
-
};
|
|
186
|
-
}
|
|
187
|
-
export {
|
|
188
|
-
createStyles as default,
|
|
189
|
-
keyframes,
|
|
190
|
-
rawStyles
|
|
191
|
-
};
|
|
192
|
-
//# sourceMappingURL=createStyles.js.map
|
package/createStyles.js.map
DELETED
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"version": 3,
|
|
3
|
-
"sources": ["../src/createStyles.ts"],
|
|
4
|
-
"sourcesContent": ["import { Properties } from 'csstype';\nimport merge from 'deepmerge';\n\nimport { generateClassName } from './generateClassName';\nimport { getPosthooks } from './plugins';\nimport { SimpleStyleRules } from './types';\n\nexport type CreateStylesOptions = Partial<{\n /**\n * If true, automatically renders generated styles\n * to the DOM in an injected <style /> tag\n */\n flush: boolean;\n\n /**\n * If set, along with flush: true,\n * will render the injected <style /> after this element\n */\n insertAfter?: HTMLElement;\n /**\n * If set, along with flush: true,\n * will render the injects <style /> before this element\n */\n insertBefore?: HTMLElement;\n}>;\n\nfunction isNestedSelector(r: string): boolean {\n return /&/g.test(r);\n}\n\nfunction isMedia(r: string): boolean {\n return r.toLowerCase().startsWith('@media');\n}\n\nfunction formatCSSRuleName(rule: string): string {\n return rule.replace(/([A-Z])/g, p1 => `-${p1.toLowerCase()}`);\n}\n\nfunction formatCSSRules(cssRules: Properties): string {\n return Object.entries(cssRules).reduce(\n (prev, [cssProp, cssVal]) => `${prev}${formatCSSRuleName(cssProp)}:${cssVal};`,\n '',\n );\n}\n\nfunction execCreateStyles<T extends SimpleStyleRules, K extends keyof T, O extends { [classKey in K]: string }>(\n rules: T,\n options: CreateStylesOptions,\n parentSelector: string | null,\n noGenerateClassName: boolean = false,\n): { classes: O; sheetBuffer: string; mediaQueriesBuffer: string } {\n const out = {} as O;\n let sheetBuffer = '';\n let mediaQueriesBuffer = '';\n const styleEntries = Object.entries(rules);\n let ruleWriteOpen = false;\n const guardCloseRuleWrite = () => {\n if (ruleWriteOpen) sheetBuffer += '}';\n ruleWriteOpen = false;\n };\n for (const [classNameOrCSSRule, classNameRules] of styleEntries) {\n // if the classNameRules is a string, we are dealing with a display: none; type rule\n if (isMedia(classNameOrCSSRule)) {\n if (typeof classNameRules !== 'object')\n throw new Error('Unable to map @media query because rules / props are an invalid type');\n guardCloseRuleWrite();\n mediaQueriesBuffer += `${classNameOrCSSRule}{`;\n const { mediaQueriesBuffer: mediaQueriesOutput, sheetBuffer: regularOutput } = execCreateStyles(\n classNameRules as T,\n options,\n parentSelector,\n );\n mediaQueriesBuffer += regularOutput;\n mediaQueriesBuffer += '}';\n mediaQueriesBuffer += mediaQueriesOutput;\n } else if (isNestedSelector(classNameOrCSSRule)) {\n if (!parentSelector) throw new Error('Unable to generate nested rule because parentSelector is missing');\n guardCloseRuleWrite();\n // format of { '& > span': { display: 'none' } } (or further nesting)\n const replaced = classNameOrCSSRule.replace(/&/g, parentSelector);\n for (const selector of replaced.split(/,\\s*/)) {\n const { mediaQueriesBuffer: mediaQueriesOutput, sheetBuffer: regularOutput } = execCreateStyles(\n classNameRules as T,\n options,\n selector,\n );\n sheetBuffer += regularOutput;\n mediaQueriesBuffer += mediaQueriesOutput;\n }\n } else if (!parentSelector && typeof classNameRules === 'object') {\n guardCloseRuleWrite();\n const generated = noGenerateClassName ? classNameOrCSSRule : generateClassName(classNameOrCSSRule);\n (out as any)[classNameOrCSSRule] = generated;\n const generatedSelector = `${noGenerateClassName ? '' : '.'}${generated}`;\n const { mediaQueriesBuffer: mediaQueriesOutput, sheetBuffer: regularOutput } = execCreateStyles(\n classNameRules as T,\n options,\n generatedSelector,\n );\n sheetBuffer += regularOutput;\n mediaQueriesBuffer += mediaQueriesOutput;\n } else {\n if (!parentSelector) throw new Error('Unable to write css props because parent selector is null');\n if (!ruleWriteOpen) {\n sheetBuffer += `${parentSelector}{${formatCSSRules({ [classNameOrCSSRule]: classNameRules })}`;\n ruleWriteOpen = true;\n } else sheetBuffer += formatCSSRules({ [classNameOrCSSRule]: classNameRules });\n }\n }\n guardCloseRuleWrite();\n return {\n classes: out,\n sheetBuffer,\n mediaQueriesBuffer,\n };\n}\n\nfunction replaceBackReferences<O extends { [key: string]: string }>(out: O, sheetContents: string): string {\n let outputSheetContents = sheetContents;\n const toReplace: string[] = [];\n const toReplaceRegex = /\\$\\w([a-zA-Z0-9_-]+)?/gm;\n let matches = toReplaceRegex.exec(outputSheetContents);\n while (matches) {\n toReplace.push(matches[0].valueOf());\n matches = toReplaceRegex.exec(outputSheetContents);\n }\n for (const r of toReplace) {\n outputSheetContents = outputSheetContents.replace(r, `.${out[r.substring(1)]}`);\n }\n return getPosthooks().reduce((prev, hook) => hook(prev), outputSheetContents);\n}\n\nfunction createSheet(sheetContents: string) {\n if (typeof document === 'undefined') return null;\n if (typeof document?.head?.appendChild !== 'function' || typeof document?.createElement !== 'function') return null;\n const styleTag = document.createElement('style');\n styleTag.innerHTML = sheetContents;\n return styleTag;\n}\n\nfunction flushSheetContents(sheetContents: string, options?: CreateStylesOptions) {\n // In case we're in come weird test environment that doesn't support JSDom\n const styleTag = createSheet(sheetContents);\n if (styleTag) {\n if (options?.insertAfter && options?.insertBefore) {\n throw new Error('Both insertAfter and insertBefore were provided. Please choose only one.');\n }\n if (options?.insertAfter?.after) options.insertAfter.after(styleTag as Node);\n else if (options?.insertBefore?.before) options.insertBefore.before(styleTag as Node);\n else document.head.appendChild(styleTag);\n }\n return styleTag;\n}\n\nfunction coerceCreateStylesOptions(options?: CreateStylesOptions): CreateStylesOptions {\n return {\n flush: options && typeof options.flush === 'boolean' ? options.flush : true,\n };\n}\n\n// eslint-disable-next-line @typescript-eslint/no-unused-vars\nexport function rawStyles<T extends SimpleStyleRules, K extends keyof T, O extends { [key in K]: string }>(\n rules: T,\n options?: Partial<CreateStylesOptions>,\n) {\n const coerced = coerceCreateStylesOptions(options);\n const { sheetBuffer: sheetContents, mediaQueriesBuffer: mediaQueriesContents } = execCreateStyles(\n rules,\n coerced,\n null,\n true,\n );\n\n const mergedContents = `${sheetContents}${mediaQueriesContents}`;\n\n if (coerced.flush) flushSheetContents(mergedContents, options);\n return mergedContents;\n}\n\nexport function keyframes<T extends { [increment: string]: Properties }>(\n frames: T,\n options?: CreateStylesOptions,\n): [string, string] {\n const coerced = coerceCreateStylesOptions(options);\n const keyframeName = generateClassName('keyframes_');\n const { sheetBuffer: keyframesContents } = execCreateStyles(frames, coerced, null, true);\n const sheetContents = `@keyframes ${keyframeName}{${keyframesContents}}`;\n if (coerced.flush) flushSheetContents(sheetContents);\n return [keyframeName, sheetContents];\n}\n\nexport default function createStyles<\n T extends SimpleStyleRules,\n K extends keyof T,\n O extends { [classKey in K]: string },\n>(rules: T, options?: Partial<CreateStylesOptions>) {\n const coerced = coerceCreateStylesOptions(options);\n const {\n classes: out,\n sheetBuffer: sheetContents,\n mediaQueriesBuffer: mediaQueriesContents,\n } = execCreateStyles(rules, coerced, null);\n\n const mergedContents = `${sheetContents}${mediaQueriesContents}`;\n\n const replacedSheetContents = replaceBackReferences(out, mergedContents);\n\n let sheet: ReturnType<typeof flushSheetContents> = null;\n\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n const updateSheet = <T2 extends SimpleStyleRules, K2 extends keyof T2, O2 extends { [classKey in K2]: string }>(\n updatedRules: Partial<T2>,\n ) => {\n if (((options?.flush && sheet) || !options?.flush) && updatedRules) {\n // We prefer the first set, and then we shallow merge\n const {\n classes: updatedOut,\n sheetBuffer: updatedSheetContents,\n mediaQueriesBuffer: updatedMediaQueriesContents,\n } = execCreateStyles(merge(rules, updatedRules), { flush: false }, null);\n\n const updatedMergedContents = `${updatedSheetContents}${updatedMediaQueriesContents}`;\n\n const updatedReplacedSheetContents = replaceBackReferences(out, updatedMergedContents);\n if (sheet) sheet.innerHTML = updatedReplacedSheetContents;\n return { classes: updatedOut, stylesheet: updatedSheetContents } as {\n classes: typeof updatedOut;\n stylesheet: string;\n };\n }\n return null;\n };\n\n if (coerced.flush) sheet = flushSheetContents(replacedSheetContents, options);\n // Need this TS cast to get solid code assist from the consumption-side\n return {\n classes: out as unknown,\n stylesheet: replacedSheetContents,\n updateSheet,\n } as {\n classes: O;\n stylesheet: string;\n updateSheet: typeof updateSheet;\n };\n}\n\nexport type CreateStylesArgs = Parameters<typeof createStyles>;\n"],
|
|
5
|
-
"m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B;AACpD,MAAI,QAAQ;AAAO,uBAAmB,aAAa;AACnD,SAAO,CAAC,cAAc,aAAa;AACrC;AAEe,SAAR,aAIL,OAAU,SAAwC;AAClD,QAAM,UAAU,0BAA0B,OAAO;AACjD,QAAM;AAAA,IACJ,SAAS;AAAA,IACT,aAAa;AAAA,IACb,oBAAoB;AAAA,EACtB,IAAI,iBAAiB,OAAO,SAAS,IAAI;AAEzC,QAAM,iBAAiB,GAAG,gBAAgB;AAE1C,QAAM,wBAAwB,sBAAsB,KAAK,cAAc;AAEvE,MAAI,QAA+C;AAGnD,QAAM,cAAc,CAClB,iBACG;AACH,UAAM,mCAAS,UAAS,SAAU,EAAC,mCAAS,WAAU,cAAc;AAElE,YAAM;AAAA,QACJ,SAAS;AAAA,QACT,aAAa;AAAA,QACb,oBAAoB;AAAA,MACtB,IAAI,iBAAiB,MAAM,OAAO,YAAY,GAAG,EAAE,OAAO,MAAM,GAAG,IAAI;AAEvE,YAAM,wBAAwB,GAAG,uBAAuB;AAExD,YAAM,+BAA+B,sBAAsB,KAAK,qBAAqB;AACrF,UAAI;AAAO,cAAM,YAAY;AAC7B,aAAO,EAAE,SAAS,YAAY,YAAY,qBAAqB;AAAA,IAIjE;AACA,WAAO;AAAA,EACT;AAEA,MAAI,QAAQ;AAAO,YAAQ,mBAAmB,uBAAuB,OAAO;AAE5E,SAAO;AAAA,IACL,SAAS;AAAA,IACT,YAAY;AAAA,IACZ;AAAA,EACF;AAKF;",
|
|
6
|
-
"names": []
|
|
7
|
-
}
|
package/generateClassName.d.ts
DELETED
package/generateClassName.js
DELETED
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
import numToAlpha from "./numToAlpha";
|
|
2
|
-
let inc = Date.now();
|
|
3
|
-
function setSeed(seed) {
|
|
4
|
-
if (seed === null) {
|
|
5
|
-
inc = Date.now();
|
|
6
|
-
return;
|
|
7
|
-
}
|
|
8
|
-
if (typeof seed !== "number")
|
|
9
|
-
throw Error("Unable to setSeed as provided seed was not a valid number");
|
|
10
|
-
if (seed === Number.MAX_SAFE_INTEGER)
|
|
11
|
-
throw Error("Unable to setSeed because the seed was already the maximum safe JavaScript number allowed");
|
|
12
|
-
if (seed === Number.POSITIVE_INFINITY || seed === Number.NEGATIVE_INFINITY)
|
|
13
|
-
throw new Error("Unable to setSeed. Positive or negative infinity is not allowed");
|
|
14
|
-
if (seed < 0)
|
|
15
|
-
throw new Error("Unable to setSeed. Seed must be a number >= 0");
|
|
16
|
-
inc = seed;
|
|
17
|
-
}
|
|
18
|
-
const numPairsRegex = /(\d{1,2})/g;
|
|
19
|
-
function getUniqueSuffix() {
|
|
20
|
-
const numPairs = [];
|
|
21
|
-
const incStr = inc.toString();
|
|
22
|
-
let result = numPairsRegex.exec(incStr);
|
|
23
|
-
while (result) {
|
|
24
|
-
numPairs.push(result[0]);
|
|
25
|
-
result = numPairsRegex.exec(incStr);
|
|
26
|
-
}
|
|
27
|
-
let out = "_";
|
|
28
|
-
numPairs.forEach((pair) => {
|
|
29
|
-
const val = +pair;
|
|
30
|
-
if (val > 25) {
|
|
31
|
-
const [first, second] = pair.split("");
|
|
32
|
-
out += `${numToAlpha(Number(first))}${numToAlpha(Number(second))}`;
|
|
33
|
-
} else
|
|
34
|
-
out += numToAlpha(val);
|
|
35
|
-
});
|
|
36
|
-
inc += 1;
|
|
37
|
-
return out;
|
|
38
|
-
}
|
|
39
|
-
function generateClassName(c) {
|
|
40
|
-
return `${c}${getUniqueSuffix()}`;
|
|
41
|
-
}
|
|
42
|
-
export {
|
|
43
|
-
generateClassName,
|
|
44
|
-
getUniqueSuffix,
|
|
45
|
-
setSeed
|
|
46
|
-
};
|
|
47
|
-
//# sourceMappingURL=generateClassName.js.map
|
package/generateClassName.js.map
DELETED
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"version": 3,
|
|
3
|
-
"sources": ["../src/generateClassName.ts"],
|
|
4
|
-
"sourcesContent": ["import numToAlpha from './numToAlpha';\n\nlet inc = Date.now();\n\nexport function setSeed(seed: number | null): void {\n if (seed === null) {\n inc = Date.now();\n return;\n }\n if (typeof seed !== 'number') throw Error('Unable to setSeed as provided seed was not a valid number');\n if (seed === Number.MAX_SAFE_INTEGER)\n throw Error('Unable to setSeed because the seed was already the maximum safe JavaScript number allowed');\n if (seed === Number.POSITIVE_INFINITY || seed === Number.NEGATIVE_INFINITY)\n throw new Error('Unable to setSeed. Positive or negative infinity is not allowed');\n if (seed < 0) throw new Error('Unable to setSeed. Seed must be a number >= 0');\n inc = seed;\n}\n\nconst numPairsRegex = /(\\d{1,2})/g;\n\nexport function getUniqueSuffix(): string {\n const numPairs: string[] = [];\n const incStr = inc.toString();\n let result = numPairsRegex.exec(incStr);\n while (result) {\n numPairs.push(result[0]);\n result = numPairsRegex.exec(incStr);\n }\n let out = '_';\n numPairs.forEach(pair => {\n const val = +pair;\n if (val > 25) {\n const [first, second] = pair.split('');\n out += `${numToAlpha(Number(first))}${numToAlpha(Number(second))}`;\n } else out += numToAlpha(val);\n });\n inc += 1;\n return out;\n}\n\nexport function generateClassName(c: string): string {\n return `${c}${getUniqueSuffix()}`;\n}\n"],
|
|
5
|
-
"mappings": "AAAA,OAAO,gBAAgB;AAEvB,IAAI,MAAM,KAAK,IAAI;AAEZ,SAAS,QAAQ,MAA2B;AACjD,MAAI,SAAS,MAAM;AACjB,UAAM,KAAK,IAAI;AACf;AAAA,EACF;AACA,MAAI,OAAO,SAAS;AAAU,UAAM,MAAM,2DAA2D;AACrG,MAAI,SAAS,OAAO;AAClB,UAAM,MAAM,2FAA2F;AACzG,MAAI,SAAS,OAAO,qBAAqB,SAAS,OAAO;AACvD,UAAM,IAAI,MAAM,iEAAiE;AACnF,MAAI,OAAO;AAAG,UAAM,IAAI,MAAM,+CAA+C;AAC7E,QAAM;AACR;AAEA,MAAM,gBAAgB;AAEf,SAAS,kBAA0B;AACxC,QAAM,WAAqB,CAAC;AAC5B,QAAM,SAAS,IAAI,SAAS;AAC5B,MAAI,SAAS,cAAc,KAAK,MAAM;AACtC,SAAO,QAAQ;AACb,aAAS,KAAK,OAAO,CAAC,CAAC;AACvB,aAAS,cAAc,KAAK,MAAM;AAAA,EACpC;AACA,MAAI,MAAM;AACV,WAAS,QAAQ,UAAQ;AACvB,UAAM,MAAM,CAAC;AACb,QAAI,MAAM,IAAI;AACZ,YAAM,CAAC,OAAO,MAAM,IAAI,KAAK,MAAM,EAAE;AACrC,aAAO,GAAG,WAAW,OAAO,KAAK,CAAC,IAAI,WAAW,OAAO,MAAM,CAAC;AAAA,IACjE;AAAO,aAAO,WAAW,GAAG;AAAA,EAC9B,CAAC;AACD,SAAO;AACP,SAAO;AACT;AAEO,SAAS,kBAAkB,GAAmB;AACnD,SAAO,GAAG,IAAI,gBAAgB;AAChC;",
|
|
6
|
-
"names": []
|
|
7
|
-
}
|
package/index.cjs.js
DELETED
|
@@ -1,285 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __create = Object.create;
|
|
3
|
-
var __defProp = Object.defineProperty;
|
|
4
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
-
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
-
var __export = (target, all) => {
|
|
9
|
-
for (var name in all)
|
|
10
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
-
};
|
|
12
|
-
var __copyProps = (to, from, except, desc) => {
|
|
13
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
-
for (let key of __getOwnPropNames(from))
|
|
15
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
-
}
|
|
18
|
-
return to;
|
|
19
|
-
};
|
|
20
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
-
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
-
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
-
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
-
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
-
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
-
mod
|
|
27
|
-
));
|
|
28
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
-
|
|
30
|
-
// src/index.ts
|
|
31
|
-
var src_exports = {};
|
|
32
|
-
__export(src_exports, {
|
|
33
|
-
createStyles: () => createStyles,
|
|
34
|
-
keyframes: () => keyframes,
|
|
35
|
-
rawStyles: () => rawStyles,
|
|
36
|
-
registerPosthook: () => registerPosthook,
|
|
37
|
-
setSeed: () => setSeed
|
|
38
|
-
});
|
|
39
|
-
module.exports = __toCommonJS(src_exports);
|
|
40
|
-
|
|
41
|
-
// src/createStyles.ts
|
|
42
|
-
var import_deepmerge = __toESM(require("deepmerge"));
|
|
43
|
-
|
|
44
|
-
// src/numToAlpha.ts
|
|
45
|
-
var alphas = "abcdefghijklmnopqrstuvwxyz".split("");
|
|
46
|
-
function numToAlpha(num) {
|
|
47
|
-
return String(alphas[num]);
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
// src/generateClassName.ts
|
|
51
|
-
var inc = Date.now();
|
|
52
|
-
function setSeed(seed) {
|
|
53
|
-
if (seed === null) {
|
|
54
|
-
inc = Date.now();
|
|
55
|
-
return;
|
|
56
|
-
}
|
|
57
|
-
if (typeof seed !== "number")
|
|
58
|
-
throw Error("Unable to setSeed as provided seed was not a valid number");
|
|
59
|
-
if (seed === Number.MAX_SAFE_INTEGER)
|
|
60
|
-
throw Error("Unable to setSeed because the seed was already the maximum safe JavaScript number allowed");
|
|
61
|
-
if (seed === Number.POSITIVE_INFINITY || seed === Number.NEGATIVE_INFINITY)
|
|
62
|
-
throw new Error("Unable to setSeed. Positive or negative infinity is not allowed");
|
|
63
|
-
if (seed < 0)
|
|
64
|
-
throw new Error("Unable to setSeed. Seed must be a number >= 0");
|
|
65
|
-
inc = seed;
|
|
66
|
-
}
|
|
67
|
-
var numPairsRegex = /(\d{1,2})/g;
|
|
68
|
-
function getUniqueSuffix() {
|
|
69
|
-
const numPairs = [];
|
|
70
|
-
const incStr = inc.toString();
|
|
71
|
-
let result = numPairsRegex.exec(incStr);
|
|
72
|
-
while (result) {
|
|
73
|
-
numPairs.push(result[0]);
|
|
74
|
-
result = numPairsRegex.exec(incStr);
|
|
75
|
-
}
|
|
76
|
-
let out = "_";
|
|
77
|
-
numPairs.forEach((pair) => {
|
|
78
|
-
const val = +pair;
|
|
79
|
-
if (val > 25) {
|
|
80
|
-
const [first, second] = pair.split("");
|
|
81
|
-
out += `${numToAlpha(Number(first))}${numToAlpha(Number(second))}`;
|
|
82
|
-
} else
|
|
83
|
-
out += numToAlpha(val);
|
|
84
|
-
});
|
|
85
|
-
inc += 1;
|
|
86
|
-
return out;
|
|
87
|
-
}
|
|
88
|
-
function generateClassName(c) {
|
|
89
|
-
return `${c}${getUniqueSuffix()}`;
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
// src/plugins.ts
|
|
93
|
-
var posthooks = [];
|
|
94
|
-
function getPosthooks() {
|
|
95
|
-
return posthooks;
|
|
96
|
-
}
|
|
97
|
-
function registerPosthook(posthook) {
|
|
98
|
-
posthooks.push(posthook);
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
// src/createStyles.ts
|
|
102
|
-
function isNestedSelector(r) {
|
|
103
|
-
return /&/g.test(r);
|
|
104
|
-
}
|
|
105
|
-
function isMedia(r) {
|
|
106
|
-
return r.toLowerCase().startsWith("@media");
|
|
107
|
-
}
|
|
108
|
-
function formatCSSRuleName(rule) {
|
|
109
|
-
return rule.replace(/([A-Z])/g, (p1) => `-${p1.toLowerCase()}`);
|
|
110
|
-
}
|
|
111
|
-
function formatCSSRules(cssRules) {
|
|
112
|
-
return Object.entries(cssRules).reduce(
|
|
113
|
-
(prev, [cssProp, cssVal]) => `${prev}${formatCSSRuleName(cssProp)}:${cssVal};`,
|
|
114
|
-
""
|
|
115
|
-
);
|
|
116
|
-
}
|
|
117
|
-
function execCreateStyles(rules, options, parentSelector, noGenerateClassName = false) {
|
|
118
|
-
const out = {};
|
|
119
|
-
let sheetBuffer = "";
|
|
120
|
-
let mediaQueriesBuffer = "";
|
|
121
|
-
const styleEntries = Object.entries(rules);
|
|
122
|
-
let ruleWriteOpen = false;
|
|
123
|
-
const guardCloseRuleWrite = () => {
|
|
124
|
-
if (ruleWriteOpen)
|
|
125
|
-
sheetBuffer += "}";
|
|
126
|
-
ruleWriteOpen = false;
|
|
127
|
-
};
|
|
128
|
-
for (const [classNameOrCSSRule, classNameRules] of styleEntries) {
|
|
129
|
-
if (isMedia(classNameOrCSSRule)) {
|
|
130
|
-
if (typeof classNameRules !== "object")
|
|
131
|
-
throw new Error("Unable to map @media query because rules / props are an invalid type");
|
|
132
|
-
guardCloseRuleWrite();
|
|
133
|
-
mediaQueriesBuffer += `${classNameOrCSSRule}{`;
|
|
134
|
-
const { mediaQueriesBuffer: mediaQueriesOutput, sheetBuffer: regularOutput } = execCreateStyles(
|
|
135
|
-
classNameRules,
|
|
136
|
-
options,
|
|
137
|
-
parentSelector
|
|
138
|
-
);
|
|
139
|
-
mediaQueriesBuffer += regularOutput;
|
|
140
|
-
mediaQueriesBuffer += "}";
|
|
141
|
-
mediaQueriesBuffer += mediaQueriesOutput;
|
|
142
|
-
} else if (isNestedSelector(classNameOrCSSRule)) {
|
|
143
|
-
if (!parentSelector)
|
|
144
|
-
throw new Error("Unable to generate nested rule because parentSelector is missing");
|
|
145
|
-
guardCloseRuleWrite();
|
|
146
|
-
const replaced = classNameOrCSSRule.replace(/&/g, parentSelector);
|
|
147
|
-
for (const selector of replaced.split(/,\s*/)) {
|
|
148
|
-
const { mediaQueriesBuffer: mediaQueriesOutput, sheetBuffer: regularOutput } = execCreateStyles(
|
|
149
|
-
classNameRules,
|
|
150
|
-
options,
|
|
151
|
-
selector
|
|
152
|
-
);
|
|
153
|
-
sheetBuffer += regularOutput;
|
|
154
|
-
mediaQueriesBuffer += mediaQueriesOutput;
|
|
155
|
-
}
|
|
156
|
-
} else if (!parentSelector && typeof classNameRules === "object") {
|
|
157
|
-
guardCloseRuleWrite();
|
|
158
|
-
const generated = noGenerateClassName ? classNameOrCSSRule : generateClassName(classNameOrCSSRule);
|
|
159
|
-
out[classNameOrCSSRule] = generated;
|
|
160
|
-
const generatedSelector = `${noGenerateClassName ? "" : "."}${generated}`;
|
|
161
|
-
const { mediaQueriesBuffer: mediaQueriesOutput, sheetBuffer: regularOutput } = execCreateStyles(
|
|
162
|
-
classNameRules,
|
|
163
|
-
options,
|
|
164
|
-
generatedSelector
|
|
165
|
-
);
|
|
166
|
-
sheetBuffer += regularOutput;
|
|
167
|
-
mediaQueriesBuffer += mediaQueriesOutput;
|
|
168
|
-
} else {
|
|
169
|
-
if (!parentSelector)
|
|
170
|
-
throw new Error("Unable to write css props because parent selector is null");
|
|
171
|
-
if (!ruleWriteOpen) {
|
|
172
|
-
sheetBuffer += `${parentSelector}{${formatCSSRules({ [classNameOrCSSRule]: classNameRules })}`;
|
|
173
|
-
ruleWriteOpen = true;
|
|
174
|
-
} else
|
|
175
|
-
sheetBuffer += formatCSSRules({ [classNameOrCSSRule]: classNameRules });
|
|
176
|
-
}
|
|
177
|
-
}
|
|
178
|
-
guardCloseRuleWrite();
|
|
179
|
-
return {
|
|
180
|
-
classes: out,
|
|
181
|
-
sheetBuffer,
|
|
182
|
-
mediaQueriesBuffer
|
|
183
|
-
};
|
|
184
|
-
}
|
|
185
|
-
function replaceBackReferences(out, sheetContents) {
|
|
186
|
-
let outputSheetContents = sheetContents;
|
|
187
|
-
const toReplace = [];
|
|
188
|
-
const toReplaceRegex = /\$\w([a-zA-Z0-9_-]+)?/gm;
|
|
189
|
-
let matches = toReplaceRegex.exec(outputSheetContents);
|
|
190
|
-
while (matches) {
|
|
191
|
-
toReplace.push(matches[0].valueOf());
|
|
192
|
-
matches = toReplaceRegex.exec(outputSheetContents);
|
|
193
|
-
}
|
|
194
|
-
for (const r of toReplace) {
|
|
195
|
-
outputSheetContents = outputSheetContents.replace(r, `.${out[r.substring(1)]}`);
|
|
196
|
-
}
|
|
197
|
-
return getPosthooks().reduce((prev, hook) => hook(prev), outputSheetContents);
|
|
198
|
-
}
|
|
199
|
-
function createSheet(sheetContents) {
|
|
200
|
-
var _a;
|
|
201
|
-
if (typeof document === "undefined")
|
|
202
|
-
return null;
|
|
203
|
-
if (typeof ((_a = document == null ? void 0 : document.head) == null ? void 0 : _a.appendChild) !== "function" || typeof (document == null ? void 0 : document.createElement) !== "function")
|
|
204
|
-
return null;
|
|
205
|
-
const styleTag = document.createElement("style");
|
|
206
|
-
styleTag.innerHTML = sheetContents;
|
|
207
|
-
return styleTag;
|
|
208
|
-
}
|
|
209
|
-
function flushSheetContents(sheetContents, options) {
|
|
210
|
-
var _a, _b;
|
|
211
|
-
const styleTag = createSheet(sheetContents);
|
|
212
|
-
if (styleTag) {
|
|
213
|
-
if ((options == null ? void 0 : options.insertAfter) && (options == null ? void 0 : options.insertBefore)) {
|
|
214
|
-
throw new Error("Both insertAfter and insertBefore were provided. Please choose only one.");
|
|
215
|
-
}
|
|
216
|
-
if ((_a = options == null ? void 0 : options.insertAfter) == null ? void 0 : _a.after)
|
|
217
|
-
options.insertAfter.after(styleTag);
|
|
218
|
-
else if ((_b = options == null ? void 0 : options.insertBefore) == null ? void 0 : _b.before)
|
|
219
|
-
options.insertBefore.before(styleTag);
|
|
220
|
-
else
|
|
221
|
-
document.head.appendChild(styleTag);
|
|
222
|
-
}
|
|
223
|
-
return styleTag;
|
|
224
|
-
}
|
|
225
|
-
function coerceCreateStylesOptions(options) {
|
|
226
|
-
return {
|
|
227
|
-
flush: options && typeof options.flush === "boolean" ? options.flush : true
|
|
228
|
-
};
|
|
229
|
-
}
|
|
230
|
-
function rawStyles(rules, options) {
|
|
231
|
-
const coerced = coerceCreateStylesOptions(options);
|
|
232
|
-
const { sheetBuffer: sheetContents, mediaQueriesBuffer: mediaQueriesContents } = execCreateStyles(
|
|
233
|
-
rules,
|
|
234
|
-
coerced,
|
|
235
|
-
null,
|
|
236
|
-
true
|
|
237
|
-
);
|
|
238
|
-
const mergedContents = `${sheetContents}${mediaQueriesContents}`;
|
|
239
|
-
if (coerced.flush)
|
|
240
|
-
flushSheetContents(mergedContents, options);
|
|
241
|
-
return mergedContents;
|
|
242
|
-
}
|
|
243
|
-
function keyframes(frames, options) {
|
|
244
|
-
const coerced = coerceCreateStylesOptions(options);
|
|
245
|
-
const keyframeName = generateClassName("keyframes_");
|
|
246
|
-
const { sheetBuffer: keyframesContents } = execCreateStyles(frames, coerced, null, true);
|
|
247
|
-
const sheetContents = `@keyframes ${keyframeName}{${keyframesContents}}`;
|
|
248
|
-
if (coerced.flush)
|
|
249
|
-
flushSheetContents(sheetContents);
|
|
250
|
-
return [keyframeName, sheetContents];
|
|
251
|
-
}
|
|
252
|
-
function createStyles(rules, options) {
|
|
253
|
-
const coerced = coerceCreateStylesOptions(options);
|
|
254
|
-
const {
|
|
255
|
-
classes: out,
|
|
256
|
-
sheetBuffer: sheetContents,
|
|
257
|
-
mediaQueriesBuffer: mediaQueriesContents
|
|
258
|
-
} = execCreateStyles(rules, coerced, null);
|
|
259
|
-
const mergedContents = `${sheetContents}${mediaQueriesContents}`;
|
|
260
|
-
const replacedSheetContents = replaceBackReferences(out, mergedContents);
|
|
261
|
-
let sheet = null;
|
|
262
|
-
const updateSheet = (updatedRules) => {
|
|
263
|
-
if (((options == null ? void 0 : options.flush) && sheet || !(options == null ? void 0 : options.flush)) && updatedRules) {
|
|
264
|
-
const {
|
|
265
|
-
classes: updatedOut,
|
|
266
|
-
sheetBuffer: updatedSheetContents,
|
|
267
|
-
mediaQueriesBuffer: updatedMediaQueriesContents
|
|
268
|
-
} = execCreateStyles((0, import_deepmerge.default)(rules, updatedRules), { flush: false }, null);
|
|
269
|
-
const updatedMergedContents = `${updatedSheetContents}${updatedMediaQueriesContents}`;
|
|
270
|
-
const updatedReplacedSheetContents = replaceBackReferences(out, updatedMergedContents);
|
|
271
|
-
if (sheet)
|
|
272
|
-
sheet.innerHTML = updatedReplacedSheetContents;
|
|
273
|
-
return { classes: updatedOut, stylesheet: updatedSheetContents };
|
|
274
|
-
}
|
|
275
|
-
return null;
|
|
276
|
-
};
|
|
277
|
-
if (coerced.flush)
|
|
278
|
-
sheet = flushSheetContents(replacedSheetContents, options);
|
|
279
|
-
return {
|
|
280
|
-
classes: out,
|
|
281
|
-
stylesheet: replacedSheetContents,
|
|
282
|
-
updateSheet
|
|
283
|
-
};
|
|
284
|
-
}
|
|
285
|
-
//# sourceMappingURL=index.js.map
|
package/index.d.ts
DELETED
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
export type { CreateStylesArgs, CreateStylesOptions } from './createStyles';
|
|
2
|
-
export { default as createStyles, keyframes, rawStyles } from './createStyles';
|
|
3
|
-
export { setSeed } from './generateClassName';
|
|
4
|
-
export type { PosthookPlugin } from './plugins';
|
|
5
|
-
export { registerPosthook } from './plugins';
|
|
6
|
-
export type { SimpleStyleRules } from './types';
|
package/index.js
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import { default as default2, keyframes, rawStyles } from "./createStyles";
|
|
2
|
-
import { setSeed } from "./generateClassName";
|
|
3
|
-
import { registerPosthook } from "./plugins";
|
|
4
|
-
export {
|
|
5
|
-
default2 as createStyles,
|
|
6
|
-
keyframes,
|
|
7
|
-
rawStyles,
|
|
8
|
-
registerPosthook,
|
|
9
|
-
setSeed
|
|
10
|
-
};
|
|
11
|
-
//# sourceMappingURL=index.js.map
|