@seanyeh77/ui 0.3.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.
package/dist/index.d.ts CHANGED
@@ -854,13 +854,20 @@ interface LayerGraphEdge {
854
854
  inferred?: boolean;
855
855
  /** Label of an inferred edge; `backticks` and numbers are in Geist Mono. */
856
856
  label?: string;
857
+ /** Names the edge for selectedEdge and onSelectEdge. Default `${from}->${to}`. */
858
+ id?: string;
857
859
  }
858
860
  interface LayerGraphProps {
859
861
  nodes: LayerGraphNode[];
860
862
  edges: LayerGraphEdge[];
861
863
  selected?: string | null;
862
- /** Selecting a box dims everything unrelated and turns its lines accent. */
864
+ /** Selecting a box dims everything unrelated and turns its lines accent. Clears the selected edge. */
863
865
  onSelect?: (id: string | null) => void;
866
+ /** The selected edge's id (controlled, like selected). */
867
+ selectedEdge?: string | null;
868
+ /** Makes edges selectable: a click on a line or its label, Enter or Space. null when it is clicked again, on a click on
869
+ * the empty diagram or on Escape. Clears the selected box. Without it, edges cannot be clicked. */
870
+ onSelectEdge?: (id: string | null) => void;
864
871
  /** px. Default 200. */
865
872
  nodeWidth?: number;
866
873
  /** Text under a box's size bar; {n} is the size. Default 「{n} 個項目」. */
@@ -1330,7 +1337,8 @@ interface TourStep {
1330
1337
  title: string;
1331
1338
  /** What it does, 1 short sentence; `backticks` are code. */
1332
1339
  text: string;
1333
- /** Default 'bottom'; flips when there is no room. */
1340
+ /** Default 'bottom'; flips when there is no room. When neither side fits (a target about as tall as the window),
1341
+ * the card sits inside the target, at the bottom center of the window. */
1334
1342
  side?: 'bottom' | 'top';
1335
1343
  /** Space around the highlighted part in px. Default 6. */
1336
1344
  padding?: number;
package/dist/index.js CHANGED
@@ -3882,12 +3882,26 @@ function LayerGraph(p) {
3882
3882
  }
3883
3883
  for (const k in outs) outs[k].sort((a, b) => pos[a.to].x - pos[b.to].x);
3884
3884
  for (const k in ins) ins[k].sort((a, b) => pos[a.from].x - pos[b.from].x);
3885
- const related = (e) => !!sel && (e.from === sel || e.to === sel);
3886
- const near = (id) => !sel || id === sel || edges.some((e) => related(e) && (e.from === id || e.to === id));
3885
+ const edgeId = (e) => e.id ?? `${e.from}->${e.to}`;
3886
+ const pickEdge = p.onSelectEdge;
3887
+ const [hover, setHover] = React.useState(null);
3888
+ const selEdge = pickEdge && edges.find((e) => edgeId(e) === p.selectedEdge) || null;
3889
+ const related = (e) => selEdge ? e === selEdge : !!sel && (e.from === sel || e.to === sel);
3890
+ const focus = !!sel || !!selEdge;
3891
+ const near = (id) => selEdge ? id === selEdge.from || id === selEdge.to : !sel || id === sel || edges.some((e) => related(e) && (e.from === id || e.to === id));
3892
+ const nameOf = (id) => all.find((n) => n.id === id)?.name || id;
3893
+ const countText = (e) => (p.countLabel ?? tr("{n} 次")).replace("{n}", String(e.count ?? 0));
3894
+ const choose = (e) => {
3895
+ const id = edgeId(e);
3896
+ const next = p.selectedEdge === id ? null : id;
3897
+ if (next && sel != null) p.onSelect?.(null);
3898
+ pickEdge?.(next);
3899
+ };
3887
3900
  const paths = [];
3888
3901
  const labels = [];
3889
3902
  const marks = [];
3890
3903
  const arrows = [];
3904
+ const hitPaths = [];
3891
3905
  edges.forEach((e, idx) => {
3892
3906
  const a = pos[e.from];
3893
3907
  const b = pos[e.to];
@@ -3924,13 +3938,36 @@ function LayerGraph(p) {
3924
3938
  [tx, ty - 8]
3925
3939
  ];
3926
3940
  const r = related(e);
3927
- const dim = !!sel && !r;
3928
- const w = e.inferred ? 1.5 : 1.5 + 5.5 * ((e.count || 0) / maxCount);
3941
+ const dim = focus && !r;
3942
+ const picked = e === selEdge;
3943
+ const w = (e.inferred ? 1.5 : 1.5 + 5.5 * ((e.count || 0) / maxCount)) + (picked ? 1.5 : 0);
3944
+ const d = route(clean(pts), 10);
3929
3945
  paths.push(/* @__PURE__ */ jsx("path", {
3930
- className: cx("ui-lg-edge", e.inferred && "is-inferred", r && "is-related", dim && "is-dim"),
3931
- d: route(clean(pts), 10),
3946
+ className: cx("ui-lg-edge", e.inferred && "is-inferred", r && "is-related", dim && "is-dim", pickEdge && hover === edgeId(e) && "is-hover"),
3947
+ d,
3932
3948
  strokeWidth: w
3933
3949
  }, "p" + idx));
3950
+ if (pickEdge) hitPaths.push(/* @__PURE__ */ jsx("path", {
3951
+ className: "ui-lg-hit",
3952
+ "data-edge": edgeId(e),
3953
+ d,
3954
+ role: "button",
3955
+ tabIndex: 0,
3956
+ "aria-label": `${nameOf(e.from)} → ${nameOf(e.to)},${e.inferred ? e.label || p.inferredLabel || tr("推測") : countText(e)}`,
3957
+ "aria-pressed": picked,
3958
+ onClick: (ev) => {
3959
+ ev.stopPropagation();
3960
+ choose(e);
3961
+ },
3962
+ onPointerEnter: () => setHover(edgeId(e)),
3963
+ onPointerLeave: () => setHover(null),
3964
+ onKeyDown: (ev) => {
3965
+ if (ev.key === "Enter" || ev.key === " ") {
3966
+ ev.preventDefault();
3967
+ choose(e);
3968
+ } else if (ev.key === "Escape" && picked) pickEdge(null);
3969
+ }
3970
+ }, "h" + idx));
3934
3971
  arrows.push([
3935
3972
  tx - 5,
3936
3973
  tx + 5,
@@ -3942,9 +3979,10 @@ function LayerGraph(p) {
3942
3979
  d: `M${tx - 5} ${ty - 9} L${tx + 5} ${ty - 9} L${tx} ${ty} Z`
3943
3980
  }, "a" + idx));
3944
3981
  if (e.inferred || r) {
3945
- const text = e.inferred ? e.label || p.inferredLabel || tr("推測") : (p.countLabel ?? tr("{n} 次")).replace("{n}", String(e.count ?? 0));
3982
+ const text = e.inferred ? e.label || p.inferredLabel || tr("推測") : countText(e);
3946
3983
  marks.push({
3947
3984
  idx,
3985
+ e,
3948
3986
  text,
3949
3987
  tx,
3950
3988
  ty,
@@ -3973,11 +4011,17 @@ function LayerGraph(p) {
3973
4011
  6
3974
4012
  ]);
3975
4013
  labels.push(/* @__PURE__ */ jsx("span", {
3976
- className: cx("ui-lg-label", m.r && "is-related", m.dim && "is-dim"),
4014
+ className: cx("ui-lg-label", pickEdge && "is-clickable", m.r && "is-related", m.dim && "is-dim"),
3977
4015
  style: {
3978
4016
  left: x0,
3979
4017
  top
3980
4018
  },
4019
+ onClick: pickEdge ? (ev) => {
4020
+ ev.stopPropagation();
4021
+ choose(m.e);
4022
+ } : void 0,
4023
+ onPointerEnter: pickEdge ? () => setHover(edgeId(m.e)) : void 0,
4024
+ onPointerLeave: pickEdge ? () => setHover(null) : void 0,
3981
4025
  children: labelParts(m.text)
3982
4026
  }, "l" + m.idx));
3983
4027
  });
@@ -3996,7 +4040,10 @@ function LayerGraph(p) {
3996
4040
  height: NH
3997
4041
  },
3998
4042
  "aria-pressed": p.onSelect ? on : void 0,
3999
- onClick: p.onSelect ? () => p.onSelect?.(on ? null : n.id) : void 0,
4043
+ onClick: p.onSelect ? () => {
4044
+ if (!on && p.selectedEdge != null) pickEdge?.(null);
4045
+ p.onSelect?.(on ? null : n.id);
4046
+ } : void 0,
4000
4047
  children: [/* @__PURE__ */ jsx("span", {
4001
4048
  className: "ui-lg-name",
4002
4049
  children: n.name || n.id
@@ -4045,6 +4092,7 @@ function LayerGraph(p) {
4045
4092
  },
4046
4093
  role: "group",
4047
4094
  "aria-label": p.label || tr("架構圖"),
4095
+ onClick: pickEdge && p.selectedEdge != null ? () => pickEdge(null) : void 0,
4048
4096
  children: [
4049
4097
  /* @__PURE__ */ jsx("svg", {
4050
4098
  width: W,
@@ -4054,7 +4102,14 @@ function LayerGraph(p) {
4054
4102
  children: paths
4055
4103
  }),
4056
4104
  nodes,
4057
- labels
4105
+ labels,
4106
+ hitPaths.length ? /* @__PURE__ */ jsx("svg", {
4107
+ className: "ui-lg-hits",
4108
+ width: W,
4109
+ height: H,
4110
+ viewBox: `0 0 ${W} ${H}`,
4111
+ children: hitPaths
4112
+ }) : null
4058
4113
  ]
4059
4114
  })
4060
4115
  }),
@@ -5139,6 +5194,53 @@ function findTarget(t) {
5139
5194
  }
5140
5195
  return t instanceof Element ? t : t.current;
5141
5196
  }
5197
+ const GAP = 12;
5198
+ /** The space kept between the card and the window edges: the --space-3 token. */
5199
+ function edge() {
5200
+ return parseFloat(getComputedStyle(document.documentElement).getPropertyValue("--space-3")) || 12;
5201
+ }
5202
+ /** Where the card goes for a highlighted box: the preferred side, the other side, or inside the box at the bottom
5203
+ * center of the window when neither side has room. Returns the anchor line the card is placed against. */
5204
+ function placeCard(rect, cardH, prefer) {
5205
+ const margin = typeof document === "undefined" ? 12 : edge();
5206
+ const vh = typeof window === "undefined" ? 0 : window.innerHeight;
5207
+ const fits = (side) => rect ? side === "bottom" ? rect.top + rect.height + GAP + cardH + margin <= vh : rect.top - GAP - cardH - margin >= 0 : false;
5208
+ if (!rect) return {
5209
+ mode: "center",
5210
+ side: "top",
5211
+ offset: 0,
5212
+ margin,
5213
+ line: {
5214
+ left: 0,
5215
+ top: Math.max(margin + cardH, vh / 2 + cardH / 2),
5216
+ width: window.innerWidth,
5217
+ height: 0
5218
+ }
5219
+ };
5220
+ const other = prefer === "bottom" ? "top" : "bottom";
5221
+ const side = fits(prefer) ? prefer : fits(other) ? other : null;
5222
+ if (side) return {
5223
+ mode: side,
5224
+ side,
5225
+ offset: GAP,
5226
+ margin,
5227
+ line: rect
5228
+ };
5229
+ const end = rect.top + rect.height;
5230
+ const bottom = Math.max(margin + cardH, end >= vh - margin ? vh - margin : end - margin);
5231
+ return {
5232
+ mode: "inside",
5233
+ side: "top",
5234
+ offset: 0,
5235
+ margin,
5236
+ line: {
5237
+ left: rect.left,
5238
+ top: bottom,
5239
+ width: rect.width,
5240
+ height: 0
5241
+ }
5242
+ };
5243
+ }
5142
5244
  /** The first-use walkthrough: the page goes dark except the part being introduced, with a card beside it. */
5143
5245
  function Tour({ open, steps, onClose, skippable = true, step: controlled, onStepChange }) {
5144
5246
  const [inner, setInner] = React.useState(0);
@@ -5146,6 +5248,8 @@ function Tour({ open, steps, onClose, skippable = true, step: controlled, onStep
5146
5248
  const step = steps[i];
5147
5249
  const [rect, setRect] = React.useState(null);
5148
5250
  const next = React.useRef(null);
5251
+ const card = React.useRef(null);
5252
+ const [cardH, setCardH] = React.useState(0);
5149
5253
  const [wasOpen, setWasOpen] = React.useState(open);
5150
5254
  if (open !== wasOpen) {
5151
5255
  setWasOpen(open);
@@ -5162,8 +5266,9 @@ function Tour({ open, steps, onClose, skippable = true, step: controlled, onStep
5162
5266
  React.useEffect(() => {
5163
5267
  if (!open || !hasStep) return;
5164
5268
  let el = findTarget(target);
5269
+ const tall = (el?.getBoundingClientRect().height ?? 0) > window.innerHeight;
5165
5270
  el?.scrollIntoView?.({
5166
- block: "center",
5271
+ block: tall ? "start" : "center",
5167
5272
  behavior: reduced() ? "auto" : "smooth"
5168
5273
  });
5169
5274
  let raf = 0;
@@ -5198,7 +5303,23 @@ function Tour({ open, steps, onClose, skippable = true, step: controlled, onStep
5198
5303
  target,
5199
5304
  pad
5200
5305
  ]);
5201
- const anchor = React.useMemo(() => ({ getBoundingClientRect: () => rect ? new DOMRect(rect.left, rect.top, rect.width, rect.height) : new DOMRect(window.innerWidth / 2, window.innerHeight / 2, 0, 0) }), [rect]);
5306
+ React.useLayoutEffect(() => {
5307
+ const el = card.current;
5308
+ if (!open || !el) return;
5309
+ const measure = () => setCardH(el.offsetHeight);
5310
+ measure();
5311
+ const ro = new ResizeObserver(measure);
5312
+ ro.observe(el);
5313
+ return () => ro.disconnect();
5314
+ }, [open, i]);
5315
+ const place = placeCard(rect, cardH, step?.side ?? "bottom");
5316
+ const line = place.line;
5317
+ const anchor = React.useMemo(() => ({ getBoundingClientRect: () => line ? new DOMRect(line.left, line.top, line.width, line.height) : new DOMRect(window.innerWidth / 2, window.innerHeight / 2, 0, 0) }), [
5318
+ line?.left,
5319
+ line?.top,
5320
+ line?.width,
5321
+ line?.height
5322
+ ]);
5202
5323
  if (!open || !step) return null;
5203
5324
  const lastStep = i === steps.length - 1;
5204
5325
  return /* @__PURE__ */ jsx(Popover$1.Root, {
@@ -5217,52 +5338,62 @@ function Tour({ open, steps, onClose, skippable = true, step: controlled, onStep
5217
5338
  }), /* @__PURE__ */ jsx(Popover$1.Positioner, {
5218
5339
  className: "ui-float ui-tour-float",
5219
5340
  anchor,
5220
- side: rect ? step.side ?? "bottom" : "bottom",
5341
+ side: place.side,
5221
5342
  align: "center",
5222
- sideOffset: rect ? 12 : -100,
5343
+ sideOffset: place.offset,
5344
+ collisionPadding: place.margin,
5345
+ collisionAvoidance: {
5346
+ side: "none",
5347
+ align: "shift",
5348
+ fallbackAxisSide: "none"
5349
+ },
5223
5350
  children: /* @__PURE__ */ jsxs(Popover$1.Popup, {
5351
+ ref: card,
5224
5352
  className: "ui-tour-card",
5353
+ "data-placement": place.mode,
5225
5354
  initialFocus: next,
5226
- children: [
5227
- /* @__PURE__ */ jsx("div", {
5228
- className: "ui-tour-count",
5229
- children: monoNums(tr("{n} / {total}", {
5230
- n: "",
5231
- total: ""
5232
- }), [i + 1, steps.length])
5233
- }),
5234
- /* @__PURE__ */ jsx(Popover$1.Title, {
5235
- className: "ui-tour-title",
5236
- children: step.title
5237
- }),
5238
- /* @__PURE__ */ jsx(CodeText, {
5239
- as: "div",
5240
- className: "ui-tour-text",
5241
- text: step.text
5242
- }),
5243
- /* @__PURE__ */ jsxs("div", {
5244
- className: "ui-tour-actions",
5245
- children: [
5246
- skippable && !lastStep ? /* @__PURE__ */ jsx("button", {
5247
- type: "button",
5248
- className: "ui-link-btn is-muted",
5249
- onClick: () => end("skip"),
5250
- children: tr("略過")
5251
- }) : null,
5252
- /* @__PURE__ */ jsx("span", { className: "ui-tour-gap" }),
5253
- i > 0 ? /* @__PURE__ */ jsx(Button, {
5254
- onClick: () => go(i - 1),
5255
- children: tr("上一步")
5256
- }) : null,
5257
- /* @__PURE__ */ jsx(Button, {
5258
- ref: next,
5259
- variant: "primary",
5260
- onClick: () => lastStep ? end("done") : go(i + 1),
5261
- children: lastStep ? tr("完成") : tr("下一步")
5262
- })
5263
- ]
5264
- })
5265
- ]
5355
+ children: [/* @__PURE__ */ jsxs("div", {
5356
+ className: "ui-tour-body",
5357
+ children: [
5358
+ /* @__PURE__ */ jsx("div", {
5359
+ className: "ui-tour-count",
5360
+ children: monoNums(tr("{n} / {total}", {
5361
+ n: "",
5362
+ total: ""
5363
+ }), [i + 1, steps.length])
5364
+ }),
5365
+ /* @__PURE__ */ jsx(Popover$1.Title, {
5366
+ className: "ui-tour-title",
5367
+ children: step.title
5368
+ }),
5369
+ /* @__PURE__ */ jsx(CodeText, {
5370
+ as: "div",
5371
+ className: "ui-tour-text",
5372
+ text: step.text
5373
+ })
5374
+ ]
5375
+ }), /* @__PURE__ */ jsxs("div", {
5376
+ className: "ui-tour-actions",
5377
+ children: [
5378
+ skippable && !lastStep ? /* @__PURE__ */ jsx("button", {
5379
+ type: "button",
5380
+ className: "ui-link-btn is-muted",
5381
+ onClick: () => end("skip"),
5382
+ children: tr("略過")
5383
+ }) : null,
5384
+ /* @__PURE__ */ jsx("span", { className: "ui-tour-gap" }),
5385
+ i > 0 ? /* @__PURE__ */ jsx(Button, {
5386
+ onClick: () => go(i - 1),
5387
+ children: tr("上一步")
5388
+ }) : null,
5389
+ /* @__PURE__ */ jsx(Button, {
5390
+ ref: next,
5391
+ variant: "primary",
5392
+ onClick: () => lastStep ? end("done") : go(i + 1),
5393
+ children: lastStep ? tr("完成") : tr("下一步")
5394
+ })
5395
+ ]
5396
+ })]
5266
5397
  }, i)
5267
5398
  })] })
5268
5399
  });