@momoi-labs/kiso-react 0.16.1 → 0.17.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.
@@ -4,6 +4,7 @@ import * as React from "react";
4
4
  import { clsx as cn } from "clsx";
5
5
  import { Check } from "lucide-react";
6
6
  import { Popover, PopoverContent, PopoverTrigger } from "./popover.js";
7
+ import { isPlayPreviewKey, toggleAudioPreview } from "./media-preview.js";
7
8
  export function DetailSelect({ label, options, value, defaultValue, onValueChange, placeholder = "Choose an option", disabled, controlSize, id, className, "aria-describedby": describedBy, "aria-invalid": invalid }) {
8
9
  const generatedId = React.useId();
9
10
  const triggerId = id ?? generatedId;
@@ -39,6 +40,11 @@ export function DetailSelect({ label, options, value, defaultValue, onValueChang
39
40
  if (!interactive || !event.currentTarget.contains(interactive))
40
41
  choose(option);
41
42
  }, "aria-labelledby": `${triggerId}-option-${index}`, children: [_jsx("div", { className: "detail-select-illustration", children: option.illustration }), _jsxs("div", { className: "detail-select-body", children: [_jsx("h3", { className: "detail-select-heading", children: _jsxs("button", { id: `${triggerId}-option-${index}`, type: "button", className: "detail-select-choice", ref: element => { buttons.current[index] = element; }, disabled: option.disabled, "aria-pressed": option.value === selected, "aria-describedby": `${triggerId}-description-${index}`, onClick: () => choose(option), onKeyDown: event => {
43
+ if (isPlayPreviewKey(event)) {
44
+ event.preventDefault();
45
+ toggleAudioPreview(event.currentTarget.closest(".detail-select-option"));
46
+ return;
47
+ }
42
48
  if (!["ArrowDown", "ArrowUp", "Home", "End"].includes(event.key))
43
49
  return;
44
50
  const enabled = options.map((option, index) => ({ option, button: buttons.current[index] }))
package/dist/diagram.js CHANGED
@@ -29,10 +29,12 @@ const statuses = {
29
29
  };
30
30
  const RADIUS = 8;
31
31
  /* An edge is two paths. The trunk is the horizontal run out of the source,
32
- shared by every edge that leaves it, so it is always drawn solid and
33
- neutral: two styles on one line would paint over each other. The branch
34
- is the rest, the turn onto the merge column, the vertical run and the
35
- straight run into the target, and it carries the edge's line and tone. */
32
+ shared by every edge that leaves it. It carries their line and tone when
33
+ they all agree, so a single dotted edge is dotted end to end; when they
34
+ differ it stays solid and neutral, since two styles on one line would
35
+ paint over each other. The branch is the rest, the turn onto the merge
36
+ column, the vertical run and the straight run into the target, and it
37
+ carries the edge's line and tone. */
36
38
  function elbow(sx, sy, tx, ty, mid) {
37
39
  const dy = ty - sy;
38
40
  if (Math.abs(dy) < 1)
@@ -102,7 +104,10 @@ function Diagram({ label, background, strength, borderStyle, cornerStyle, corner
102
104
  // labels never land on the same spot. One edge enters at the middle.
103
105
  const measured = edges.flatMap((edge) => {
104
106
  const target = box(edge.to);
105
- const sources = edge.from.map(box).filter((b) => b !== null);
107
+ const sources = edge.from.flatMap((id) => {
108
+ const found = box(id);
109
+ return found ? [{ ...found, id }] : [];
110
+ });
106
111
  return target && sources.length ? [{ edge, target, sources }] : [];
107
112
  });
108
113
  const entries = new Map();
@@ -113,6 +118,14 @@ function Diagram({ label, background, strength, borderStyle, cornerStyle, corner
113
118
  entries.set(item.edge, item.target.top + (item.target.height * (index + 1)) / (group.length + 1));
114
119
  });
115
120
  }
121
+ const leaving = new Map();
122
+ for (const edge of edges)
123
+ for (const id of edge.from)
124
+ leaving.set(id, [...(leaving.get(id) ?? []), edge]);
125
+ const trunkStyle = (id) => {
126
+ const [first, ...rest] = leaving.get(id) ?? [];
127
+ return first && rest.every((edge) => edge.line === first.line && edge.tone === first.tone) ? { line: first.line, tone: first.tone } : {};
128
+ };
116
129
  const next = { paths: [], labels: [] };
117
130
  for (const { edge, target, sources } of measured) {
118
131
  const ty = entries.get(edge) ?? target.y;
@@ -120,7 +133,7 @@ function Diagram({ label, background, strength, borderStyle, cornerStyle, corner
120
133
  const mid = (start + target.left) / 2;
121
134
  sources.forEach((source, index) => {
122
135
  const { trunk, branch } = elbow(source.right, source.y, target.left, ty, mid);
123
- next.paths.push({ key: `${edge.key}:${index}:trunk`, d: trunk, arrow: false });
136
+ next.paths.push({ key: `${edge.key}:${index}:trunk`, d: trunk, arrow: false, ...trunkStyle(source.id) });
124
137
  next.paths.push({ key: `${edge.key}:${index}`, d: branch, line: edge.line, tone: edge.tone, arrow: true });
125
138
  });
126
139
  // The label sits on the edge's own run into the target, above the line.
package/dist/index.d.ts CHANGED
@@ -34,6 +34,7 @@ export * from "./popover.js";
34
34
  export * from "./search.js";
35
35
  export * from "./select.js";
36
36
  export * from "./detail-select.js";
37
+ export * from "./media-preview.js";
37
38
  export type { ChartStyle } from "./chart-style.js";
38
39
  export * from "./separator.js";
39
40
  export * from "./sidebar.js";
package/dist/index.js CHANGED
@@ -35,6 +35,7 @@ export * from "./popover.js";
35
35
  export * from "./search.js";
36
36
  export * from "./select.js";
37
37
  export * from "./detail-select.js";
38
+ export * from "./media-preview.js";
38
39
  export * from "./separator.js";
39
40
  export * from "./sidebar.js";
40
41
  export * from "./skeleton.js";
@@ -0,0 +1,40 @@
1
+ import * as React from "react";
2
+ export type ImagePreviewProps = Omit<React.ComponentProps<"img">, "src" | "alt"> & {
3
+ src: string;
4
+ /** Describes the image, or "" when the text beside it already names it. */
5
+ alt: string;
6
+ };
7
+ /** A thumbnail that crops the image to fill its box. */
8
+ export declare function ImagePreview({ className, loading, decoding, ...props }: ImagePreviewProps): React.JSX.Element;
9
+ export type AudioPreviewProps = Omit<React.ComponentProps<"button">, "children" | "type"> & {
10
+ src: string;
11
+ /** What plays. The button is named "Play {label}" or "Pause {label}". */
12
+ label: string;
13
+ };
14
+ /**
15
+ * A button that plays and pauses a short sound. It loads nothing until it is
16
+ * pressed and never starts by itself.
17
+ */
18
+ export declare function AudioPreview({ src, label, className, onClick, ref, ...props }: AudioPreviewProps): React.JSX.Element;
19
+ /**
20
+ * Ctrl+Enter, or Cmd+Enter on macOS, plays or pauses the audio preview of the
21
+ * highlighted option while the focus stays on the selection.
22
+ */
23
+ export declare function isPlayPreviewKey(event: Pick<KeyboardEvent, "key" | "ctrlKey" | "metaKey" | "altKey" | "shiftKey">): boolean;
24
+ /** Plays or pauses the first audio preview inside `element`. False when it has none. */
25
+ export declare function toggleAudioPreview(element: Element | null | undefined): boolean;
26
+ export type DetailOptionProps = Omit<React.ComponentProps<"div">, "children"> & {
27
+ label: React.ReactNode;
28
+ illustration?: React.ReactNode;
29
+ description?: React.ReactNode;
30
+ /** `sm` fits a row in a compact list, such as an autocomplete. */
31
+ size?: "sm" | "md";
32
+ };
33
+ /**
34
+ * DetailSelect's option layout, for lists it does not own: an illustration
35
+ * beside a title and its description. Pass `role="option"` and
36
+ * `aria-selected` to use it in a listbox. The title names the option and the
37
+ * description describes it, so controls in the illustration stay out of its
38
+ * name.
39
+ */
40
+ export declare function DetailOption({ label, illustration, description, size, className, id, ...props }: DetailOptionProps): React.JSX.Element;
@@ -0,0 +1,89 @@
1
+ "use client";
2
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
3
+ import * as React from "react";
4
+ import { clsx as cn } from "clsx";
5
+ import { Pause, Play } from "lucide-react";
6
+ /** A thumbnail that crops the image to fill its box. */
7
+ export function ImagePreview({ className, loading = "lazy", decoding = "async", ...props }) {
8
+ return _jsx("img", { "data-slot": "image-preview", className: cn("media-preview", className), loading: loading, decoding: decoding, ...props });
9
+ }
10
+ // One preview plays at a time: starting one pauses the one before.
11
+ let playing = null;
12
+ /**
13
+ * A button that plays and pauses a short sound. It loads nothing until it is
14
+ * pressed and never starts by itself.
15
+ */
16
+ export function AudioPreview({ src, label, className, onClick, ref, ...props }) {
17
+ const audio = React.useRef(null);
18
+ const [on, setOn] = React.useState(false);
19
+ const stop = React.useCallback(() => {
20
+ const current = audio.current;
21
+ if (current)
22
+ current.pause();
23
+ if (playing?.audio === current)
24
+ playing = null;
25
+ setOn(false);
26
+ }, []);
27
+ // A new source, or leaving the page, stops the old sound.
28
+ React.useEffect(() => () => {
29
+ stop();
30
+ audio.current = null;
31
+ }, [src, stop]);
32
+ const toggle = () => {
33
+ // The player, not the last render, says whether this one plays, so a
34
+ // quick second press pauses.
35
+ if (audio.current && playing?.audio === audio.current)
36
+ return stop();
37
+ if (!audio.current) {
38
+ const element = new Audio(src);
39
+ element.addEventListener("ended", () => {
40
+ element.currentTime = 0;
41
+ if (playing?.audio === element)
42
+ playing = null;
43
+ setOn(false);
44
+ });
45
+ element.addEventListener("error", stop);
46
+ audio.current = element;
47
+ }
48
+ const element = audio.current;
49
+ if (playing && playing.audio !== element)
50
+ playing.stop();
51
+ playing = { audio: element, stop };
52
+ setOn(true);
53
+ element.play().catch(error => {
54
+ // A pause before playback starts rejects too; that is not a failure.
55
+ if (error?.name !== "AbortError")
56
+ stop();
57
+ });
58
+ };
59
+ return _jsx("button", { ref: ref, type: "button", "data-slot": "audio-preview", "data-state": on ? "playing" : "paused", "aria-label": `${on ? "Pause" : "Play"} ${label}`, className: cn("btn btn-outline btn-sm btn-icon audio-preview", className), ...props, onClick: event => {
60
+ onClick?.(event);
61
+ if (!event.defaultPrevented)
62
+ toggle();
63
+ }, children: on ? _jsx(Pause, { className: "icon icon-sm", "aria-hidden": "true" }) : _jsx(Play, { className: "icon icon-sm", "aria-hidden": "true" }) });
64
+ }
65
+ /**
66
+ * Ctrl+Enter, or Cmd+Enter on macOS, plays or pauses the audio preview of the
67
+ * highlighted option while the focus stays on the selection.
68
+ */
69
+ export function isPlayPreviewKey(event) {
70
+ return event.key === "Enter" && (event.ctrlKey || event.metaKey) && !event.altKey && !event.shiftKey;
71
+ }
72
+ /** Plays or pauses the first audio preview inside `element`. False when it has none. */
73
+ export function toggleAudioPreview(element) {
74
+ const button = element?.querySelector('[data-slot="audio-preview"]:not(:disabled)');
75
+ button?.click();
76
+ return !!button;
77
+ }
78
+ /**
79
+ * DetailSelect's option layout, for lists it does not own: an illustration
80
+ * beside a title and its description. Pass `role="option"` and
81
+ * `aria-selected` to use it in a listbox. The title names the option and the
82
+ * description describes it, so controls in the illustration stay out of its
83
+ * name.
84
+ */
85
+ export function DetailOption({ label, illustration, description, size = "md", className, id, ...props }) {
86
+ const generated = React.useId();
87
+ const base = id ?? generated;
88
+ return _jsxs("div", { id: id, "data-slot": "detail-option", "data-size": size, className: cn("detail-option", className), "aria-labelledby": `${base}-label`, "aria-describedby": description != null ? `${base}-description` : undefined, ...props, children: [illustration != null && _jsx("div", { className: "detail-option-illustration", children: illustration }), _jsxs("div", { className: "detail-option-body", children: [_jsx("div", { id: `${base}-label`, className: "detail-option-label", children: label }), description != null && _jsx("div", { id: `${base}-description`, className: "detail-option-description", children: description })] })] });
89
+ }
package/dist/styles.css CHANGED
@@ -364,6 +364,31 @@
364
364
  .detail-select-choice { min-height: var(--size-touch-min); }
365
365
  .detail-select-description a { display: inline-block; min-height: var(--size-touch-min); align-content: center; }
366
366
  }
367
+ .media-preview { display: block; width: 100%; aspect-ratio: 4 / 3; object-fit: cover; border-radius: var(--radius-sm); background-color: var(--color-muted); }
368
+ .audio-preview { flex: none; }
369
+ .detail-option { display: grid; grid-template-columns: minmax(min-content, 8rem) minmax(0, 1fr); gap: var(--spacing-lg); padding: var(--spacing-md); color: var(--color-foreground); }
370
+ .detail-option:not(:has(> .detail-option-illustration)) { grid-template-columns: minmax(0, 1fr); }
371
+ .detail-option-illustration { min-width: 0; align-self: start; }
372
+ .detail-option-illustration :is(img, svg, video) { max-width: 100%; }
373
+ .detail-option-body { min-width: 0; align-self: center; overflow-wrap: anywhere; }
374
+ .detail-option-label { font-size: var(--type-size-body); font-weight: var(--type-weight-medium); }
375
+ .detail-option-description { color: var(--color-muted-foreground); font-size: var(--type-size-label); }
376
+ .detail-option[data-size="sm"] { grid-template-columns: var(--size-control-sm) minmax(0, 1fr); align-items: center; gap: var(--spacing-sm); padding: var(--spacing-xs) var(--spacing-sm); }
377
+ .detail-option[data-size="sm"]:not(:has(> .detail-option-illustration)) { grid-template-columns: minmax(0, 1fr); }
378
+ .detail-option[data-size="sm"] .detail-option-illustration { align-self: center; }
379
+ .detail-option[data-size="sm"] .detail-option-label { font-size: var(--type-size-label); }
380
+ .detail-option[data-size="sm"] .media-preview { aspect-ratio: 1; }
381
+ @media (hover: hover) {
382
+ .detail-option:where([role="option"]:not([aria-disabled="true"]):hover) { background-color: var(--color-accent-surface-hover); }
383
+ }
384
+ .detail-option:where([aria-selected="true"]) { background-color: var(--color-selected); color: var(--color-selected-foreground); }
385
+ .detail-option:where([aria-selected="true"]) .detail-option-description { color: inherit; }
386
+ @media (forced-colors: active) {
387
+ .detail-option:where([aria-selected="true"]) { outline: 2px solid Highlight; outline-offset: -2px; }
388
+ }
389
+ @media (max-width: 480px) {
390
+ .detail-option:not([data-size="sm"]) { grid-template-columns: minmax(0, 4rem) minmax(0, 1fr); gap: var(--spacing-sm); padding: var(--spacing-sm); }
391
+ }
367
392
  /* When frames include controls, these surfaces take the control shape, as
368
393
  the control-sized surfaces in ui.css do. */
369
394
  :where([data-frame-scope="all"]) :is(.kiso-react-popover, .theme-buttons, .accent-preview) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@momoi-labs/kiso-react",
3
- "version": "0.16.1",
3
+ "version": "0.17.0",
4
4
  "description": "Kiso React components built with shadcn/ui and Radix",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -32,7 +32,7 @@
32
32
  "prepack": "npm run build"
33
33
  },
34
34
  "dependencies": {
35
- "@momoi-labs/kiso": "^0.20.3",
35
+ "@momoi-labs/kiso": "^0.21.0",
36
36
  "class-variance-authority": "0.7.1",
37
37
  "clsx": "2.1.1",
38
38
  "radix-ui": "1.6.7",