@umber-style/css 0.1.0 → 0.2.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.
@@ -31,7 +31,6 @@ function readTemplate(name, result) {
31
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
32
  return result;
33
33
  }
34
- /** Makes an instance: a cache of its own (or the one given) and the functions bound to it. */
35
34
  function createInstance(options = {}) {
36
35
  const { key, cache: given, sheet: givenSheet, hasLayers = false, ...cacheOptions } = options;
37
36
  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.`);
@@ -82,7 +81,8 @@ function createInstance(options = {}) {
82
81
  sheet,
83
82
  flush: () => {
84
83
  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);
84
+ const own = sheet;
85
+ for (let index = own.rules.length - 1; index >= 0; index--) own.remove?.(index);
86
86
  hydrated.clear();
87
87
  current = makeCache();
88
88
  },
@@ -1,4 +1,4 @@
1
- import { Cache, CacheOptions, GlobalStyleObject, KeyframesSteps, Sheet, StyleObject, WebStyleObject } from "@umber-style/core";
1
+ import { Cache, CacheOptions, GlobalStyleObject, KeyframesSteps, Sheet, StyleObject, UmberCache, UmberSheet, WebStyleObject } from "@umber-style/core";
2
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
3
  export type CssInput = WebStyleObject | string | boolean | null | undefined | readonly CssInput[];
4
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. */
@@ -20,9 +20,12 @@ export interface InjectGlobalFunction {
20
20
  (template: TemplateStringsArray, ...values: unknown[]): void;
21
21
  (...styles: InjectGlobalInput[]): void;
22
22
  }
23
- /** A cache made by other code, that an instance shares: it may carry the sheet it writes to. */
23
+ /**
24
+ * A cache made by other code, that an instance shares: it may carry the sheet it writes to, or, as a cache
25
+ * of `@umber-style/cache` does, the style elements that hold its rules (`UmberSheet`).
26
+ */
24
27
  export type SharedCache = Cache & {
25
- readonly sheet?: Sheet;
28
+ readonly sheet?: Sheet | UmberSheet;
26
29
  };
27
30
  /** The options of `createInstance`: those of the core's `createCache`, with `key` for `prefix`, or a cache to share. */
28
31
  export interface InstanceOptions extends Omit<CacheOptions, 'prefix'> {
@@ -36,12 +39,13 @@ export interface InstanceOptions extends Omit<CacheOptions, 'prefix'> {
36
39
  /**
37
40
  * A cache to use instead of making one, so that the classes of this instance and those of other code
38
41
  * 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.
42
+ * differs from its prefix throws. The sheet of the instance is `sheet`, or the cache's own `sheet`
43
+ * member: for a cache of `@umber-style/cache`, its style elements.
40
44
  */
41
45
  cache?: SharedCache;
42
46
  }
43
- /** The functions of one instance, bound to its cache. */
44
- export interface CssInstance {
47
+ /** The functions of one instance, bound to its cache; `S` is the type of its sheet. */
48
+ export interface CssInstance<S extends Sheet | UmberSheet = Sheet> {
45
49
  /**
46
50
  * One class for style objects and class names this cache made (resolved back to their styles), merged
47
51
  * left to right so the last wins; a string that holds `:` or `{` is CSS text, read as a template is and
@@ -62,8 +66,8 @@ export interface CssInstance {
62
66
  injectGlobal: InjectGlobalFunction;
63
67
  /** The cache. After `flush`, it reads the new one. */
64
68
  readonly cache: Cache;
65
- /** The sheet the cache writes to. */
66
- readonly sheet: Sheet;
69
+ /** The sheet the cache writes to; for an instance given a cache of `@umber-style/cache` and no `sheet`, the style elements of that cache. */
70
+ readonly sheet: S;
67
71
  /**
68
72
  * Removes every rule from the sheet and starts a new, empty cache over it: the classes, the global
69
73
  * rules and the hydrated ids are forgotten, and a style is written again when it is next used. Throws
@@ -83,5 +87,11 @@ export interface CssInstance {
83
87
  */
84
88
  merge(className: string): string;
85
89
  }
90
+ /** Makes an instance over a cache of `@umber-style/cache`: its sheet is the style elements of that cache. */
91
+ declare function createInstance(options: InstanceOptions & {
92
+ cache: UmberCache;
93
+ sheet?: undefined;
94
+ }): CssInstance<UmberSheet>;
86
95
  /** 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;
96
+ declare function createInstance(options?: InstanceOptions): CssInstance;
97
+ export { createInstance as default };
@@ -1,4 +1,4 @@
1
- import { Cache, CacheOptions, GlobalStyleObject, KeyframesSteps, Sheet, StyleObject, WebStyleObject } from "@umber-style/core";
1
+ import { Cache, CacheOptions, GlobalStyleObject, KeyframesSteps, Sheet, StyleObject, UmberCache, UmberSheet, WebStyleObject } from "@umber-style/core";
2
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
3
  export type CssInput = WebStyleObject | string | boolean | null | undefined | readonly CssInput[];
4
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. */
@@ -20,9 +20,12 @@ export interface InjectGlobalFunction {
20
20
  (template: TemplateStringsArray, ...values: unknown[]): void;
21
21
  (...styles: InjectGlobalInput[]): void;
22
22
  }
23
- /** A cache made by other code, that an instance shares: it may carry the sheet it writes to. */
23
+ /**
24
+ * A cache made by other code, that an instance shares: it may carry the sheet it writes to, or, as a cache
25
+ * of `@umber-style/cache` does, the style elements that hold its rules (`UmberSheet`).
26
+ */
24
27
  export type SharedCache = Cache & {
25
- readonly sheet?: Sheet;
28
+ readonly sheet?: Sheet | UmberSheet;
26
29
  };
27
30
  /** The options of `createInstance`: those of the core's `createCache`, with `key` for `prefix`, or a cache to share. */
28
31
  export interface InstanceOptions extends Omit<CacheOptions, 'prefix'> {
@@ -36,12 +39,13 @@ export interface InstanceOptions extends Omit<CacheOptions, 'prefix'> {
36
39
  /**
37
40
  * A cache to use instead of making one, so that the classes of this instance and those of other code
38
41
  * 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.
42
+ * differs from its prefix throws. The sheet of the instance is `sheet`, or the cache's own `sheet`
43
+ * member: for a cache of `@umber-style/cache`, its style elements.
40
44
  */
41
45
  cache?: SharedCache;
42
46
  }
43
- /** The functions of one instance, bound to its cache. */
44
- export interface CssInstance {
47
+ /** The functions of one instance, bound to its cache; `S` is the type of its sheet. */
48
+ export interface CssInstance<S extends Sheet | UmberSheet = Sheet> {
45
49
  /**
46
50
  * One class for style objects and class names this cache made (resolved back to their styles), merged
47
51
  * left to right so the last wins; a string that holds `:` or `{` is CSS text, read as a template is and
@@ -62,8 +66,8 @@ export interface CssInstance {
62
66
  injectGlobal: InjectGlobalFunction;
63
67
  /** The cache. After `flush`, it reads the new one. */
64
68
  readonly cache: Cache;
65
- /** The sheet the cache writes to. */
66
- readonly sheet: Sheet;
69
+ /** The sheet the cache writes to; for an instance given a cache of `@umber-style/cache` and no `sheet`, the style elements of that cache. */
70
+ readonly sheet: S;
67
71
  /**
68
72
  * Removes every rule from the sheet and starts a new, empty cache over it: the classes, the global
69
73
  * rules and the hydrated ids are forgotten, and a style is written again when it is next used. Throws
@@ -83,5 +87,11 @@ export interface CssInstance {
83
87
  */
84
88
  merge(className: string): string;
85
89
  }
90
+ /** Makes an instance over a cache of `@umber-style/cache`: its sheet is the style elements of that cache. */
91
+ declare function createInstance(options: InstanceOptions & {
92
+ cache: UmberCache;
93
+ sheet?: undefined;
94
+ }): CssInstance<UmberSheet>;
86
95
  /** 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;
96
+ declare function createInstance(options?: InstanceOptions): CssInstance;
97
+ export { createInstance as default };
@@ -27,7 +27,6 @@ function readTemplate(name, result) {
27
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
28
  return result;
29
29
  }
30
- /** Makes an instance: a cache of its own (or the one given) and the functions bound to it. */
31
30
  function createInstance(options = {}) {
32
31
  const { key, cache: given, sheet: givenSheet, hasLayers = false, ...cacheOptions } = options;
33
32
  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.`);
@@ -78,7 +77,8 @@ function createInstance(options = {}) {
78
77
  sheet,
79
78
  flush: () => {
80
79
  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);
80
+ const own = sheet;
81
+ for (let index = own.rules.length - 1; index >= 0; index--) own.remove?.(index);
82
82
  hydrated.clear();
83
83
  current = makeCache();
84
84
  },
package/dist/index.cjs CHANGED
@@ -1,7 +1,9 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
+ let _umber_style_core = require("@umber-style/core");
3
+ const require_create_instance = require("./create-instance.cjs");
2
4
  // 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
+ /** The default instance: key `css`, rules outside every cascade layer. Every copy of the package in the program shares it, so that a class one copy made is merged and extracted by another. */
6
+ const { css, cx, keyframes, injectGlobal, cache, sheet, flush, hydrate, getRegisteredStyles, merge } = (0, _umber_style_core.readSharedState)("css.default-instance@1", () => require_create_instance.default({ key: "css" }));
5
7
  exports.cache = cache;
6
8
  exports.css = css;
7
9
  exports.cx = cx;
package/dist/index.d.cts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { CSSObject, Interpolation, Keyframes, SerializedStyles, StyleTypeOptions, Theme, UmberCache } from "@umber-style/core";
2
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. */
3
+ /** The default instance: key `css`, rules outside every cascade layer. Every copy of the package in the program shares it, so that a class one copy made is merged and extracted by another. */
4
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
5
  export type { CSSObject, ClassNamesInput, CssFunction, CssInput, CssInstance, InjectGlobalFunction, InjectGlobalInput, InstanceOptions, Interpolation, Keyframes, KeyframesFunction, SerializedStyles, SharedCache, StyleTypeOptions, Theme, UmberCache };
package/dist/index.d.mts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { ClassNamesInput, CssFunction, CssInput, CssInstance, InjectGlobalFunction, InjectGlobalInput, InstanceOptions, KeyframesFunction, SharedCache } from "./create-instance.mjs";
2
2
  import { CSSObject, Interpolation, Keyframes, SerializedStyles, StyleTypeOptions, Theme, UmberCache } from "@umber-style/core";
3
- /** The default instance: key `css`, rules outside every cascade layer. */
3
+ /** The default instance: key `css`, rules outside every cascade layer. Every copy of the package in the program shares it, so that a class one copy made is merged and extracted by another. */
4
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
5
  export type { CSSObject, ClassNamesInput, CssFunction, CssInput, CssInstance, InjectGlobalFunction, InjectGlobalInput, InstanceOptions, Interpolation, Keyframes, KeyframesFunction, SerializedStyles, SharedCache, StyleTypeOptions, Theme, UmberCache };
package/dist/index.mjs CHANGED
@@ -1,5 +1,6 @@
1
+ import { readSharedState } from "@umber-style/core";
1
2
  import createInstance from "./create-instance.mjs";
2
3
  // 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" });
4
+ /** The default instance: key `css`, rules outside every cascade layer. Every copy of the package in the program shares it, so that a class one copy made is merged and extracted by another. */
5
+ const { css, cx, keyframes, injectGlobal, cache, sheet, flush, hydrate, getRegisteredStyles, merge } = readSharedState("css.default-instance@1", () => createInstance({ key: "css" }));
5
6
  export { cache, css, cx, flush, getRegisteredStyles, hydrate, injectGlobal, keyframes, merge, sheet };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@umber-style/css",
3
- "version": "0.1.0",
3
+ "version": "0.2.0-alpha.1",
4
4
  "description": "UmberStyle's plain layer: a style goes to a cache and comes back as a class name. No framework, no theme.",
5
5
  "keywords": [
6
6
  "styling",
@@ -51,7 +51,7 @@
51
51
  "access": "public"
52
52
  },
53
53
  "dependencies": {
54
- "@umber-style/core": "0.1.0"
54
+ "@umber-style/core": "^0.2.0-alpha.1"
55
55
  },
56
56
  "devDependencies": {
57
57
  "@soroush.tech/bench": "^2.2.3",