simplestyle-js 3.4.0 → 3.4.1

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.
Files changed (70) hide show
  1. package/LICENSE +1 -1
  2. package/dist/cjs/createStyles.cjs +191 -0
  3. package/dist/cjs/createStyles.d.ts +30 -0
  4. package/dist/cjs/generateClassName.cjs +62 -0
  5. package/dist/cjs/generateClassName.d.ts +3 -0
  6. package/dist/cjs/index.cjs +71 -0
  7. package/dist/cjs/numToAlpha.cjs +14 -0
  8. package/dist/cjs/numToAlpha.d.ts +1 -0
  9. package/dist/cjs/package.json +1 -0
  10. package/dist/cjs/plugins.cjs +25 -0
  11. package/dist/cjs/plugins.d.ts +3 -0
  12. package/dist/cjs/react/index.cjs +18 -0
  13. package/dist/cjs/react/useCreateStyles.cjs +73 -0
  14. package/dist/cjs/react/useCreateStyles.d.ts +3 -0
  15. package/dist/cjs/types.cjs +4 -0
  16. package/{src/types.ts → dist/cjs/types.d.ts} +1 -3
  17. package/dist/cjs/util/deepEqual.cjs +27 -0
  18. package/dist/cjs/util/deepEqual.d.ts +1 -0
  19. package/dist/cjs/util/index.cjs +18 -0
  20. package/dist/esm/createStyles.d.ts +30 -0
  21. package/dist/esm/createStyles.mjs +166 -0
  22. package/dist/esm/generateClassName.d.ts +3 -0
  23. package/dist/esm/generateClassName.mjs +36 -0
  24. package/dist/esm/index.d.ts +6 -0
  25. package/dist/esm/index.mjs +3 -0
  26. package/dist/esm/numToAlpha.d.ts +1 -0
  27. package/dist/esm/numToAlpha.mjs +4 -0
  28. package/dist/esm/package.json +1 -0
  29. package/dist/esm/plugins.d.ts +3 -0
  30. package/dist/esm/plugins.mjs +7 -0
  31. package/dist/esm/react/index.d.ts +1 -0
  32. package/dist/esm/react/index.mjs +1 -0
  33. package/dist/esm/react/useCreateStyles.d.ts +3 -0
  34. package/dist/esm/react/useCreateStyles.mjs +58 -0
  35. package/dist/esm/types.d.ts +5 -0
  36. package/dist/esm/types.mjs +1 -0
  37. package/dist/esm/util/deepEqual.d.ts +1 -0
  38. package/dist/esm/util/deepEqual.mjs +17 -0
  39. package/dist/esm/util/index.d.ts +1 -0
  40. package/dist/esm/util/index.mjs +1 -0
  41. package/package.json +4 -1
  42. package/.coveralls.yml +0 -2
  43. package/.editorconfig +0 -7
  44. package/.prettierrc +0 -7
  45. package/.tool-versions +0 -3
  46. package/.travis.yml +0 -8
  47. package/.vscode/launch.json +0 -36
  48. package/.vscode/settings.json +0 -3
  49. package/CHANGELOG.md +0 -273
  50. package/eslint.config.js +0 -3
  51. package/setup.sh +0 -4
  52. package/src/createStyles.ts +0 -251
  53. package/src/generateClassName.ts +0 -43
  54. package/src/numToAlpha.ts +0 -5
  55. package/src/plugins.ts +0 -11
  56. package/src/react/useCreateStyles.ts +0 -58
  57. package/src/util/deepEqual.ts +0 -20
  58. package/test/createStyles.spec.ts +0 -330
  59. package/test/deepEqual.spec.ts +0 -97
  60. package/test/generateClassName.spec.ts +0 -48
  61. package/test/keyframes.spec.ts +0 -19
  62. package/test/plugins.spec.ts +0 -43
  63. package/test/react/useCreateStyles.spec.tsx +0 -65
  64. package/test/updateStyles.spec.ts +0 -41
  65. package/tsconfig.build.json +0 -8
  66. package/tsconfig.json +0 -37
  67. package/vite.config.ts +0 -9
  68. /package/{src/index.ts → dist/cjs/index.d.ts} +0 -0
  69. /package/{src/react/index.ts → dist/cjs/react/index.d.ts} +0 -0
  70. /package/{src/util/index.ts → dist/cjs/util/index.d.ts} +0 -0
package/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2020 Benjamin Duran
3
+ Copyright (c) 2025 Benjamin Duran
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
@@ -0,0 +1,191 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ function _export(target, all) {
6
+ for(var name in all)Object.defineProperty(target, name, {
7
+ enumerable: true,
8
+ get: Object.getOwnPropertyDescriptor(all, name).get
9
+ });
10
+ }
11
+ _export(exports, {
12
+ get default () {
13
+ return createStyles;
14
+ },
15
+ get keyframes () {
16
+ return keyframes;
17
+ },
18
+ get rawStyles () {
19
+ return rawStyles;
20
+ }
21
+ });
22
+ const _deepmerge = /*#__PURE__*/ _interop_require_default(require("deepmerge"));
23
+ const _generateClassName = require("./generateClassName.cjs");
24
+ const _plugins = require("./plugins.cjs");
25
+ function _interop_require_default(obj) {
26
+ return obj && obj.__esModule ? obj : {
27
+ default: obj
28
+ };
29
+ }
30
+ function isNestedSelector(r) {
31
+ return /&/g.test(r);
32
+ }
33
+ function isMedia(r) {
34
+ return r.toLowerCase().startsWith('@media');
35
+ }
36
+ function formatCSSRuleName(rule) {
37
+ return rule.replaceAll(/([A-Z])/g, (p1)=>`-${p1.toLowerCase()}`);
38
+ }
39
+ function formatCSSRules(cssRules) {
40
+ return Object.entries(cssRules).reduce((prev, [cssProp, cssVal])=>`${prev}${formatCSSRuleName(cssProp)}:${String(cssVal)};`, '');
41
+ }
42
+ function execCreateStyles(rules, options, parentSelector, noGenerateClassName = false) {
43
+ const out = {};
44
+ let sheetBuffer = '';
45
+ let mediaQueriesBuffer = '';
46
+ const styleEntries = Object.entries(rules);
47
+ let ruleWriteOpen = false;
48
+ const guardCloseRuleWrite = ()=>{
49
+ if (ruleWriteOpen) sheetBuffer += '}';
50
+ ruleWriteOpen = false;
51
+ };
52
+ for (const [classNameOrCSSRule, classNameRules] of styleEntries){
53
+ // if the classNameRules is a string, we are dealing with a display: none; type rule
54
+ if (isMedia(classNameOrCSSRule)) {
55
+ if (typeof classNameRules !== 'object') throw new Error('Unable to map @media query because rules / props are an invalid type');
56
+ guardCloseRuleWrite();
57
+ mediaQueriesBuffer += `${classNameOrCSSRule}{`;
58
+ const { mediaQueriesBuffer: mediaQueriesOutput, sheetBuffer: regularOutput } = execCreateStyles(classNameRules, options, parentSelector);
59
+ mediaQueriesBuffer += regularOutput;
60
+ mediaQueriesBuffer += '}';
61
+ mediaQueriesBuffer += mediaQueriesOutput;
62
+ } else if (isNestedSelector(classNameOrCSSRule)) {
63
+ if (!parentSelector) throw new Error('Unable to generate nested rule because parentSelector is missing');
64
+ guardCloseRuleWrite();
65
+ // format of { '& > span': { display: 'none' } } (or further nesting)
66
+ const replaced = classNameOrCSSRule.replaceAll('&', parentSelector);
67
+ for (const selector of replaced.split(/,\s*/)){
68
+ const { mediaQueriesBuffer: mediaQueriesOutput, sheetBuffer: regularOutput } = execCreateStyles(classNameRules, options, selector);
69
+ sheetBuffer += regularOutput;
70
+ mediaQueriesBuffer += mediaQueriesOutput;
71
+ }
72
+ } else if (!parentSelector && typeof classNameRules === 'object') {
73
+ guardCloseRuleWrite();
74
+ const generated = noGenerateClassName ? classNameOrCSSRule : (0, _generateClassName.generateClassName)(classNameOrCSSRule);
75
+ // @ts-expect-error - yes, we can index this object here, so be quiet
76
+ out[classNameOrCSSRule] = generated;
77
+ const generatedSelector = `${noGenerateClassName ? '' : '.'}${generated}`;
78
+ const { mediaQueriesBuffer: mediaQueriesOutput, sheetBuffer: regularOutput } = execCreateStyles(classNameRules, options, generatedSelector);
79
+ sheetBuffer += regularOutput;
80
+ mediaQueriesBuffer += mediaQueriesOutput;
81
+ } else {
82
+ if (!parentSelector) throw new Error('Unable to write css props because parent selector is null');
83
+ if (ruleWriteOpen) {
84
+ sheetBuffer += formatCSSRules({
85
+ [classNameOrCSSRule]: classNameRules
86
+ });
87
+ } else {
88
+ sheetBuffer += `${parentSelector}{${formatCSSRules({
89
+ [classNameOrCSSRule]: classNameRules
90
+ })}`;
91
+ ruleWriteOpen = true;
92
+ }
93
+ }
94
+ }
95
+ guardCloseRuleWrite();
96
+ return {
97
+ classes: out,
98
+ sheetBuffer,
99
+ mediaQueriesBuffer
100
+ };
101
+ }
102
+ function replaceBackReferences(out, sheetContents) {
103
+ let outputSheetContents = sheetContents;
104
+ const toReplace = [];
105
+ const toReplaceRegex = /\$\w([a-zA-Z0-9_-]+)?/gm;
106
+ let matches = toReplaceRegex.exec(outputSheetContents);
107
+ while(matches){
108
+ toReplace.push(matches[0].valueOf());
109
+ matches = toReplaceRegex.exec(outputSheetContents);
110
+ }
111
+ for (const r of toReplace){
112
+ outputSheetContents = outputSheetContents.replace(r, `.${out[r.slice(1)] ?? ''}`);
113
+ }
114
+ return (0, _plugins.getPosthooks)().reduce((prev, hook)=>hook(prev), outputSheetContents);
115
+ }
116
+ function createSheet(sheetContents) {
117
+ const doc = globalThis.document;
118
+ if (doc === undefined) return null;
119
+ // eslint-disable-next-line @typescript-eslint/no-deprecated
120
+ if (typeof doc?.head?.appendChild !== 'function' || typeof doc.createElement !== 'function') return null;
121
+ const styleTag = doc.createElement('style');
122
+ styleTag.innerHTML = sheetContents;
123
+ return styleTag;
124
+ }
125
+ function flushSheetContents(sheetContents, options) {
126
+ // In case we're in come weird test environment that doesn't support JSDom
127
+ const styleTag = createSheet(sheetContents);
128
+ if (styleTag) {
129
+ if (options?.insertAfter && options.insertBefore) {
130
+ throw new Error('Both insertAfter and insertBefore were provided. Please choose only one.');
131
+ }
132
+ if (options?.insertAfter?.after) options.insertAfter.after(styleTag);
133
+ else if (options?.insertBefore?.before) options.insertBefore.before(styleTag);
134
+ else document.head.append(styleTag);
135
+ }
136
+ return styleTag;
137
+ }
138
+ function coerceCreateStylesOptions(options) {
139
+ return {
140
+ flush: options && typeof options.flush === 'boolean' ? options.flush : true
141
+ };
142
+ }
143
+ function rawStyles(rules, options) {
144
+ const coerced = coerceCreateStylesOptions(options);
145
+ const { sheetBuffer: sheetContents, mediaQueriesBuffer: mediaQueriesContents } = execCreateStyles(rules, coerced, null, true);
146
+ const mergedContents = `${sheetContents}${mediaQueriesContents}`;
147
+ if (coerced.flush) flushSheetContents(mergedContents, options);
148
+ return mergedContents;
149
+ }
150
+ function keyframes(frames, options) {
151
+ const coerced = coerceCreateStylesOptions(options);
152
+ const keyframeName = (0, _generateClassName.generateClassName)('keyframes_');
153
+ const { sheetBuffer: keyframesContents } = execCreateStyles(frames, coerced, null, true);
154
+ const sheetContents = `@keyframes ${keyframeName}{${keyframesContents}}`;
155
+ if (coerced.flush) flushSheetContents(sheetContents);
156
+ return [
157
+ keyframeName,
158
+ sheetContents
159
+ ];
160
+ }
161
+ function createStyles(rules, options) {
162
+ const coerced = coerceCreateStylesOptions(options);
163
+ const { classes: out, sheetBuffer: sheetContents, mediaQueriesBuffer: mediaQueriesContents } = execCreateStyles(rules, coerced, null);
164
+ const mergedContents = `${sheetContents}${mediaQueriesContents}`;
165
+ const replacedSheetContents = replaceBackReferences(out, mergedContents);
166
+ let sheet = null;
167
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
168
+ const updateSheet = (updatedRules)=>{
169
+ if (options?.flush && sheet || !options?.flush) {
170
+ // We prefer the first set, and then we shallow merge
171
+ const { classes: updatedOut, sheetBuffer: updatedSheetContents, mediaQueriesBuffer: updatedMediaQueriesContents } = execCreateStyles((0, _deepmerge.default)(rules, updatedRules), {
172
+ flush: false
173
+ }, null);
174
+ const updatedMergedContents = `${updatedSheetContents}${updatedMediaQueriesContents}`;
175
+ const updatedReplacedSheetContents = replaceBackReferences(out, updatedMergedContents);
176
+ if (sheet) sheet.innerHTML = updatedReplacedSheetContents;
177
+ return {
178
+ classes: updatedOut,
179
+ stylesheet: updatedSheetContents
180
+ };
181
+ }
182
+ return null;
183
+ };
184
+ if (coerced.flush) sheet = flushSheetContents(replacedSheetContents, options);
185
+ // Need this TS cast to get solid code assist from the consumption-side
186
+ return {
187
+ classes: out,
188
+ stylesheet: replacedSheetContents,
189
+ updateSheet
190
+ };
191
+ }
@@ -0,0 +1,30 @@
1
+ import { Properties } from 'csstype';
2
+ import { SimpleStyleRules } from './types.js';
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 Record<K, string>>(rules: T, options?: Partial<CreateStylesOptions>): string;
21
+ export declare function keyframes<T extends Record<string, Properties>>(frames: T, options?: CreateStylesOptions): [string, string];
22
+ export default function createStyles<T extends SimpleStyleRules, K extends keyof T, O extends Record<K, string>>(rules: T, options?: Partial<CreateStylesOptions>): {
23
+ classes: O;
24
+ stylesheet: string;
25
+ updateSheet: <T2 extends SimpleStyleRules, K2 extends keyof T2, O2 extends Record<K2, string>>(updatedRules: Partial<T2>) => {
26
+ classes: Record<string | number | keyof T2, string>;
27
+ stylesheet: string;
28
+ } | null;
29
+ };
30
+ export type CreateStylesArgs = Parameters<typeof createStyles>;
@@ -0,0 +1,62 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ function _export(target, all) {
6
+ for(var name in all)Object.defineProperty(target, name, {
7
+ enumerable: true,
8
+ get: Object.getOwnPropertyDescriptor(all, name).get
9
+ });
10
+ }
11
+ _export(exports, {
12
+ get generateClassName () {
13
+ return generateClassName;
14
+ },
15
+ get getUniqueSuffix () {
16
+ return getUniqueSuffix;
17
+ },
18
+ get setSeed () {
19
+ return setSeed;
20
+ }
21
+ });
22
+ const _numToAlpha = /*#__PURE__*/ _interop_require_default(require("./numToAlpha.cjs"));
23
+ function _interop_require_default(obj) {
24
+ return obj && obj.__esModule ? obj : {
25
+ default: obj
26
+ };
27
+ }
28
+ let inc = Date.now();
29
+ function setSeed(seed) {
30
+ if (seed === null) {
31
+ inc = Date.now();
32
+ return;
33
+ }
34
+ if (typeof seed !== 'number') throw new Error('Unable to setSeed as provided seed was not a valid number');
35
+ if (seed === Number.MAX_SAFE_INTEGER) throw new Error('Unable to setSeed because the seed was already the maximum safe JavaScript number allowed');
36
+ if (seed === Number.POSITIVE_INFINITY || seed === Number.NEGATIVE_INFINITY) throw new Error('Unable to setSeed. Positive or negative infinity is not allowed');
37
+ if (seed < 0) throw new Error('Unable to setSeed. Seed must be a number >= 0');
38
+ inc = seed;
39
+ }
40
+ const numPairsRegex = /(\d{1,2})/g;
41
+ function getUniqueSuffix() {
42
+ const numPairs = [];
43
+ const incStr = inc.toString();
44
+ let result = numPairsRegex.exec(incStr);
45
+ while(result){
46
+ numPairs.push(result[0]);
47
+ result = numPairsRegex.exec(incStr);
48
+ }
49
+ let out = '_';
50
+ for (const pair of numPairs){
51
+ const val = +pair;
52
+ if (val > 25) {
53
+ const [first, second] = pair.split('');
54
+ out += `${(0, _numToAlpha.default)(Number(first))}${(0, _numToAlpha.default)(Number(second))}`;
55
+ } else out += (0, _numToAlpha.default)(val);
56
+ }
57
+ inc += 1;
58
+ return out;
59
+ }
60
+ function generateClassName(c) {
61
+ return `${c}${getUniqueSuffix()}`;
62
+ }
@@ -0,0 +1,3 @@
1
+ export declare function setSeed(seed: number | null): void;
2
+ export declare function getUniqueSuffix(): string;
3
+ export declare function generateClassName(c: string): string;
@@ -0,0 +1,71 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ function _export(target, all) {
6
+ for(var name in all)Object.defineProperty(target, name, {
7
+ enumerable: true,
8
+ get: Object.getOwnPropertyDescriptor(all, name).get
9
+ });
10
+ }
11
+ _export(exports, {
12
+ get createStyles () {
13
+ return _createStyles.default;
14
+ },
15
+ get keyframes () {
16
+ return _createStyles.keyframes;
17
+ },
18
+ get rawStyles () {
19
+ return _createStyles.rawStyles;
20
+ },
21
+ get registerPosthook () {
22
+ return _plugins.registerPosthook;
23
+ },
24
+ get setSeed () {
25
+ return _generateClassName.setSeed;
26
+ }
27
+ });
28
+ const _createStyles = /*#__PURE__*/ _interop_require_wildcard(require("./createStyles.cjs"));
29
+ const _generateClassName = require("./generateClassName.cjs");
30
+ const _plugins = require("./plugins.cjs");
31
+ function _getRequireWildcardCache(nodeInterop) {
32
+ if (typeof WeakMap !== "function") return null;
33
+ var cacheBabelInterop = new WeakMap();
34
+ var cacheNodeInterop = new WeakMap();
35
+ return (_getRequireWildcardCache = function(nodeInterop) {
36
+ return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
37
+ })(nodeInterop);
38
+ }
39
+ function _interop_require_wildcard(obj, nodeInterop) {
40
+ if (!nodeInterop && obj && obj.__esModule) {
41
+ return obj;
42
+ }
43
+ if (obj === null || typeof obj !== "object" && typeof obj !== "function") {
44
+ return {
45
+ default: obj
46
+ };
47
+ }
48
+ var cache = _getRequireWildcardCache(nodeInterop);
49
+ if (cache && cache.has(obj)) {
50
+ return cache.get(obj);
51
+ }
52
+ var newObj = {
53
+ __proto__: null
54
+ };
55
+ var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;
56
+ for(var key in obj){
57
+ if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) {
58
+ var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null;
59
+ if (desc && (desc.get || desc.set)) {
60
+ Object.defineProperty(newObj, key, desc);
61
+ } else {
62
+ newObj[key] = obj[key];
63
+ }
64
+ }
65
+ }
66
+ newObj.default = obj;
67
+ if (cache) {
68
+ cache.set(obj, newObj);
69
+ }
70
+ return newObj;
71
+ }
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ Object.defineProperty(exports, "default", {
6
+ enumerable: true,
7
+ get: function() {
8
+ return numToAlpha;
9
+ }
10
+ });
11
+ const alphas = 'abcdefghijklmnopqrstuvwxyz'.split('');
12
+ function numToAlpha(num) {
13
+ return String(alphas[num]);
14
+ }
@@ -0,0 +1 @@
1
+ export default function numToAlpha(num: number): string;
@@ -0,0 +1 @@
1
+ { "type": "commonjs" }
@@ -0,0 +1,25 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ function _export(target, all) {
6
+ for(var name in all)Object.defineProperty(target, name, {
7
+ enumerable: true,
8
+ get: Object.getOwnPropertyDescriptor(all, name).get
9
+ });
10
+ }
11
+ _export(exports, {
12
+ get getPosthooks () {
13
+ return getPosthooks;
14
+ },
15
+ get registerPosthook () {
16
+ return registerPosthook;
17
+ }
18
+ });
19
+ const posthooks = [];
20
+ function getPosthooks() {
21
+ return posthooks;
22
+ }
23
+ function registerPosthook(posthook) {
24
+ posthooks.push(posthook);
25
+ }
@@ -0,0 +1,3 @@
1
+ export type PosthookPlugin = (sheetContents: string) => string;
2
+ export declare function getPosthooks(): PosthookPlugin[];
3
+ export declare function registerPosthook(posthook: PosthookPlugin): void;
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ _export_star(require("./useCreateStyles.cjs"), exports);
6
+ function _export_star(from, to) {
7
+ Object.keys(from).forEach(function(k) {
8
+ if (k !== "default" && !Object.prototype.hasOwnProperty.call(to, k)) {
9
+ Object.defineProperty(to, k, {
10
+ enumerable: true,
11
+ get: function() {
12
+ return from[k];
13
+ }
14
+ });
15
+ }
16
+ });
17
+ return from;
18
+ }
@@ -0,0 +1,73 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ Object.defineProperty(exports, "useCreateStyles", {
6
+ enumerable: true,
7
+ get: function() {
8
+ return useCreateStyles;
9
+ }
10
+ });
11
+ const _react = require("react");
12
+ const _createStyles = /*#__PURE__*/ _interop_require_default(require("../createStyles.cjs"));
13
+ const _index = require("../util/index.cjs");
14
+ function _interop_require_default(obj) {
15
+ return obj && obj.__esModule ? obj : {
16
+ default: obj
17
+ };
18
+ }
19
+ function useCreateStyles(rules, options) {
20
+ // cache rules to compare later
21
+ const [cachedRules, setCachedRules] = (0, _react.useState)(()=>rules);
22
+ // memoize options but keep them live
23
+ const cachedOptions = (0, _react.useMemo)(()=>({
24
+ ...options
25
+ }), [
26
+ options
27
+ ]);
28
+ const didFirstWriteRef = (0, _react.useRef)(false);
29
+ const styleTagRef = (0, _react.useRef)(typeof document === 'undefined' ? null : document.createElement('style'));
30
+ // initialize styles
31
+ const [styleState, setStyleState] = (0, _react.useState)(()=>(0, _createStyles.default)(rules, {
32
+ ...cachedOptions,
33
+ flush: false
34
+ }));
35
+ const { classes, stylesheet, updateSheet } = styleState;
36
+ // mount/unmount style tag
37
+ (0, _react.useEffect)(()=>{
38
+ if (!styleTagRef.current) return;
39
+ const { current: s } = styleTagRef;
40
+ document.head.append(s);
41
+ return ()=>{
42
+ s.remove();
43
+ };
44
+ }, []);
45
+ // update stylesheet when rules change
46
+ (0, _react.useEffect)(()=>{
47
+ if (!styleTagRef.current) return;
48
+ if (!didFirstWriteRef.current) {
49
+ didFirstWriteRef.current = true;
50
+ styleTagRef.current.innerHTML = stylesheet;
51
+ return;
52
+ }
53
+ if (!(0, _index.deepEqual)(rules, cachedRules)) {
54
+ setCachedRules(rules);
55
+ const updated = updateSheet(rules);
56
+ if (updated) {
57
+ styleTagRef.current.innerHTML = updated.stylesheet;
58
+ // use the fresh updateSheet from updated
59
+ // @ts-expect-error - this cast is safe and is only for us, internally, anyways
60
+ setStyleState({
61
+ ...updated,
62
+ updateSheet
63
+ });
64
+ }
65
+ }
66
+ }, [
67
+ cachedRules,
68
+ rules,
69
+ stylesheet,
70
+ updateSheet
71
+ ]); // only depend on rules + updater
72
+ return classes;
73
+ }
@@ -0,0 +1,3 @@
1
+ import { CreateStylesOptions } from '../createStyles.js';
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;
@@ -0,0 +1,4 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
@@ -1,7 +1,5 @@
1
1
  import { Properties } from 'csstype';
2
-
3
2
  export type SimpleStyleRules = {
4
- [key: string]: Properties | SimpleStyleRules;
3
+ [key: string]: Properties | SimpleStyleRules;
5
4
  };
6
-
7
5
  export type RenderableSimpleStyleRules = SimpleStyleRules & Record<string, Properties[]>;
@@ -0,0 +1,27 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ Object.defineProperty(exports, "deepEqual", {
6
+ enumerable: true,
7
+ get: function() {
8
+ return deepEqual;
9
+ }
10
+ });
11
+ function deepEqual(o1, o2) {
12
+ // We'll sort the keys, just in case the user kept things the same but the object is all wonky, order-wise
13
+ const o1Keys = Object.keys(o1).sort();
14
+ const o2Keys = Object.keys(o2).sort();
15
+ if (o1Keys.length !== o2Keys.length) return false;
16
+ if (o1Keys.some((key, i)=>o2Keys[i] !== key)) return false;
17
+ // Okay, the keys SHOULD be the same
18
+ // so we need to test their values, recursively, to verify equality
19
+ return o1Keys.reduce((prev, key)=>{
20
+ if (!prev) return prev; // we've already failed equality checks here
21
+ if (!(key in o2)) return false;
22
+ if (typeof o1[key] !== 'object') {
23
+ return o1[key] === o2[key];
24
+ }
25
+ return deepEqual(o1[key], o2[key]);
26
+ }, true);
27
+ }
@@ -0,0 +1 @@
1
+ export declare function deepEqual<O1 extends Record<string | number | symbol, any>, O2 extends Record<string | number | symbol, any>>(o1: O1, o2: O2): boolean;
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ _export_star(require("./deepEqual.cjs"), exports);
6
+ function _export_star(from, to) {
7
+ Object.keys(from).forEach(function(k) {
8
+ if (k !== "default" && !Object.prototype.hasOwnProperty.call(to, k)) {
9
+ Object.defineProperty(to, k, {
10
+ enumerable: true,
11
+ get: function() {
12
+ return from[k];
13
+ }
14
+ });
15
+ }
16
+ });
17
+ return from;
18
+ }
@@ -0,0 +1,30 @@
1
+ import { Properties } from 'csstype';
2
+ import { SimpleStyleRules } from './types.js';
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 Record<K, string>>(rules: T, options?: Partial<CreateStylesOptions>): string;
21
+ export declare function keyframes<T extends Record<string, Properties>>(frames: T, options?: CreateStylesOptions): [string, string];
22
+ export default function createStyles<T extends SimpleStyleRules, K extends keyof T, O extends Record<K, string>>(rules: T, options?: Partial<CreateStylesOptions>): {
23
+ classes: O;
24
+ stylesheet: string;
25
+ updateSheet: <T2 extends SimpleStyleRules, K2 extends keyof T2, O2 extends Record<K2, string>>(updatedRules: Partial<T2>) => {
26
+ classes: Record<string | number | keyof T2, string>;
27
+ stylesheet: string;
28
+ } | null;
29
+ };
30
+ export type CreateStylesArgs = Parameters<typeof createStyles>;