@zonsh/ui 0.2.0 → 0.5.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.
Files changed (200) hide show
  1. package/LICENSE +22 -0
  2. package/README.md +19 -13
  3. package/dist/components/accordion.js +1 -1
  4. package/dist/components/alert-dialog.js +1 -1
  5. package/dist/components/ascii-field.d.ts +1 -1
  6. package/dist/components/ascii-field.js +2 -1
  7. package/dist/components/bottom-nav.d.ts +86 -0
  8. package/dist/components/bottom-nav.js +175 -0
  9. package/dist/components/breadcrumb.d.ts +9 -0
  10. package/dist/components/breadcrumb.js +6 -1
  11. package/dist/components/button-group.d.ts +1 -1
  12. package/dist/components/button-group.js +2 -2
  13. package/dist/components/button.js +1 -1
  14. package/dist/components/calendar.d.ts +1 -1
  15. package/dist/components/calendar.js +18 -3
  16. package/dist/components/carousel.d.ts +22 -3
  17. package/dist/components/carousel.js +25 -7
  18. package/dist/components/chart.d.ts +20 -0
  19. package/dist/components/chart.js +52 -3
  20. package/dist/components/code-block.d.ts +20 -10
  21. package/dist/components/code-block.js +54 -12
  22. package/dist/components/collapsible.js +3 -3
  23. package/dist/components/color-picker.d.ts +30 -8
  24. package/dist/components/color-picker.js +48 -27
  25. package/dist/components/combobox.d.ts +21 -3
  26. package/dist/components/combobox.js +79 -7
  27. package/dist/components/command.d.ts +23 -3
  28. package/dist/components/command.js +63 -18
  29. package/dist/components/conversation.d.ts +10 -1
  30. package/dist/components/conversation.js +24 -7
  31. package/dist/components/copy-button.d.ts +15 -6
  32. package/dist/components/copy-button.js +27 -10
  33. package/dist/components/data-table.d.ts +66 -6
  34. package/dist/components/data-table.js +376 -23
  35. package/dist/components/date-picker.d.ts +73 -4
  36. package/dist/components/date-picker.js +138 -7
  37. package/dist/components/dialog.d.ts +10 -1
  38. package/dist/components/dialog.js +6 -3
  39. package/dist/components/drawer.d.ts +10 -1
  40. package/dist/components/drawer.js +6 -3
  41. package/dist/components/feature-list.js +2 -2
  42. package/dist/components/field.d.ts +27 -8
  43. package/dist/components/field.js +49 -4
  44. package/dist/components/file-drop.d.ts +26 -4
  45. package/dist/components/file-drop.js +125 -64
  46. package/dist/components/filter-bar.d.ts +50 -3
  47. package/dist/components/filter-bar.js +71 -33
  48. package/dist/components/form.d.ts +4 -2
  49. package/dist/components/form.js +24 -2
  50. package/dist/components/graph.d.ts +41 -0
  51. package/dist/components/graph.js +106 -50
  52. package/dist/components/illustration.js +51 -4
  53. package/dist/components/input-group.d.ts +18 -7
  54. package/dist/components/input-group.js +41 -19
  55. package/dist/components/input.d.ts +2 -2
  56. package/dist/components/input.js +3 -3
  57. package/dist/components/kbd.d.ts +17 -0
  58. package/dist/components/kbd.js +8 -1
  59. package/dist/components/layout.d.ts +2 -1
  60. package/dist/components/layout.js +55 -11
  61. package/dist/components/lightbox.d.ts +100 -0
  62. package/dist/components/lightbox.js +457 -0
  63. package/dist/components/locale.d.ts +44 -0
  64. package/dist/components/locale.js +37 -0
  65. package/dist/components/logo.js +1 -1
  66. package/dist/components/menu.js +3 -1
  67. package/dist/components/menubar.js +60 -13
  68. package/dist/components/message.d.ts +3 -3
  69. package/dist/components/message.js +12 -8
  70. package/dist/components/meter.js +3 -1
  71. package/dist/components/money-input.d.ts +80 -0
  72. package/dist/components/money-input.js +253 -0
  73. package/dist/components/navigation-menu.js +4 -2
  74. package/dist/components/number-field.d.ts +21 -2
  75. package/dist/components/number-field.js +53 -28
  76. package/dist/components/otp-field.d.ts +14 -1
  77. package/dist/components/otp-field.js +10 -2
  78. package/dist/components/pagination.d.ts +13 -7
  79. package/dist/components/pagination.js +18 -7
  80. package/dist/components/pdf-viewer.d.ts +67 -2
  81. package/dist/components/pdf-viewer.js +88 -21
  82. package/dist/components/phone-input.d.ts +79 -0
  83. package/dist/components/phone-input.js +282 -0
  84. package/dist/components/popover.js +1 -1
  85. package/dist/components/progress.js +3 -1
  86. package/dist/components/prompt-input.d.ts +34 -9
  87. package/dist/components/prompt-input.js +37 -17
  88. package/dist/components/radio-group.js +7 -2
  89. package/dist/components/rating.d.ts +73 -0
  90. package/dist/components/rating.js +124 -0
  91. package/dist/components/reasoning.d.ts +16 -1
  92. package/dist/components/reasoning.js +10 -7
  93. package/dist/components/resizable.d.ts +10 -1
  94. package/dist/components/resizable.js +4 -1
  95. package/dist/components/response.d.ts +17 -5
  96. package/dist/components/response.js +58 -18
  97. package/dist/components/scroll-area.d.ts +1 -1
  98. package/dist/components/select.js +14 -3
  99. package/dist/components/separator.js +1 -1
  100. package/dist/components/sidebar.d.ts +20 -4
  101. package/dist/components/sidebar.js +70 -35
  102. package/dist/components/site-header.d.ts +46 -12
  103. package/dist/components/site-header.js +78 -19
  104. package/dist/components/skeleton.d.ts +1 -1
  105. package/dist/components/skeleton.js +1 -1
  106. package/dist/components/slider.js +3 -1
  107. package/dist/components/sortable.d.ts +178 -0
  108. package/dist/components/sortable.js +1282 -0
  109. package/dist/components/sources.d.ts +17 -2
  110. package/dist/components/sources.js +11 -4
  111. package/dist/components/spinner.d.ts +13 -1
  112. package/dist/components/spinner.js +5 -2
  113. package/dist/components/stat.d.ts +91 -0
  114. package/dist/components/stat.js +141 -0
  115. package/dist/components/stepper.d.ts +71 -0
  116. package/dist/components/stepper.js +101 -0
  117. package/dist/components/switch.d.ts +2 -2
  118. package/dist/components/switch.js +2 -2
  119. package/dist/components/table.d.ts +13 -3
  120. package/dist/components/table.js +231 -41
  121. package/dist/components/tabs.js +8 -4
  122. package/dist/components/tags-input.d.ts +18 -2
  123. package/dist/components/tags-input.js +66 -34
  124. package/dist/components/theme.d.ts +16 -2
  125. package/dist/components/theme.js +34 -8
  126. package/dist/components/time-field.d.ts +24 -4
  127. package/dist/components/time-field.js +32 -9
  128. package/dist/components/timeline.d.ts +18 -2
  129. package/dist/components/timeline.js +13 -4
  130. package/dist/components/tippy.d.ts +48 -0
  131. package/dist/components/tippy.js +648 -0
  132. package/dist/components/toast.d.ts +18 -2
  133. package/dist/components/toast.js +12 -3
  134. package/dist/components/toggle-group.js +1 -0
  135. package/dist/components/tool-call.d.ts +19 -8
  136. package/dist/components/tool-call.js +23 -7
  137. package/dist/components/toolbar.d.ts +1 -1
  138. package/dist/components/toolbar.js +10 -2
  139. package/dist/components/tooltip.d.ts +4 -2
  140. package/dist/components/tooltip.js +10 -4
  141. package/dist/components/tour.d.ts +127 -0
  142. package/dist/components/tour.js +673 -0
  143. package/dist/components/tree.js +45 -9
  144. package/dist/components/video-player.d.ts +10 -2
  145. package/dist/components/video-player.js +134 -38
  146. package/dist/illustration.d.ts +2 -0
  147. package/dist/illustration.js +7 -0
  148. package/dist/index.d.ts +11 -3
  149. package/dist/index.js +11 -3
  150. package/dist/lib/code-highlight.d.ts +3 -16
  151. package/dist/lib/code-highlight.js +160 -10
  152. package/dist/lib/code-theme.js +3 -3
  153. package/dist/lib/digit-roll.js +8 -8
  154. package/dist/lib/field-errors.d.ts +28 -0
  155. package/dist/lib/field-errors.js +24 -0
  156. package/dist/lib/field-value.d.ts +13 -0
  157. package/dist/lib/field-value.js +44 -0
  158. package/dist/lib/glide.d.ts +5 -4
  159. package/dist/lib/glide.js +5 -4
  160. package/dist/lib/graph-layout.js +11 -2
  161. package/dist/lib/money.d.ts +73 -0
  162. package/dist/lib/money.js +343 -0
  163. package/dist/lib/phone-countries.d.ts +1 -0
  164. package/dist/lib/phone-countries.js +281 -0
  165. package/dist/lib/phone.d.ts +90 -0
  166. package/dist/lib/phone.js +286 -0
  167. package/dist/lib/platform.d.ts +8 -3
  168. package/dist/lib/platform.js +13 -6
  169. package/dist/lib/row-index.d.ts +15 -0
  170. package/dist/lib/row-index.js +136 -0
  171. package/dist/lib/table-fit.d.ts +3 -2
  172. package/dist/lib/table-fit.js +52 -23
  173. package/dist/lib/theme-script.d.ts +15 -2
  174. package/dist/lib/theme-script.js +23 -3
  175. package/dist/lib/tippy/pixels.d.ts +47 -0
  176. package/dist/lib/tippy/pixels.js +187 -0
  177. package/dist/lib/tippy/sprite.d.ts +86 -0
  178. package/dist/lib/tippy/sprite.js +753 -0
  179. package/dist/messages/de.d.ts +12 -0
  180. package/dist/messages/de.js +326 -0
  181. package/dist/styles/expressive.css +60 -26
  182. package/dist/styles/index.css +11 -2
  183. package/dist/styles/material.css +47 -23
  184. package/dist/styles/parts/ai.css +4 -2
  185. package/dist/styles/parts/controls.css +85 -32
  186. package/dist/styles/parts/data.css +33 -5
  187. package/dist/styles/parts/feedback.css +110 -13
  188. package/dist/styles/parts/foundations.css +1 -1
  189. package/dist/styles/parts/navigation.css +230 -5
  190. package/dist/styles/parts/overlays.css +98 -11
  191. package/dist/styles/parts/pdf.css +13 -7
  192. package/dist/styles/tokens.css +28 -4
  193. package/dist/vite.d.ts +0 -50
  194. package/dist/vite.js +0 -100
  195. package/package.json +28 -33
  196. package/dist/components/code-highlight.d.ts +0 -13
  197. package/dist/components/code-highlight.js +0 -107
  198. package/dist/components/whiteboard.d.ts +0 -80
  199. package/dist/components/whiteboard.js +0 -389
  200. package/dist/styles/parts/whiteboard.css +0 -855
@@ -6,6 +6,7 @@
6
6
  * fits on one line beside them; an `inline` column moves into the title cell instead. Only once
7
7
  * every column that can step out has done so may titles wrap.
8
8
  */
9
+ import { cssDuration } from "./motion.js";
9
10
  /* ── Fit ───────────────────────────────────────────────────────────────────── */
10
11
  /** The columns that can step out, in the order they do. The title never does. */
11
12
  export function removalOrder(columns, title) {
@@ -177,23 +178,27 @@ export function fitStep(options) {
177
178
  /**
178
179
  * One beat for the whole row: a leaving cell fades over exactly the time its neighbours take to
179
180
  * glide into the space it frees, on the same curve and from the same start, so the gap closes as
180
- * the cell goes. Entering cells start a moment later and land with them.
181
+ * the cell goes. Entering cells start a moment later and land with them. The beat is
182
+ * `--duration-glide` (1.6 × `--duration`, 240ms; 0 under reduced motion), read from the table.
181
183
  */
182
- const MOVE_MS = 280;
184
+ const moveMs = (el) => cssDuration(el) * 1.6;
183
185
  const ENTER_DELAY_MS = 40;
184
- const ENTER_MS = MOVE_MS - ENTER_DELAY_MS;
186
+ /** Entering cells grow from this scale: near their size, so they arrive rather than appear. */
187
+ const ENTER_SCALE = "0.9";
185
188
  /**
186
- * Fades run on a gentle standard curve, not the brand's front-loaded ease-out: on that curve a
187
- * fade is ~85% done in the first frames, so a returning column seemed to pop straight in.
189
+ * Fades run on `--ease-glide`, not the brand's front-loaded ease-out: on that curve a fade is
190
+ * ~85% done in the first frames, so a returning column seemed to pop straight in.
188
191
  */
189
- const FADE_EASING = "cubic-bezier(0.4, 0, 0.2, 1)";
190
- const EXIT_MS = MOVE_MS;
192
+ const fadeEasing = (el) => getComputedStyle(el).getPropertyValue("--ease-glide").trim() || "cubic-bezier(0.33, 1, 0.68, 1)";
191
193
  /** How far a leaving cell drifts towards its own side as it fades. */
192
194
  const EXIT_DRIFT_PX = 8;
193
195
  /** Delay between one row (or band) and the next, top to bottom — exits and glides alike. */
194
196
  const STAGGER_MS = 30;
195
- /** The last row in view starts by then, so a long table still settles within ~520ms. */
196
- const SPREAD_MS = 240;
197
+ /**
198
+ * Only the first rows in view are staggered; the rest start with the last of them. Two steps of
199
+ * 30ms and a 240ms glide: the whole table has settled within 300ms.
200
+ */
201
+ const STAGGER_ROWS = 3;
197
202
  const ZERO = { x: 0, y: 0 };
198
203
  const keyOf = (el) => el.getAttribute("data-flip-key") ?? el;
199
204
  /** Rows of a closed group are not drawn, nor a virtual body's sizer rows. */
@@ -306,8 +311,9 @@ export class Flip {
306
311
  return { els, rects, rel, view: box };
307
312
  }
308
313
  /**
309
- * The cascade: bands and rows in view count 0, 1, 2… from the top; those above the view start
310
- * at once, those below with the last one in view.
314
+ * The cascade: rows in view count 0, 1, 2… from the top, and a band takes the step of the row
315
+ * after it (it has nothing to glide, so it must not hold the rows back); those above the view
316
+ * start at once, those below with the last one in view.
311
317
  */
312
318
  cascade(rects, view) {
313
319
  const index = new Map();
@@ -322,12 +328,11 @@ export class Flip {
322
328
  else if (r.top >= view.bottom)
323
329
  below.push(el);
324
330
  else
325
- index.set(el, count++);
331
+ index.set(el, el.getAttribute("data-flip") === "row" ? count++ : count);
326
332
  }
327
333
  for (const el of below)
328
334
  index.set(el, Math.max(0, count - 1));
329
- const gap = count > 1 ? Math.min(STAGGER_MS, SPREAD_MS / (count - 1)) : 0;
330
- return (seq) => (seq ? (index.get(seq) ?? 0) * gap : 0);
335
+ return (seq) => (seq ? Math.min(index.get(seq) ?? 0, STAGGER_ROWS - 1) * STAGGER_MS : 0);
331
336
  }
332
337
  /** Forget the last positions: something other than the width moved things (scroll, data, a group). */
333
338
  invalidate() {
@@ -343,6 +348,7 @@ export class Flip {
343
348
  /** Glides from what is drawn now to the layout that `commit` applies (synchronously) for `next`. */
344
349
  tier(next, commit) {
345
350
  const easing = this.easing();
351
+ const move = moveMs(this.root);
346
352
  this.direction();
347
353
  // Read: positions (mid-glide included), the fade of anything mid-entrance, the ghosts still
348
354
  // fading out, and clones of the pieces about to step out.
@@ -412,9 +418,13 @@ export class Flip {
412
418
  }
413
419
  if (!from) {
414
420
  this.run(el, [
415
- { opacity: 0, scale: "0.6", transformOrigin: side },
421
+ { opacity: 0, scale: ENTER_SCALE, transformOrigin: side },
416
422
  { opacity: 1, scale: "1", transformOrigin: side },
417
- ], { duration: ENTER_MS, delay: wait + ENTER_DELAY_MS, easing: FADE_EASING });
423
+ ], {
424
+ duration: Math.max(0, move - ENTER_DELAY_MS),
425
+ delay: move ? wait + ENTER_DELAY_MS : 0,
426
+ easing: fadeEasing(this.root),
427
+ });
418
428
  continue;
419
429
  }
420
430
  const to = last.rel.get(keyOf(el));
@@ -431,7 +441,7 @@ export class Flip {
431
441
  this.run(el, [
432
442
  { translate: `${dx}px ${dy}px`, ...fade[0] },
433
443
  { translate: "0px 0px", ...fade[1] },
434
- ], { duration: MOVE_MS, delay: wait, easing });
444
+ ], { duration: move, delay: wait, easing });
435
445
  }
436
446
  for (const l of leaving) {
437
447
  if (l.key)
@@ -465,7 +475,7 @@ export class Flip {
465
475
  transformOrigin: side,
466
476
  offset: 1,
467
477
  },
468
- ], { duration: EXIT_MS, delay: delay(l.seq), easing, fill: "both" }));
478
+ ], { duration: move, delay: delay(l.seq), easing, fill: "both" }));
469
479
  const ghost = { clone, anim };
470
480
  if (l.key)
471
481
  this.ghosts.set(l.key, ghost);
@@ -521,7 +531,7 @@ export class Flip {
521
531
  this.run(el, [
522
532
  { translate: `${dx}px ${dy}px`, ...fade[0] },
523
533
  { translate: "0px 0px", ...fade[1] },
524
- ], { duration: MOVE_MS, easing });
534
+ ], { duration: moveMs(this.root), easing });
525
535
  }
526
536
  }
527
537
  this.cache = now.rel;
@@ -531,12 +541,14 @@ export const reducedMotion = () => typeof window !== "undefined" && window.match
531
541
  /* ── Virtual rows ──────────────────────────────────────────────────────────── */
532
542
  /** East Asian wide characters (CJK, Hangul, full-width forms): drawn about twice as wide. */
533
543
  const WIDE = /[ᄀ-ᅟ⺀-꓏가-힣豈-﫿︰-﹏＀-⦆¢-₩]/g;
544
+ /** No character a wide one could be: most values, checked faster than counting. */
545
+ const ASCII = /^[\x00-\u10ff]*$/;
534
546
  /** How long a value reads, in characters (wide ones twice): a stand-in for how wide its cell is drawn. */
535
547
  function textLength(value) {
536
548
  if (value == null)
537
549
  return 0;
538
550
  if (typeof value === "string")
539
- return value.length + (value.match(WIDE)?.length ?? 0);
551
+ return value.length + (ASCII.test(value) ? 0 : (value.match(WIDE)?.length ?? 0));
540
552
  if (typeof value === "number" || typeof value === "bigint" || typeof value === "boolean")
541
553
  return String(value).length;
542
554
  if (value instanceof Date)
@@ -563,15 +575,32 @@ export function widestRows(rows, valuesOf, per = 2) {
563
575
  if (list.length > per)
564
576
  list.pop();
565
577
  };
566
- rows.forEach((row, at) => {
578
+ const visit = (at) => {
567
579
  let total = 0;
568
- valuesOf(row).forEach((value, j) => {
580
+ valuesOf(rows[at]).forEach((value, j) => {
569
581
  const len = textLength(value);
570
582
  total += len;
571
583
  if (len > 0)
572
584
  keep(j + 1, len, at);
573
585
  });
574
586
  keep(0, total, at);
575
- });
587
+ };
588
+ // Every row, up to SAMPLE_FROM; past it, the first and last rows and an even spread between —
589
+ // reading a million values would hold up the table's first paint for the width of a column.
590
+ if (rows.length <= SAMPLE_FROM)
591
+ for (let at = 0; at < rows.length; at++)
592
+ visit(at);
593
+ else {
594
+ const ends = 1000;
595
+ const step = (rows.length - 2 * ends) / (SAMPLE_FROM - 2 * ends);
596
+ for (let at = 0; at < ends; at++)
597
+ visit(at);
598
+ for (let x = ends; x < rows.length - ends; x += step)
599
+ visit(Math.floor(x));
600
+ for (let at = rows.length - ends; at < rows.length; at++)
601
+ visit(at);
602
+ }
576
603
  return [...new Set(best.flat().map((entry) => entry.at))].sort((a, b) => a - b);
577
604
  }
605
+ /** Past this many rows, `widestRows` reads a sample of them. */
606
+ const SAMPLE_FROM = 50_000;
@@ -3,13 +3,26 @@ export type ThemeMode = "light" | "dark" | "system";
3
3
  export declare const THEME_STORAGE_KEY = "zon-theme";
4
4
  /** The media query `system` follows. Not part of the public API. */
5
5
  export declare const DARK_QUERY = "(prefers-color-scheme: dark)";
6
+ /**
7
+ * The page ground as one flat colour in each theme, --page-tone in tokens.css: what the grain
8
+ * over the background averages to. The browser's own bars (the status bar and toolbar on a
9
+ * phone) take it through `<meta name="theme-color">`, so they match the page.
10
+ */
11
+ export declare const PAGE_TONE: {
12
+ readonly light: "#f9f9f9";
13
+ readonly dark: "#141414";
14
+ };
15
+ /** The `theme-color` meta that names no media, made if the page has none. Not part of the public API. */
16
+ export declare function setThemeColor(dark: boolean): void;
6
17
  /**
7
18
  * Build the no-flash script for `<head>`. It applies `.dark` before first paint, using the
8
- * same storage key and default as `ThemeProvider`. Pass the same options to both.
19
+ * same storage key and default as `ThemeProvider`, and sets `<meta name="theme-color">` to the
20
+ * theme's page tone (making the tag if there is none). Pass the same options to both.
9
21
  */
10
- export declare function createThemeScript({ storageKey, defaultTheme, }?: {
22
+ export declare function createThemeScript({ storageKey, defaultTheme, themeColor, }?: {
11
23
  storageKey?: string;
12
24
  defaultTheme?: ThemeMode;
25
+ themeColor?: boolean;
13
26
  }): string;
14
27
  /**
15
28
  * Inline this in `<head>`, before any stylesheet paints, to avoid a flash of the wrong theme:
@@ -6,6 +6,22 @@
6
6
  export const THEME_STORAGE_KEY = "zon-theme";
7
7
  /** The media query `system` follows. Not part of the public API. */
8
8
  export const DARK_QUERY = "(prefers-color-scheme: dark)";
9
+ /**
10
+ * The page ground as one flat colour in each theme, --page-tone in tokens.css: what the grain
11
+ * over the background averages to. The browser's own bars (the status bar and toolbar on a
12
+ * phone) take it through `<meta name="theme-color">`, so they match the page.
13
+ */
14
+ export const PAGE_TONE = { light: "#f9f9f9", dark: "#141414" };
15
+ /** The `theme-color` meta that names no media, made if the page has none. Not part of the public API. */
16
+ export function setThemeColor(dark) {
17
+ let meta = document.querySelector('meta[name="theme-color"]:not([media])');
18
+ if (!meta) {
19
+ meta = document.createElement("meta");
20
+ meta.name = "theme-color";
21
+ document.head.appendChild(meta);
22
+ }
23
+ meta.content = dark ? PAGE_TONE.dark : PAGE_TONE.light;
24
+ }
9
25
  /**
10
26
  * A value as a JavaScript literal that is safe inside an HTML `<script>`: its JSON, with `<`
11
27
  * written as `\u003c`, so a `</script>` or `<!--` in it cannot end the element or change how
@@ -15,12 +31,16 @@ export const DARK_QUERY = "(prefers-color-scheme: dark)";
15
31
  const literal = (value) => JSON.stringify(value).replace(/[<>&\u2028\u2029]/g, (c) => `\\u${c.charCodeAt(0).toString(16).padStart(4, "0")}`);
16
32
  /**
17
33
  * Build the no-flash script for `<head>`. It applies `.dark` before first paint, using the
18
- * same storage key and default as `ThemeProvider`. Pass the same options to both.
34
+ * same storage key and default as `ThemeProvider`, and sets `<meta name="theme-color">` to the
35
+ * theme's page tone (making the tag if there is none). Pass the same options to both.
19
36
  */
20
- export function createThemeScript({ storageKey = THEME_STORAGE_KEY, defaultTheme = "system", } = {}) {
37
+ export function createThemeScript({ storageKey = THEME_STORAGE_KEY, defaultTheme = "system", themeColor = true, } = {}) {
21
38
  const key = literal(storageKey);
22
39
  const fallback = literal(defaultTheme);
23
- return `(function(){var t=null;try{t=localStorage.getItem(${key})}catch(e){}if(t!=="light"&&t!=="dark"&&t!=="system")t=${fallback};var d=t==="dark";if(t==="system"){try{d=matchMedia(${literal(DARK_QUERY)}).matches}catch(e){}}document.documentElement.classList.toggle("dark",d)})();`;
40
+ const meta = themeColor
41
+ ? `var m=document.querySelector('meta[name="theme-color"]:not([media])');if(!m){m=document.createElement("meta");m.name="theme-color";document.head.appendChild(m)}m.content=d?${literal(PAGE_TONE.dark)}:${literal(PAGE_TONE.light)};`
42
+ : "";
43
+ return `(function(){var t=null;try{t=localStorage.getItem(${key})}catch(e){}if(t!=="light"&&t!=="dark"&&t!=="system")t=${fallback};var d=t==="dark";if(t==="system"){try{d=matchMedia(${literal(DARK_QUERY)}).matches}catch(e){}}document.documentElement.classList.toggle("dark",d);${meta}})();`;
24
44
  }
25
45
  /**
26
46
  * Inline this in `<head>`, before any stylesheet paints, to avoid a flash of the wrong theme:
@@ -0,0 +1,47 @@
1
+ /** A palette index (0 darkest) or any CSS colour. */
2
+ export type Ink = number | string;
3
+ /** The pen a sprite draws with: canvas shapes in its inks. */
4
+ export interface Pen {
5
+ c: CanvasRenderingContext2D;
6
+ color(k: Ink): Pen;
7
+ rect(x: number, y: number, w: number, h: number, k?: Ink): Pen;
8
+ circle(x: number, y: number, r: number, k?: Ink): Pen;
9
+ poly(pts: [number, number][], k?: Ink): Pen;
10
+ line(x1: number, y1: number, x2: number, y2: number, w?: number, k?: Ink): Pen;
11
+ path(pts: [number, number][], w?: number, k?: Ink): Pen;
12
+ /** A pixel picture: rows of characters, "a" in ink `ka`, "b" in `kb`, the rest clear. */
13
+ bitmap(x: number, y: number, rows: string[], ka: Ink, kb: Ink, flip?: boolean): Pen;
14
+ }
15
+ /** What the engine needs of a sprite. */
16
+ export interface PixelSprite<Pose> {
17
+ /** Its inks, darkest first. */
18
+ palette: readonly string[];
19
+ /** The outline colour. */
20
+ outline: string;
21
+ /** Sprite size in pixels, and the room over it for jumps and signs. */
22
+ size: number;
23
+ headroom: number;
24
+ /** Draw the pose with the origin on the ground under its middle (y up is negative). */
25
+ draw(g: Pen, pose: Pose): void;
26
+ }
27
+ /** The size of the picture a sprite is drawn into: room either side for arms and signs. */
28
+ export declare function frameSize(s: {
29
+ size: number;
30
+ headroom: number;
31
+ }): {
32
+ width: number;
33
+ height: number;
34
+ };
35
+ export interface FrameOptions {
36
+ /** Sprite pixels it is lifted off the ground, and shaken sideways. */
37
+ lift?: number;
38
+ jitter?: number;
39
+ /** A shadow on the ground under it, smaller as it lifts. */
40
+ shadow?: boolean;
41
+ }
42
+ /**
43
+ * Draw `sprite` in `pose` and snap it to pixels: its palette (with a Bayer dither where it
44
+ * shades), hard edges and a one-pixel outline. Returns the frame as ImageData, ready to put on a
45
+ * canvas of `frameSize(sprite)`.
46
+ */
47
+ export declare function rasterise<Pose>(sprite: PixelSprite<Pose>, pose: Pose, o?: FrameOptions): ImageData;
@@ -0,0 +1,187 @@
1
+ /*
2
+ * The pixel engine Tippy is drawn with, ported from the zon gallery's sprite engine (only what
3
+ * Tippy uses). A sprite is a small vector drawing made at sprite size that is snapped to its
4
+ * palette, with an ordered (Bayer) dither where the drawing shades, cut to hard edges and outlined
5
+ * in one pixel. The result is put on a canvas at one canvas pixel to a sprite pixel and scaled up
6
+ * by CSS with `image-rendering: pixelated`, a whole number of device pixels to a sprite pixel.
7
+ *
8
+ * Nothing here touches the DOM until a frame is drawn, so the module is safe to import on a server.
9
+ */
10
+ const TAU = Math.PI * 2;
11
+ function pen(c, palette) {
12
+ const col = (k) => (typeof k === "number" ? palette[k] : k);
13
+ const g = {
14
+ c,
15
+ color(k) {
16
+ c.fillStyle = col(k);
17
+ c.strokeStyle = col(k);
18
+ return g;
19
+ },
20
+ rect(x, y, w, h, k) {
21
+ if (k !== undefined)
22
+ g.color(k);
23
+ c.fillRect(x, y, w, h);
24
+ return g;
25
+ },
26
+ circle(x, y, r, k) {
27
+ if (k !== undefined)
28
+ g.color(k);
29
+ c.beginPath();
30
+ c.arc(x, y, r, 0, TAU);
31
+ c.fill();
32
+ return g;
33
+ },
34
+ poly(pts, k) {
35
+ if (k !== undefined)
36
+ g.color(k);
37
+ c.beginPath();
38
+ pts.forEach(([x, y], i) => (i ? c.lineTo(x, y) : c.moveTo(x, y)));
39
+ c.closePath();
40
+ c.fill();
41
+ return g;
42
+ },
43
+ line(x1, y1, x2, y2, w = 1, k) {
44
+ if (k !== undefined)
45
+ g.color(k);
46
+ c.lineWidth = w;
47
+ c.lineCap = "round";
48
+ c.beginPath();
49
+ c.moveTo(x1, y1);
50
+ c.lineTo(x2, y2);
51
+ c.stroke();
52
+ return g;
53
+ },
54
+ path(pts, w = 1, k) {
55
+ if (k !== undefined)
56
+ g.color(k);
57
+ c.lineWidth = w;
58
+ c.lineCap = "round";
59
+ c.lineJoin = "round";
60
+ c.beginPath();
61
+ pts.forEach(([x, y], i) => (i ? c.lineTo(x, y) : c.moveTo(x, y)));
62
+ c.stroke();
63
+ return g;
64
+ },
65
+ bitmap(x, y, rows, ka, kb, flip = false) {
66
+ const w = rows[0].length;
67
+ x = Math.round(x);
68
+ y = Math.round(y);
69
+ rows.forEach((row, j) => {
70
+ for (let i = 0; i < row.length; i++) {
71
+ const ch = row[i];
72
+ if (ch !== "a" && ch !== "b")
73
+ continue;
74
+ c.fillStyle = col(ch === "a" ? ka : kb);
75
+ c.fillRect(flip ? x + w - 1 - i : x + i, y + j, 1, 1);
76
+ }
77
+ });
78
+ return g;
79
+ },
80
+ };
81
+ return g;
82
+ }
83
+ /** The size of the picture a sprite is drawn into: room either side for arms and signs. */
84
+ export function frameSize(s) {
85
+ return { width: s.size * 2, height: s.size + s.headroom };
86
+ }
87
+ const hex = (h) => [
88
+ parseInt(h.slice(1, 3), 16),
89
+ parseInt(h.slice(3, 5), 16),
90
+ parseInt(h.slice(5, 7), 16),
91
+ ];
92
+ const BAYER = [0, 8, 2, 10, 12, 4, 14, 6, 3, 11, 1, 9, 15, 7, 13, 5];
93
+ /** How far the dither threshold moves a channel either way. */
94
+ const SPREAD = 56;
95
+ let scratch = null;
96
+ /** The drawing context frames are made on before they are snapped (made on first use). */
97
+ function scratchContext(width, height) {
98
+ if (!scratch) {
99
+ const canvas = document.createElement("canvas");
100
+ scratch = canvas.getContext("2d", { willReadFrequently: true });
101
+ if (!scratch)
102
+ throw new Error("no 2d canvas");
103
+ }
104
+ const canvas = scratch.canvas;
105
+ if (canvas.width !== width)
106
+ canvas.width = width;
107
+ if (canvas.height !== height)
108
+ canvas.height = height;
109
+ scratch.setTransform(1, 0, 0, 1, 0, 0);
110
+ scratch.clearRect(0, 0, width, height);
111
+ return scratch;
112
+ }
113
+ /**
114
+ * Draw `sprite` in `pose` and snap it to pixels: its palette (with a Bayer dither where it
115
+ * shades), hard edges and a one-pixel outline. Returns the frame as ImageData, ready to put on a
116
+ * canvas of `frameSize(sprite)`.
117
+ */
118
+ export function rasterise(sprite, pose, o = {}) {
119
+ const { width: W, height: H } = frameSize(sprite);
120
+ const ctx = scratchContext(W, H);
121
+ const lift = o.lift ?? 0;
122
+ ctx.save();
123
+ ctx.translate(W / 2 + Math.round(o.jitter ?? 0), H - 2 - lift);
124
+ sprite.draw(pen(ctx, sprite.palette), pose);
125
+ ctx.restore();
126
+ const img = ctx.getImageData(0, 0, W, H);
127
+ const d = img.data;
128
+ const inks = sprite.palette.map(hex);
129
+ const outline = hex(sprite.outline);
130
+ const solid = new Uint8Array(W * H);
131
+ for (let y = 0; y < H; y++)
132
+ for (let x = 0; x < W; x++) {
133
+ const i = (y * W + x) * 4;
134
+ if (d[i + 3] < 128) {
135
+ d[i + 3] = 0;
136
+ continue;
137
+ }
138
+ solid[y * W + x] = 1;
139
+ const th = ((BAYER[((y & 3) << 2) | (x & 3)] + 0.5) / 16 - 0.5) * SPREAD;
140
+ let best = 0;
141
+ let bd = Infinity;
142
+ for (let p = 0; p < inks.length; p++) {
143
+ const q = inks[p];
144
+ const dd = (d[i] + th - q[0]) ** 2 + (d[i + 1] + th - q[1]) ** 2 + (d[i + 2] + th - q[2]) ** 2;
145
+ if (dd < bd) {
146
+ bd = dd;
147
+ best = p;
148
+ }
149
+ }
150
+ const q = inks[best];
151
+ d[i] = q[0];
152
+ d[i + 1] = q[1];
153
+ d[i + 2] = q[2];
154
+ d[i + 3] = 255;
155
+ }
156
+ // The outline: every clear pixel beside a solid one.
157
+ for (let y = 0; y < H; y++)
158
+ for (let x = 0; x < W; x++) {
159
+ if (solid[y * W + x])
160
+ continue;
161
+ const n = (x > 0 && solid[y * W + x - 1]) ||
162
+ (x < W - 1 && solid[y * W + x + 1]) ||
163
+ (y > 0 && solid[(y - 1) * W + x]) ||
164
+ (y < H - 1 && solid[(y + 1) * W + x]);
165
+ if (!n)
166
+ continue;
167
+ const i = (y * W + x) * 4;
168
+ d[i] = outline[0];
169
+ d[i + 1] = outline[1];
170
+ d[i + 2] = outline[2];
171
+ d[i + 3] = 255;
172
+ }
173
+ if (o.shadow) {
174
+ // A soft shadow on the ground, under whatever is clear there: narrower the higher it is.
175
+ const S = sprite.size;
176
+ const half = Math.round(Math.max(S * 0.08, S * 0.32 - lift * 0.4));
177
+ for (const y of [H - 3, H - 2])
178
+ for (let x = W / 2 - half; x < W / 2 + half; x++) {
179
+ const i = (y * W + x) * 4;
180
+ if (d[i + 3])
181
+ continue;
182
+ d[i] = d[i + 1] = d[i + 2] = 0;
183
+ d[i + 3] = 90;
184
+ }
185
+ }
186
+ return img;
187
+ }
@@ -0,0 +1,86 @@
1
+ import type { Pen, PixelSprite } from "./pixels.js";
2
+ /** Every move Tippy plays here. */
3
+ export declare const TIPPY_MOVES: readonly ["idle", "walk", "run", "jump", "sit", "wave", "nod", "shake", "shrug", "think", "look", "cheer", "giggle", "love", "surprise", "shy", "error", "success"];
4
+ export type TippyMove = (typeof TIPPY_MOVES)[number];
5
+ /** How long one round of each move takes, in seconds (sit holds once it is down). */
6
+ export declare const MOVE_LENGTH: Record<TippyMove, number>;
7
+ /** The moment of each move to show when it holds still (reduced motion): the move at its clearest. */
8
+ export declare const STILL: Record<TippyMove, number>;
9
+ declare const EYES: Record<string, {
10
+ rows: string[];
11
+ mirror?: boolean;
12
+ }>;
13
+ type Eye = keyof typeof EYES;
14
+ declare const MOUTHS: {
15
+ w: string[];
16
+ o: string[];
17
+ smile: string[];
18
+ };
19
+ type Mouth = keyof typeof MOUTHS;
20
+ type Sign = "!" | "?" | "check" | "cross" | "heart" | "confetti";
21
+ /** An arm: from the shoulder at an angle outward from straight down, or with its hand put somewhere. */
22
+ type Arm = {
23
+ ang: number;
24
+ len?: number;
25
+ to?: undefined;
26
+ behind?: undefined;
27
+ } | {
28
+ to: [number, number];
29
+ behind?: boolean;
30
+ ang?: undefined;
31
+ len?: undefined;
32
+ } | null;
33
+ interface Body {
34
+ x: number;
35
+ y: number;
36
+ dx: number;
37
+ rot: number;
38
+ px: number;
39
+ py: number;
40
+ cx: number;
41
+ sx: number;
42
+ sy: number;
43
+ lift: number;
44
+ look: number;
45
+ eyeDy: number;
46
+ blink: boolean | null;
47
+ eyes: Eye | [Eye | null, Eye | null] | null;
48
+ mouth: Mouth | null;
49
+ blush: boolean;
50
+ mark: Sign | null;
51
+ markHop: number;
52
+ armL: Arm;
53
+ armR: Arm;
54
+ behind: ((g: Pen) => void) | null;
55
+ front: ((g: Pen) => void) | null;
56
+ /** Its highest point once grounded, for a sign over it. */
57
+ top: number;
58
+ topX: number;
59
+ }
60
+ /** The move it was in when it was changed, how far into it (seconds), and what that came from. */
61
+ export interface TippyFrom {
62
+ move: TippyMove;
63
+ t: number;
64
+ from: TippyFrom | null;
65
+ }
66
+ /** Tippy as it is drawn in one frame. */
67
+ export interface TippyPose {
68
+ body: Body;
69
+ /** Sprite pixels off the ground, and shaken sideways. */
70
+ lift: number;
71
+ jitter: number;
72
+ /** Which way it faces: the picture is mirrored for -1, and its signs are flipped back. */
73
+ dir: 1 | -1;
74
+ /** Seconds, for the confetti. */
75
+ t: number;
76
+ }
77
+ /** Frames a second Tippy is posed at: time is held to whole frames. */
78
+ export declare const FPS = 24;
79
+ /**
80
+ * Tippy in `move`, `t` seconds after it began (held to whole frames at 24 a second), grown out of
81
+ * `from`, facing `dir`.
82
+ */
83
+ export declare function poseTippy(move: TippyMove, t: number, from: TippyFrom | null, dir?: 1 | -1): TippyPose;
84
+ /** Tippy for the pixel engine: its inks, darkest first, 64 pixels with room over it for a jump and its sign. */
85
+ export declare const TIPPY: PixelSprite<TippyPose>;
86
+ export {};