simplestyle-js 3.2.3-beta.0 → 3.3.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/createStyles.d.ts +1 -1
- package/generateClassName.js +1 -1
- package/generateClassName.js.map +2 -2
- package/index.cjs.js +2 -2
- package/index.js.map +2 -2
- package/numToAlpha.js +1 -1
- package/numToAlpha.js.map +2 -2
- package/package.json +19 -28
- package/react/index.cjs.js +18 -28
- package/react/index.js.map +4 -4
- package/react/useCreateStyles.d.ts +1 -5
- package/react/useCreateStyles.js +14 -24
- package/react/useCreateStyles.js.map +3 -3
package/createStyles.d.ts
CHANGED
|
@@ -29,7 +29,7 @@ export default function createStyles<T extends SimpleStyleRules, K extends keyof
|
|
|
29
29
|
classes: O;
|
|
30
30
|
stylesheet: string;
|
|
31
31
|
updateSheet: <T2 extends SimpleStyleRules, K2 extends keyof T2, O2 extends { [classKey in K2]: string; }>(updatedRules: Partial<T2>) => {
|
|
32
|
-
classes:
|
|
32
|
+
classes: string | number | keyof T2 extends infer T extends keyof T_1 ? { [classKey_1 in T]: string; } : never;
|
|
33
33
|
stylesheet: string;
|
|
34
34
|
} | null;
|
|
35
35
|
};
|
package/generateClassName.js
CHANGED
|
@@ -29,7 +29,7 @@ function getUniqueSuffix() {
|
|
|
29
29
|
const val = +pair;
|
|
30
30
|
if (val > 25) {
|
|
31
31
|
const [first, second] = pair.split("");
|
|
32
|
-
out += `${numToAlpha(
|
|
32
|
+
out += `${numToAlpha(Number(first))}${numToAlpha(Number(second))}`;
|
|
33
33
|
} else
|
|
34
34
|
out += numToAlpha(val);
|
|
35
35
|
});
|
package/generateClassName.js.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
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(
|
|
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,
|
|
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
6
|
"names": []
|
|
7
7
|
}
|
package/index.cjs.js
CHANGED
|
@@ -44,7 +44,7 @@ var import_deepmerge = __toESM(require("deepmerge"));
|
|
|
44
44
|
// src/numToAlpha.ts
|
|
45
45
|
var alphas = "abcdefghijklmnopqrstuvwxyz".split("");
|
|
46
46
|
function numToAlpha(num) {
|
|
47
|
-
return alphas[num];
|
|
47
|
+
return String(alphas[num]);
|
|
48
48
|
}
|
|
49
49
|
|
|
50
50
|
// src/generateClassName.ts
|
|
@@ -78,7 +78,7 @@ function getUniqueSuffix() {
|
|
|
78
78
|
const val = +pair;
|
|
79
79
|
if (val > 25) {
|
|
80
80
|
const [first, second] = pair.split("");
|
|
81
|
-
out += `${numToAlpha(
|
|
81
|
+
out += `${numToAlpha(Number(first))}${numToAlpha(Number(second))}`;
|
|
82
82
|
} else
|
|
83
83
|
out += numToAlpha(val);
|
|
84
84
|
});
|
package/index.js.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/index.ts", "../../src/createStyles.ts", "../../src/numToAlpha.ts", "../../src/generateClassName.ts", "../../src/plugins.ts"],
|
|
4
|
-
"sourcesContent": ["export type { CreateStylesArgs, CreateStylesOptions } from './createStyles';\nexport { default as createStyles, keyframes, rawStyles } from './createStyles';\nexport { setSeed } from './generateClassName';\nexport type { PosthookPlugin } from './plugins';\nexport { registerPosthook } from './plugins';\nexport type { SimpleStyleRules } from './types';\n", "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", "const alphas = 'abcdefghijklmnopqrstuvwxyz'.split('');\n\nexport default function numToAlpha(num: number): string {\n return alphas[num];\n}\n", "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(+first)}${numToAlpha(+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", "export type PosthookPlugin = (sheetContents: string) => string;\n\nconst posthooks: PosthookPlugin[] = [];\n\nexport function getPosthooks(): PosthookPlugin[] {\n return posthooks;\n}\n\nexport function registerPosthook(posthook: PosthookPlugin) {\n posthooks.push(posthook);\n}\n"],
|
|
5
|
-
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACCA,uBAAkB;;;ACDlB,IAAM,SAAS,6BAA6B,MAAM,EAAE;AAErC,SAAR,WAA4B,KAAqB;AACtD,SAAO,OAAO,GAAG;
|
|
4
|
+
"sourcesContent": ["export type { CreateStylesArgs, CreateStylesOptions } from './createStyles';\nexport { default as createStyles, keyframes, rawStyles } from './createStyles';\nexport { setSeed } from './generateClassName';\nexport type { PosthookPlugin } from './plugins';\nexport { registerPosthook } from './plugins';\nexport type { SimpleStyleRules } from './types';\n", "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", "const alphas = 'abcdefghijklmnopqrstuvwxyz'.split('');\n\nexport default function numToAlpha(num: number): string {\n return String(alphas[num]);\n}\n", "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", "export type PosthookPlugin = (sheetContents: string) => string;\n\nconst posthooks: PosthookPlugin[] = [];\n\nexport function getPosthooks(): PosthookPlugin[] {\n return posthooks;\n}\n\nexport function registerPosthook(posthook: PosthookPlugin) {\n posthooks.push(posthook);\n}\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;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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,qBAAiB,iBAAAA,SAAM,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
6
|
"names": ["merge"]
|
|
7
7
|
}
|
package/numToAlpha.js
CHANGED
package/numToAlpha.js.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../src/numToAlpha.ts"],
|
|
4
|
-
"sourcesContent": ["const alphas = 'abcdefghijklmnopqrstuvwxyz'.split('');\n\nexport default function numToAlpha(num: number): string {\n return alphas[num];\n}\n"],
|
|
5
|
-
"mappings": "AAAA,MAAM,SAAS,6BAA6B,MAAM,EAAE;AAErC,SAAR,WAA4B,KAAqB;AACtD,SAAO,OAAO,GAAG;
|
|
4
|
+
"sourcesContent": ["const alphas = 'abcdefghijklmnopqrstuvwxyz'.split('');\n\nexport default function numToAlpha(num: number): string {\n return String(alphas[num]);\n}\n"],
|
|
5
|
+
"mappings": "AAAA,MAAM,SAAS,6BAA6B,MAAM,EAAE;AAErC,SAAR,WAA4B,KAAqB;AACtD,SAAO,OAAO,OAAO,GAAG,CAAC;AAC3B;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "simplestyle-js",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.3.0",
|
|
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
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -16,10 +16,9 @@
|
|
|
16
16
|
"lint": "eslint './src/**/*.ts'",
|
|
17
17
|
"lint:fixall": "eslint './src/**/*.ts' --fix",
|
|
18
18
|
"setup": "npm install -g pnpm@$(cat pnpm-version) && pnpm install",
|
|
19
|
-
"test:coverage": "
|
|
20
|
-
"test": "
|
|
21
|
-
"test:watch": "
|
|
22
|
-
"test:debug": "node --inspect-brk ./node_modules/.bin/jest",
|
|
19
|
+
"test:coverage": "vitest run --coverage",
|
|
20
|
+
"test": "vitest run",
|
|
21
|
+
"test:watch": "vitest",
|
|
23
22
|
"git-pre-commit": "pnpm lint && pnpm test"
|
|
24
23
|
},
|
|
25
24
|
"keywords": [
|
|
@@ -32,41 +31,33 @@
|
|
|
32
31
|
"author": "Benjamin Duran <stratodyne@gmail.com>",
|
|
33
32
|
"license": "MIT",
|
|
34
33
|
"devDependencies": {
|
|
35
|
-
"@better-builds/
|
|
36
|
-
"@
|
|
37
|
-
"@testing-library/react": "^14.
|
|
34
|
+
"@better-builds/lets-version": "^0.8.2",
|
|
35
|
+
"@better-builds/package-bundler": "^1.6.1",
|
|
36
|
+
"@testing-library/react": "^14.1.2",
|
|
38
37
|
"@types/autoprefixer": "^10.2.0",
|
|
39
|
-
"@types/
|
|
40
|
-
"@types/
|
|
41
|
-
"@
|
|
42
|
-
"@
|
|
43
|
-
"
|
|
44
|
-
"autoprefixer": "^10.4.14",
|
|
45
|
-
"babel-jest": "^29.5.0",
|
|
38
|
+
"@types/react": "^18.2.47",
|
|
39
|
+
"@types/react-dom": "^18.2.18",
|
|
40
|
+
"@vitejs/plugin-react": "^4.2.1",
|
|
41
|
+
"@zeit/git-hooks": "^0.1.4",
|
|
42
|
+
"autoprefixer": "^10.4.16",
|
|
46
43
|
"coveralls": "^3.1.1",
|
|
47
|
-
"eslint-config-react-yas": "^4.
|
|
48
|
-
"jest": "^29.5.0",
|
|
49
|
-
"jest-environment-jsdom": "^29.5.0",
|
|
50
|
-
"jsdom": "^21.1.1",
|
|
44
|
+
"eslint-config-react-yas": "^4.1.0",
|
|
51
45
|
"npm-run-all": "^4.1.5",
|
|
52
|
-
"postcss": "^8.4.
|
|
46
|
+
"postcss": "^8.4.33",
|
|
53
47
|
"react": "^18.2.0",
|
|
54
48
|
"react-dom": "^18.2.0",
|
|
55
|
-
"ts-
|
|
56
|
-
"
|
|
57
|
-
"
|
|
49
|
+
"ts-node": "^10.9.2",
|
|
50
|
+
"typescript": "^5.3.3",
|
|
51
|
+
"vite": "^5.0.11",
|
|
52
|
+
"vitest": "^1.2.0"
|
|
58
53
|
},
|
|
59
54
|
"dependencies": {
|
|
60
|
-
"csstype": "^3.1.
|
|
55
|
+
"csstype": "^3.1.3",
|
|
61
56
|
"deepmerge": "^4.3.1"
|
|
62
57
|
},
|
|
63
58
|
"peerDependencies": {
|
|
64
59
|
"react": ">=16.8"
|
|
65
60
|
},
|
|
66
|
-
"engines": {
|
|
67
|
-
"node": "^18.15.0",
|
|
68
|
-
"npm": "^9.2.0"
|
|
69
|
-
},
|
|
70
61
|
"main": "./index.cjs.js",
|
|
71
62
|
"module": "./index.js",
|
|
72
63
|
"types": "./index.d.ts"
|
package/react/index.cjs.js
CHANGED
|
@@ -34,8 +34,8 @@ __export(react_exports, {
|
|
|
34
34
|
});
|
|
35
35
|
module.exports = __toCommonJS(react_exports);
|
|
36
36
|
|
|
37
|
-
// src/react/useCreateStyles.
|
|
38
|
-
var import_react =
|
|
37
|
+
// src/react/useCreateStyles.ts
|
|
38
|
+
var import_react = require("react");
|
|
39
39
|
|
|
40
40
|
// src/createStyles.ts
|
|
41
41
|
var import_deepmerge = __toESM(require("deepmerge"));
|
|
@@ -43,7 +43,7 @@ var import_deepmerge = __toESM(require("deepmerge"));
|
|
|
43
43
|
// src/numToAlpha.ts
|
|
44
44
|
var alphas = "abcdefghijklmnopqrstuvwxyz".split("");
|
|
45
45
|
function numToAlpha(num) {
|
|
46
|
-
return alphas[num];
|
|
46
|
+
return String(alphas[num]);
|
|
47
47
|
}
|
|
48
48
|
|
|
49
49
|
// src/generateClassName.ts
|
|
@@ -62,7 +62,7 @@ function getUniqueSuffix() {
|
|
|
62
62
|
const val = +pair;
|
|
63
63
|
if (val > 25) {
|
|
64
64
|
const [first, second] = pair.split("");
|
|
65
|
-
out += `${numToAlpha(
|
|
65
|
+
out += `${numToAlpha(Number(first))}${numToAlpha(Number(second))}`;
|
|
66
66
|
} else
|
|
67
67
|
out += numToAlpha(val);
|
|
68
68
|
});
|
|
@@ -262,48 +262,38 @@ function deepEqual(o1, o2) {
|
|
|
262
262
|
}, true);
|
|
263
263
|
}
|
|
264
264
|
|
|
265
|
-
// src/react/useCreateStyles.
|
|
265
|
+
// src/react/useCreateStyles.ts
|
|
266
266
|
function useCreateStyles(rules, options) {
|
|
267
|
-
const [cachedRules, setCachedRules] = import_react.
|
|
268
|
-
const
|
|
269
|
-
|
|
267
|
+
const [cachedRules, setCachedRules] = (0, import_react.useState)(() => rules);
|
|
268
|
+
const cachedOptions = (0, import_react.useMemo)(() => ({ ...options }), [options]);
|
|
269
|
+
const didFirstWriteRef = (0, import_react.useRef)(false);
|
|
270
|
+
const styleTagRef = (0, import_react.useRef)(typeof document !== "undefined" ? document.createElement("style") : null);
|
|
271
|
+
const [{ classes, stylesheet, updateSheet }, setCreateStyles] = (0, import_react.useState)(
|
|
272
|
+
() => createStyles(rules, { ...cachedOptions, flush: false })
|
|
270
273
|
);
|
|
271
|
-
|
|
272
|
-
const styleTagRef = import_react.default.useRef(typeof document !== "undefined" ? document.createElement("style") : null);
|
|
273
|
-
const rulesRef = import_react.default.useRef(rules);
|
|
274
|
-
const cachedRulesRef = import_react.default.useRef(cachedRules);
|
|
275
|
-
rulesRef.current = rules;
|
|
276
|
-
cachedRulesRef.current = cachedRules;
|
|
277
|
-
const rulesChanged = !deepEqual(rulesRef.current, cachedRulesRef.current);
|
|
278
|
-
const styleTag = import_react.default.useMemo(() => {
|
|
279
|
-
if (styleTagRef.current)
|
|
280
|
-
return null;
|
|
281
|
-
return /* @__PURE__ */ import_react.default.createElement("style", null, stylesheet);
|
|
282
|
-
}, [stylesheet]);
|
|
283
|
-
import_react.default.useEffect(() => {
|
|
274
|
+
(0, import_react.useEffect)(() => {
|
|
284
275
|
if (!styleTagRef.current)
|
|
285
276
|
return;
|
|
286
277
|
const { current: s } = styleTagRef;
|
|
287
278
|
document.head.appendChild(s);
|
|
288
279
|
return () => s.remove();
|
|
289
280
|
}, []);
|
|
290
|
-
import_react.
|
|
281
|
+
(0, import_react.useEffect)(() => {
|
|
291
282
|
if (!styleTagRef.current)
|
|
292
283
|
return;
|
|
293
284
|
if (!didFirstWriteRef.current && !styleTagRef.current.innerHTML) {
|
|
294
285
|
didFirstWriteRef.current = true;
|
|
295
286
|
styleTagRef.current.innerHTML = stylesheet;
|
|
296
287
|
return;
|
|
297
|
-
}
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
const updated = updateSheet(rulesRef.current);
|
|
288
|
+
} else if (!deepEqual(rules, cachedRules)) {
|
|
289
|
+
setCachedRules(rules);
|
|
290
|
+
const updated = updateSheet(rules);
|
|
301
291
|
if (updated == null ? void 0 : updated.stylesheet) {
|
|
302
292
|
styleTagRef.current.innerHTML = updated.stylesheet;
|
|
303
293
|
setCreateStyles({ ...updated, updateSheet });
|
|
304
294
|
}
|
|
305
295
|
}
|
|
306
|
-
}, [
|
|
307
|
-
return
|
|
296
|
+
}, [cachedRules, rules, stylesheet, updateSheet]);
|
|
297
|
+
return classes;
|
|
308
298
|
}
|
|
309
299
|
//# sourceMappingURL=index.js.map
|
package/react/index.js.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
|
-
"sources": ["../../../src/react/index.ts", "../../../src/react/useCreateStyles.
|
|
4
|
-
"sourcesContent": ["export * from './useCreateStyles';\n", "import React from 'react';\n\nimport createStyles, { CreateStylesOptions } from '../createStyles';\nimport { SimpleStyleRules } from '../types';\nimport { deepEqual } from '../util';\n\nexport function useCreateStyles<T extends SimpleStyleRules, K extends keyof T, O extends { [classKey in K]: string }>(\n rules: T,\n options?: Partial<Omit<CreateStylesOptions, 'flush'>>,\n) {\n // state\n const [cachedRules, setCachedRules] = React.useState(() => rules);\n const [{ classes, stylesheet, updateSheet }, setCreateStyles] = React.useState(() =>\n createStyles<T, K, O>(rules, { ...options, flush: false }),\n );\n\n // refs\n const didFirstWriteRef = React.useRef(false);\n const styleTagRef = React.useRef(typeof document !== 'undefined' ? document.createElement('style') : null);\n const rulesRef = React.useRef(rules);\n const cachedRulesRef = React.useRef(cachedRules);\n\n // we leverage refs for these objects, as their referential equality is likely always changing.\n // this prevent unnecessary executions of the effects below\n rulesRef.current = rules;\n cachedRulesRef.current = cachedRules;\n\n // local variables\n const rulesChanged = !deepEqual(rulesRef.current, cachedRulesRef.current);\n\n // memos\n const styleTag = React.useMemo(() => {\n if (styleTagRef.current) return null;\n\n return <style>{stylesheet}</style>;\n }, [stylesheet]);\n\n // effects\n React.useEffect(() => {\n if (!styleTagRef.current) return;\n\n const { current: s } = styleTagRef;\n document.head.appendChild(s);\n return () => s.remove();\n }, []);\n React.useEffect(() => {\n if (!styleTagRef.current) return;\n\n if (!didFirstWriteRef.current && !styleTagRef.current.innerHTML) {\n didFirstWriteRef.current = true;\n styleTagRef.current.innerHTML = stylesheet;\n return;\n }\n if (rulesChanged) {\n setCachedRules(rulesRef.current);\n const updated = updateSheet(rulesRef.current);\n if (updated?.stylesheet) {\n styleTagRef.current.innerHTML = updated.stylesheet;\n setCreateStyles({ ...updated, updateSheet } as any);\n }\n }\n }, [rulesChanged, stylesheet, updateSheet]);\n return React.useMemo(() => ({ classes, styleTag }), [classes, styleTag]);\n}\n", "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", "const alphas = 'abcdefghijklmnopqrstuvwxyz'.split('');\n\nexport default function numToAlpha(num: number): string {\n return alphas[num];\n}\n", "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(+first)}${numToAlpha(+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", "export type PosthookPlugin = (sheetContents: string) => string;\n\nconst posthooks: PosthookPlugin[] = [];\n\nexport function getPosthooks(): PosthookPlugin[] {\n return posthooks;\n}\n\nexport function registerPosthook(posthook: PosthookPlugin) {\n posthooks.push(posthook);\n}\n", "export function deepEqual<\n O1 extends Record<string | number | symbol, any>,\n O2 extends Record<string | number | symbol, any>,\n>(o1: O1, o2: O2): boolean {\n // We'll sort the keys, just in case the user kept things the same but the object is all wonky, order-wise\n const o1Keys = Object.keys(o1).sort();\n const o2Keys = Object.keys(o2).sort();\n if (o1Keys.length !== o2Keys.length) return false;\n if (o1Keys.some((key, i) => o2Keys[i] !== key)) return false;\n // Okay, the keys SHOULD be the same\n // so we need to test their values, recursively, to verify equality\n return o1Keys.reduce<boolean>((prev, key) => {\n if (!prev) return prev; // we've already failed equality checks here\n if (!(key in o2)) return false;\n if (typeof o1[key] !== 'object') {\n return prev && o1[key] === o2[key];\n }\n return prev && deepEqual(o1[key], o2[key]);\n }, true);\n}\n"],
|
|
5
|
-
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,
|
|
6
|
-
"names": ["merge"
|
|
3
|
+
"sources": ["../../../src/react/index.ts", "../../../src/react/useCreateStyles.ts", "../../../src/createStyles.ts", "../../../src/numToAlpha.ts", "../../../src/generateClassName.ts", "../../../src/plugins.ts", "../../../src/util/deepEqual.ts"],
|
|
4
|
+
"sourcesContent": ["export * from './useCreateStyles';\n", "import { useEffect, useMemo, useRef, useState } from 'react';\n\nimport createStyles, { CreateStylesOptions } from '../createStyles';\nimport { SimpleStyleRules } from '../types';\nimport { deepEqual } from '../util';\n\nexport function useCreateStyles<T extends SimpleStyleRules, K extends keyof T, O extends { [classKey in K]: string }>(\n rules: T,\n options?: Partial<Omit<CreateStylesOptions, 'flush'>>,\n) {\n const [cachedRules, setCachedRules] = useState(() => rules);\n const cachedOptions = useMemo(() => ({ ...options } as Partial<Omit<CreateStylesOptions, 'flush'>>), [options]);\n const didFirstWriteRef = useRef(false);\n const styleTagRef = useRef(typeof document !== 'undefined' ? document.createElement('style') : null);\n\n const [{ classes, stylesheet, updateSheet }, setCreateStyles] = useState(() =>\n createStyles<T, K, O>(rules, { ...cachedOptions, flush: false }),\n );\n\n useEffect(() => {\n if (!styleTagRef.current) return;\n const { current: s } = styleTagRef;\n document.head.appendChild(s);\n return () => s.remove();\n }, []);\n useEffect(() => {\n if (!styleTagRef.current) return;\n if (!didFirstWriteRef.current && !styleTagRef.current.innerHTML) {\n didFirstWriteRef.current = true;\n styleTagRef.current.innerHTML = stylesheet;\n return;\n } else if (!deepEqual(rules, cachedRules)) {\n setCachedRules(rules);\n const updated = updateSheet(rules);\n if (updated?.stylesheet) {\n styleTagRef.current.innerHTML = updated.stylesheet;\n setCreateStyles({ ...updated, updateSheet } as any);\n }\n }\n }, [cachedRules, rules, stylesheet, updateSheet]);\n return classes;\n}\n", "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", "const alphas = 'abcdefghijklmnopqrstuvwxyz'.split('');\n\nexport default function numToAlpha(num: number): string {\n return String(alphas[num]);\n}\n", "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", "export type PosthookPlugin = (sheetContents: string) => string;\n\nconst posthooks: PosthookPlugin[] = [];\n\nexport function getPosthooks(): PosthookPlugin[] {\n return posthooks;\n}\n\nexport function registerPosthook(posthook: PosthookPlugin) {\n posthooks.push(posthook);\n}\n", "export function deepEqual<\n O1 extends Record<string | number | symbol, any>,\n O2 extends Record<string | number | symbol, any>,\n>(o1: O1, o2: O2): boolean {\n // We'll sort the keys, just in case the user kept things the same but the object is all wonky, order-wise\n const o1Keys = Object.keys(o1).sort();\n const o2Keys = Object.keys(o2).sort();\n if (o1Keys.length !== o2Keys.length) return false;\n if (o1Keys.some((key, i) => o2Keys[i] !== key)) return false;\n // Okay, the keys SHOULD be the same\n // so we need to test their values, recursively, to verify equality\n return o1Keys.reduce<boolean>((prev, key) => {\n if (!prev) return prev; // we've already failed equality checks here\n if (!(key in o2)) return false;\n if (typeof o1[key] !== 'object') {\n return prev && o1[key] === o2[key];\n }\n return prev && deepEqual(o1[key], o2[key]);\n }, true);\n}\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,mBAAqD;;;ACCrD,uBAAkB;;;ACDlB,IAAM,SAAS,6BAA6B,MAAM,EAAE;AAErC,SAAR,WAA4B,KAAqB;AACtD,SAAO,OAAO,OAAO,GAAG,CAAC;AAC3B;;;ACFA,IAAI,MAAM,KAAK,IAAI;AAgBnB,IAAM,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,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C9G,QAAM,uBAAmB,qBAAO,KAAK;AACrC,QAAM,kBAAc,qBAAO,OAAO,aAAa,cAAc,SAAS,cAAc,OAAO,IAAI,IAAI;AAEnG,QAAM,CAAC,EAAE,SAAS,YAAY,YAAY,GAAG,eAAe,QAAI;AAAA,IAAS,MACvE,aAAsB,OAAO,EAAE,GAAG,eAAe,OAAO,MAAM,CAAC;AAAA,EACjE;AAEA,8BAAU,MAAM;AACd,QAAI,CAAC,YAAY;AAAS;AAC1B,UAAM,EAAE,SAAS,EAAE,IAAI;AACvB,aAAS,KAAK,YAAY,CAAC;AAC3B,WAAO,MAAM,EAAE,OAAO;AAAA,EACxB,GAAG,CAAC,CAAC;AACL,8BAAU,MAAM;AACd,QAAI,CAAC,YAAY;AAAS;AAC1B,QAAI,CAAC,iBAAiB,WAAW,CAAC,YAAY,QAAQ,WAAW;AAC/D,uBAAiB,UAAU;AAC3B,kBAAY,QAAQ,YAAY;AAChC;AAAA,IACF,WAAW,CAAC,UAAU,OAAO,WAAW,GAAG;AACzC,qBAAe,KAAK;AACpB,YAAM,UAAU,YAAY,KAAK;AACjC,UAAI,mCAAS,YAAY;AACvB,oBAAY,QAAQ,YAAY,QAAQ;AACxC,wBAAgB,EAAE,GAAG,SAAS,YAAY,CAAQ;AAAA,MACpD;AAAA,IACF;AAAA,EACF,GAAG,CAAC,aAAa,OAAO,YAAY,WAAW,CAAC;AAChD,SAAO;AACT;",
|
|
6
|
+
"names": ["merge"]
|
|
7
7
|
}
|
|
@@ -1,9 +1,5 @@
|
|
|
1
|
-
/// <reference types="react" />
|
|
2
1
|
import { CreateStylesOptions } from '../createStyles';
|
|
3
2
|
import { SimpleStyleRules } from '../types';
|
|
4
3
|
export declare function useCreateStyles<T extends SimpleStyleRules, K extends keyof T, O extends {
|
|
5
4
|
[classKey in K]: string;
|
|
6
|
-
}>(rules: T, options?: Partial<Omit<CreateStylesOptions, 'flush'>>):
|
|
7
|
-
classes: O;
|
|
8
|
-
styleTag: JSX.Element | null;
|
|
9
|
-
};
|
|
5
|
+
}>(rules: T, options?: Partial<Omit<CreateStylesOptions, 'flush'>>): O;
|
package/react/useCreateStyles.js
CHANGED
|
@@ -1,48 +1,38 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { useEffect, useMemo, useRef, useState } from "react";
|
|
2
2
|
import createStyles from "../createStyles";
|
|
3
3
|
import { deepEqual } from "../util";
|
|
4
4
|
function useCreateStyles(rules, options) {
|
|
5
|
-
const [cachedRules, setCachedRules] =
|
|
6
|
-
const
|
|
7
|
-
|
|
5
|
+
const [cachedRules, setCachedRules] = useState(() => rules);
|
|
6
|
+
const cachedOptions = useMemo(() => ({ ...options }), [options]);
|
|
7
|
+
const didFirstWriteRef = useRef(false);
|
|
8
|
+
const styleTagRef = useRef(typeof document !== "undefined" ? document.createElement("style") : null);
|
|
9
|
+
const [{ classes, stylesheet, updateSheet }, setCreateStyles] = useState(
|
|
10
|
+
() => createStyles(rules, { ...cachedOptions, flush: false })
|
|
8
11
|
);
|
|
9
|
-
|
|
10
|
-
const styleTagRef = React.useRef(typeof document !== "undefined" ? document.createElement("style") : null);
|
|
11
|
-
const rulesRef = React.useRef(rules);
|
|
12
|
-
const cachedRulesRef = React.useRef(cachedRules);
|
|
13
|
-
rulesRef.current = rules;
|
|
14
|
-
cachedRulesRef.current = cachedRules;
|
|
15
|
-
const rulesChanged = !deepEqual(rulesRef.current, cachedRulesRef.current);
|
|
16
|
-
const styleTag = React.useMemo(() => {
|
|
17
|
-
if (styleTagRef.current)
|
|
18
|
-
return null;
|
|
19
|
-
return /* @__PURE__ */ React.createElement("style", null, stylesheet);
|
|
20
|
-
}, [stylesheet]);
|
|
21
|
-
React.useEffect(() => {
|
|
12
|
+
useEffect(() => {
|
|
22
13
|
if (!styleTagRef.current)
|
|
23
14
|
return;
|
|
24
15
|
const { current: s } = styleTagRef;
|
|
25
16
|
document.head.appendChild(s);
|
|
26
17
|
return () => s.remove();
|
|
27
18
|
}, []);
|
|
28
|
-
|
|
19
|
+
useEffect(() => {
|
|
29
20
|
if (!styleTagRef.current)
|
|
30
21
|
return;
|
|
31
22
|
if (!didFirstWriteRef.current && !styleTagRef.current.innerHTML) {
|
|
32
23
|
didFirstWriteRef.current = true;
|
|
33
24
|
styleTagRef.current.innerHTML = stylesheet;
|
|
34
25
|
return;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
const updated = updateSheet(rulesRef.current);
|
|
26
|
+
} else if (!deepEqual(rules, cachedRules)) {
|
|
27
|
+
setCachedRules(rules);
|
|
28
|
+
const updated = updateSheet(rules);
|
|
39
29
|
if (updated == null ? void 0 : updated.stylesheet) {
|
|
40
30
|
styleTagRef.current.innerHTML = updated.stylesheet;
|
|
41
31
|
setCreateStyles({ ...updated, updateSheet });
|
|
42
32
|
}
|
|
43
33
|
}
|
|
44
|
-
}, [
|
|
45
|
-
return
|
|
34
|
+
}, [cachedRules, rules, stylesheet, updateSheet]);
|
|
35
|
+
return classes;
|
|
46
36
|
}
|
|
47
37
|
export {
|
|
48
38
|
useCreateStyles
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
|
-
"sources": ["../../src/react/useCreateStyles.
|
|
4
|
-
"sourcesContent": ["import
|
|
5
|
-
"mappings": "AAAA,
|
|
3
|
+
"sources": ["../../src/react/useCreateStyles.ts"],
|
|
4
|
+
"sourcesContent": ["import { useEffect, useMemo, useRef, useState } from 'react';\n\nimport createStyles, { CreateStylesOptions } from '../createStyles';\nimport { SimpleStyleRules } from '../types';\nimport { deepEqual } from '../util';\n\nexport function useCreateStyles<T extends SimpleStyleRules, K extends keyof T, O extends { [classKey in K]: string }>(\n rules: T,\n options?: Partial<Omit<CreateStylesOptions, 'flush'>>,\n) {\n const [cachedRules, setCachedRules] = useState(() => rules);\n const cachedOptions = useMemo(() => ({ ...options } as Partial<Omit<CreateStylesOptions, 'flush'>>), [options]);\n const didFirstWriteRef = useRef(false);\n const styleTagRef = useRef(typeof document !== 'undefined' ? document.createElement('style') : null);\n\n const [{ classes, stylesheet, updateSheet }, setCreateStyles] = useState(() =>\n createStyles<T, K, O>(rules, { ...cachedOptions, flush: false }),\n );\n\n useEffect(() => {\n if (!styleTagRef.current) return;\n const { current: s } = styleTagRef;\n document.head.appendChild(s);\n return () => s.remove();\n }, []);\n useEffect(() => {\n if (!styleTagRef.current) return;\n if (!didFirstWriteRef.current && !styleTagRef.current.innerHTML) {\n didFirstWriteRef.current = true;\n styleTagRef.current.innerHTML = stylesheet;\n return;\n } else if (!deepEqual(rules, cachedRules)) {\n setCachedRules(rules);\n const updated = updateSheet(rules);\n if (updated?.stylesheet) {\n styleTagRef.current.innerHTML = updated.stylesheet;\n setCreateStyles({ ...updated, updateSheet } as any);\n }\n }\n }, [cachedRules, rules, stylesheet, updateSheet]);\n return classes;\n}\n"],
|
|
5
|
+
"mappings": "AAAA,SAAS,WAAW,SAAS,QAAQ,gBAAgB;AAErD,OAAO,kBAA2C;AAElD,SAAS,iBAAiB;AAEnB,SAAS,gBACd,OACA,SACA;AACA,QAAM,CAAC,aAAa,cAAc,IAAI,SAAS,MAAM,KAAK;AAC1D,QAAM,gBAAgB,QAAQ,OAAO,EAAE,GAAG,QAAQ,IAAmD,CAAC,OAAO,CAAC;AAC9G,QAAM,mBAAmB,OAAO,KAAK;AACrC,QAAM,cAAc,OAAO,OAAO,aAAa,cAAc,SAAS,cAAc,OAAO,IAAI,IAAI;AAEnG,QAAM,CAAC,EAAE,SAAS,YAAY,YAAY,GAAG,eAAe,IAAI;AAAA,IAAS,MACvE,aAAsB,OAAO,EAAE,GAAG,eAAe,OAAO,MAAM,CAAC;AAAA,EACjE;AAEA,YAAU,MAAM;AACd,QAAI,CAAC,YAAY;AAAS;AAC1B,UAAM,EAAE,SAAS,EAAE,IAAI;AACvB,aAAS,KAAK,YAAY,CAAC;AAC3B,WAAO,MAAM,EAAE,OAAO;AAAA,EACxB,GAAG,CAAC,CAAC;AACL,YAAU,MAAM;AACd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