@expressive/dom 0.8.2 → 0.9.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/README.md +2 -2
- package/dist/index.d.ts +30 -40
- package/dist/index.js +195 -175
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -5,105 +5,23 @@ import { Component, Context, Model, has, map, unbind } from "@expressive/mvc";
|
|
|
5
5
|
import { watch } from "@expressive/mvc/observable";
|
|
6
6
|
import { Fragment as Fragment$1 } from "@expressive/mvc/jsx-runtime";
|
|
7
7
|
|
|
8
|
-
//#region src/style/css.ts
|
|
9
|
-
const unitless = /* @__PURE__ */ new Map();
|
|
10
|
-
let probe;
|
|
11
|
-
/**
|
|
12
|
-
* Does the CSSOM accept a bare number for this property? Probed once with a
|
|
13
|
-
* canonical value; the answer is a property of CSS, not of the value.
|
|
14
|
-
*/
|
|
15
|
-
function bare$1(property) {
|
|
16
|
-
let known = unitless.get(property);
|
|
17
|
-
if (known === void 0) {
|
|
18
|
-
probe ||= document.createElement("div").style;
|
|
19
|
-
probe[property] = "";
|
|
20
|
-
probe[property] = 1;
|
|
21
|
-
unitless.set(property, known = Boolean(probe[property]));
|
|
22
|
-
}
|
|
23
|
-
return known;
|
|
24
|
-
}
|
|
25
|
-
function sized(property, value) {
|
|
26
|
-
if (Array.isArray(value)) return value.map((entry) => sized(property, entry));
|
|
27
|
-
return typeof value == "number" && value !== 0 && !bare$1(property) ? `${value}px` : value;
|
|
28
|
-
}
|
|
29
|
-
/**
|
|
30
|
-
* Web macro pack. Register with `macro(css)` to opt into pixel units for
|
|
31
|
-
* numbers and the `size` shorthand.
|
|
32
|
-
*/
|
|
33
|
-
const css = {
|
|
34
|
-
"*": (value, key) => ({ [key]: sized(key, value) }),
|
|
35
|
-
size: (value) => Array.isArray(value) ? {
|
|
36
|
-
width: value[0],
|
|
37
|
-
height: value[1]
|
|
38
|
-
} : {
|
|
39
|
-
width: value,
|
|
40
|
-
height: value
|
|
41
|
-
}
|
|
42
|
-
};
|
|
43
|
-
|
|
44
|
-
//#endregion
|
|
45
|
-
//#region src/style/units.ts
|
|
46
|
-
function unit(suffix) {
|
|
47
|
-
return (...values) => values.map((value) => value + suffix).join(" ");
|
|
48
|
-
}
|
|
49
|
-
const rem = unit("rem");
|
|
50
|
-
const em = unit("em");
|
|
51
|
-
const px = unit("px");
|
|
52
|
-
|
|
53
|
-
//#endregion
|
|
54
|
-
//#region src/style/media.ts
|
|
55
|
-
const order = [
|
|
56
|
-
"minWidth",
|
|
57
|
-
"maxWidth",
|
|
58
|
-
"minHeight",
|
|
59
|
-
"maxHeight",
|
|
60
|
-
"orientation",
|
|
61
|
-
"hover",
|
|
62
|
-
"pointer",
|
|
63
|
-
"prefersColorScheme",
|
|
64
|
-
"prefersReducedMotion"
|
|
65
|
-
];
|
|
66
|
-
function media(query) {
|
|
67
|
-
const features = [];
|
|
68
|
-
for (const key of order) {
|
|
69
|
-
const value = query[key];
|
|
70
|
-
if (value === void 0) continue;
|
|
71
|
-
const feature = key.replace(/[A-Z]/g, (char) => `-${char.toLowerCase()}`);
|
|
72
|
-
features.push(`(${feature}: ${typeof value == "number" ? `${value}px` : value})`);
|
|
73
|
-
}
|
|
74
|
-
return `@media ${features.join(" and ")}`;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
//#endregion
|
|
78
8
|
//#region src/style/protocol.ts
|
|
79
9
|
const SUBCOMPONENT = /^_[A-Z]/;
|
|
80
10
|
const registrations = /* @__PURE__ */ new WeakMap();
|
|
81
11
|
const tokens = /* @__PURE__ */ new WeakMap();
|
|
82
|
-
let root;
|
|
83
12
|
let emitter;
|
|
84
|
-
let rendered = false;
|
|
85
13
|
function registerAppearance(type, enter) {
|
|
86
14
|
registrations.set(type, enter);
|
|
87
15
|
}
|
|
88
|
-
function registerAppearanceRoot(factory) {
|
|
89
|
-
root = factory;
|
|
90
|
-
}
|
|
91
16
|
function registerEmitter(emit) {
|
|
92
17
|
emitter = emit;
|
|
93
18
|
}
|
|
94
19
|
function emitClass(block, depth, document) {
|
|
95
20
|
return emitter(block, depth, document);
|
|
96
21
|
}
|
|
97
|
-
function
|
|
98
|
-
rendered = true;
|
|
99
|
-
return root?.();
|
|
100
|
-
}
|
|
101
|
-
/** Has anything rendered yet - global registration is closed after it has. */
|
|
102
|
-
function appearanceSealed() {
|
|
103
|
-
return rendered;
|
|
104
|
-
}
|
|
105
|
-
function enterAppearance(parent, type, key) {
|
|
22
|
+
function enterAppearance(type, key) {
|
|
106
23
|
const chain = [];
|
|
24
|
+
let parent;
|
|
107
25
|
let current = type;
|
|
108
26
|
while (current && current !== Function.prototype) {
|
|
109
27
|
const enter = registrations.get(current);
|
|
@@ -136,9 +54,7 @@ const contexts = /* @__PURE__ */ new WeakMap();
|
|
|
136
54
|
const rootScopes = /* @__PURE__ */ new WeakMap();
|
|
137
55
|
const STYLES = Symbol("@expressive/dom.styles");
|
|
138
56
|
const entered = /* @__PURE__ */ new WeakSet();
|
|
139
|
-
|
|
140
|
-
let globalContext;
|
|
141
|
-
function createStyleScope(parent, value, label, defines) {
|
|
57
|
+
function createStyleScope(parent, value, label, pack) {
|
|
142
58
|
if (!isObject(value)) return parent;
|
|
143
59
|
const cache = parent?.children || rootScopes;
|
|
144
60
|
const cached = cache.get(value);
|
|
@@ -148,56 +64,59 @@ function createStyleScope(parent, value, label, defines) {
|
|
|
148
64
|
const base = {};
|
|
149
65
|
const scopeLabel = label || parent?.label || "style";
|
|
150
66
|
const root = { name: scopeLabel };
|
|
67
|
+
const macros = pack ? Object.create(parent?.macros || null) : parent?.macros || Object.create(null);
|
|
151
68
|
for (const key of Object.keys(value)) {
|
|
152
|
-
const entry = value[key];
|
|
69
|
+
const entry = flatten(value[key], key);
|
|
153
70
|
if (key[0] == "$") throw new Error(`Reserved key "${key}" in style map.`);
|
|
154
71
|
if (SUBCOMPONENT.test(key)) throw new Error(`Subcomponent "${key}" must be at the top of a style() map.`);
|
|
155
72
|
if (key[0] == "_") {
|
|
156
|
-
if (!isObject(entry)) throw new Error(`Rule "${key}" must be an object. Macros are defined
|
|
73
|
+
if (!isObject(entry)) throw new Error(`Rule "${key}" must be an object. Macros are defined on a Style class.`);
|
|
157
74
|
const name = key.slice(1);
|
|
158
75
|
if (!own.includes(name)) own.push(name);
|
|
159
76
|
source[name] = merge(source[name], entry);
|
|
160
|
-
} else if (isCondition(key) && isObject(entry))
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
const name = inner.slice(1);
|
|
164
|
-
if (!own.includes(name)) own.push(name);
|
|
165
|
-
for (const when of split(key)) source[name] = merge(source[name], { [hoist(root, when)]: entry[inner] });
|
|
166
|
-
} else rest[inner] = entry[inner];
|
|
167
|
-
base[key] = merge(base[key], rest);
|
|
168
|
-
} else if (defines && typeof entry == "function") {
|
|
169
|
-
own.push(key);
|
|
170
|
-
source[key] = entry;
|
|
171
|
-
} else base[key] = entry;
|
|
77
|
+
} else if (isCondition(key) && isObject(entry)) base[key] = merge(base[key], lift(root, split(key).map((when) => [when]), entry, own, source));
|
|
78
|
+
else if (pack && typeof entry == "function") macros[key] = entry;
|
|
79
|
+
else base[key] = entry;
|
|
172
80
|
}
|
|
173
|
-
const
|
|
174
|
-
const
|
|
175
|
-
const local = defines ? [] : [...before, ...own.filter((name) => !before.includes(name))];
|
|
81
|
+
const before = parent ? parent.local : [];
|
|
82
|
+
const local = [...before, ...own.filter((name) => !before.includes(name))];
|
|
176
83
|
const rules = Object.create(parent?.rules || null);
|
|
177
84
|
const labels = Object.create(parent?.labels || null);
|
|
178
85
|
for (const name of own) {
|
|
179
86
|
const rule = source[name];
|
|
180
87
|
const inherited = parent?.rules[name];
|
|
181
|
-
rules[name] =
|
|
88
|
+
rules[name] = inherited ? mergeRule(inherited, rule) : rule;
|
|
182
89
|
labels[name] = scopeLabel;
|
|
183
90
|
}
|
|
184
91
|
const scope = {
|
|
185
92
|
children: /* @__PURE__ */ new WeakMap(),
|
|
186
93
|
expansions: /* @__PURE__ */ new Map(),
|
|
187
|
-
globals: parent?.globals,
|
|
188
94
|
label: scopeLabel,
|
|
189
95
|
labels,
|
|
190
96
|
local,
|
|
191
|
-
|
|
97
|
+
macros,
|
|
192
98
|
own: base,
|
|
99
|
+
pack: pack || parent?.pack,
|
|
193
100
|
routes: /* @__PURE__ */ new Map(),
|
|
194
101
|
rules
|
|
195
102
|
};
|
|
196
|
-
if (defines) scope.globals = scope;
|
|
197
103
|
if (root.group) ownGroups.set(scope, root.group);
|
|
198
104
|
cache.set(value, scope);
|
|
199
105
|
return scope;
|
|
200
106
|
}
|
|
107
|
+
function lift(frame, paths, body, own, source) {
|
|
108
|
+
const rest = {};
|
|
109
|
+
for (const key of Object.keys(body)) {
|
|
110
|
+
const entry = flatten(body[key], key);
|
|
111
|
+
if (key[0] == "_") {
|
|
112
|
+
const name = key.slice(1);
|
|
113
|
+
if (!own.includes(name)) own.push(name);
|
|
114
|
+
for (const path of paths) source[name] = merge(source[name], path.reduceRight((rule, when) => ({ [hoist(frame, when)]: rule }), entry));
|
|
115
|
+
} else if (isCondition(key) && isObject(entry)) rest[key] = lift(frame, paths.flatMap((path) => split(key).map((when) => [...path, when])), entry, own, source);
|
|
116
|
+
else rest[key] = entry;
|
|
117
|
+
}
|
|
118
|
+
return rest;
|
|
119
|
+
}
|
|
201
120
|
function createContext(scope) {
|
|
202
121
|
const cached = contexts.get(scope);
|
|
203
122
|
if (cached) return cached;
|
|
@@ -211,12 +130,12 @@ function createContext(scope) {
|
|
|
211
130
|
contexts.set(scope, context);
|
|
212
131
|
return context;
|
|
213
132
|
}
|
|
214
|
-
function extendContext(parent, maps, label,
|
|
133
|
+
function extendContext(parent, maps, label, pack) {
|
|
215
134
|
let scope = parent?.scope;
|
|
216
135
|
const base = { ...scope && bases.get(scope) };
|
|
217
136
|
const anchors = new Set(scope && groups$1.get(scope));
|
|
218
137
|
for (const map of maps) {
|
|
219
|
-
scope = createStyleScope(scope, map, label,
|
|
138
|
+
scope = createStyleScope(scope, map, label, pack);
|
|
220
139
|
Object.assign(base, mergeRule(base, scope.own));
|
|
221
140
|
const own = ownGroups.get(scope);
|
|
222
141
|
if (own) anchors.add(own);
|
|
@@ -231,16 +150,16 @@ function baseToken(scope) {
|
|
|
231
150
|
if (!base && !anchors) return void 0;
|
|
232
151
|
const frame = { name: scope.label };
|
|
233
152
|
const expansion = expand(scope, base, /* @__PURE__ */ new Map(), frame);
|
|
234
|
-
const blocks = [...toBlocks(expansion, frame,
|
|
153
|
+
const blocks = [...toBlocks(expansion, frame, -.5), ...anchors || []];
|
|
235
154
|
const appearance = {};
|
|
236
155
|
if (blocks.length) appearance.blocks = blocks;
|
|
237
156
|
if (expansion.classes.length) appearance.classes = expansion.classes;
|
|
238
157
|
return createAppearanceToken(appearance);
|
|
239
158
|
}
|
|
240
|
-
function style$1(type, rules) {
|
|
159
|
+
function style$1(type, rules, pack) {
|
|
241
160
|
if (entered.has(type)) throw new Error("Cannot add styles after a component has rendered.");
|
|
242
161
|
if (!Object.hasOwn(type, STYLES)) {
|
|
243
|
-
const
|
|
162
|
+
const name = label(type);
|
|
244
163
|
Object.defineProperty(type, STYLES, {
|
|
245
164
|
configurable: true,
|
|
246
165
|
value: {
|
|
@@ -249,29 +168,26 @@ function style$1(type, rules) {
|
|
|
249
168
|
}
|
|
250
169
|
});
|
|
251
170
|
registerAppearance(type, (parent, key) => {
|
|
252
|
-
const { own, subs } = type[STYLES];
|
|
171
|
+
const { own, pack, subs } = type[STYLES];
|
|
253
172
|
entered.add(type);
|
|
254
|
-
|
|
255
|
-
|
|
173
|
+
const maps = key ? subs[key] : own;
|
|
174
|
+
if (!maps) return parent;
|
|
175
|
+
if (pack && (parent?.scope)?.pack !== pack) parent = extendContext(parent, pack.layers, name, pack);
|
|
176
|
+
return extendContext(parent, maps, key ? `${name}_${key}` : name);
|
|
256
177
|
});
|
|
257
178
|
}
|
|
258
|
-
const
|
|
179
|
+
const styles = type[STYLES];
|
|
259
180
|
const rest = {};
|
|
181
|
+
if (pack && styles.pack && styles.pack !== pack) throw new Error(`"${label(type)}" is already styled by another Style class.`);
|
|
182
|
+
styles.pack ||= pack;
|
|
260
183
|
for (const key of Object.keys(rules)) if (SUBCOMPONENT.test(key)) {
|
|
261
|
-
|
|
262
|
-
(
|
|
184
|
+
const body = flatten(rules[key], key);
|
|
185
|
+
if (!isObject(body)) throw new Error(`Subcomponent "${key}" must be an object.`);
|
|
186
|
+
(styles.subs[key.slice(1)] ||= []).push(body);
|
|
263
187
|
} else rest[key] = rules[key];
|
|
264
|
-
own.push(rest);
|
|
188
|
+
styles.own.push(rest);
|
|
265
189
|
return type;
|
|
266
190
|
}
|
|
267
|
-
function macro$1(rules) {
|
|
268
|
-
for (const key of Object.keys(rules)) if (key[0] != "_" && key[0] != "$" && typeof rules[key] != "function") throw new Error(`Macro "${key}" must be a function. Use "_${key}" to register a rule.`);
|
|
269
|
-
if (appearanceSealed()) throw new Error("Cannot add macros after rendering has started.");
|
|
270
|
-
globals.push(rules);
|
|
271
|
-
globalContext = void 0;
|
|
272
|
-
registerAppearanceRoot(() => globalContext ||= extendContext(void 0, globals, "global", true));
|
|
273
|
-
return rules;
|
|
274
|
-
}
|
|
275
191
|
function createAppearanceRoute(scope, props, tag) {
|
|
276
192
|
if (!scope) return void 0;
|
|
277
193
|
const keys = Object.keys(props).filter((key) => key.startsWith("_")).sort();
|
|
@@ -280,14 +196,11 @@ function createAppearanceRoute(scope, props, tag) {
|
|
|
280
196
|
if (cached) return cached;
|
|
281
197
|
const present = new Set(keys.map((key) => key.slice(1)));
|
|
282
198
|
const flags = [];
|
|
283
|
-
for (const name of scope.
|
|
284
|
-
if (!present.has(name) || name == tag) continue;
|
|
285
|
-
if (isObject(scope.rules[name])) flags.push(name);
|
|
286
|
-
}
|
|
199
|
+
for (const name of scope.local) if (present.has(name) && name != tag) flags.push(name);
|
|
287
200
|
const route = {
|
|
288
201
|
flags,
|
|
289
202
|
scope,
|
|
290
|
-
tag: tag &&
|
|
203
|
+
tag: tag && scope.rules[tag] ? tag : void 0
|
|
291
204
|
};
|
|
292
205
|
scope.routes.set(id, route);
|
|
293
206
|
return route;
|
|
@@ -298,7 +211,7 @@ function resolveAppearance(scope, props, tag) {
|
|
|
298
211
|
const parts = [];
|
|
299
212
|
const names = route.tag ? [route.tag] : [];
|
|
300
213
|
for (const name of route.flags) if (props[`_${name}`]) names.push(name);
|
|
301
|
-
for (const name of names)
|
|
214
|
+
for (const name of names) parts.push(expandRule(route.scope, name));
|
|
302
215
|
const blocks = parts.flatMap((part) => part.blocks);
|
|
303
216
|
const classes = parts.flatMap((part) => part.classes);
|
|
304
217
|
let child = route.scope;
|
|
@@ -315,7 +228,7 @@ function expandRule(scope, name) {
|
|
|
315
228
|
if (!expansion) {
|
|
316
229
|
const frame = { name: `${scope.labels[name]}_${name}` };
|
|
317
230
|
expansion = expand(scope, scope.rules[name], /* @__PURE__ */ new Map(), frame);
|
|
318
|
-
expansion.blocks = toBlocks(expansion, frame,
|
|
231
|
+
expansion.blocks = toBlocks(expansion, frame, scope.local.indexOf(name));
|
|
319
232
|
scope.expansions.set(name, expansion);
|
|
320
233
|
}
|
|
321
234
|
return expansion;
|
|
@@ -352,17 +265,6 @@ function hoist(frame, condition) {
|
|
|
352
265
|
});
|
|
353
266
|
return key;
|
|
354
267
|
}
|
|
355
|
-
function shared(scope) {
|
|
356
|
-
return scope.globals ? scope.globals.names.length : 0;
|
|
357
|
-
}
|
|
358
|
-
function ordinal(scope, name) {
|
|
359
|
-
return scope.rules[name] === scope.globals?.rules[name] ? scope.names.indexOf(name) : shared(scope) + scope.local.indexOf(name);
|
|
360
|
-
}
|
|
361
|
-
function expandShadowed(scope, name, parts) {
|
|
362
|
-
const { globals } = scope;
|
|
363
|
-
if (globals && isObject(globals.rules[name]) && globals.rules[name] !== scope.rules[name]) parts.push(expandRule(globals, name));
|
|
364
|
-
parts.push(expandRule(scope, name));
|
|
365
|
-
}
|
|
366
268
|
function handlers(scope, name) {
|
|
367
269
|
let known = chains.get(scope);
|
|
368
270
|
if (!known) chains.set(scope, known = /* @__PURE__ */ new Map());
|
|
@@ -370,9 +272,9 @@ function handlers(scope, name) {
|
|
|
370
272
|
if (!chain) {
|
|
371
273
|
chain = Object.assign([], { named: 0 });
|
|
372
274
|
for (const key of [name, "*"]) {
|
|
373
|
-
for (let
|
|
374
|
-
const own = Object.getOwnPropertyDescriptor(
|
|
375
|
-
if (own
|
|
275
|
+
for (let macros = scope.macros; macros; macros = Object.getPrototypeOf(macros)) {
|
|
276
|
+
const own = Object.getOwnPropertyDescriptor(macros, key);
|
|
277
|
+
if (own) chain.push(own.value);
|
|
376
278
|
}
|
|
377
279
|
if (key == name) chain.named = chain.length;
|
|
378
280
|
}
|
|
@@ -400,7 +302,8 @@ function expand(scope, value, depths, frame) {
|
|
|
400
302
|
value.forEach((entry) => walk(entry, depths));
|
|
401
303
|
return;
|
|
402
304
|
}
|
|
403
|
-
for (const [name,
|
|
305
|
+
for (const [name, raw] of Object.entries(value)) {
|
|
306
|
+
const entry = flatten(raw, name);
|
|
404
307
|
if (name[0] == "_") {
|
|
405
308
|
if (isObject(entry)) nested[name] = merge(nested[name], entry);
|
|
406
309
|
continue;
|
|
@@ -471,9 +374,27 @@ function mergeRule(left, right) {
|
|
|
471
374
|
...left,
|
|
472
375
|
...right
|
|
473
376
|
};
|
|
474
|
-
for (const key of Object.keys(right))
|
|
377
|
+
for (const key of Object.keys(right)) {
|
|
378
|
+
if (key[0] != "_" && !isCondition(key)) continue;
|
|
379
|
+
const before = flatten(left[key], key);
|
|
380
|
+
const after = flatten(right[key], key);
|
|
381
|
+
if (isObject(before) && isObject(after)) output[key] = mergeRule(before, after);
|
|
382
|
+
}
|
|
383
|
+
return output;
|
|
384
|
+
}
|
|
385
|
+
function flatten(value, key) {
|
|
386
|
+
if (!Array.isArray(value) || key[0] != "_" && !isCondition(key)) return value;
|
|
387
|
+
let output = {};
|
|
388
|
+
for (const entry of value) {
|
|
389
|
+
const map = flatten(entry, key);
|
|
390
|
+
if (isObject(map)) output = mergeRule(output, map);
|
|
391
|
+
else if (isPresent(map)) return map;
|
|
392
|
+
}
|
|
475
393
|
return output;
|
|
476
394
|
}
|
|
395
|
+
function label(type) {
|
|
396
|
+
return type.displayName || type.name;
|
|
397
|
+
}
|
|
477
398
|
function isPresent(value) {
|
|
478
399
|
return value !== false && value !== null && value !== void 0;
|
|
479
400
|
}
|
|
@@ -502,17 +423,56 @@ function applyDeclarations(style, declarations, previous = {}) {
|
|
|
502
423
|
|
|
503
424
|
//#endregion
|
|
504
425
|
//#region src/style/stylesheet.ts
|
|
426
|
+
/**
|
|
427
|
+
* Base class for a macro pack. Each method is a macro, layered over the class
|
|
428
|
+
* it extends; `'*'` handles any key no macro owns.
|
|
429
|
+
*/
|
|
430
|
+
var Style = class {
|
|
431
|
+
/** The `style()` for this pack, created once per class. */
|
|
432
|
+
static use() {
|
|
433
|
+
let apply = bound.get(this);
|
|
434
|
+
if (!apply) {
|
|
435
|
+
let pack;
|
|
436
|
+
let built = false;
|
|
437
|
+
apply = (type, ...maps) => {
|
|
438
|
+
if (!built) {
|
|
439
|
+
pack = layers(this);
|
|
440
|
+
built = true;
|
|
441
|
+
}
|
|
442
|
+
return register(type, maps, pack);
|
|
443
|
+
};
|
|
444
|
+
bound.set(this, apply);
|
|
445
|
+
}
|
|
446
|
+
return apply;
|
|
447
|
+
}
|
|
448
|
+
};
|
|
505
449
|
const sheets = /* @__PURE__ */ new WeakMap();
|
|
450
|
+
const bound = /* @__PURE__ */ new WeakMap();
|
|
506
451
|
let groups = 0;
|
|
507
452
|
function style(type, ...maps) {
|
|
508
|
-
|
|
453
|
+
return register(type, maps);
|
|
454
|
+
}
|
|
455
|
+
function register(type, maps, pack) {
|
|
456
|
+
for (const rules of maps) if (rules) style$1(type, rules, pack);
|
|
509
457
|
registerEmitter(emit);
|
|
510
458
|
return type;
|
|
511
459
|
}
|
|
512
|
-
function
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
460
|
+
function layers(type) {
|
|
461
|
+
const instance = new type();
|
|
462
|
+
const maps = [];
|
|
463
|
+
const [field] = Object.keys(instance);
|
|
464
|
+
if (field) throw new Error(`Field "${field}" on ${type.name} is reserved. Define macros as methods.`);
|
|
465
|
+
for (let proto = type.prototype; proto !== Style.prototype; proto = Object.getPrototypeOf(proto)) {
|
|
466
|
+
const map = {};
|
|
467
|
+
for (const key of Object.getOwnPropertyNames(proto)) {
|
|
468
|
+
if (key == "constructor") continue;
|
|
469
|
+
const { value } = Object.getOwnPropertyDescriptor(proto, key);
|
|
470
|
+
if (typeof value != "function" || key[0] == "_" || key[0] == "$") throw new Error(`"${key}" on ${type.name} must be a method with a bare name.`);
|
|
471
|
+
map[key] = value.bind(instance);
|
|
472
|
+
}
|
|
473
|
+
maps.unshift(map);
|
|
474
|
+
}
|
|
475
|
+
return maps.length ? { layers: maps } : void 0;
|
|
516
476
|
}
|
|
517
477
|
function emit(block, depth, document) {
|
|
518
478
|
let sheet = sheets.get(document);
|
|
@@ -572,6 +532,80 @@ function serialize(document, declarations) {
|
|
|
572
532
|
return value.cssText;
|
|
573
533
|
}
|
|
574
534
|
|
|
535
|
+
//#endregion
|
|
536
|
+
//#region src/style/css.ts
|
|
537
|
+
const unitless = /* @__PURE__ */ new Map();
|
|
538
|
+
let probe;
|
|
539
|
+
/**
|
|
540
|
+
* Does the CSSOM accept a bare number for this property? Probed once with a
|
|
541
|
+
* canonical value; the answer is a property of CSS, not of the value.
|
|
542
|
+
*/
|
|
543
|
+
function bare$1(property) {
|
|
544
|
+
let known = unitless.get(property);
|
|
545
|
+
if (known === void 0) {
|
|
546
|
+
probe ||= document.createElement("div").style;
|
|
547
|
+
probe[property] = "";
|
|
548
|
+
probe[property] = 1;
|
|
549
|
+
unitless.set(property, known = Boolean(probe[property]));
|
|
550
|
+
}
|
|
551
|
+
return known;
|
|
552
|
+
}
|
|
553
|
+
function sized(property, value) {
|
|
554
|
+
if (Array.isArray(value)) return value.map((entry) => sized(property, entry));
|
|
555
|
+
return typeof value == "number" && value !== 0 && !bare$1(property) ? `${value}px` : value;
|
|
556
|
+
}
|
|
557
|
+
/**
|
|
558
|
+
* Web macro pack - pixel units for numbers and the `size` shorthand. Extend
|
|
559
|
+
* it, or use it as is with `Css.use()`.
|
|
560
|
+
*/
|
|
561
|
+
var Css = class extends Style {
|
|
562
|
+
"*"(value, key) {
|
|
563
|
+
return { [key]: sized(key, value) };
|
|
564
|
+
}
|
|
565
|
+
size(value) {
|
|
566
|
+
return Array.isArray(value) ? {
|
|
567
|
+
width: value[0],
|
|
568
|
+
height: value[1]
|
|
569
|
+
} : {
|
|
570
|
+
width: value,
|
|
571
|
+
height: value
|
|
572
|
+
};
|
|
573
|
+
}
|
|
574
|
+
};
|
|
575
|
+
|
|
576
|
+
//#endregion
|
|
577
|
+
//#region src/style/units.ts
|
|
578
|
+
function unit(suffix) {
|
|
579
|
+
return (...values) => values.map((value) => value + suffix).join(" ");
|
|
580
|
+
}
|
|
581
|
+
const rem = unit("rem");
|
|
582
|
+
const em = unit("em");
|
|
583
|
+
const px = unit("px");
|
|
584
|
+
|
|
585
|
+
//#endregion
|
|
586
|
+
//#region src/style/media.ts
|
|
587
|
+
const order = [
|
|
588
|
+
"minWidth",
|
|
589
|
+
"maxWidth",
|
|
590
|
+
"minHeight",
|
|
591
|
+
"maxHeight",
|
|
592
|
+
"orientation",
|
|
593
|
+
"hover",
|
|
594
|
+
"pointer",
|
|
595
|
+
"prefersColorScheme",
|
|
596
|
+
"prefersReducedMotion"
|
|
597
|
+
];
|
|
598
|
+
function media(query) {
|
|
599
|
+
const features = [];
|
|
600
|
+
for (const key of order) {
|
|
601
|
+
const value = query[key];
|
|
602
|
+
if (value === void 0) continue;
|
|
603
|
+
const feature = key.replace(/[A-Z]/g, (char) => `-${char.toLowerCase()}`);
|
|
604
|
+
features.push(`(${feature}: ${typeof value == "number" ? `${value}px` : value})`);
|
|
605
|
+
}
|
|
606
|
+
return `@media ${features.join(" and ")}`;
|
|
607
|
+
}
|
|
608
|
+
|
|
575
609
|
//#endregion
|
|
576
610
|
//#region src/render/commits.ts
|
|
577
611
|
const DETACHED = /* @__PURE__ */ new WeakMap();
|
|
@@ -688,7 +722,6 @@ function componentProps(type, props, appearance) {
|
|
|
688
722
|
return {
|
|
689
723
|
appearance: {
|
|
690
724
|
author,
|
|
691
|
-
context: value.context,
|
|
692
725
|
entries,
|
|
693
726
|
lexical: value.context || context
|
|
694
727
|
},
|
|
@@ -727,8 +760,6 @@ function claim(element, appearance, resolved, className, value) {
|
|
|
727
760
|
tokens: []
|
|
728
761
|
};
|
|
729
762
|
const entries = appearance?.entries || [];
|
|
730
|
-
let context = appearance?.context;
|
|
731
|
-
let reach = -1;
|
|
732
763
|
if (resolved) collect(createAppearanceToken(resolved), collected, 0);
|
|
733
764
|
appendClasses(collected.classes, className);
|
|
734
765
|
collect(value, collected, 0);
|
|
@@ -736,14 +767,10 @@ function claim(element, appearance, resolved, className, value) {
|
|
|
736
767
|
for (const { appearance, doors } of collected.tokens) {
|
|
737
768
|
for (const block of appearance.blocks || []) collected.classes.push(emitClass(block, doors, element.ownerDocument));
|
|
738
769
|
if (appearance.classes) collected.classes.push(...appearance.classes);
|
|
739
|
-
if (appearance.context && doors >= reach) {
|
|
740
|
-
context = appearance.context;
|
|
741
|
-
reach = doors;
|
|
742
|
-
}
|
|
743
770
|
}
|
|
744
771
|
return {
|
|
745
772
|
className: [...new Set(collected.classes)].join(" "),
|
|
746
|
-
context,
|
|
773
|
+
context: resolved?.context || appearance?.context,
|
|
747
774
|
declarations: collected.declarations
|
|
748
775
|
};
|
|
749
776
|
}
|
|
@@ -896,10 +923,9 @@ function renderedAppearance(fiber) {
|
|
|
896
923
|
hops: hops + 1,
|
|
897
924
|
style
|
|
898
925
|
}));
|
|
899
|
-
const inherited = fiber.appearance?.context || appearanceRoot();
|
|
900
926
|
const sub = owner(fiber.type);
|
|
901
|
-
const context = sub ? enterAppearance(
|
|
902
|
-
if (context?.base
|
|
927
|
+
const context = sub ? enterAppearance(latest(sub.owner.constructor), sub.key) : enterAppearance(latest(fiber.type));
|
|
928
|
+
if (context?.base) entries.push({
|
|
903
929
|
hops: 0,
|
|
904
930
|
style: context.base
|
|
905
931
|
});
|
|
@@ -1577,13 +1603,7 @@ function render(node, container) {
|
|
|
1577
1603
|
const context = new Context(Context.root);
|
|
1578
1604
|
const fiber = range("fragment", container, null, context);
|
|
1579
1605
|
try {
|
|
1580
|
-
pass(() =>
|
|
1581
|
-
const appearanceContext = appearanceRoot();
|
|
1582
|
-
reconcile(fiber, node, context, void 0, appearanceContext ? {
|
|
1583
|
-
context: appearanceContext,
|
|
1584
|
-
entries: []
|
|
1585
|
-
} : void 0);
|
|
1586
|
-
});
|
|
1606
|
+
pass(() => reconcile(fiber, node, context, void 0, void 0));
|
|
1587
1607
|
} catch (error) {
|
|
1588
1608
|
unmountFiber(fiber);
|
|
1589
1609
|
context.pop();
|
|
@@ -1645,5 +1665,5 @@ function attempt(fiber, passive, render) {
|
|
|
1645
1665
|
}
|
|
1646
1666
|
|
|
1647
1667
|
//#endregion
|
|
1648
|
-
export { Fragment, Portal,
|
|
1668
|
+
export { Css, Fragment, Portal, Style, createElement, em, media, px, rem, render, style };
|
|
1649
1669
|
//# sourceMappingURL=index.js.map
|