@momoi-labs/kiso-react 0.16.2 → 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.
- package/dist/detail-select.js +6 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/media-preview.d.ts +40 -0
- package/dist/media-preview.js +89 -0
- package/dist/styles.css +25 -0
- package/package.json +2 -2
package/dist/detail-select.js
CHANGED
|
@@ -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/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.
|
|
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.
|
|
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",
|