@umber-style/css 0.0.0 → 0.1.0-alpha.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.
- package/LICENSE +21 -0
- package/README.md +13 -1
- package/dist/create-instance.cjs +107 -0
- package/dist/create-instance.d.cts +87 -0
- package/dist/create-instance.d.mts +87 -0
- package/dist/create-instance.mjs +103 -0
- package/dist/index.cjs +14 -0
- package/dist/index.d.cts +5 -0
- package/dist/index.d.mts +5 -0
- package/dist/index.mjs +5 -0
- package/package.json +72 -5
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 soroush.tech
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1 +1,13 @@
|
|
|
1
|
-
|
|
1
|
+
# @umber-style/css
|
|
2
|
+
|
|
3
|
+
UmberStyle's plain layer: a style goes to a cache and comes back as a class name. No framework, no theme, no provider.
|
|
4
|
+
|
|
5
|
+
Install: `pnpm add @umber-style/css`.
|
|
6
|
+
|
|
7
|
+
```ts
|
|
8
|
+
import { css, cx } from '@umber-style/css'
|
|
9
|
+
|
|
10
|
+
const card = css({ padding: 8, '&:hover': { opacity: 0.9 } })
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
Documentation: [docs](../../docs/README.md), and [class names without a theme](../../docs/css.md) for this package.
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
Object.defineProperties(exports, {
|
|
2
|
+
__esModule: { value: true },
|
|
3
|
+
[Symbol.toStringTag]: { value: "Module" }
|
|
4
|
+
});
|
|
5
|
+
let _umber_style_core = require("@umber-style/core");
|
|
6
|
+
// src/createInstance.ts
|
|
7
|
+
/** What makes a string given to `css` CSS text rather than class names: a class name holds neither. */
|
|
8
|
+
const CSS_TEXT = /[:{]/;
|
|
9
|
+
/** Whether `value` is the strings of a tagged template. */
|
|
10
|
+
function isTemplate(value) {
|
|
11
|
+
return Array.isArray(value) && Object.hasOwn(value, "raw");
|
|
12
|
+
}
|
|
13
|
+
/** The sheet of a cache made here: the style elements of the document, or one in memory where there is no document. */
|
|
14
|
+
function createDefaultSheet(prefix) {
|
|
15
|
+
return typeof document === "undefined" ? (0, _umber_style_core.createMemorySheet)() : (0, _umber_style_core.createDomSheet)(prefix);
|
|
16
|
+
}
|
|
17
|
+
/** The items of `args`, lists flattened, without `false`, `null`, `undefined`, `''` and `true`. */
|
|
18
|
+
function flatten(args, out = []) {
|
|
19
|
+
for (const arg of args) if (Array.isArray(arg)) flatten(arg, out);
|
|
20
|
+
else if (arg && arg !== true) out.push(arg);
|
|
21
|
+
return out;
|
|
22
|
+
}
|
|
23
|
+
/** The class names of `cx` arguments, in order. */
|
|
24
|
+
function collectClassNames(args, out = []) {
|
|
25
|
+
for (const arg of flatten(args)) if (typeof arg === "string") out.push(arg);
|
|
26
|
+
else for (const [name, isOn] of Object.entries(arg)) if (isOn) out.push(name);
|
|
27
|
+
return out;
|
|
28
|
+
}
|
|
29
|
+
/** What a template gave, or a `TypeError` when it gave a function: an interpolation needs props or a theme, and there are none here. */
|
|
30
|
+
function readTemplate(name, result) {
|
|
31
|
+
if (typeof result === "function") throw new TypeError(`${name}(): an interpolation of this template is a function, which needs props or a theme, and ${name}() has none. Call it yourself and interpolate what it returns.`);
|
|
32
|
+
return result;
|
|
33
|
+
}
|
|
34
|
+
/** Makes an instance: a cache of its own (or the one given) and the functions bound to it. */
|
|
35
|
+
function createInstance(options = {}) {
|
|
36
|
+
const { key, cache: given, sheet: givenSheet, hasLayers = false, ...cacheOptions } = options;
|
|
37
|
+
if (given && key !== void 0 && key !== given.prefix) throw new TypeError(`createInstance(): the key "${key}" differs from the prefix "${given.prefix}" of the given cache. Leave out \`key\`, or give the same one.`);
|
|
38
|
+
const prefix = key ?? "css";
|
|
39
|
+
const sheet = givenSheet ?? (given ? given.sheet : createDefaultSheet(prefix));
|
|
40
|
+
if (!sheet) throw new TypeError("createInstance(): the given cache has no `sheet` member: give the sheet it writes to as `sheet` too.");
|
|
41
|
+
const makeCache = () => (0, _umber_style_core.createCache)({
|
|
42
|
+
...cacheOptions,
|
|
43
|
+
prefix,
|
|
44
|
+
hasLayers,
|
|
45
|
+
sheet
|
|
46
|
+
});
|
|
47
|
+
let current = given ?? makeCache();
|
|
48
|
+
const hydrated = /* @__PURE__ */ new Set();
|
|
49
|
+
/** A class name this cache made, as its style, so that a template merges it; anything else as it is. */
|
|
50
|
+
const resolveClass = (value) => typeof value === "string" && current.registered.get(value) || value;
|
|
51
|
+
/** A string that is not a class of this cache and holds `:` or `{`, as the style of its CSS text; anything else as it is. */
|
|
52
|
+
const readText = (value) => typeof value === "string" && CSS_TEXT.test(value) && !current.registered.has(value) ? (0, _umber_style_core.parseStyleTemplate)([value], []) : value;
|
|
53
|
+
const css = ((...args) => {
|
|
54
|
+
const [first, ...values] = args;
|
|
55
|
+
if (args.length === 1 && typeof first === "object" && first !== null && !Array.isArray(first)) return current.css(first);
|
|
56
|
+
if (isTemplate(first)) return current.css(readTemplate("css", (0, _umber_style_core.parseStyleTemplate)(first, values.map(resolveClass))));
|
|
57
|
+
return current.css(...flatten(args).map(readText));
|
|
58
|
+
});
|
|
59
|
+
const merge = (className) => {
|
|
60
|
+
return className.split(/\s+/).filter((name) => current.registered.has(name)).length < 2 ? className : current.css(className);
|
|
61
|
+
};
|
|
62
|
+
const keyframes = ((first, ...values) => {
|
|
63
|
+
const steps = isTemplate(first) ? readTemplate("keyframes", (0, _umber_style_core.parseKeyframesTemplate)(first, values)) : first;
|
|
64
|
+
const name = (0, _umber_style_core.keyframes)(steps);
|
|
65
|
+
current.insertKeyframes(name);
|
|
66
|
+
return name;
|
|
67
|
+
});
|
|
68
|
+
const injectGlobal = ((...args) => {
|
|
69
|
+
const [first, ...values] = args;
|
|
70
|
+
const styles = isTemplate(first) ? [readTemplate("injectGlobal", (0, _umber_style_core.parseStyleTemplate)(first, values))] : flatten(args);
|
|
71
|
+
for (const style of styles) {
|
|
72
|
+
const rules = current.compileGlobal(style);
|
|
73
|
+
if (!hydrated.has(rules.id)) current.insertGlobal(rules);
|
|
74
|
+
}
|
|
75
|
+
});
|
|
76
|
+
return {
|
|
77
|
+
css,
|
|
78
|
+
cx: (...classNames) => merge(collectClassNames(classNames).join(" ")),
|
|
79
|
+
keyframes,
|
|
80
|
+
injectGlobal,
|
|
81
|
+
cache: given ?? new Proxy({}, { get: (_target, member) => Reflect.get(current, member) }),
|
|
82
|
+
sheet,
|
|
83
|
+
flush: () => {
|
|
84
|
+
if (given) throw new TypeError("flush(): this instance was given its cache, which other code shares, so it cannot empty it.");
|
|
85
|
+
for (let index = sheet.rules.length - 1; index >= 0; index--) sheet.remove?.(index);
|
|
86
|
+
hydrated.clear();
|
|
87
|
+
current = makeCache();
|
|
88
|
+
},
|
|
89
|
+
hydrate: (ids) => {
|
|
90
|
+
for (const id of ids) {
|
|
91
|
+
hydrated.add(id);
|
|
92
|
+
current.insertOnce(id, []);
|
|
93
|
+
}
|
|
94
|
+
},
|
|
95
|
+
getRegisteredStyles: (registeredStyles, classNames) => {
|
|
96
|
+
let raw = "";
|
|
97
|
+
for (const name of classNames.split(/\s+/)) {
|
|
98
|
+
const style = current.registered.get(name);
|
|
99
|
+
if (style) registeredStyles.push(style);
|
|
100
|
+
else if (name) raw += `${name} `;
|
|
101
|
+
}
|
|
102
|
+
return raw;
|
|
103
|
+
},
|
|
104
|
+
merge
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
exports.default = createInstance;
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { Cache, CacheOptions, GlobalStyleObject, KeyframesSteps, Sheet, StyleObject, WebStyleObject } from "@umber-style/core";
|
|
2
|
+
/** What `css` takes: style objects, class names, CSS text (a string with `:` or `{`), nothing (`false`, `null`, `undefined`, `''`, `true`), and lists of them, nested. */
|
|
3
|
+
export type CssInput = WebStyleObject | string | boolean | null | undefined | readonly CssInput[];
|
|
4
|
+
/** What `cx` takes: class names, objects whose keys are class names that are used when their value is truthy, nothing, and lists of them, nested. */
|
|
5
|
+
export type ClassNamesInput = string | boolean | null | undefined | Readonly<Record<string, unknown>> | readonly ClassNamesInput[];
|
|
6
|
+
/** What `injectGlobal` takes: global style objects (selectors and at-rules as keys), nothing, and lists of them, nested. */
|
|
7
|
+
export type InjectGlobalInput = GlobalStyleObject | boolean | null | undefined | readonly InjectGlobalInput[];
|
|
8
|
+
/** `css`: one class name for its arguments, or for a tagged template. */
|
|
9
|
+
export interface CssFunction {
|
|
10
|
+
(template: TemplateStringsArray, ...values: unknown[]): string;
|
|
11
|
+
(...args: CssInput[]): string;
|
|
12
|
+
}
|
|
13
|
+
/** `keyframes`: the name of an animation, from its steps or from a tagged template. */
|
|
14
|
+
export interface KeyframesFunction {
|
|
15
|
+
(template: TemplateStringsArray, ...values: unknown[]): string;
|
|
16
|
+
(steps: KeyframesSteps): string;
|
|
17
|
+
}
|
|
18
|
+
/** `injectGlobal`: writes global rules, from style objects or from a tagged template. */
|
|
19
|
+
export interface InjectGlobalFunction {
|
|
20
|
+
(template: TemplateStringsArray, ...values: unknown[]): void;
|
|
21
|
+
(...styles: InjectGlobalInput[]): void;
|
|
22
|
+
}
|
|
23
|
+
/** A cache made by other code, that an instance shares: it may carry the sheet it writes to. */
|
|
24
|
+
export type SharedCache = Cache & {
|
|
25
|
+
readonly sheet?: Sheet;
|
|
26
|
+
};
|
|
27
|
+
/** The options of `createInstance`: those of the core's `createCache`, with `key` for `prefix`, or a cache to share. */
|
|
28
|
+
export interface InstanceOptions extends Omit<CacheOptions, 'prefix'> {
|
|
29
|
+
/** The first part of every class name, and the key of the style element (`data-style="<key>"`). Default `css`. */
|
|
30
|
+
key?: string;
|
|
31
|
+
/**
|
|
32
|
+
* Whether every rule goes into a cascade layer. Default `false` here (the core's default is `true`):
|
|
33
|
+
* a class of this package is then an ordinary class next to the app's own CSS, which is not layered.
|
|
34
|
+
*/
|
|
35
|
+
hasLayers?: boolean;
|
|
36
|
+
/**
|
|
37
|
+
* A cache to use instead of making one, so that the classes of this instance and those of other code
|
|
38
|
+
* live in one cache. Its options are its own: the other cache options are not read, and a `key` that
|
|
39
|
+
* differs from its prefix throws. The sheet is `sheet`, or the cache's own `sheet` member.
|
|
40
|
+
*/
|
|
41
|
+
cache?: SharedCache;
|
|
42
|
+
}
|
|
43
|
+
/** The functions of one instance, bound to its cache. */
|
|
44
|
+
export interface CssInstance {
|
|
45
|
+
/**
|
|
46
|
+
* One class for style objects and class names this cache made (resolved back to their styles), merged
|
|
47
|
+
* left to right so the last wins; a string that holds `:` or `{` is CSS text, read as a template is and
|
|
48
|
+
* merged at its place; other class names pass through, in front of it. Lists are
|
|
49
|
+
* flattened; `false`, `null`, `undefined`, `''` and `true` are nothing. As a tag, the template is
|
|
50
|
+
* read as CSS text. Returns the class string, '' when there is nothing.
|
|
51
|
+
*/
|
|
52
|
+
css: CssFunction;
|
|
53
|
+
/**
|
|
54
|
+
* The class string of its arguments: when two or more of the classes were made by this cache, they
|
|
55
|
+
* are merged into one new class, the last winning, after the other class names; else the classes as
|
|
56
|
+
* they are.
|
|
57
|
+
*/
|
|
58
|
+
cx(...classNames: ClassNamesInput[]): string;
|
|
59
|
+
/** Registers the animation, writes its `@keyframes` rule at once, and returns its name. */
|
|
60
|
+
keyframes: KeyframesFunction;
|
|
61
|
+
/** Writes global rules at once; they stay. Rules of the same text are written once. */
|
|
62
|
+
injectGlobal: InjectGlobalFunction;
|
|
63
|
+
/** The cache. After `flush`, it reads the new one. */
|
|
64
|
+
readonly cache: Cache;
|
|
65
|
+
/** The sheet the cache writes to. */
|
|
66
|
+
readonly sheet: Sheet;
|
|
67
|
+
/**
|
|
68
|
+
* Removes every rule from the sheet and starts a new, empty cache over it: the classes, the global
|
|
69
|
+
* rules and the hydrated ids are forgotten, and a style is written again when it is next used. Throws
|
|
70
|
+
* a `TypeError` for an instance that was given its cache, which other code shares.
|
|
71
|
+
*/
|
|
72
|
+
flush(): void;
|
|
73
|
+
/** Marks rules as present on the page (the ids of the core's `extract()`), so that they are not written again. */
|
|
74
|
+
hydrate(ids: readonly string[]): void;
|
|
75
|
+
/**
|
|
76
|
+
* Pushes the style object of each class of `classNames` that this cache made onto `registeredStyles`,
|
|
77
|
+
* in order, and returns the other class names, each followed by a space.
|
|
78
|
+
*/
|
|
79
|
+
getRegisteredStyles(registeredStyles: StyleObject[], classNames: string): string;
|
|
80
|
+
/**
|
|
81
|
+
* `className` with the classes this cache made merged into one new class, the last winning, after the
|
|
82
|
+
* other class names, when there are two or more of them; else `className` as it is.
|
|
83
|
+
*/
|
|
84
|
+
merge(className: string): string;
|
|
85
|
+
}
|
|
86
|
+
/** Makes an instance: a cache of its own (or the one given) and the functions bound to it. */
|
|
87
|
+
export default function createInstance(options?: InstanceOptions): CssInstance;
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { Cache, CacheOptions, GlobalStyleObject, KeyframesSteps, Sheet, StyleObject, WebStyleObject } from "@umber-style/core";
|
|
2
|
+
/** What `css` takes: style objects, class names, CSS text (a string with `:` or `{`), nothing (`false`, `null`, `undefined`, `''`, `true`), and lists of them, nested. */
|
|
3
|
+
export type CssInput = WebStyleObject | string | boolean | null | undefined | readonly CssInput[];
|
|
4
|
+
/** What `cx` takes: class names, objects whose keys are class names that are used when their value is truthy, nothing, and lists of them, nested. */
|
|
5
|
+
export type ClassNamesInput = string | boolean | null | undefined | Readonly<Record<string, unknown>> | readonly ClassNamesInput[];
|
|
6
|
+
/** What `injectGlobal` takes: global style objects (selectors and at-rules as keys), nothing, and lists of them, nested. */
|
|
7
|
+
export type InjectGlobalInput = GlobalStyleObject | boolean | null | undefined | readonly InjectGlobalInput[];
|
|
8
|
+
/** `css`: one class name for its arguments, or for a tagged template. */
|
|
9
|
+
export interface CssFunction {
|
|
10
|
+
(template: TemplateStringsArray, ...values: unknown[]): string;
|
|
11
|
+
(...args: CssInput[]): string;
|
|
12
|
+
}
|
|
13
|
+
/** `keyframes`: the name of an animation, from its steps or from a tagged template. */
|
|
14
|
+
export interface KeyframesFunction {
|
|
15
|
+
(template: TemplateStringsArray, ...values: unknown[]): string;
|
|
16
|
+
(steps: KeyframesSteps): string;
|
|
17
|
+
}
|
|
18
|
+
/** `injectGlobal`: writes global rules, from style objects or from a tagged template. */
|
|
19
|
+
export interface InjectGlobalFunction {
|
|
20
|
+
(template: TemplateStringsArray, ...values: unknown[]): void;
|
|
21
|
+
(...styles: InjectGlobalInput[]): void;
|
|
22
|
+
}
|
|
23
|
+
/** A cache made by other code, that an instance shares: it may carry the sheet it writes to. */
|
|
24
|
+
export type SharedCache = Cache & {
|
|
25
|
+
readonly sheet?: Sheet;
|
|
26
|
+
};
|
|
27
|
+
/** The options of `createInstance`: those of the core's `createCache`, with `key` for `prefix`, or a cache to share. */
|
|
28
|
+
export interface InstanceOptions extends Omit<CacheOptions, 'prefix'> {
|
|
29
|
+
/** The first part of every class name, and the key of the style element (`data-style="<key>"`). Default `css`. */
|
|
30
|
+
key?: string;
|
|
31
|
+
/**
|
|
32
|
+
* Whether every rule goes into a cascade layer. Default `false` here (the core's default is `true`):
|
|
33
|
+
* a class of this package is then an ordinary class next to the app's own CSS, which is not layered.
|
|
34
|
+
*/
|
|
35
|
+
hasLayers?: boolean;
|
|
36
|
+
/**
|
|
37
|
+
* A cache to use instead of making one, so that the classes of this instance and those of other code
|
|
38
|
+
* live in one cache. Its options are its own: the other cache options are not read, and a `key` that
|
|
39
|
+
* differs from its prefix throws. The sheet is `sheet`, or the cache's own `sheet` member.
|
|
40
|
+
*/
|
|
41
|
+
cache?: SharedCache;
|
|
42
|
+
}
|
|
43
|
+
/** The functions of one instance, bound to its cache. */
|
|
44
|
+
export interface CssInstance {
|
|
45
|
+
/**
|
|
46
|
+
* One class for style objects and class names this cache made (resolved back to their styles), merged
|
|
47
|
+
* left to right so the last wins; a string that holds `:` or `{` is CSS text, read as a template is and
|
|
48
|
+
* merged at its place; other class names pass through, in front of it. Lists are
|
|
49
|
+
* flattened; `false`, `null`, `undefined`, `''` and `true` are nothing. As a tag, the template is
|
|
50
|
+
* read as CSS text. Returns the class string, '' when there is nothing.
|
|
51
|
+
*/
|
|
52
|
+
css: CssFunction;
|
|
53
|
+
/**
|
|
54
|
+
* The class string of its arguments: when two or more of the classes were made by this cache, they
|
|
55
|
+
* are merged into one new class, the last winning, after the other class names; else the classes as
|
|
56
|
+
* they are.
|
|
57
|
+
*/
|
|
58
|
+
cx(...classNames: ClassNamesInput[]): string;
|
|
59
|
+
/** Registers the animation, writes its `@keyframes` rule at once, and returns its name. */
|
|
60
|
+
keyframes: KeyframesFunction;
|
|
61
|
+
/** Writes global rules at once; they stay. Rules of the same text are written once. */
|
|
62
|
+
injectGlobal: InjectGlobalFunction;
|
|
63
|
+
/** The cache. After `flush`, it reads the new one. */
|
|
64
|
+
readonly cache: Cache;
|
|
65
|
+
/** The sheet the cache writes to. */
|
|
66
|
+
readonly sheet: Sheet;
|
|
67
|
+
/**
|
|
68
|
+
* Removes every rule from the sheet and starts a new, empty cache over it: the classes, the global
|
|
69
|
+
* rules and the hydrated ids are forgotten, and a style is written again when it is next used. Throws
|
|
70
|
+
* a `TypeError` for an instance that was given its cache, which other code shares.
|
|
71
|
+
*/
|
|
72
|
+
flush(): void;
|
|
73
|
+
/** Marks rules as present on the page (the ids of the core's `extract()`), so that they are not written again. */
|
|
74
|
+
hydrate(ids: readonly string[]): void;
|
|
75
|
+
/**
|
|
76
|
+
* Pushes the style object of each class of `classNames` that this cache made onto `registeredStyles`,
|
|
77
|
+
* in order, and returns the other class names, each followed by a space.
|
|
78
|
+
*/
|
|
79
|
+
getRegisteredStyles(registeredStyles: StyleObject[], classNames: string): string;
|
|
80
|
+
/**
|
|
81
|
+
* `className` with the classes this cache made merged into one new class, the last winning, after the
|
|
82
|
+
* other class names, when there are two or more of them; else `className` as it is.
|
|
83
|
+
*/
|
|
84
|
+
merge(className: string): string;
|
|
85
|
+
}
|
|
86
|
+
/** Makes an instance: a cache of its own (or the one given) and the functions bound to it. */
|
|
87
|
+
export default function createInstance(options?: InstanceOptions): CssInstance;
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import { createCache, createDomSheet, createMemorySheet, keyframes, parseKeyframesTemplate, parseStyleTemplate } from "@umber-style/core";
|
|
2
|
+
// src/createInstance.ts
|
|
3
|
+
/** What makes a string given to `css` CSS text rather than class names: a class name holds neither. */
|
|
4
|
+
const CSS_TEXT = /[:{]/;
|
|
5
|
+
/** Whether `value` is the strings of a tagged template. */
|
|
6
|
+
function isTemplate(value) {
|
|
7
|
+
return Array.isArray(value) && Object.hasOwn(value, "raw");
|
|
8
|
+
}
|
|
9
|
+
/** The sheet of a cache made here: the style elements of the document, or one in memory where there is no document. */
|
|
10
|
+
function createDefaultSheet(prefix) {
|
|
11
|
+
return typeof document === "undefined" ? createMemorySheet() : createDomSheet(prefix);
|
|
12
|
+
}
|
|
13
|
+
/** The items of `args`, lists flattened, without `false`, `null`, `undefined`, `''` and `true`. */
|
|
14
|
+
function flatten(args, out = []) {
|
|
15
|
+
for (const arg of args) if (Array.isArray(arg)) flatten(arg, out);
|
|
16
|
+
else if (arg && arg !== true) out.push(arg);
|
|
17
|
+
return out;
|
|
18
|
+
}
|
|
19
|
+
/** The class names of `cx` arguments, in order. */
|
|
20
|
+
function collectClassNames(args, out = []) {
|
|
21
|
+
for (const arg of flatten(args)) if (typeof arg === "string") out.push(arg);
|
|
22
|
+
else for (const [name, isOn] of Object.entries(arg)) if (isOn) out.push(name);
|
|
23
|
+
return out;
|
|
24
|
+
}
|
|
25
|
+
/** What a template gave, or a `TypeError` when it gave a function: an interpolation needs props or a theme, and there are none here. */
|
|
26
|
+
function readTemplate(name, result) {
|
|
27
|
+
if (typeof result === "function") throw new TypeError(`${name}(): an interpolation of this template is a function, which needs props or a theme, and ${name}() has none. Call it yourself and interpolate what it returns.`);
|
|
28
|
+
return result;
|
|
29
|
+
}
|
|
30
|
+
/** Makes an instance: a cache of its own (or the one given) and the functions bound to it. */
|
|
31
|
+
function createInstance(options = {}) {
|
|
32
|
+
const { key, cache: given, sheet: givenSheet, hasLayers = false, ...cacheOptions } = options;
|
|
33
|
+
if (given && key !== void 0 && key !== given.prefix) throw new TypeError(`createInstance(): the key "${key}" differs from the prefix "${given.prefix}" of the given cache. Leave out \`key\`, or give the same one.`);
|
|
34
|
+
const prefix = key ?? "css";
|
|
35
|
+
const sheet = givenSheet ?? (given ? given.sheet : createDefaultSheet(prefix));
|
|
36
|
+
if (!sheet) throw new TypeError("createInstance(): the given cache has no `sheet` member: give the sheet it writes to as `sheet` too.");
|
|
37
|
+
const makeCache = () => createCache({
|
|
38
|
+
...cacheOptions,
|
|
39
|
+
prefix,
|
|
40
|
+
hasLayers,
|
|
41
|
+
sheet
|
|
42
|
+
});
|
|
43
|
+
let current = given ?? makeCache();
|
|
44
|
+
const hydrated = /* @__PURE__ */ new Set();
|
|
45
|
+
/** A class name this cache made, as its style, so that a template merges it; anything else as it is. */
|
|
46
|
+
const resolveClass = (value) => typeof value === "string" && current.registered.get(value) || value;
|
|
47
|
+
/** A string that is not a class of this cache and holds `:` or `{`, as the style of its CSS text; anything else as it is. */
|
|
48
|
+
const readText = (value) => typeof value === "string" && CSS_TEXT.test(value) && !current.registered.has(value) ? parseStyleTemplate([value], []) : value;
|
|
49
|
+
const css = ((...args) => {
|
|
50
|
+
const [first, ...values] = args;
|
|
51
|
+
if (args.length === 1 && typeof first === "object" && first !== null && !Array.isArray(first)) return current.css(first);
|
|
52
|
+
if (isTemplate(first)) return current.css(readTemplate("css", parseStyleTemplate(first, values.map(resolveClass))));
|
|
53
|
+
return current.css(...flatten(args).map(readText));
|
|
54
|
+
});
|
|
55
|
+
const merge = (className) => {
|
|
56
|
+
return className.split(/\s+/).filter((name) => current.registered.has(name)).length < 2 ? className : current.css(className);
|
|
57
|
+
};
|
|
58
|
+
const keyframes$1 = ((first, ...values) => {
|
|
59
|
+
const steps = isTemplate(first) ? readTemplate("keyframes", parseKeyframesTemplate(first, values)) : first;
|
|
60
|
+
const name = keyframes(steps);
|
|
61
|
+
current.insertKeyframes(name);
|
|
62
|
+
return name;
|
|
63
|
+
});
|
|
64
|
+
const injectGlobal = ((...args) => {
|
|
65
|
+
const [first, ...values] = args;
|
|
66
|
+
const styles = isTemplate(first) ? [readTemplate("injectGlobal", parseStyleTemplate(first, values))] : flatten(args);
|
|
67
|
+
for (const style of styles) {
|
|
68
|
+
const rules = current.compileGlobal(style);
|
|
69
|
+
if (!hydrated.has(rules.id)) current.insertGlobal(rules);
|
|
70
|
+
}
|
|
71
|
+
});
|
|
72
|
+
return {
|
|
73
|
+
css,
|
|
74
|
+
cx: (...classNames) => merge(collectClassNames(classNames).join(" ")),
|
|
75
|
+
keyframes: keyframes$1,
|
|
76
|
+
injectGlobal,
|
|
77
|
+
cache: given ?? new Proxy({}, { get: (_target, member) => Reflect.get(current, member) }),
|
|
78
|
+
sheet,
|
|
79
|
+
flush: () => {
|
|
80
|
+
if (given) throw new TypeError("flush(): this instance was given its cache, which other code shares, so it cannot empty it.");
|
|
81
|
+
for (let index = sheet.rules.length - 1; index >= 0; index--) sheet.remove?.(index);
|
|
82
|
+
hydrated.clear();
|
|
83
|
+
current = makeCache();
|
|
84
|
+
},
|
|
85
|
+
hydrate: (ids) => {
|
|
86
|
+
for (const id of ids) {
|
|
87
|
+
hydrated.add(id);
|
|
88
|
+
current.insertOnce(id, []);
|
|
89
|
+
}
|
|
90
|
+
},
|
|
91
|
+
getRegisteredStyles: (registeredStyles, classNames) => {
|
|
92
|
+
let raw = "";
|
|
93
|
+
for (const name of classNames.split(/\s+/)) {
|
|
94
|
+
const style = current.registered.get(name);
|
|
95
|
+
if (style) registeredStyles.push(style);
|
|
96
|
+
else if (name) raw += `${name} `;
|
|
97
|
+
}
|
|
98
|
+
return raw;
|
|
99
|
+
},
|
|
100
|
+
merge
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
export { createInstance as default };
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
// src/index.ts
|
|
3
|
+
/** The default instance: key `css`, rules outside every cascade layer. */
|
|
4
|
+
const { css, cx, keyframes, injectGlobal, cache, sheet, flush, hydrate, getRegisteredStyles, merge } = require("./create-instance.cjs").default({ key: "css" });
|
|
5
|
+
exports.cache = cache;
|
|
6
|
+
exports.css = css;
|
|
7
|
+
exports.cx = cx;
|
|
8
|
+
exports.flush = flush;
|
|
9
|
+
exports.getRegisteredStyles = getRegisteredStyles;
|
|
10
|
+
exports.hydrate = hydrate;
|
|
11
|
+
exports.injectGlobal = injectGlobal;
|
|
12
|
+
exports.keyframes = keyframes;
|
|
13
|
+
exports.merge = merge;
|
|
14
|
+
exports.sheet = sheet;
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { CSSObject, Interpolation, Keyframes, SerializedStyles, Theme, UmberCache } from "@umber-style/core";
|
|
2
|
+
import { ClassNamesInput, CssFunction, CssInput, CssInstance, InjectGlobalFunction, InjectGlobalInput, InstanceOptions, KeyframesFunction, SharedCache } from "./create-instance.cjs";
|
|
3
|
+
/** The default instance: key `css`, rules outside every cascade layer. */
|
|
4
|
+
export declare const css: CssFunction, cx: (...classNames: ClassNamesInput[]) => string, keyframes: KeyframesFunction, injectGlobal: InjectGlobalFunction, cache: import("@umber-style/core").Cache, sheet: import("@umber-style/core").Sheet, flush: () => void, hydrate: (ids: readonly string[]) => void, getRegisteredStyles: (registeredStyles: import("@umber-style/core").StyleObject[], classNames: string) => string, merge: (className: string) => string;
|
|
5
|
+
export type { CSSObject, ClassNamesInput, CssFunction, CssInput, CssInstance, InjectGlobalFunction, InjectGlobalInput, InstanceOptions, Interpolation, Keyframes, KeyframesFunction, SerializedStyles, SharedCache, Theme, UmberCache };
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { ClassNamesInput, CssFunction, CssInput, CssInstance, InjectGlobalFunction, InjectGlobalInput, InstanceOptions, KeyframesFunction, SharedCache } from "./create-instance.mjs";
|
|
2
|
+
import { CSSObject, Interpolation, Keyframes, SerializedStyles, Theme, UmberCache } from "@umber-style/core";
|
|
3
|
+
/** The default instance: key `css`, rules outside every cascade layer. */
|
|
4
|
+
export declare const css: CssFunction, cx: (...classNames: ClassNamesInput[]) => string, keyframes: KeyframesFunction, injectGlobal: InjectGlobalFunction, cache: import("@umber-style/core").Cache, sheet: import("@umber-style/core").Sheet, flush: () => void, hydrate: (ids: readonly string[]) => void, getRegisteredStyles: (registeredStyles: import("@umber-style/core").StyleObject[], classNames: string) => string, merge: (className: string) => string;
|
|
5
|
+
export type { CSSObject, ClassNamesInput, CssFunction, CssInput, CssInstance, InjectGlobalFunction, InjectGlobalInput, InstanceOptions, Interpolation, Keyframes, KeyframesFunction, SerializedStyles, SharedCache, Theme, UmberCache };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import createInstance from "./create-instance.mjs";
|
|
2
|
+
// src/index.ts
|
|
3
|
+
/** The default instance: key `css`, rules outside every cascade layer. */
|
|
4
|
+
const { css, cx, keyframes, injectGlobal, cache, sheet, flush, hydrate, getRegisteredStyles, merge } = createInstance({ key: "css" });
|
|
5
|
+
export { cache, css, cx, flush, getRegisteredStyles, hydrate, injectGlobal, keyframes, merge, sheet };
|
package/package.json
CHANGED
|
@@ -1,10 +1,77 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@umber-style/css",
|
|
3
|
-
"version": "0.0.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "0.1.0-alpha.1",
|
|
4
|
+
"description": "UmberStyle's plain layer: a style goes to a cache and comes back as a class name. No framework, no theme.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"styling",
|
|
7
|
+
"css",
|
|
8
|
+
"class-names",
|
|
9
|
+
"ssr",
|
|
10
|
+
"typescript"
|
|
11
|
+
],
|
|
5
12
|
"license": "MIT",
|
|
13
|
+
"author": "Masoud Soroush <masoud@soroush.tech>",
|
|
14
|
+
"homepage": "https://umber-style.soroush.design",
|
|
6
15
|
"repository": {
|
|
7
16
|
"type": "git",
|
|
8
|
-
"url": "git+https://github.com/soroush-tech/umber-style.git"
|
|
9
|
-
|
|
10
|
-
}
|
|
17
|
+
"url": "git+https://github.com/soroush-tech/umber-style.git",
|
|
18
|
+
"directory": "packages/css"
|
|
19
|
+
},
|
|
20
|
+
"bugs": {
|
|
21
|
+
"url": "https://github.com/soroush-tech/umber-style/issues"
|
|
22
|
+
},
|
|
23
|
+
"type": "module",
|
|
24
|
+
"sideEffects": false,
|
|
25
|
+
"files": [
|
|
26
|
+
"dist"
|
|
27
|
+
],
|
|
28
|
+
"exports": {
|
|
29
|
+
".": {
|
|
30
|
+
"import": {
|
|
31
|
+
"types": "./dist/index.d.mts",
|
|
32
|
+
"default": "./dist/index.mjs"
|
|
33
|
+
},
|
|
34
|
+
"require": {
|
|
35
|
+
"types": "./dist/index.d.cts",
|
|
36
|
+
"default": "./dist/index.cjs"
|
|
37
|
+
}
|
|
38
|
+
},
|
|
39
|
+
"./create-instance": {
|
|
40
|
+
"import": {
|
|
41
|
+
"types": "./dist/create-instance.d.mts",
|
|
42
|
+
"default": "./dist/create-instance.mjs"
|
|
43
|
+
},
|
|
44
|
+
"require": {
|
|
45
|
+
"types": "./dist/create-instance.d.cts",
|
|
46
|
+
"default": "./dist/create-instance.cjs"
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
},
|
|
50
|
+
"publishConfig": {
|
|
51
|
+
"access": "public"
|
|
52
|
+
},
|
|
53
|
+
"dependencies": {
|
|
54
|
+
"@umber-style/core": "0.1.0-alpha.3"
|
|
55
|
+
},
|
|
56
|
+
"devDependencies": {
|
|
57
|
+
"@soroush.tech/bench": "^2.2.3",
|
|
58
|
+
"@types/node": "^26.6.6",
|
|
59
|
+
"@vitest/coverage-v8": "^5.1.0",
|
|
60
|
+
"jsdom": "^31.0.0",
|
|
61
|
+
"oxlint": "^1.93.0",
|
|
62
|
+
"tsdown": "^0.23.7",
|
|
63
|
+
"typescript": "~7.0.2",
|
|
64
|
+
"vitest": "^5.1.0",
|
|
65
|
+
"@umber-style/eslint-config": "0.0.0"
|
|
66
|
+
},
|
|
67
|
+
"scripts": {
|
|
68
|
+
"build": "tsdown",
|
|
69
|
+
"lint": "oxlint --deny-warnings",
|
|
70
|
+
"typecheck": "tsc --noEmit",
|
|
71
|
+
"test": "vitest run",
|
|
72
|
+
"test:coverage": "vitest run --coverage"
|
|
73
|
+
},
|
|
74
|
+
"main": "./dist/index.cjs",
|
|
75
|
+
"module": "./dist/index.mjs",
|
|
76
|
+
"types": "./dist/index.d.mts"
|
|
77
|
+
}
|