actual-css 0.19.0 → 0.19.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.
Files changed (50) hide show
  1. package/CHANGELOG.md +16 -1
  2. package/dist/actual.full.css +20 -0
  3. package/dist/actual.full.js +1 -1
  4. package/dist/actual.full.min.css +1 -1
  5. package/dist/types/ajax.d.ts +1 -0
  6. package/dist/types/command.d.ts +41 -0
  7. package/dist/types/context-menu.d.ts +1 -0
  8. package/dist/types/dialog.d.ts +3 -0
  9. package/dist/types/dirty.d.ts +15 -0
  10. package/dist/types/dismiss.d.ts +1 -0
  11. package/dist/types/enhance.d.ts +36 -0
  12. package/dist/types/enhancement-loader.d.ts +27 -0
  13. package/dist/types/escape.d.ts +1 -0
  14. package/dist/types/events.d.ts +14 -0
  15. package/dist/types/filter.d.ts +1 -0
  16. package/dist/types/floating-space.d.ts +16 -0
  17. package/dist/types/flyout.d.ts +1 -0
  18. package/dist/types/focus-group.d.ts +4 -0
  19. package/dist/types/focus.d.ts +4 -0
  20. package/dist/types/form-submission.d.ts +12 -0
  21. package/dist/types/full.d.ts +22 -0
  22. package/dist/types/in-view.d.ts +1 -0
  23. package/dist/types/index.d.ts +1 -0
  24. package/dist/types/inline-visible.d.ts +1 -0
  25. package/dist/types/input.d.ts +4 -0
  26. package/dist/types/keys.d.ts +5 -0
  27. package/dist/types/mask.d.ts +1 -0
  28. package/dist/types/menu.d.ts +12 -0
  29. package/dist/types/parse-config.d.ts +2 -0
  30. package/dist/types/parsed.d.ts +1 -0
  31. package/dist/types/password.d.ts +1 -0
  32. package/dist/types/range.d.ts +1 -0
  33. package/dist/types/scrollspy.d.ts +1 -0
  34. package/dist/types/selectors.d.ts +14 -0
  35. package/dist/types/status.d.ts +5 -0
  36. package/dist/types/surface.d.ts +6 -0
  37. package/dist/types/tab.d.ts +1 -0
  38. package/dist/types/tooltip.d.ts +68 -0
  39. package/dist/types/transition.d.ts +1 -0
  40. package/dist/types/validation.d.ts +17 -0
  41. package/docs/pages/enhancements/flyout.md +3 -1
  42. package/docs/pages/guides/modular-import.md +19 -3
  43. package/docs/pages/guides/progressive-enhancement.md +5 -1
  44. package/package.json +36 -34
  45. package/src/cli/actual-css.js +3 -1
  46. package/src/css/components/flyout.css +20 -0
  47. package/src/js/context-menu.js +10 -2
  48. package/src/js/enhance.js +18 -4
  49. package/src/js/full.js +3 -0
  50. package/src/tooling/css-bundle.js +153 -6
@@ -103,9 +103,11 @@ function openContextMenu(context, menu, opts = {}) {
103
103
  if (!requestContextMenu(context, menu, opts)) return;
104
104
  if (isSurfaceOpen(menu)) closeSurface(menu);
105
105
 
106
- // Measured after the cancelable event: a handler may have moved the anchor.
106
+ // A button anchors on its own rect, like a flyout trigger, so alignment and
107
+ // flipping see its full box. Pointer and keyboard openings anchor on a point,
108
+ // measured after the cancelable event: a handler may have moved the origin.
107
109
  let { x, y } = opts;
108
- if (x == null || y == null) {
110
+ if (!opts.anchor && (x == null || y == null)) {
109
111
  const rect = (opts.origin || context).getBoundingClientRect();
110
112
  x ??= rect.left;
111
113
  y ??= rect.bottom;
@@ -114,6 +116,7 @@ function openContextMenu(context, menu, opts = {}) {
114
116
  if (
115
117
  openSurface(menu, {
116
118
  source: context,
119
+ trigger: opts.anchor,
117
120
  x,
118
121
  y,
119
122
  placement: opts.placement || "bottom-start",
@@ -237,6 +240,10 @@ function connectContextTarget(target) {
237
240
  target.addEventListener(
238
241
  "pointerdown",
239
242
  (e) => {
243
+ // Suppression targets the click synthesized by the long press itself. A
244
+ // fresh press starts a new gesture, so its click (a tap on "…" right
245
+ // after the menu opened) is legitimate.
246
+ state.suppressClickUntil = 0;
240
247
  const delay = getLongPressDelay(target, menuFor(target));
241
248
  if (e.pointerType !== "touch" || delay == null || shouldIgnoreNativeTarget(e.target)) {
242
249
  return;
@@ -321,6 +328,7 @@ function connectContextTarget(target) {
321
328
 
322
329
  openContextMenu(target, menu, {
323
330
  distance: 4,
331
+ anchor: trigger,
324
332
  origin: trigger,
325
333
  trigger: "button",
326
334
  restoreFocusTo: trigger,
package/src/js/enhance.js CHANGED
@@ -28,6 +28,14 @@
28
28
  * See docs/design-notes/enhancement-contract.md.
29
29
  */
30
30
 
31
+ /** @typedef {(el: Element) => (() => void) | void} Enhancer */
32
+
33
+ /**
34
+ * @typedef {object} EnhancementHandle
35
+ * @property {(node: Node) => void} refresh Scan a node and its descendants.
36
+ * @property {() => void} disconnect Stop observing and run active cleanups.
37
+ */
38
+
31
39
  const registries = new WeakMap();
32
40
  const ownedNames = new WeakMap();
33
41
 
@@ -197,7 +205,7 @@ function enhancementSelector(name) {
197
205
  * Apply a named opt-in once; presentation selectors remain application-owned.
198
206
  * @param {string} name
199
207
  * @param {string} selector
200
- * @param {Document | Element | DocumentFragment} [root] Query scope, excluding itself.
208
+ * @param {Document | Element | DocumentFragment | null} [root] Query scope, excluding itself.
201
209
  * @returns {Element[]} Matched descendants.
202
210
  */
203
211
  export function applyEnhancement(name, selector, root) {
@@ -224,6 +232,12 @@ export function applyEnhancement(name, selector, root) {
224
232
  return elements;
225
233
  }
226
234
 
235
+ /**
236
+ * @param {string} name
237
+ * @param {Enhancer} init
238
+ * @param {Document | Element | DocumentFragment | null} [root]
239
+ * @returns {EnhancementHandle}
240
+ */
227
241
  export function registerEnhancement(name, init, root) {
228
242
  if (typeof document === "undefined") return noopRuntime();
229
243
 
@@ -260,11 +274,11 @@ export function registerEnhancement(name, init, root) {
260
274
  }
261
275
 
262
276
  /**
263
- * @param {Record<string, (el: Element) => (() => void) | void>} enhancers
264
- * @param {Document | Element | DocumentFragment} [root]
277
+ * @param {Record<string, Enhancer>} enhancers
278
+ * @param {Document | Element | DocumentFragment | null} [root]
265
279
  * @param {object} [options]
266
280
  * @param {(el: Element) => boolean} [options.accept] Lifecycle eligibility, rechecked on moves.
267
- * @returns {{ refresh: (node: Node) => void, disconnect: () => void }}
281
+ * @returns {EnhancementHandle}
268
282
  */
269
283
  export default function enhance(enhancers, root, { accept } = {}) {
270
284
  if (accept !== undefined && typeof accept !== "function") {
package/src/js/full.js CHANGED
@@ -32,3 +32,6 @@ import "./status.js";
32
32
  import "./index.js";
33
33
 
34
34
  export { applyEnhancement, default as enhance, registerEnhancement } from "./enhance.js";
35
+
36
+ /** @typedef {import("./enhance.js").Enhancer} Enhancer */
37
+ /** @typedef {import("./enhance.js").EnhancementHandle} EnhancementHandle */
@@ -6,7 +6,7 @@
6
6
  import { existsSync } from "node:fs";
7
7
  import { readFile } from "node:fs/promises";
8
8
  import { createRequire } from "node:module";
9
- import { dirname, resolve } from "node:path";
9
+ import { dirname, isAbsolute, relative, resolve, sep } from "node:path";
10
10
  import { pathToFileURL } from "node:url";
11
11
 
12
12
  const IMPORT_PREFIX_RE = /^\s*@import\b/;
@@ -16,6 +16,12 @@ const EXTERNAL_SPECIFIER_RE = /^(?:[a-z][a-z0-9+.-]+:|\/\/|\/)/i;
16
16
  const CHARSET_RE = /^\s*@charset\s+["'][^"']*["']\s*;[^\n]*\n?/;
17
17
  const LAYER_TRAILER_RE = /^layer(?:\(\s*([A-Za-z_-][\w-]*(?:\.[A-Za-z_-][\w-]*)*)?\s*\))?$/;
18
18
  const RESOLVER_STUB = "__actual_css_resolve__.js";
19
+ // url() values that stay valid wherever the bundle lands: a scheme (data:,
20
+ // https:; two characters or more, as above), a root- or protocol-relative
21
+ // path, a bare query or fragment (same document).
22
+ const LOCATION_FREE_URL_RE = /^(?:[a-z][a-z0-9+.-]+:|\/|[?#])/i;
23
+ const IDENT_CHAR_RE = /[\w-]/;
24
+ const NODE_MODULES_SEGMENT = `${sep}node_modules${sep}`;
19
25
 
20
26
  function skipWhitespace(source, index) {
21
27
  let next = index;
@@ -157,7 +163,142 @@ function wrapInLayer(css, name) {
157
163
  return `${head}\n${body}\n}\n`;
158
164
  }
159
165
 
160
- async function inlineFile(path, stack, preserved, layered) {
166
+ // Index just past the string opened by the quote at `start` (escapes
167
+ // honoured), or the line end when the string is left open.
168
+ function stringEnd(line, start) {
169
+ const quote = line[start];
170
+
171
+ for (let i = start + 1; i < line.length; i += 1) {
172
+ if (line[i] === "\\") i += 1;
173
+ else if (line[i] === quote) return i + 1;
174
+ }
175
+
176
+ return line.length;
177
+ }
178
+
179
+ // Reads the url() whose "(" sits at `open`: { value, quote, end } with `end`
180
+ // just past ")", or null when the call does not close on this line.
181
+ function readUrlCall(line, open) {
182
+ let i = skipWhitespace(line, open + 1);
183
+ const quote = line[i] === '"' || line[i] === "'" ? line[i] : "";
184
+ let value;
185
+
186
+ if (quote) {
187
+ // A string left open runs to the line end, so no ")" follows and the
188
+ // check below returns null: the slice is never used then.
189
+ const end = stringEnd(line, i);
190
+ value = line.slice(i + 1, end - 1);
191
+ i = skipWhitespace(line, end);
192
+ } else {
193
+ const start = i;
194
+ while (i < line.length && line[i] !== ")" && !/\s/.test(line[i])) {
195
+ i += line[i] === "\\" ? 2 : 1;
196
+ }
197
+ value = line.slice(start, i);
198
+ i = skipWhitespace(line, i);
199
+ }
200
+
201
+ return line[i] === ")" ? { value, quote, end: i + 1 } : null;
202
+ }
203
+
204
+ // The url() value pointing at the same file as `value` written from `file`,
205
+ // seen from `base`; null when it does not depend on the stylesheet location.
206
+ function rebasedUrl(value, file, base) {
207
+ if (value === "" || LOCATION_FREE_URL_RE.test(value)) return null;
208
+
209
+ // Escapes would need decoding before the path means anything: refuse
210
+ // rather than rewrite a path that is not the one the browser reads.
211
+ if (value.includes("\\")) {
212
+ throw new Error(
213
+ `Escaped characters in a relative url() are not supported in ${file}: ${value}`,
214
+ );
215
+ }
216
+ // A package directory is never served: the rewritten reference would only
217
+ // ever 404. Copy such assets into the served tree and reference them there.
218
+ if (file.includes(NODE_MODULES_SEGMENT)) {
219
+ throw new Error(
220
+ `Relative url() in a node_modules stylesheet cannot be served from the bundle (${file}): ${value}`,
221
+ );
222
+ }
223
+
224
+ const cut = value.search(/[?#]/);
225
+ const path = cut === -1 ? value : value.slice(0, cut);
226
+ const suffix = cut === -1 ? "" : value.slice(cut);
227
+ const target = resolve(dirname(file), path);
228
+ const fromBase = relative(base, target);
229
+ // Another Windows drive has no relative path: fail rather than emit a URL
230
+ // the browser can never resolve.
231
+ if (isAbsolute(fromBase)) {
232
+ throw new Error(`Cannot reach ${target} by a relative url() from ${base} (${file})`);
233
+ }
234
+
235
+ const rebased = fromBase.split(sep).join("/");
236
+ return `${rebased.startsWith(".") ? "" : "./"}${rebased}${suffix}`;
237
+ }
238
+
239
+ // Rewrites the relative url() references of one line (see rebasedUrl).
240
+ // Comments and strings are copied verbatim — a url() quoted inside a data URI
241
+ // or a `content` string is text, not a reference; `state.inComment` carries a
242
+ // comment that spans lines. A url() split over several lines is left as is.
243
+ function rebaseUrls(line, file, base, state) {
244
+ let out = "";
245
+ let i = 0;
246
+
247
+ while (i < line.length) {
248
+ if (state.inComment) {
249
+ const close = line.indexOf("*/", i);
250
+ const end = close === -1 ? line.length : close + 2;
251
+ out += line.slice(i, end);
252
+ state.inComment = close === -1;
253
+ i = end;
254
+ continue;
255
+ }
256
+
257
+ const char = line[i];
258
+
259
+ if (char === "/" && line[i + 1] === "*") {
260
+ state.inComment = true;
261
+ out += "/*";
262
+ i += 2;
263
+ continue;
264
+ }
265
+
266
+ if (char === '"' || char === "'") {
267
+ const end = stringEnd(line, i);
268
+ out += line.slice(i, end);
269
+ i = end;
270
+ continue;
271
+ }
272
+
273
+ const call =
274
+ line.slice(i, i + 4).toLowerCase() === "url(" && !IDENT_CHAR_RE.test(line[i - 1] ?? "")
275
+ ? readUrlCall(line, i + 3)
276
+ : null;
277
+ if (!call) {
278
+ out += char;
279
+ i += 1;
280
+ continue;
281
+ }
282
+
283
+ const rebased = rebasedUrl(call.value, file, base);
284
+
285
+ if (rebased === null) {
286
+ out += line.slice(i, call.end);
287
+ i = call.end;
288
+ continue;
289
+ }
290
+
291
+ // Quote whatever an unquoted url() could not hold; escape the quote kept.
292
+ const quote = call.quote || (/[\s"'()]/.test(rebased) ? '"' : "");
293
+ const body = quote ? rebased.replaceAll(quote, `\\${quote}`) : rebased;
294
+ out += `url(${quote}${body}${quote})`;
295
+ i = call.end;
296
+ }
297
+
298
+ return out;
299
+ }
300
+
301
+ async function inlineFile(path, stack, preserved, layered, base) {
161
302
  if (stack.includes(path)) {
162
303
  const chain = [...stack, path].map((item) => item.replace(/.*[/\\]/, ""));
163
304
  throw new Error(`Circular CSS import: ${chain.join(" -> ")}`);
@@ -166,6 +307,7 @@ async function inlineFile(path, stack, preserved, layered) {
166
307
  const css = await readFile(path, "utf8");
167
308
  const nextStack = [...stack, path];
168
309
  const chunks = [];
310
+ const urlState = { inComment: false };
169
311
 
170
312
  for (const line of css.split(/\r?\n/)) {
171
313
  const parsed = parseImportLine(line);
@@ -174,7 +316,7 @@ async function inlineFile(path, stack, preserved, layered) {
174
316
  if (IMPORT_PREFIX_RE.test(line)) {
175
317
  throw new Error(`Unsupported CSS @import syntax in ${path}: ${line.trim()}`);
176
318
  }
177
- chunks.push(line);
319
+ chunks.push(base ? rebaseUrls(line, path, base, urlState) : line);
178
320
  continue;
179
321
  }
180
322
 
@@ -199,16 +341,21 @@ async function inlineFile(path, stack, preserved, layered) {
199
341
  }
200
342
 
201
343
  const target = resolveImportTarget(path, parsed.specifier);
202
- const inlined = await inlineFile(target, nextStack, preserved, layered || layer !== null);
344
+ const inlined = await inlineFile(target, nextStack, preserved, layered || layer !== null, base);
203
345
  chunks.push(layer ? wrapInLayer(inlined, layer.name) : inlined);
204
346
  }
205
347
 
206
348
  return `${chunks.join("\n").trim()}\n`;
207
349
  }
208
350
 
209
- export async function inlineImports(file) {
351
+ // `outDir`: the directory the bundle will be written to. When given, relative
352
+ // url() references are rewritten to keep pointing at their files from there
353
+ // (fonts, images next to an imported stylesheet); without it they are copied
354
+ // as written, which is only right when the bundle sits beside its entry.
355
+ export async function inlineImports(file, { outDir } = {}) {
210
356
  const preserved = [];
211
- const body = await inlineFile(resolve(file), [], preserved, false);
357
+ const base = outDir ? resolve(outDir) : null;
358
+ const body = await inlineFile(resolve(file), [], preserved, false, base);
212
359
 
213
360
  if (preserved.length === 0) return body;
214
361