@umber-style/vue 0.1.0-alpha.0 → 0.1.0-alpha.2

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/dist/index.mjs CHANGED
@@ -1,45 +1,217 @@
1
- import { NO_THEME_MESSAGE, add, alpha, at, createDeferredRecord, createEngine, createLazyEngine, divide, flattenStyle, isThemedSheet, mergeDeep, multiply, negate, subtract } from "@umber-style/core";
2
- import { Fragment, computed, defineComponent, h, inject, normalizeClass, provide } from "vue";
1
+ import { EMPTY_THEME, add, alpha, at, createComponentSelector, createCssEntry, createDeferredObject, createDeferredRecord, createEngine, createGlobalMarker, createLazyEngine, divide, findEntryCache, flattenStyle, globalStyles as globalStyles$1, isThemedSheet, keyframes as keyframes$1, mergeTheme, multiply, negate, readStyleArguments, resolveThemeInput, subtract, withTagShorthands } from "@umber-style/core";
2
+ import { Comment, computed, defineComponent, h, inject, mergeProps, normalizeClass, onUnmounted, provide, watch } from "vue";
3
+ // src/CacheProvider.ts
4
+ /** What the nearest `CacheProvider` provides: its cache. */
5
+ const CACHE_KEY = Symbol("cache");
6
+ let isProvided = false;
7
+ /** The cache of the nearest `CacheProvider`, or undefined with none above. Call it in `setup` or in a functional component. */
8
+ function injectCache() {
9
+ return isProvided ? inject(CACHE_KEY, void 0) : void 0;
10
+ }
11
+ /**
12
+ * Gives the subtree in its slot its cache: every rule that a component of the binding writes there goes
13
+ * to `value` (classes, sheet entries, global rules, keyframes, the root theme and the theme classes of the
14
+ * providers below), through the engine of the same theme over that cache. Outside any provider the
15
+ * engine's own cache is used. `value` is read once, in `setup`: to give the subtree another cache, render
16
+ * another provider (a new `key`).
17
+ */
18
+ const CacheProvider = defineComponent({
19
+ name: "CacheProvider",
20
+ props: {
21
+ /** The cache of the subtree: one that `createCache` of `@umber-style/cache` (or of the core) made. */
22
+ value: {
23
+ type: Object,
24
+ required: true
25
+ } },
26
+ setup(props, { slots }) {
27
+ isProvided = true;
28
+ provide(CACHE_KEY, props.value);
29
+ return () => slots.default?.();
30
+ }
31
+ });
32
+ // src/Global.ts
33
+ /** Writes `sets` into the cache of `engine`, in order, as rules that a component rendered, and returns the function that releases them all. */
34
+ function insertSets(engine, sets) {
35
+ const releases = sets.map((set) => engine.cache.insertGlobal(set, { isRendered: true }));
36
+ return () => {
37
+ for (const release of releases) release();
38
+ };
39
+ }
40
+ /**
41
+ * Makes the `Global` of an engine: it renders nothing, and writes the rules of its styles with the view
42
+ * of the theme of the nearest provider (references for tokens, real values for raw keys), or of the base
43
+ * theme (or an empty one) with none. In the browser they are
44
+ * written in `setup`, before it mounts, and released when it unmounts; when the rules change (its styles,
45
+ * or what they read of the theme) the new ones are written first and then the old ones released, so that
46
+ * there is no moment with neither. The cache counts the rules of each set, so two components with equal
47
+ * styles share them, and rules that a server render left in the page are taken over. Where there is no
48
+ * document (a server render) nothing unmounts: the rules are written for good, for `extractPageStyles`,
49
+ * and the comment it renders (a component that renders nothing renders a comment) is the marker that
50
+ * names its sets, `<!--<prefix>-global <ids>-->`, so that the CSS of this page holds them and the CSS of
51
+ * another page does not. A hydration does not compare the text of a comment.
52
+ * Under a `CacheProvider` the rules go to the cache it gives, through the engine over that cache, with
53
+ * the view of that engine.
54
+ */
55
+ function createGlobal(engine, keys) {
56
+ return defineComponent({
57
+ name: "Global",
58
+ props: {
59
+ /** A handle that `globalStyles` made, a style object, a function of the theme, or a list of them, written in the order of the list. */
60
+ styles: {
61
+ type: [
62
+ Object,
63
+ Function,
64
+ Array
65
+ ],
66
+ required: true
67
+ } },
68
+ setup(props) {
69
+ const cache = injectCache();
70
+ const local = cache === void 0 ? engine : engine.withCache(cache);
71
+ const outerView = inject(keys.view, null);
72
+ const outerTheme = inject(keys.theme, null);
73
+ const readOwnTheme = () => engine.baseTheme ?? EMPTY_THEME;
74
+ const sets = computed(() => {
75
+ const view = local === engine ? outerView?.value ?? engine.createThemeView(readOwnTheme()) : local.createThemeView(outerTheme?.value ?? readOwnTheme());
76
+ return local.resolveGlobal(props.styles, view);
77
+ });
78
+ if (typeof document === "undefined") {
79
+ insertSets(local, sets.value);
80
+ const marker = createGlobalMarker(local.cache.prefix, sets.value);
81
+ return () => marker ? h(Comment, `${marker.name} ${marker.ids}`) : null;
82
+ }
83
+ const ids = computed(() => sets.value.map(({ id }) => id).join(" "));
84
+ let release = insertSets(local, sets.value);
85
+ watch(ids, () => {
86
+ const previous = release;
87
+ release = insertSets(local, sets.value);
88
+ previous();
89
+ });
90
+ onUnmounted(() => release());
91
+ return () => null;
92
+ }
93
+ });
94
+ }
3
95
  // src/styled.ts
4
96
  /** Whether `tag` is a component made by `styled`. */
5
97
  function isStyledComponent(tag) {
6
98
  return typeof tag === "function" && "_styles" in tag;
7
99
  }
100
+ /** The function that lets go of the class that each element (or component instance) holds. */
101
+ const releases = /* @__PURE__ */ new WeakMap();
102
+ /**
103
+ * The function ref that makes the element hold the class of `hold`. Vue calls the ref of each render
104
+ * with the element when it patches it, and the last one with null when the element is unmounted or no
105
+ * longer has a ref: the element then holds the new class first and lets the old one go, or lets its
106
+ * class go. A functional component has no lifecycle of its own; the ref is the element's.
107
+ */
108
+ function createHoldRef(hold) {
109
+ let element;
110
+ return (value) => {
111
+ if (value) {
112
+ element = value;
113
+ const previous = releases.get(element);
114
+ releases.set(element, hold());
115
+ previous?.();
116
+ } else {
117
+ releases.get(element)?.();
118
+ releases.delete(element);
119
+ }
120
+ };
121
+ }
122
+ /** The attributes that the element is given: all but those of `styled` (`as`, `theme`, `class`, `style`), and of those only what `filter` lets through when there is one. */
123
+ function forwardAttributes(attrs, filter) {
124
+ const forwarded = {};
125
+ for (const [key, value] of Object.entries(attrs)) if (!(key === "as" || key === "theme" || key === "class" || key === "style") && (!filter || filter(key))) forwarded[key] = value;
126
+ return forwarded;
127
+ }
8
128
  /**
9
129
  * Makes `styled` over an engine: a functional component that renders the tag with one class. A
10
- * function style gets the view of the theme of the nearest provider, which `useThemeView` reads.
130
+ * function style gets the view of the theme of the nearest provider, which `useThemeView` reads. In the
131
+ * browser, an element given a plain object in `style` holds the class that the object made, so that the
132
+ * class is removed when no element uses it. Under a `CacheProvider` the class is made by the engine over
133
+ * the cache it gives, and written there.
11
134
  */
12
135
  function createStyled(engine, useThemeView) {
13
- return (tag, options = {}) => (...styles) => {
136
+ const styled = (tag, options = {}) => (...args) => {
137
+ const styles = readStyleArguments(args);
14
138
  const inner = isStyledComponent(tag) ? tag : void 0;
15
139
  const baseTag = inner?._tag ?? tag;
16
140
  const shouldForward = options.shouldForwardProp ?? inner?._shouldForwardProp;
141
+ const target = [inner?._target, options.target].filter(Boolean).join(" ") || void 0;
17
142
  const styler = engine.createStyler(options, styles, inner?._styles);
143
+ const stylers = /* @__PURE__ */ new WeakMap();
144
+ const readStyler = (local) => {
145
+ let made = stylers.get(local);
146
+ if (made === void 0) {
147
+ made = local.createStyler(options, styles, inner?._styles);
148
+ stylers.set(local, made);
149
+ }
150
+ return made;
151
+ };
18
152
  const Styled = Object.assign((_props, { attrs, slots }) => {
19
- const className = styler.getClassName(attrs, [...flattenStyle(attrs.style, engine.cache.findEntry), normalizeClass(attrs.class)], styler.isDynamic ? useThemeView() : void 0);
153
+ const cache = injectCache();
154
+ const local = cache === void 0 ? engine : engine.withCache(cache);
155
+ const own = local === engine ? styler : readStyler(local);
156
+ const findEntry = local === engine ? engine.cache.findEntry : (name) => local.cache.findEntry(name) ?? engine.cache.findEntry(name);
157
+ const extra = [...flattenStyle(attrs.style, findEntry), normalizeClass(attrs.class)];
158
+ const theme = own.isDynamic ? useThemeView(cache) : void 0;
159
+ const held = typeof document !== "undefined" && attrs.style ? own.getHeldClass(attrs, extra, theme) : void 0;
160
+ let className = held ? held.className : own.getClassName(attrs, extra, theme);
161
+ if (target) className = className ? `${className} ${target}` : target;
20
162
  const element = attrs.as ?? baseTag;
21
- const filter = typeof element === "string" ? shouldForward : void 0;
22
- const forwarded = {};
23
- for (const [key, value] of Object.entries(attrs)) if (!(key === "as" || key === "theme" || key === "class" || key === "style") && (!filter || filter(key))) forwarded[key] = value;
163
+ const forwarded = forwardAttributes(attrs, typeof element === "string" ? shouldForward : void 0);
24
164
  forwarded.class = className || void 0;
165
+ if (held?.hold) forwarded.ref = createHoldRef(held.hold);
25
166
  return h(element, forwarded, slots);
26
167
  }, {
27
168
  _styles: styler.styles,
28
169
  _tag: baseTag,
29
- _shouldForwardProp: shouldForward
170
+ _shouldForwardProp: shouldForward,
171
+ _target: target,
172
+ toString: createComponentSelector(options.target, styler)
30
173
  });
31
174
  Styled.inheritAttrs = false;
32
175
  return Styled;
33
176
  };
177
+ return withTagShorthands(styled);
34
178
  }
35
179
  // src/ThemeProvider.ts
36
- /** Makes the `ThemeProvider` of an engine: an element with the theme class, which provides the real values below it. */
37
- function createThemeProvider(engine, key) {
180
+ /** Makes the keys of one style. */
181
+ function createThemeKeys() {
182
+ return {
183
+ theme: Symbol("theme"),
184
+ view: Symbol("view"),
185
+ scope: Symbol("scope"),
186
+ engine: Symbol("engine")
187
+ };
188
+ }
189
+ /**
190
+ * Makes the `ThemeProvider` of an engine. Its theme (or what a function of the theme above returns) is
191
+ * merged over the theme above and provided below it, with its view.
192
+ *
193
+ * - The outermost provider (none above it) renders no element: its theme is declared on `:root`, in
194
+ * `setup` and again when it changes, which renders no styled component. It gives the engine its base
195
+ * theme when it has none.
196
+ * - A nested provider renders its wrapper: the theme class of the theme it was given (with the boundary
197
+ * and scheme markers), the theme class of its merged theme (every variable, for where it is not inside
198
+ * the wrapper above it), and the scope class, which has `display: contents`.
199
+ * - With `isRoot` it renders no element and makes no rule: the app puts `themeClass(theme)` on an
200
+ * ancestor, usually `<html>`.
201
+ *
202
+ * Whether a provider is the outermost one, and `isRoot`, are read once, in `setup`. Under a
203
+ * `CacheProvider` it works with the engine over the cache that the provider gives (the same base theme,
204
+ * the prefix of that cache), and is the outermost one when the nearest provider above it has another
205
+ * engine: its theme is declared on `:root` in that cache.
206
+ */
207
+ function createThemeProvider(baseEngine, keys) {
38
208
  return defineComponent({
39
209
  name: "ThemeProvider",
210
+ inheritAttrs: false,
40
211
  props: {
212
+ /** The theme, or the part of it that this subtree changes; or a function of the theme above that returns it. */
41
213
  theme: {
42
- type: Object,
214
+ type: [Object, Function],
43
215
  required: true
44
216
  },
45
217
  colorScheme: {
@@ -50,41 +222,81 @@ function createThemeProvider(engine, key) {
50
222
  type: String,
51
223
  default: "div"
52
224
  },
53
- /** Renders no element and no class: the app puts `themeClass(theme)` on an ancestor, usually `<html>`. */
225
+ /** Renders no element, no class and no rule: the app puts `themeClass(theme)` on an ancestor, usually `<html>`. */
54
226
  isRoot: {
55
227
  type: Boolean,
56
228
  default: false
57
229
  }
58
230
  },
59
- setup(props, { slots }) {
60
- const outer = inject(key, null);
61
- provide(key, computed(() => {
62
- const base = outer?.value ?? engine.baseTheme;
63
- const theme = props.theme;
64
- return base === void 0 ? theme : mergeDeep(base, theme);
65
- }));
66
- return () => {
67
- const theme = props.theme;
68
- if (props.isRoot) {
69
- if (!engine.hasTheme) engine.setTheme(theme);
70
- return h(Fragment, null, slots.default?.());
71
- }
72
- const scope = engine.themeClass(theme, props.colorScheme);
73
- return h(props.as, { class: [scope, engine.scopeClass()] }, slots.default?.());
74
- };
231
+ setup(props, { attrs, slots }) {
232
+ const cache = injectCache();
233
+ const engine = cache === void 0 ? baseEngine : baseEngine.withCache(cache);
234
+ const outer = inject(keys.theme, null);
235
+ const scope = inject(keys.engine, null) === engine ? inject(keys.scope, null) : null;
236
+ const isNested = scope !== null && !props.isRoot;
237
+ const readOuter = () => outer?.value ?? engine.baseTheme;
238
+ const given = computed(() => resolveThemeInput(props.theme, readOuter() ?? EMPTY_THEME));
239
+ if (!engine.hasTheme) engine.setTheme(given.value);
240
+ const merged = computed(() => mergeTheme(readOuter(), given.value));
241
+ const classes = computed(() => {
242
+ const own = engine.themeClass(given.value, props.colorScheme);
243
+ const full = engine.themeId(merged.value, props.colorScheme);
244
+ return [
245
+ own,
246
+ own.startsWith(`${full} `) ? "" : full,
247
+ engine.scopeClass()
248
+ ].filter(Boolean).join(" ");
249
+ });
250
+ provide(keys.engine, engine);
251
+ provide(keys.theme, merged);
252
+ provide(keys.view, computed(() => engine.createThemeView(merged.value)));
253
+ provide(keys.scope, isNested ? computed(() => ({
254
+ as: props.as,
255
+ className: classes.value
256
+ })) : scope ?? computed(() => null));
257
+ if (scope === null && !props.isRoot) {
258
+ const declare = () => engine.declareRoot(merged.value, props.colorScheme);
259
+ declare();
260
+ watch([merged, () => props.colorScheme], declare);
261
+ }
262
+ return () => isNested ? h(props.as, mergeProps({ class: classes.value }, attrs), slots.default?.()) : slots.default?.();
75
263
  }
76
264
  });
77
265
  }
266
+ /** Makes `ThemeScope` and `useThemeClass` over the keys of one style. */
267
+ function createThemeScope(keys) {
268
+ /** The class string of the wrapper of the nearest nested provider, reactive; undefined under the outermost provider, or none. Call it in `setup`. */
269
+ const useThemeClass = () => {
270
+ const scope = inject(keys.scope, null);
271
+ return computed(() => scope?.value?.className);
272
+ };
273
+ return {
274
+ useThemeClass,
275
+ ThemeScope: defineComponent({
276
+ name: "ThemeScope",
277
+ setup(_props, { slots }) {
278
+ const scope = inject(keys.scope, null);
279
+ return () => {
280
+ const value = scope?.value;
281
+ return value ? h(value.as, { class: value.className }, slots.default?.()) : slots.default?.();
282
+ };
283
+ }
284
+ })
285
+ };
286
+ }
78
287
  // src/createStyle.ts
79
288
  /** Makes the Vue styling over an engine: `styled`, sheets, the theme provider and `useTheme`. */
80
289
  function bindEngine(engine) {
81
- const key = Symbol("theme");
82
- const useTheme = () => inject(key, null) ?? computed(() => {
83
- const { baseTheme } = engine;
84
- if (baseTheme === void 0) throw new Error(NO_THEME_MESSAGE);
85
- return baseTheme;
86
- });
87
- const useThemeView = () => engine.createThemeView(useTheme().value);
290
+ const keys = createThemeKeys();
291
+ const { ThemeScope, useThemeClass } = createThemeScope(keys);
292
+ /** The theme where no provider is above: the base theme, or the empty theme. */
293
+ const readOwnTheme = () => engine.baseTheme ?? EMPTY_THEME;
294
+ const useTheme = () => inject(keys.theme, null) ?? computed(readOwnTheme);
295
+ const useThemeView = (cache) => {
296
+ const local = cache === void 0 ? engine : engine.withCache(cache);
297
+ if (local === engine) return inject(keys.view, null)?.value ?? engine.createThemeView(readOwnTheme());
298
+ return local.createThemeView(inject(keys.theme, null)?.value ?? readOwnTheme());
299
+ };
88
300
  function create(styles) {
89
301
  return engine.createSheet(styles);
90
302
  }
@@ -93,13 +305,23 @@ function bindEngine(engine) {
93
305
  cache: engine.cache,
94
306
  styled: createStyled(engine, useThemeView),
95
307
  StyleSheet: { create },
96
- useStyle: ((sheet) => isThemedSheet(sheet) ? sheet.resolve(useThemeView()) : sheet),
97
- apply: (...entries) => ({ class: engine.css(...entries) || void 0 }),
98
- ThemeProvider: createThemeProvider(engine, key),
308
+ useStyle: ((sheet) => {
309
+ const cache = injectCache();
310
+ const entries = isThemedSheet(sheet) ? sheet.resolve(useThemeView(cache)) : sheet;
311
+ return cache === void 0 ? entries : engine.withCache(cache).adoptSheet(entries);
312
+ }),
313
+ apply: (...entries) => ({ class: (findEntryCache(entries) ?? engine.cache).css(...entries) || void 0 }),
314
+ css: ((...args) => createCssEntry(engine.cache, args)),
315
+ ThemeProvider: createThemeProvider(engine, keys),
316
+ ThemeScope,
99
317
  useTheme,
318
+ useThemeClass,
100
319
  themeClass: engine.themeClass,
101
320
  when: engine.when,
102
- whenScheme: engine.whenScheme
321
+ whenScheme: engine.whenScheme,
322
+ keyframes: engine.keyframes,
323
+ globalStyles: globalStyles$1,
324
+ Global: createGlobal(engine, keys)
103
325
  };
104
326
  }
105
327
  /** Makes the Vue styling of one theme shape, apart from the default one: its own engine, or the one given. */
@@ -126,12 +348,38 @@ function createDefaultStyle() {
126
348
  if (bound) throw new Error("configure must be called before the default style is used.");
127
349
  pending = options;
128
350
  }
129
- const styled = (tag, options) => getStyle().styled(tag, options);
351
+ const styled = withTagShorthands(((tag, options) => (...args) => getStyle().styled(tag, options)(...args)));
130
352
  const StyleSheet = { create: ((styles) => typeof styles === "function" ? getStyle().StyleSheet.create(styles) : createDeferredRecord(styles, () => getStyle().StyleSheet.create(styles))) };
131
353
  const useStyle = ((sheet) => getStyle().useStyle(sheet));
132
354
  function useTheme() {
133
355
  return getStyle().useTheme();
134
356
  }
357
+ const ThemeProvider = defineComponent({
358
+ name: "ThemeProvider",
359
+ inheritAttrs: false,
360
+ setup(_props, { attrs, slots }) {
361
+ const Provider = getStyle().ThemeProvider;
362
+ return () => h(Provider, attrs, slots);
363
+ }
364
+ });
365
+ const ThemeScope = defineComponent({
366
+ name: "ThemeScope",
367
+ setup(_props, { slots }) {
368
+ const Real = getStyle().ThemeScope;
369
+ return () => h(Real, null, slots);
370
+ }
371
+ });
372
+ function useThemeClass() {
373
+ return getStyle().useThemeClass();
374
+ }
375
+ const Global = defineComponent({
376
+ name: "Global",
377
+ inheritAttrs: false,
378
+ setup(_props, { attrs }) {
379
+ const Real = getStyle().Global;
380
+ return () => h(Real, attrs);
381
+ }
382
+ });
135
383
  return {
136
384
  configure,
137
385
  getEngine: () => getStyle().engine,
@@ -139,19 +387,18 @@ function createDefaultStyle() {
139
387
  StyleSheet,
140
388
  useStyle,
141
389
  useTheme,
142
- ThemeProvider: defineComponent({
143
- name: "ThemeProvider",
144
- inheritAttrs: false,
145
- setup(_props, { attrs, slots }) {
146
- const Provider = getStyle().ThemeProvider;
147
- return () => h(Provider, attrs, slots);
148
- }
149
- }),
390
+ ThemeProvider,
391
+ ThemeScope,
392
+ useThemeClass,
150
393
  apply: ((...entries) => getStyle().apply(...entries)),
394
+ css: ((...args) => createDeferredObject(() => getStyle().css(...args))),
151
395
  when: ((theme, colorScheme) => getStyle().when(theme, colorScheme)),
152
396
  whenScheme: ((scheme) => getStyle().whenScheme(scheme)),
153
- themeClass: ((theme, colorScheme) => getStyle().themeClass(theme, colorScheme))
397
+ themeClass: ((theme, colorScheme) => getStyle().themeClass(theme, colorScheme)),
398
+ keyframes: keyframes$1,
399
+ globalStyles: globalStyles$1,
400
+ Global
154
401
  };
155
402
  }
156
- const { configure, getEngine, styled, StyleSheet, useStyle, useTheme, ThemeProvider, apply, themeClass, when, whenScheme } = createDefaultStyle();
157
- export { StyleSheet, ThemeProvider, add, alpha, apply, at, bindEngine, configure, createDefaultStyle, createStyle, divide, getEngine, multiply, negate, styled, subtract, themeClass, useStyle, useTheme, when, whenScheme };
403
+ const { configure, getEngine, styled, StyleSheet, useStyle, useTheme, ThemeProvider, ThemeScope, useThemeClass, apply, css, themeClass, when, whenScheme, keyframes, globalStyles, Global } = createDefaultStyle();
404
+ export { CacheProvider, Global, StyleSheet, ThemeProvider, ThemeScope, add, alpha, apply, at, bindEngine, configure, createDefaultStyle, createStyle, css, divide, getEngine, globalStyles, keyframes, multiply, negate, styled, subtract, themeClass, useStyle, useTheme, useThemeClass, when, whenScheme };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@umber-style/vue",
3
- "version": "0.1.0-alpha.0",
3
+ "version": "0.1.0-alpha.2",
4
4
  "description": "UmberStyle for Vue: the same names as the React binding, with a reactive useTheme.",
5
5
  "keywords": [
6
6
  "styling",
@@ -43,12 +43,13 @@
43
43
  "access": "public"
44
44
  },
45
45
  "dependencies": {
46
- "@umber-style/core": "0.1.0-alpha.0"
46
+ "@umber-style/core": "0.1.0-alpha.5"
47
47
  },
48
48
  "peerDependencies": {
49
49
  "vue": "^3.5.0"
50
50
  },
51
51
  "devDependencies": {
52
+ "@soroush.tech/bench": "^2.2.3",
52
53
  "@types/node": "^26.6.6",
53
54
  "@vitest/coverage-v8": "^5.1.0",
54
55
  "jsdom": "^31.0.0",
@@ -57,7 +58,6 @@
57
58
  "typescript": "~7.0.2",
58
59
  "vitest": "^5.1.0",
59
60
  "vue": "^3.5.45",
60
- "@umber-bench/shared": "0.1.0",
61
61
  "@umber-style/eslint-config": "0.0.0"
62
62
  },
63
63
  "scripts": {