@luxalgo/vela-ui 0.2.0 → 0.4.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 (217) hide show
  1. package/CHANGELOG.md +153 -91
  2. package/LICENSE +201 -201
  3. package/NOTICE +28 -28
  4. package/README.md +86 -86
  5. package/cli/engine.mjs +282 -282
  6. package/cli/index.mjs +154 -154
  7. package/dist/badge.js +1 -1
  8. package/dist/bar-chart.d.ts +1 -1
  9. package/dist/bar-chart.js +1 -1
  10. package/dist/button.d.ts +1 -1
  11. package/dist/button.js +4 -4
  12. package/dist/callout-bubble.d.ts +1 -1
  13. package/dist/callout-bubble.js +4 -4
  14. package/dist/card.d.ts +52 -0
  15. package/dist/card.js +179 -0
  16. package/dist/checkbox.js +5 -5
  17. package/dist/{chunk-B5ZSRUDQ.js → chunk-BWSOMG3G.js} +2 -2
  18. package/dist/{chunk-G7IQLY3A.js → chunk-C3PXY2SW.js} +5 -0
  19. package/dist/{chunk-CC3DDNFC.js → chunk-DXDCKZ2M.js} +4 -4
  20. package/dist/chunk-GUIYFQ27.js +30 -0
  21. package/dist/{chunk-WGULXRWD.js → chunk-KUWTI2PE.js} +4 -4
  22. package/dist/{chunk-Z5PINFQO.js → chunk-QGWAN3EP.js} +5 -5
  23. package/dist/{chunk-V2Q4MRMS.js → chunk-RH53NQGE.js} +30 -4
  24. package/dist/{chunk-WBGSYCV2.js → chunk-SKFAYTDK.js} +4 -4
  25. package/dist/{chunk-MJ4RUGX7.js → chunk-TGJNHUYV.js} +3 -3
  26. package/dist/{chunk-WXH3EFV5.js → chunk-VC7SVSDK.js} +4 -4
  27. package/dist/{chunk-7VMN7NKJ.js → chunk-YT2BSP7O.js} +3 -3
  28. package/dist/collapsible.d.ts +1 -1
  29. package/dist/collapsible.js +3 -3
  30. package/dist/color-picker.js +5 -5
  31. package/dist/context-menu.js +5 -5
  32. package/dist/data-table.d.ts +1 -1
  33. package/dist/data-table.js +1 -1
  34. package/dist/date-picker.d.ts +1 -1
  35. package/dist/date-picker.js +2 -2
  36. package/dist/dialog.d.ts +9 -1
  37. package/dist/dialog.js +15 -5
  38. package/dist/donut.d.ts +1 -1
  39. package/dist/donut.js +1 -1
  40. package/dist/drawer.d.ts +1 -1
  41. package/dist/drawer.js +2 -2
  42. package/dist/dropdown-menu.d.ts +1 -1
  43. package/dist/dropdown-menu.js +5 -5
  44. package/dist/empty.d.ts +1 -1
  45. package/dist/empty.js +2 -2
  46. package/dist/field.js +13 -13
  47. package/dist/{stat.d.ts → figure.d.ts} +9 -9
  48. package/dist/figure.js +41 -0
  49. package/dist/glyph-select.js +5 -5
  50. package/dist/hover-card.d.ts +1 -1
  51. package/dist/hover-card.js +3 -3
  52. package/dist/icons.js +1 -1
  53. package/dist/index.d.ts +4 -2
  54. package/dist/index.js +3 -3
  55. package/dist/input-group.d.ts +1 -1
  56. package/dist/input-group.js +4 -4
  57. package/dist/input.js +2 -2
  58. package/dist/item.d.ts +1 -1
  59. package/dist/item.js +2 -2
  60. package/dist/kbd.d.ts +1 -1
  61. package/dist/kbd.js +2 -2
  62. package/dist/line-chart.d.ts +1 -1
  63. package/dist/line-chart.js +1 -1
  64. package/dist/manifest.json +691 -74
  65. package/dist/month-grid.js +1 -1
  66. package/dist/number-input.d.ts +1 -1
  67. package/dist/number-input.js +5 -5
  68. package/dist/popover.js +3 -3
  69. package/dist/scatter-chart.d.ts +1 -1
  70. package/dist/scatter-chart.js +1 -1
  71. package/dist/select.d.ts +1 -1
  72. package/dist/select.js +7 -7
  73. package/dist/slider.d.ts +1 -1
  74. package/dist/slider.js +2 -2
  75. package/dist/sort-button.js +4 -4
  76. package/dist/sparkline.d.ts +1 -1
  77. package/dist/sparkline.js +1 -1
  78. package/dist/switch.d.ts +1 -1
  79. package/dist/switch.js +3 -3
  80. package/dist/tabs.d.ts +1 -1
  81. package/dist/tabs.js +4 -4
  82. package/dist/text-area.d.ts +1 -1
  83. package/dist/text-area.js +2 -2
  84. package/dist/theme-TefB8vtl.d.ts +341 -0
  85. package/dist/theme.d.ts +42 -192
  86. package/dist/theme.js +1 -1
  87. package/dist/theme.schema.json +321 -30
  88. package/dist/toast.d.ts +49 -0
  89. package/dist/toast.js +463 -0
  90. package/dist/toggle-group.d.ts +1 -1
  91. package/dist/toggle-group.js +4 -4
  92. package/dist/tooltip.js +2 -2
  93. package/dist/treemap.d.ts +80 -0
  94. package/dist/treemap.js +470 -0
  95. package/dist/zag.d.ts +11 -2
  96. package/dist/zag.js +1 -1
  97. package/docs/theming.md +217 -217
  98. package/docs/what-to-change.md +20 -20
  99. package/docs/writing-a-component.md +250 -250
  100. package/llms.txt +6 -3
  101. package/package.json +90 -90
  102. package/registry/badge.ts +119 -119
  103. package/registry/bar-chart.ts +215 -215
  104. package/registry/button.ts +387 -379
  105. package/registry/callout-bubble.ts +347 -347
  106. package/registry/card.ts +379 -0
  107. package/registry/checkbox.ts +234 -234
  108. package/registry/collapsible.ts +271 -271
  109. package/registry/color-picker.ts +582 -582
  110. package/registry/context-menu.ts +208 -208
  111. package/registry/data-table.ts +215 -215
  112. package/registry/date-picker.ts +483 -483
  113. package/registry/dialog.ts +551 -535
  114. package/registry/donut.ts +145 -145
  115. package/registry/drawer.ts +636 -636
  116. package/registry/dropdown-menu.ts +762 -762
  117. package/registry/empty.ts +91 -91
  118. package/registry/field.ts +584 -584
  119. package/registry/{stat.ts → figure.ts} +96 -96
  120. package/registry/glyph-select.ts +268 -268
  121. package/registry/hover-card.ts +222 -222
  122. package/registry/input-group.ts +229 -229
  123. package/registry/input.ts +181 -181
  124. package/registry/item.ts +376 -376
  125. package/registry/kbd.ts +64 -64
  126. package/registry/line-chart.ts +218 -218
  127. package/registry/month-grid.ts +328 -328
  128. package/registry/number-input.ts +443 -443
  129. package/registry/popover.ts +368 -368
  130. package/registry/scatter-chart.ts +221 -221
  131. package/registry/select.ts +620 -620
  132. package/registry/slider.ts +259 -259
  133. package/registry/sort-button.ts +189 -189
  134. package/registry/sparkline.ts +114 -114
  135. package/registry/switch.ts +205 -205
  136. package/registry/tabs.ts +426 -426
  137. package/registry/text-area.ts +186 -186
  138. package/registry/toast.ts +965 -0
  139. package/registry/toggle-group.ts +295 -295
  140. package/registry/tooltip.ts +156 -156
  141. package/registry/treemap.ts +847 -0
  142. package/skills/theming-vela-ui/SKILL.md +137 -137
  143. package/skills/using-vela-ui-components/SKILL.md +139 -137
  144. package/skills/writing-vela-ui-components/SKILL.md +163 -163
  145. package/src/components/badge.ts +119 -119
  146. package/src/components/bar-chart.ts +215 -215
  147. package/src/components/button.ts +387 -379
  148. package/src/components/callout-bubble.ts +347 -347
  149. package/src/components/card.ts +379 -0
  150. package/src/components/checkbox.ts +234 -234
  151. package/src/components/collapsible.ts +271 -271
  152. package/src/components/color-picker.ts +582 -582
  153. package/src/components/context-menu.ts +208 -208
  154. package/src/components/data-table.ts +215 -215
  155. package/src/components/date-picker.ts +483 -483
  156. package/src/components/dialog.ts +551 -535
  157. package/src/components/donut.ts +145 -145
  158. package/src/components/drawer.ts +636 -636
  159. package/src/components/dropdown-menu.ts +762 -762
  160. package/src/components/empty.ts +91 -91
  161. package/src/components/field.ts +584 -584
  162. package/src/components/{stat.ts → figure.ts} +96 -96
  163. package/src/components/glyph-select.ts +268 -268
  164. package/src/components/hover-card.ts +222 -222
  165. package/src/components/input-group.ts +229 -229
  166. package/src/components/input.ts +181 -181
  167. package/src/components/item.ts +376 -376
  168. package/src/components/kbd.ts +64 -64
  169. package/src/components/line-chart.ts +218 -218
  170. package/src/components/month-grid.ts +328 -328
  171. package/src/components/number-input.ts +443 -443
  172. package/src/components/popover.ts +368 -368
  173. package/src/components/scatter-chart.ts +221 -221
  174. package/src/components/select.ts +620 -620
  175. package/src/components/slider.ts +259 -259
  176. package/src/components/sort-button.ts +189 -189
  177. package/src/components/sparkline.ts +114 -114
  178. package/src/components/switch.ts +205 -205
  179. package/src/components/tabs.ts +426 -426
  180. package/src/components/text-area.ts +186 -186
  181. package/src/components/toast.ts +965 -0
  182. package/src/components/toggle-group.ts +295 -295
  183. package/src/components/tooltip.ts +156 -156
  184. package/src/components/treemap.ts +847 -0
  185. package/src/index.ts +24 -24
  186. package/src/lib/calendar.ts +117 -117
  187. package/src/lib/chart.ts +72 -72
  188. package/src/lib/color.ts +89 -89
  189. package/src/lib/component-index.ts +163 -151
  190. package/src/lib/css.ts +48 -48
  191. package/src/lib/define.ts +45 -45
  192. package/src/lib/dispose.ts +36 -36
  193. package/src/lib/h.ts +55 -55
  194. package/src/lib/headless.ts +16 -16
  195. package/src/lib/i18n.ts +30 -30
  196. package/src/lib/icons.ts +244 -237
  197. package/src/lib/kit-text.ts +26 -26
  198. package/src/lib/page.ts +24 -24
  199. package/src/lib/palette.ts +81 -81
  200. package/src/lib/placement.ts +188 -188
  201. package/src/lib/scope.ts +135 -135
  202. package/src/lib/styles.ts +53 -53
  203. package/src/lib/surface-events.ts +31 -31
  204. package/src/lib/surface-exit.ts +87 -87
  205. package/src/lib/t.ts +68 -68
  206. package/src/lib/theme.ts +684 -664
  207. package/src/lib/tokens.ts +110 -110
  208. package/src/lib/variants.ts +32 -32
  209. package/src/lib/zag.ts +61 -46
  210. package/stylelint/index.mjs +60 -60
  211. package/stylelint/lint.mjs +81 -81
  212. package/stylelint/plugin.mjs +115 -115
  213. package/dist/chunk-P4MTEMT7.js +0 -23
  214. package/dist/stat.js +0 -41
  215. package/dist/{chunk-U3EH42YY.js → chunk-5BJV5FY5.js} +1 -1
  216. package/dist/{chunk-E2FE6V2N.js → chunk-ALAHGSLC.js} +1 -1
  217. package/dist/{chunk-C3ICYMI3.js → chunk-FYVQO5TN.js} +1 -1
@@ -0,0 +1,470 @@
1
+ import { kitTranslator } from './chunk-FVH6L7FM.js';
2
+ import { msg, textOf } from './chunk-FN2BUFNM.js';
3
+ import { showTip } from './chunk-A2FJ7Y7D.js';
4
+ import { h } from './chunk-Q6WTDVH6.js';
5
+ import { t } from './chunk-6QCILIT2.js';
6
+ import { defineComponent } from './chunk-VMCR3S2A.js';
7
+ import { defineStyles, css } from './chunk-XBEL4K33.js';
8
+ import './chunk-2GQIZM5Z.js';
9
+ import './chunk-BXMWQIBO.js';
10
+ import './chunk-X66RJAIS.js';
11
+
12
+ // src/components/treemap.ts
13
+ var treemapDef = defineComponent({
14
+ name: "treemap",
15
+ parts: ["root", "crumbs", "crumb", "map", "group", "group-name", "group-value", "tile", "tile-label", "tile-value", "legend", "legend-bar", "tip"],
16
+ settings: {
17
+ radius: t.radius.sm,
18
+ up: t.up,
19
+ down: t.down,
20
+ "tile-bg": `color-mix(in srgb, ${t.fg.bright} 2.5%, ${t.surface})`,
21
+ "tint-min": "3%",
22
+ "tint-max": "25%",
23
+ "hover-z": "1",
24
+ "tip-radius": t.radius.md,
25
+ "tip-bg": t.surface.elev,
26
+ "tip-shadow": t.shadow,
27
+ "tip-z": "2"
28
+ }
29
+ });
30
+ function squarify(items, bounds) {
31
+ const total = items.reduce((s, item) => s + Math.max(0, item.weight), 0);
32
+ if (!total || bounds.w <= 0 || bounds.h <= 0) return [];
33
+ const scale = bounds.w * bounds.h / total;
34
+ let queue = items.filter((item) => item.weight > 0).map((item) => ({ item, area: item.weight * scale })).sort((a, b) => b.area - a.area);
35
+ let { x, y, w, h: h2 } = bounds;
36
+ const out = [];
37
+ const worst = (row, side) => {
38
+ const sum = row.reduce((s, r) => s + r.area, 0);
39
+ const max = Math.max(...row.map((r) => r.area));
40
+ const min = Math.min(...row.map((r) => r.area));
41
+ return Math.max(side * side * max / (sum * sum), sum * sum / (side * side * min));
42
+ };
43
+ while (queue.length) {
44
+ const side = Math.min(w, h2);
45
+ const row = [queue[0]];
46
+ let i = 1;
47
+ while (i < queue.length && worst([...row, queue[i]], side) <= worst(row, side)) row.push(queue[i++]);
48
+ queue = queue.slice(i);
49
+ const sum = row.reduce((s, r) => s + r.area, 0);
50
+ if (w >= h2) {
51
+ const cw = sum / h2;
52
+ let cy = y;
53
+ for (const r of row) {
54
+ const ch = r.area / cw;
55
+ out.push({ ...r.item, x, y: cy, w: cw, h: ch });
56
+ cy += ch;
57
+ }
58
+ x += cw;
59
+ w -= cw;
60
+ } else {
61
+ const rh = sum / w;
62
+ let cx = x;
63
+ for (const r of row) {
64
+ const rw = r.area / rh;
65
+ out.push({ ...r.item, x: cx, y, w: rw, h: rh });
66
+ cx += rw;
67
+ }
68
+ y += rh;
69
+ h2 -= rh;
70
+ }
71
+ }
72
+ return out;
73
+ }
74
+ var TEXT = {
75
+ all: msg("ui.treemap.all", "All"),
76
+ zoomIn: msg("ui.treemap.zoomIn", "Zoom in")
77
+ };
78
+ var HEADER = 16;
79
+ var HEADED = { w: 80, h: 64 };
80
+ var injectStyles = defineStyles(
81
+ "vela-ui-treemap",
82
+ css`@layer vela.ui{[data-slot=treemap]{width:100%;height:100%;min-height:0;color:${t.fg};font-size:${t.fontSize.sm};position:relative}[data-slot=treemap]:where(:not([hidden])){flex-direction:column;gap:8px;display:flex}[data-slot=treemap-crumbs]{min-height:22px;color:${t.fg.muted};flex-wrap:wrap;align-items:center;gap:2px;font-size:12px;display:flex}[data-slot=treemap-crumb]{all:unset;border-radius:${treemapDef.vars.radius};cursor:pointer;padding:2px 6px}[data-slot=treemap-crumb]:hover{background:${t.hover};color:${t.fg.bright}}[data-slot=treemap-crumb]:focus-visible{outline:2px solid ${t.focus}}[data-slot=treemap-crumb][aria-current]{cursor:default;color:${t.fg.bright};background:0 0;font-weight:600}[data-slot=treemap-crumb]:not(:first-child):before{content:"›";color:${t.fg.faint};margin-inline-end:8px}[data-slot=treemap-map]{isolation:isolate;flex:auto;min-height:0;position:relative}[data-slot=treemap-group]{all:unset;letter-spacing:.06em;text-transform:uppercase;white-space:nowrap;height:16px;color:${t.fg.muted};transition:left ${t.dur.med} ${t.ease}, top ${t.dur.med} ${t.ease}, width ${t.dur.med} ${t.ease};align-items:center;gap:6px;padding-inline:2px;font-size:9px;font-weight:500;display:flex;position:absolute;overflow:hidden}[data-slot=treemap-group][data-drill]{border-radius:${treemapDef.vars.radius};cursor:zoom-in}[data-slot=treemap-group][data-drill]:hover{color:${t.fg.bright};background:${t.hover}}[data-slot=treemap-group]:focus-visible{outline:2px solid ${t.focus}}[data-slot=treemap-group-name]{text-overflow:ellipsis;overflow:hidden}[data-slot=treemap-group-value]{font-variant-numeric:tabular-nums;margin-inline-start:auto}[data-slot=treemap-group-value][data-tone=up]{color:${treemapDef.vars.up}}[data-slot=treemap-group-value][data-tone=down]{color:${treemapDef.vars.down}}[data-slot=treemap-tile]{all:unset;--_tone:${t.fg.muted};--_tint:calc(${treemapDef.vars["tint-min"]} + (${treemapDef.vars["tint-max"]} - ${treemapDef.vars["tint-min"]}) * var(--_level,0));border:1px solid color-mix(in srgb, var(--_tone) calc(var(--_tint) * .8), ${t.border});border-radius:${treemapDef.vars.radius};background:color-mix(in srgb, var(--_tone) var(--_tint), ${treemapDef.vars["tile-bg"]});text-align:center;cursor:pointer;transition:left ${t.dur.med} ${t.ease}, top ${t.dur.med} ${t.ease}, width ${t.dur.med} ${t.ease}, height ${t.dur.med} ${t.ease}, background-color ${t.dur.med} linear, scale ${t.dur.fast} ${t.ease};flex-direction:column;justify-content:center;align-items:center;gap:1px;display:flex;position:absolute;overflow:hidden}[data-slot=treemap-tile][data-tone=up]{--_tone:${treemapDef.vars.up}}[data-slot=treemap-tile][data-tone=down]{--_tone:${treemapDef.vars.down}}[data-slot=treemap-tile][data-tone=custom]{--_tone:var(--_color)}[data-slot=treemap-tile][data-tone=none]{--_tint:0%}[data-slot=treemap-tile]:hover{z-index:${treemapDef.vars["hover-z"]};border-color:${t.fg.bright};scale:1.04}[data-slot=treemap-tile]:focus-visible{z-index:${treemapDef.vars["hover-z"]};outline:2px solid ${t.focus}}[data-slot=treemap-tile][data-selected]{border-color:${t.fg.bright};outline:1px solid ${t.fg.bright}}[data-slot=treemap-tile-label]{text-overflow:ellipsis;white-space:nowrap;max-width:100%;color:${t.fg.bright};font-weight:600;line-height:1.1;overflow:hidden}[data-slot=treemap-tile-value]{white-space:nowrap;font-variant-numeric:tabular-nums;color:${t.fg.bright};opacity:.92}[data-slot=treemap-legend]{font-variant-numeric:tabular-nums;color:${t.fg.muted};grid-template-columns:auto 1fr auto;align-items:center;gap:8px;font-size:10px;display:grid}[data-slot=treemap-legend-bar]{border:1px solid ${t.border};background:linear-gradient(to right, color-mix(in srgb, ${treemapDef.vars.down} 70%, ${treemapDef.vars["tile-bg"]}), ${treemapDef.vars["tile-bg"]}, color-mix(in srgb, ${treemapDef.vars.up} 70%, ${treemapDef.vars["tile-bg"]}));border-radius:999px;height:6px}[data-slot=treemap-tip]{z-index:${treemapDef.vars["tip-z"]};pointer-events:none;border:1px solid ${t.border};border-radius:${treemapDef.vars["tip-radius"]};background:${treemapDef.vars["tip-bg"]};box-shadow:${treemapDef.vars["tip-shadow"]};color:${t.fg.bright};white-space:nowrap;font-variant-numeric:tabular-nums;transition:left ${t.dur.fast} ease-out, top ${t.dur.fast} ease-out;gap:2px;padding:8px;font-size:12px;line-height:1.35;position:absolute;top:0}[data-slot=treemap-tip]:where(:not([hidden])){display:grid}[data-slot=treemap-tip][data-instant]{transition:none}[data-slot=treemap-tip] [data-tone=up]{color:${treemapDef.vars.up}}[data-slot=treemap-tip] [data-tone=down]{color:${treemapDef.vars.down}}[data-slot=treemap-tip] small{color:${t.fg.muted}}}`
83
+ );
84
+ var weightOf = (item) => item.children ? item.children.reduce((s, c) => s + weightOf(c), 0) : Math.max(0, item.weight ?? 1);
85
+ function median(values) {
86
+ if (values.length === 0) return null;
87
+ const sorted = [...values].sort((a, b) => a - b);
88
+ const mid = (sorted.length - 1) / 2;
89
+ return (sorted[Math.floor(mid)] + sorted[Math.ceil(mid)]) / 2;
90
+ }
91
+ var toneOf = (value) => value === null ? "none" : value > 0 ? "up" : value < 0 ? "down" : "flat";
92
+ var defaultFormat = (value) => `${value > 0 ? "+" : ""}${value.toFixed(2)}`;
93
+ var TreemapImpl = class {
94
+ constructor(props) {
95
+ this.props = props;
96
+ this.values = /* @__PURE__ */ new Map();
97
+ this.tiles = /* @__PURE__ */ new Map();
98
+ this.groups = /* @__PURE__ */ new Map();
99
+ /** The zoomed groups' ids, outermost first. */
100
+ this.zoom = [];
101
+ /** The tile that takes the map's tab stop. */
102
+ this.current = null;
103
+ /** The tile the readout describes. */
104
+ this.tipFor = null;
105
+ this.box = { w: 0, h: 0 };
106
+ injectStyles(document);
107
+ this.i18n = props.i18n ?? kitTranslator();
108
+ this.gap = Math.max(0, props.gap ?? 3);
109
+ this.items = props.items ?? [];
110
+ this.selected = props.selected ?? null;
111
+ this.map = h("div", { slot: treemapDef.slot("map"), attrs: { role: "group", dir: "ltr", "aria-label": props.label ?? null } });
112
+ this.tip = h("div", { slot: treemapDef.slot("tip"), attrs: { "aria-hidden": "true" } });
113
+ this.tip.hidden = true;
114
+ this.crumbs = props.drill ? h("nav", { slot: treemapDef.slot("crumbs") }) : null;
115
+ let legendEl = null;
116
+ if (props.legend) {
117
+ const low = h("span");
118
+ const high = h("span");
119
+ legendEl = h("div", { slot: treemapDef.slot("legend"), attrs: { "aria-hidden": "true", dir: "ltr" } }, low, h("i", { slot: treemapDef.slot("legend-bar") }), high);
120
+ this.legend = { low, high };
121
+ } else this.legend = null;
122
+ this.el = h("div", { className: props.className, slot: treemapDef.slot("root") }, this.crumbs, this.map, legendEl, this.tip);
123
+ this.map.addEventListener("keydown", (e) => this.onKey(e));
124
+ this.map.addEventListener("pointerleave", () => this.hideTip());
125
+ this.el.addEventListener("keydown", (e) => {
126
+ if (e.key !== "Escape" || this.zoom.length === 0) return;
127
+ e.preventDefault();
128
+ e.stopPropagation();
129
+ this.setZoom(this.zoom.slice(0, -1), true);
130
+ });
131
+ this.ro = typeof ResizeObserver === "function" ? new ResizeObserver(() => this.measure()) : null;
132
+ this.ro?.observe(this.map);
133
+ this.renderCrumbs();
134
+ this.layout();
135
+ }
136
+ setData(items) {
137
+ this.items = items;
138
+ this.values.clear();
139
+ const zoom = [];
140
+ let level = items;
141
+ for (const id of this.zoom) {
142
+ const group = level.find((item) => item.id === id && item.children);
143
+ if (!group) break;
144
+ zoom.push(id);
145
+ level = group.children;
146
+ }
147
+ const changed = zoom.length !== this.zoom.length;
148
+ this.zoom = zoom;
149
+ this.renderCrumbs();
150
+ this.layout();
151
+ if (changed) this.props.onDrill?.(this.zoomPath());
152
+ }
153
+ setValues(values) {
154
+ const entries = values instanceof Map ? values.entries() : Object.entries(values);
155
+ for (const [id, value] of entries) this.values.set(id, value);
156
+ this.paint();
157
+ }
158
+ setSelected(id) {
159
+ this.selected = id;
160
+ for (const [key, tile] of this.tiles) tile.el.toggleAttribute("data-selected", key === id);
161
+ }
162
+ drillTo(id) {
163
+ if (id === null) {
164
+ this.setZoom([], true);
165
+ return;
166
+ }
167
+ const path = this.pathTo(id, this.items, []);
168
+ if (path) this.setZoom([...path.map((g) => g.id), id], true);
169
+ }
170
+ destroy() {
171
+ this.ro?.disconnect();
172
+ this.el.remove();
173
+ }
174
+ pathTo(id, level, above) {
175
+ for (const item of level) {
176
+ if (!item.children) continue;
177
+ if (item.id === id) return above;
178
+ const found = this.pathTo(id, item.children, [...above, item]);
179
+ if (found) return found;
180
+ }
181
+ return null;
182
+ }
183
+ /** The zoomed groups, outermost first. */
184
+ zoomPath() {
185
+ const path = [];
186
+ let level = this.items;
187
+ for (const id of this.zoom) {
188
+ const group = level.find((item) => item.id === id);
189
+ if (!group?.children) break;
190
+ path.push(group);
191
+ level = group.children;
192
+ }
193
+ return path;
194
+ }
195
+ setZoom(zoom, announce) {
196
+ if (zoom.join("\0") === this.zoom.join("\0")) return;
197
+ this.zoom = zoom;
198
+ this.current = null;
199
+ this.hideTip();
200
+ this.renderCrumbs();
201
+ this.layout();
202
+ if (announce) this.props.onDrill?.(this.zoomPath());
203
+ }
204
+ renderCrumbs() {
205
+ if (!this.crumbs) return;
206
+ const path = this.zoomPath();
207
+ const entries = [{ label: this.props.rootLabel ?? textOf(this.i18n, TEXT.all), zoom: [] }];
208
+ path.forEach((group, i) => entries.push({ label: group.label, zoom: this.zoom.slice(0, i + 1) }));
209
+ this.crumbs.replaceChildren(
210
+ ...entries.map((entry, i) => {
211
+ const last = i === entries.length - 1;
212
+ return h("button", {
213
+ slot: treemapDef.slot("crumb"),
214
+ text: entry.label,
215
+ attrs: { type: "button", "aria-current": last ? "true" : null, tabindex: last ? "-1" : null },
216
+ on: { click: () => !last && this.setZoom(entry.zoom, true) }
217
+ });
218
+ })
219
+ );
220
+ }
221
+ measure() {
222
+ const w = this.map.clientWidth;
223
+ const hh = this.map.clientHeight;
224
+ if (Math.abs(w - this.box.w) < 0.5 && Math.abs(hh - this.box.h) < 0.5) return;
225
+ this.box = { w, h: hh };
226
+ this.layout();
227
+ }
228
+ /** The items on the map: the zoomed group's, or the top level's. */
229
+ level() {
230
+ const path = this.zoomPath();
231
+ return { items: path.length > 0 ? path[path.length - 1].children : this.items, path };
232
+ }
233
+ place(items, bounds, path, groups, tiles) {
234
+ const half = this.gap / 2;
235
+ for (const r of squarify(
236
+ items.map((item) => ({ item, weight: weightOf(item) })),
237
+ bounds
238
+ )) {
239
+ const box = { x: r.x + half, y: r.y + half, w: Math.max(0, r.w - this.gap), h: Math.max(0, r.h - this.gap) };
240
+ if (r.item.children) {
241
+ const headed = box.w >= HEADED.w && box.h >= HEADED.h;
242
+ if (headed) groups.push({ item: r.item, path, ...box, h: HEADER });
243
+ const top = headed ? HEADER : 0;
244
+ this.place(r.item.children, { x: box.x, y: box.y + top, w: box.w, h: box.h - top }, [...path, r.item], groups, tiles);
245
+ } else tiles.push({ item: r.item, path, ...box });
246
+ }
247
+ }
248
+ layout() {
249
+ const level = this.level();
250
+ const groups = [];
251
+ const tiles = [];
252
+ this.place(level.items, { x: 0, y: 0, w: this.box.w, h: this.box.h }, level.path, groups, tiles);
253
+ const nextGroups = /* @__PURE__ */ new Map();
254
+ for (const placed of groups) {
255
+ const view = this.groups.get(placed.item.id) ?? this.groupView(placed);
256
+ view.placed = placed;
257
+ view.el.firstElementChild.textContent = placed.item.label;
258
+ position(view.el, placed);
259
+ nextGroups.set(placed.item.id, view);
260
+ }
261
+ const nextTiles = /* @__PURE__ */ new Map();
262
+ for (const placed of tiles) {
263
+ const old = this.tiles.get(placed.item.id);
264
+ const view = old ?? this.tileView(placed);
265
+ view.placed = placed;
266
+ this.fitTile(view);
267
+ position(view.el, placed);
268
+ nextTiles.set(placed.item.id, view);
269
+ }
270
+ for (const [id, view] of this.groups) if (!nextGroups.has(id)) view.el.remove();
271
+ for (const [id, view] of this.tiles) if (!nextTiles.has(id)) view.el.remove();
272
+ this.groups = nextGroups;
273
+ this.tiles = nextTiles;
274
+ const order = [];
275
+ const headerOf = new Map(groups.map((g) => [g.item.id, nextGroups.get(g.item.id).el]));
276
+ const written = /* @__PURE__ */ new Set();
277
+ for (const placed of tiles) {
278
+ for (const group of placed.path) {
279
+ const header = headerOf.get(group.id);
280
+ if (header && !written.has(group.id)) {
281
+ written.add(group.id);
282
+ order.push(header);
283
+ }
284
+ }
285
+ order.push(nextTiles.get(placed.item.id).el);
286
+ }
287
+ const children = this.map.children;
288
+ const same = children.length === order.length && order.every((node, i) => children[i] === node);
289
+ if (!same) this.map.replaceChildren(...order);
290
+ if (this.current === null || !this.tiles.has(this.current)) this.current = tiles[0]?.item.id ?? null;
291
+ for (const [id, tile] of this.tiles) {
292
+ tile.el.tabIndex = id === this.current ? 0 : -1;
293
+ tile.el.toggleAttribute("data-selected", id === this.selected);
294
+ }
295
+ this.paint();
296
+ }
297
+ groupView(placed) {
298
+ const name = h("span", { slot: treemapDef.slot("group-name") });
299
+ const value = h("span", { slot: treemapDef.slot("group-value") });
300
+ const drill = this.props.drill === true;
301
+ const el = drill ? h(
302
+ "button",
303
+ {
304
+ slot: treemapDef.slot("group"),
305
+ data: { drill: true },
306
+ attrs: { type: "button", title: textOf(this.i18n, TEXT.zoomIn) }
307
+ },
308
+ name,
309
+ value
310
+ ) : h("div", { slot: treemapDef.slot("group") }, name, value);
311
+ const view = { el, value, placed };
312
+ if (drill) el.addEventListener("click", () => this.drillTo(view.placed.item.id));
313
+ return view;
314
+ }
315
+ tileView(placed) {
316
+ const el = h("button", { slot: treemapDef.slot("tile"), attrs: { type: "button" } });
317
+ const view = { el, label: null, value: null, placed };
318
+ el.addEventListener("click", () => this.choose(view));
319
+ el.addEventListener("focus", () => {
320
+ this.current = view.placed.item.id;
321
+ for (const tile of this.tiles.values()) tile.el.tabIndex = tile === view ? 0 : -1;
322
+ this.showTipFor(view);
323
+ });
324
+ el.addEventListener("blur", () => this.hideTip());
325
+ el.addEventListener("pointerenter", () => this.showTipFor(view));
326
+ return view;
327
+ }
328
+ /** Text sized to the tile: the label from a small tile up, the value from a larger one. */
329
+ fitTile(view) {
330
+ const { w, h: ht } = view.placed;
331
+ const font = Math.max(10, Math.min(18, Math.min(w, ht) / 4.2));
332
+ const showLabel = w > 34 && ht > 20;
333
+ const showValue = w > 48 && ht > 38;
334
+ if (showLabel && !view.label) view.label = h("span", { slot: treemapDef.slot("tile-label") });
335
+ if (!showLabel) view.label = null;
336
+ if (showValue && !view.value) view.value = h("span", { slot: treemapDef.slot("tile-value") });
337
+ if (!showValue) view.value = null;
338
+ if (view.label) {
339
+ view.label.textContent = view.placed.item.label;
340
+ view.label.style.fontSize = `${font}px`;
341
+ }
342
+ if (view.value) view.value.style.fontSize = `${Math.max(9.5, font * 0.72)}px`;
343
+ const parts = [view.label, view.value].filter((part) => part !== null);
344
+ if (parts.length !== view.el.childElementCount || parts.some((part, i) => view.el.children[i] !== part)) view.el.replaceChildren(...parts);
345
+ }
346
+ valueOf(item) {
347
+ const value = this.values.has(item.id) ? this.values.get(item.id) : item.value ?? null;
348
+ return value === null || !Number.isFinite(value) ? null : value;
349
+ }
350
+ leafValues(item) {
351
+ if (!item.children) {
352
+ const value = this.valueOf(item);
353
+ return value === null ? [] : [value];
354
+ }
355
+ return item.children.flatMap((child) => this.leafValues(child));
356
+ }
357
+ groupFigure(group) {
358
+ const mode = this.props.groupValue ?? "median";
359
+ if (mode === "none") return null;
360
+ const values = this.leafValues(group);
361
+ if (typeof mode === "function") return mode(values, group);
362
+ if (values.length === 0) return null;
363
+ if (mode === "sum") return values.reduce((s, v) => s + v, 0);
364
+ if (mode === "mean") return values.reduce((s, v) => s + v, 0) / values.length;
365
+ return median(values);
366
+ }
367
+ format(value) {
368
+ return (this.props.format ?? defaultFormat)(value);
369
+ }
370
+ paint() {
371
+ let largest = 0;
372
+ for (const tile of this.tiles.values()) largest = Math.max(largest, Math.abs(this.valueOf(tile.placed.item) ?? 0));
373
+ const domain = this.props.domain !== void 0 && this.props.domain > 0 ? this.props.domain : largest;
374
+ for (const tile of this.tiles.values()) {
375
+ const item = tile.placed.item;
376
+ const value = this.valueOf(item);
377
+ if (item.color !== void 0) {
378
+ tile.el.dataset.tone = "custom";
379
+ tile.el.style.setProperty("--_color", item.color);
380
+ } else tile.el.dataset.tone = toneOf(value);
381
+ tile.el.style.setProperty("--_level", value === null || domain === 0 ? "0" : String(Math.min(Math.abs(value) / domain, 1)));
382
+ const text = value === null ? "" : this.format(value);
383
+ if (tile.value) tile.value.textContent = text;
384
+ tile.el.setAttribute("aria-label", text === "" ? item.label : `${item.label} ${text}`);
385
+ }
386
+ for (const group of this.groups.values()) {
387
+ const figure = this.groupFigure(group.placed.item);
388
+ group.value.textContent = figure === null ? "" : this.format(figure);
389
+ group.value.dataset.tone = toneOf(figure);
390
+ const name = group.placed.item.label;
391
+ group.el.setAttribute("aria-label", figure === null ? name : `${name} ${this.format(figure)}`);
392
+ }
393
+ if (this.legend) {
394
+ this.legend.low.textContent = domain > 0 ? this.format(-domain) : "";
395
+ this.legend.high.textContent = domain > 0 ? this.format(domain) : "";
396
+ }
397
+ const shown = this.tipFor !== null ? this.tiles.get(this.tipFor) : void 0;
398
+ if (shown) this.showTipFor(shown);
399
+ else this.hideTip();
400
+ }
401
+ choose(view) {
402
+ this.props.onSelect?.(view.placed.item, view.placed.path);
403
+ }
404
+ tipContent(view) {
405
+ const { item, path } = view.placed;
406
+ if (this.props.tip === false) return null;
407
+ if (this.props.tip) return this.props.tip(item, path);
408
+ const value = this.valueOf(item);
409
+ return h(
410
+ "div",
411
+ {},
412
+ h("div", {}, h("b", { text: item.label }), value === null ? null : h("span", { text: ` ${this.format(value)}`, data: { tone: toneOf(value) } })),
413
+ path.length > 0 ? h("small", { text: path.map((g) => g.label).join(" \u203A ") }) : null
414
+ );
415
+ }
416
+ showTipFor(view) {
417
+ const content = this.tipContent(view);
418
+ if (content === null) return;
419
+ this.tipFor = view.placed.item.id;
420
+ const { x, y, w, h: ht } = view.placed;
421
+ showTip(this.tip, content, { w: this.el.clientWidth, h: this.el.clientHeight }, this.map.offsetLeft + x + w / 2, this.map.offsetTop + y + ht / 2);
422
+ }
423
+ hideTip() {
424
+ this.tipFor = null;
425
+ this.tip.hidden = true;
426
+ }
427
+ /** Arrows move to the nearest tile that way, Home and End to the first and the last. */
428
+ onKey(e) {
429
+ const from = this.current !== null ? this.tiles.get(this.current) : void 0;
430
+ if (!from || !(e.target instanceof HTMLElement) || e.target.dataset.slot !== treemapDef.slot("tile")) return;
431
+ const list = [...this.tiles.values()];
432
+ let next;
433
+ if (e.key === "Home") next = list[0];
434
+ else if (e.key === "End") next = list[list.length - 1];
435
+ else {
436
+ const dir = { ArrowRight: [1, 0], ArrowLeft: [-1, 0], ArrowDown: [0, 1], ArrowUp: [0, -1] }[e.key];
437
+ if (!dir) return;
438
+ const cx = from.placed.x + from.placed.w / 2;
439
+ const cy = from.placed.y + from.placed.h / 2;
440
+ let best = Infinity;
441
+ for (const tile of list) {
442
+ if (tile === from) continue;
443
+ const dx = tile.placed.x + tile.placed.w / 2 - cx;
444
+ const dy = tile.placed.y + tile.placed.h / 2 - cy;
445
+ const along = dx * dir[0] + dy * dir[1];
446
+ if (along <= 0) continue;
447
+ const across = Math.abs(dx * dir[1] - dy * dir[0]);
448
+ const score = along + across * 2;
449
+ if (score < best) {
450
+ best = score;
451
+ next = tile;
452
+ }
453
+ }
454
+ }
455
+ if (!next) return;
456
+ e.preventDefault();
457
+ next.el.focus();
458
+ }
459
+ };
460
+ function position(el, box) {
461
+ el.style.left = `${box.x}px`;
462
+ el.style.top = `${box.y}px`;
463
+ el.style.width = `${box.w}px`;
464
+ el.style.height = `${box.h}px`;
465
+ }
466
+ function createTreemap(props = {}) {
467
+ return new TreemapImpl(props);
468
+ }
469
+
470
+ export { createTreemap, squarify, treemapDef };
package/dist/zag.d.ts CHANGED
@@ -1,6 +1,15 @@
1
- export { normalizeProps, spreadProps } from '@zag-js/vanilla';
1
+ import { spreadProps as spreadProps$1 } from '@zag-js/vanilla';
2
+ export { normalizeProps } from '@zag-js/vanilla';
2
3
  import { Machine, MachineSchema, Service } from '@zag-js/core';
3
4
 
5
+ /**
6
+ * Zag's `spreadProps`, without the style keys a part leaves unset. Zag's own spread removes
7
+ * those from the element, which wipes what Zag writes there itself: from 1.47 a positioner's
8
+ * style carries `zIndex: undefined` while the popper sets its z-index inline, so every
9
+ * re-render dropped an open menu under the page. A key the part set and then clears is still
10
+ * removed: that runs off the previous style.
11
+ */
12
+ declare function spreadProps(...[node, attrs, machineId]: Parameters<typeof spreadProps$1>): ReturnType<typeof spreadProps$1>;
4
13
  /** The MachineHandle for a given machine definition (package schemas aren't exported —
5
14
  * infer them from the machine value: `HandleOf<typeof tooltip.machine>`). */
6
15
  type HandleOf<M> = M extends Machine<infer T extends MachineSchema> ? MachineHandle<T> : never;
@@ -16,4 +25,4 @@ declare function runMachine<T extends MachineSchema>(machine: Machine<T>, props:
16
25
  /** Unique ids for machine instances (Zag keys its DOM lookups on them). */
17
26
  declare function nextUid(prefix: string): string;
18
27
 
19
- export { type HandleOf, type MachineHandle, nextUid, runMachine };
28
+ export { type HandleOf, type MachineHandle, nextUid, runMachine, spreadProps };
package/dist/zag.js CHANGED
@@ -1,2 +1,2 @@
1
- export { nextUid, normalizeProps, runMachine, spreadProps } from './chunk-P4MTEMT7.js';
1
+ export { nextUid, normalizeProps, runMachine, spreadProps } from './chunk-GUIYFQ27.js';
2
2
  import './chunk-X66RJAIS.js';