@volvel/ui 0.0.0-stage → 0.7.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/LICENSE +202 -0
- package/THIRD_PARTY_NOTICES.md +23036 -0
- package/dist/Badge.d.ts +20 -0
- package/dist/Badge.js +35 -0
- package/dist/Button.d.ts +56 -0
- package/dist/Button.js +52 -0
- package/dist/Chart.d.ts +23 -0
- package/dist/Chart.js +98 -0
- package/dist/Combobox.d.ts +45 -0
- package/dist/Combobox.js +42 -0
- package/dist/ContextMenu.d.ts +16 -0
- package/dist/ContextMenu.js +38 -0
- package/dist/DateInput.d.ts +31 -0
- package/dist/DateInput.js +329 -0
- package/dist/Details.d.ts +12 -0
- package/dist/Details.js +9 -0
- package/dist/Dialog.d.ts +44 -0
- package/dist/Dialog.js +95 -0
- package/dist/Dialogs.d.ts +56 -0
- package/dist/Dialogs.js +95 -0
- package/dist/Doc.d.ts +25 -0
- package/dist/Doc.js +118 -0
- package/dist/DocServed.d.ts +6 -0
- package/dist/DocServed.js +154 -0
- package/dist/DocZoom.d.ts +11 -0
- package/dist/DocZoom.js +133 -0
- package/dist/Facts.d.ts +12 -0
- package/dist/Facts.js +10 -0
- package/dist/Fields.d.ts +184 -0
- package/dist/Fields.js +249 -0
- package/dist/Form.d.ts +47 -0
- package/dist/Form.js +189 -0
- package/dist/Grid.d.ts +118 -0
- package/dist/Grid.js +475 -0
- package/dist/HeadingSlot.d.ts +13 -0
- package/dist/HeadingSlot.js +19 -0
- package/dist/JsonView.d.ts +11 -0
- package/dist/JsonView.js +68 -0
- package/dist/LogViewer.d.ts +45 -0
- package/dist/LogViewer.js +299 -0
- package/dist/Markdown.d.ts +21 -0
- package/dist/Markdown.js +27 -0
- package/dist/MarkdownBody.d.ts +6 -0
- package/dist/MarkdownBody.js +14 -0
- package/dist/Menu.d.ts +29 -0
- package/dist/Menu.js +8 -0
- package/dist/MoveButtons.d.ts +19 -0
- package/dist/MoveButtons.js +22 -0
- package/dist/Notice.d.ts +47 -0
- package/dist/Notice.js +50 -0
- package/dist/PageShell.d.ts +42 -0
- package/dist/PageShell.js +14 -0
- package/dist/Pagination.d.ts +13 -0
- package/dist/Pagination.js +9 -0
- package/dist/Panel.d.ts +31 -0
- package/dist/Panel.js +33 -0
- package/dist/Pickers.d.ts +37 -0
- package/dist/Pickers.js +44 -0
- package/dist/Popover.d.ts +24 -0
- package/dist/Popover.js +12 -0
- package/dist/ResultView.d.ts +28 -0
- package/dist/ResultView.js +140 -0
- package/dist/SchemaForm.d.ts +45 -0
- package/dist/SchemaForm.js +626 -0
- package/dist/SideSheet.d.ts +31 -0
- package/dist/SideSheet.js +136 -0
- package/dist/Sparkline.d.ts +14 -0
- package/dist/Sparkline.js +23 -0
- package/dist/Surfaces.d.ts +30 -0
- package/dist/Surfaces.js +34 -0
- package/dist/Switch.d.ts +18 -0
- package/dist/Switch.js +11 -0
- package/dist/Table.d.ts +15 -0
- package/dist/Table.js +6 -0
- package/dist/Tabs.d.ts +32 -0
- package/dist/Tabs.js +11 -0
- package/dist/Term.d.ts +82 -0
- package/dist/Term.js +153 -0
- package/dist/Timeline.d.ts +29 -0
- package/dist/Timeline.js +39 -0
- package/dist/Toast.d.ts +43 -0
- package/dist/Toast.js +140 -0
- package/dist/Tooltip.d.ts +34 -0
- package/dist/Tooltip.js +70 -0
- package/dist/Tree.d.ts +51 -0
- package/dist/Tree.js +233 -0
- package/dist/chartEngine.d.ts +32 -0
- package/dist/chartEngine.js +72 -0
- package/dist/chartTheme.d.ts +41 -0
- package/dist/chartTheme.js +131 -0
- package/dist/contrast.d.ts +27 -0
- package/dist/contrast.js +110 -0
- package/dist/dates.d.ts +61 -0
- package/dist/dates.js +294 -0
- package/dist/drawer/Chips.d.ts +37 -0
- package/dist/drawer/Chips.js +32 -0
- package/dist/drawer/Choices.d.ts +47 -0
- package/dist/drawer/Choices.js +81 -0
- package/dist/drawer/Drawer.d.ts +125 -0
- package/dist/drawer/Drawer.js +150 -0
- package/dist/drawer/EditorBodies.d.ts +95 -0
- package/dist/drawer/EditorBodies.js +169 -0
- package/dist/drawer/NumberFormat.d.ts +36 -0
- package/dist/drawer/NumberFormat.js +98 -0
- package/dist/drawer/SortableRows.d.ts +35 -0
- package/dist/drawer/SortableRows.js +23 -0
- package/dist/drawer/edgeShadows.d.ts +7 -0
- package/dist/drawer/edgeShadows.js +17 -0
- package/dist/drawer/index.d.ts +8 -0
- package/dist/drawer/index.js +8 -0
- package/dist/drawer/useSortable.d.ts +53 -0
- package/dist/drawer/useSortable.js +323 -0
- package/dist/format.d.ts +6 -0
- package/dist/format.js +17 -0
- package/dist/icons.d.ts +20 -0
- package/dist/icons.js +16 -0
- package/dist/index.d.ts +48 -0
- package/dist/index.js +48 -0
- package/dist/markdownKnowledge.d.ts +45 -0
- package/dist/markdownKnowledge.js +37 -0
- package/dist/overlay.d.ts +7 -0
- package/dist/overlay.js +10 -0
- package/dist/pageIcons.d.ts +12 -0
- package/dist/pageIcons.js +32 -0
- package/dist/presence.d.ts +41 -0
- package/dist/presence.js +108 -0
- package/dist/returnFocus.d.ts +9 -0
- package/dist/returnFocus.js +44 -0
- package/dist/tokens.css +437 -0
- package/dist/type-scale.css +34 -0
- package/dist/useFlip.d.ts +32 -0
- package/dist/useFlip.js +118 -0
- package/fonts/OFL.txt +193 -0
- package/fonts/geist-latin.woff2 +0 -0
- package/fonts/geist-mono-latin.woff2 +0 -0
- package/package.json +52 -3
- package/README.md +0 -3
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { Button } from "./Button.js";
|
|
3
|
+
/**
|
|
4
|
+
* Moves through a list one page at a time. It asks only whether there is more
|
|
5
|
+
* each way, which is all a cursor can say: there are no page numbers.
|
|
6
|
+
*/
|
|
7
|
+
export function Pagination({ hasPrevious, hasNext, onPrevious, onNext, label }) {
|
|
8
|
+
return (_jsxs("nav", { "aria-label": "Pagination", className: "flex items-center justify-between gap-3", children: [_jsx(Button, { size: "small", disabled: !hasPrevious, onClick: onPrevious, children: "Previous" }), label ? (_jsx("span", { "aria-live": "polite", className: "text-body-sm text-ink-muted", children: label })) : null, _jsx(Button, { size: "small", disabled: !hasNext, onClick: onNext, children: "Next" })] }));
|
|
9
|
+
}
|
package/dist/Panel.d.ts
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { type HTMLAttributes, type ReactNode } from "react";
|
|
2
|
+
/** Marks what it holds as inside a container: for a sheet or a pane that is not a `Panel`. */
|
|
3
|
+
export declare function ContainedArea({ children }: {
|
|
4
|
+
children: ReactNode;
|
|
5
|
+
}): import("react").JSX.Element;
|
|
6
|
+
/** True inside a `Panel`, or a `ContainedArea`. */
|
|
7
|
+
export declare const useContained: () => boolean;
|
|
8
|
+
/**
|
|
9
|
+
* The one container (07 §7.5): a surface with a hairline border. It may hold
|
|
10
|
+
* a table, a form or a list, never another panel.
|
|
11
|
+
*
|
|
12
|
+
* Bare, it is the border and the surface, and the caller lays out what is
|
|
13
|
+
* inside. `padded`, or with a `title`, an `action` or a `footer`, it lays itself out: the
|
|
14
|
+
* heading and action at the top, the footer under a rule at the bottom. With
|
|
15
|
+
* `onSelect` the whole panel is one button, as a pet or an order in a grid is.
|
|
16
|
+
*/
|
|
17
|
+
export declare function Panel({ title, description, action, footer, onSelect, selected, as: As, padded, children, className, ...rest }: Omit<HTMLAttributes<HTMLElement>, "title" | "onSelect"> & {
|
|
18
|
+
title?: ReactNode;
|
|
19
|
+
description?: ReactNode;
|
|
20
|
+
/** A control at the top right, such as a menu or a badge. */
|
|
21
|
+
action?: ReactNode;
|
|
22
|
+
footer?: ReactNode;
|
|
23
|
+
/** Makes the panel one button. Leave it out when the panel holds buttons of its own. */
|
|
24
|
+
onSelect?: () => void;
|
|
25
|
+
selected?: boolean;
|
|
26
|
+
as?: "section" | "div" | "aside";
|
|
27
|
+
/** Lays out what it holds with the panel's own padding and gaps, without a heading. */
|
|
28
|
+
padded?: boolean;
|
|
29
|
+
children?: ReactNode;
|
|
30
|
+
className?: string;
|
|
31
|
+
}): import("react").JSX.Element;
|
package/dist/Panel.js
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { Fragment as _Fragment, jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { createContext, useContext } from "react";
|
|
3
|
+
/**
|
|
4
|
+
* Whether what is drawn sits inside a container already (07 §7.5): a notice
|
|
5
|
+
* there drops its border, keeping its tint and icon, so no box is drawn
|
|
6
|
+
* inside another.
|
|
7
|
+
*/
|
|
8
|
+
const Contained = createContext(false);
|
|
9
|
+
/** Marks what it holds as inside a container: for a sheet or a pane that is not a `Panel`. */
|
|
10
|
+
export function ContainedArea({ children }) {
|
|
11
|
+
return _jsx(Contained.Provider, { value: true, children: children });
|
|
12
|
+
}
|
|
13
|
+
/** True inside a `Panel`, or a `ContainedArea`. */
|
|
14
|
+
export const useContained = () => useContext(Contained);
|
|
15
|
+
/**
|
|
16
|
+
* The one container (07 §7.5): a surface with a hairline border. It may hold
|
|
17
|
+
* a table, a form or a list, never another panel.
|
|
18
|
+
*
|
|
19
|
+
* Bare, it is the border and the surface, and the caller lays out what is
|
|
20
|
+
* inside. `padded`, or with a `title`, an `action` or a `footer`, it lays itself out: the
|
|
21
|
+
* heading and action at the top, the footer under a rule at the bottom. With
|
|
22
|
+
* `onSelect` the whole panel is one button, as a pet or an order in a grid is.
|
|
23
|
+
*/
|
|
24
|
+
export function Panel({ title, description, action, footer, onSelect, selected, as: As = "section", padded = false, children, className = "", ...rest }) {
|
|
25
|
+
const laidOut = Boolean(padded || title || description || action || footer || onSelect);
|
|
26
|
+
if (!laidOut) {
|
|
27
|
+
return (_jsx(Contained.Provider, { value: true, children: _jsx(As, { ...rest, className: `rounded-panel border border-line bg-surface ${className}`, children: children }) }));
|
|
28
|
+
}
|
|
29
|
+
const head = title || description || action ? (_jsxs("div", { className: "flex items-start justify-between gap-3", children: [_jsxs("div", { className: "flex min-w-0 flex-col gap-0.5", children: [title ? _jsx("h3", { className: "m-0 text-title font-semibold", children: title }) : null, description ? _jsx("p", { className: "m-0 text-body-sm text-ink-muted", children: description }) : null] }), action ? _jsx("div", { className: "shrink-0", children: action }) : null] })) : null;
|
|
30
|
+
const body = (_jsxs(_Fragment, { children: [head, children, footer ? (_jsx("div", { className: "-mx-4 -mb-4 mt-1 flex flex-wrap items-center gap-2 border-line border-t px-4 py-3", children: footer })) : null] }));
|
|
31
|
+
const frame = `flex flex-col gap-3 rounded-panel border bg-surface p-4 text-left text-ink ${selected ? "border-action ring-1 ring-action" : "border-line"} ${className}`;
|
|
32
|
+
return (_jsx(Contained.Provider, { value: true, children: onSelect ? (_jsx("button", { type: "button", onClick: onSelect, "aria-pressed": selected, className: `${frame} w-full cursor-pointer hover:border-line-strong focus-visible:outline-2 focus-visible:outline-focus`, children: body })) : (_jsx(As, { ...rest, className: frame, children: body })) }));
|
|
33
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
export interface SegmentOption<Value extends string> {
|
|
2
|
+
value: Value;
|
|
3
|
+
label: string;
|
|
4
|
+
/** A count shown after the label, such as how many are in that state. */
|
|
5
|
+
count?: number;
|
|
6
|
+
disabled?: boolean;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* One choice among a few, shown side by side: a view, a clinic, a period. It
|
|
10
|
+
* is a group of native radio buttons: the arrow keys move the choice and Tab
|
|
11
|
+
* leaves it. For more than about five choices use a select field.
|
|
12
|
+
*/
|
|
13
|
+
export declare function SegmentedControl<Value extends string>({ label, options, value, onChange, size, }: {
|
|
14
|
+
/** Names the group for screen readers. */
|
|
15
|
+
label: string;
|
|
16
|
+
options: readonly SegmentOption<Value>[];
|
|
17
|
+
value: Value;
|
|
18
|
+
onChange: (value: Value) => void;
|
|
19
|
+
size?: "regular" | "small";
|
|
20
|
+
}): import("react").JSX.Element;
|
|
21
|
+
/** `YYYY-MM-DD` of a date, in the person's own calendar. */
|
|
22
|
+
export declare function isoDay(date: Date): string;
|
|
23
|
+
/**
|
|
24
|
+
* A day, with buttons to the day before and after, and back to today. The
|
|
25
|
+
* value is a `YYYY-MM-DD` date in the person's own calendar.
|
|
26
|
+
*/
|
|
27
|
+
export declare function DateStepper({ value, onChange, min, max, label, locale, }: {
|
|
28
|
+
value: string;
|
|
29
|
+
onChange: (value: string) => void;
|
|
30
|
+
/** The first and last days allowed, as `YYYY-MM-DD`. */
|
|
31
|
+
min?: string;
|
|
32
|
+
max?: string;
|
|
33
|
+
/** Names the control for screen readers. */
|
|
34
|
+
label?: string;
|
|
35
|
+
/** How the day is written. The browser's own by default. */
|
|
36
|
+
locale?: string;
|
|
37
|
+
}): import("react").JSX.Element;
|
package/dist/Pickers.js
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { ChevronLeft, ChevronRight } from "lucide-react";
|
|
3
|
+
import { useId } from "react";
|
|
4
|
+
/**
|
|
5
|
+
* One choice among a few, shown side by side: a view, a clinic, a period. It
|
|
6
|
+
* is a group of native radio buttons: the arrow keys move the choice and Tab
|
|
7
|
+
* leaves it. For more than about five choices use a select field.
|
|
8
|
+
*/
|
|
9
|
+
export function SegmentedControl({ label, options, value, onChange, size = "regular", }) {
|
|
10
|
+
const name = useId();
|
|
11
|
+
return (_jsxs("fieldset", { className: `m-0 inline-flex max-w-full min-w-0 flex-wrap items-stretch gap-0.5 rounded-control border border-line bg-track ${size === "small" ? "p-0.5" : "p-1"}`, children: [_jsx("legend", { className: "sr-only", children: label }), options.map((option) => {
|
|
12
|
+
const on = option.value === value;
|
|
13
|
+
return (_jsxs("label", { className: `relative inline-flex cursor-pointer items-center justify-center gap-1.5 rounded-inner font-medium whitespace-nowrap has-[:disabled]:cursor-default has-[:disabled]:opacity-50 has-[:focus-visible]:outline-2 has-[:focus-visible]:outline-focus ${size === "small" ? "h-6 px-2 text-body-sm" : "h-7 px-3 text-body-sm"} ${on ? "bg-raised text-ink shadow-[0_0_0_1px_var(--vx-line-strong)]" : "text-ink-muted hover:text-ink"}`, children: [_jsx("input", { type: "radio", name: name, value: option.value, checked: on, disabled: option.disabled, onChange: () => onChange(option.value), className: "sr-only" }), option.label, option.count !== undefined ? (_jsx("span", { className: "text-meta text-ink-muted tabular-nums", children: option.count })) : null] }, option.value));
|
|
14
|
+
})] }));
|
|
15
|
+
}
|
|
16
|
+
/** `YYYY-MM-DD` of a date, in the person's own calendar. */
|
|
17
|
+
export function isoDay(date) {
|
|
18
|
+
const pad = (n) => String(n).padStart(2, "0");
|
|
19
|
+
return `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())}`;
|
|
20
|
+
}
|
|
21
|
+
const dayOf = (iso) => {
|
|
22
|
+
const [year, month, day] = iso.split("-").map(Number);
|
|
23
|
+
return new Date(year, month - 1, day);
|
|
24
|
+
};
|
|
25
|
+
/**
|
|
26
|
+
* A day, with buttons to the day before and after, and back to today. The
|
|
27
|
+
* value is a `YYYY-MM-DD` date in the person's own calendar.
|
|
28
|
+
*/
|
|
29
|
+
export function DateStepper({ value, onChange, min, max, label = "Day", locale, }) {
|
|
30
|
+
const today = isoDay(new Date());
|
|
31
|
+
const shift = (days) => {
|
|
32
|
+
const date = dayOf(value);
|
|
33
|
+
date.setDate(date.getDate() + days);
|
|
34
|
+
onChange(isoDay(date));
|
|
35
|
+
};
|
|
36
|
+
const written = dayOf(value).toLocaleDateString(locale, {
|
|
37
|
+
weekday: "short",
|
|
38
|
+
month: "short",
|
|
39
|
+
day: "numeric",
|
|
40
|
+
...(value.slice(0, 4) !== today.slice(0, 4) ? { year: "numeric" } : {}),
|
|
41
|
+
});
|
|
42
|
+
const arrow = "inline-flex size-8 items-center justify-center rounded-control border border-line-control bg-surface text-ink hover:bg-sunken disabled:opacity-50 disabled:pointer-events-none";
|
|
43
|
+
return (_jsxs("fieldset", { className: "m-0 flex min-w-0 items-center gap-2 border-0 p-0", children: [_jsx("legend", { className: "sr-only", children: label }), _jsx("button", { type: "button", className: arrow, "aria-label": "The day before", disabled: min !== undefined && value <= min, onClick: () => shift(-1), children: _jsx(ChevronLeft, { "aria-hidden": "true", size: 16 }) }), _jsx("span", { className: "min-w-0 truncate font-semibold", "aria-live": "polite", children: value === today ? `Today, ${written}` : written }), _jsx("button", { type: "button", className: arrow, "aria-label": "The day after", disabled: max !== undefined && value >= max, onClick: () => shift(1), children: _jsx(ChevronRight, { "aria-hidden": "true", size: 16 }) }), value !== today ? (_jsx("button", { type: "button", className: "h-8 rounded-control px-2 font-medium text-action hover:bg-action-soft", onClick: () => onChange(today), children: "Today" })) : null] }));
|
|
44
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { ReactElement, ReactNode, RefObject } from "react";
|
|
2
|
+
/**
|
|
3
|
+
* A small panel of words or controls that opens from a control and closes on
|
|
4
|
+
* Escape or a click outside (08 §3.17): what a page is for, behind its help
|
|
5
|
+
* control. Focus moves into it while it is open and back to the control after.
|
|
6
|
+
*/
|
|
7
|
+
export declare function Popover({ trigger, title, children, side, align, open, onOpenChange, wide, titleHidden, anchor, }: {
|
|
8
|
+
/** One element that takes focus: a button. */
|
|
9
|
+
trigger: ReactElement;
|
|
10
|
+
/** Names the panel for screen readers, and heads it. */
|
|
11
|
+
title: string;
|
|
12
|
+
children: ReactNode;
|
|
13
|
+
side?: "top" | "bottom" | "left" | "right";
|
|
14
|
+
align?: "start" | "center" | "end";
|
|
15
|
+
/** Opened and closed from outside, such as by a key typed elsewhere. */
|
|
16
|
+
open?: boolean;
|
|
17
|
+
onOpenChange?: (open: boolean) => void;
|
|
18
|
+
/** For a panel that holds a list to choose from, with no padding of its own. */
|
|
19
|
+
wide?: boolean;
|
|
20
|
+
/** The title names the panel for screen readers only, for one whose contents say what it is. */
|
|
21
|
+
titleHidden?: boolean;
|
|
22
|
+
/** What the panel is placed against, when that is more than its trigger: a split button's whole width. */
|
|
23
|
+
anchor?: RefObject<Element | null>;
|
|
24
|
+
}): import("react").JSX.Element;
|
package/dist/Popover.js
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { Popover as Base } from "@base-ui/react/popover";
|
|
3
|
+
/**
|
|
4
|
+
* A small panel of words or controls that opens from a control and closes on
|
|
5
|
+
* Escape or a click outside (08 §3.17): what a page is for, behind its help
|
|
6
|
+
* control. Focus moves into it while it is open and back to the control after.
|
|
7
|
+
*/
|
|
8
|
+
export function Popover({ trigger, title, children, side = "bottom", align = "start", open, onOpenChange, wide = false, titleHidden = false, anchor, }) {
|
|
9
|
+
return (_jsxs(Base.Root, { ...(open === undefined ? {} : { open }), ...(onOpenChange ? { onOpenChange: (next) => onOpenChange(next) } : {}), children: [_jsx(Base.Trigger, { render: trigger }), _jsx(Base.Portal, { children: _jsx(Base.Positioner, { side: side, align: align, sideOffset: 6, ...(anchor ? { anchor } : {}), className: "layer-popover", children: _jsxs(Base.Popup, { className: `max-h-(--available-height) overflow-y-auto rounded-panel border border-line-strong text-ink shadow-overlay transition-[opacity,translate] motion-base data-[ending-style]:opacity-0 data-[starting-style]:-translate-y-1 data-[starting-style]:opacity-0 ${wide
|
|
10
|
+
? "w-[23.875rem] max-w-[90vw] overflow-hidden bg-raised p-0"
|
|
11
|
+
: "max-w-sm bg-raised p-3"}`, children: [_jsx(Base.Title, { className: titleHidden ? "sr-only" : "m-0 mb-1.5 text-body-sm font-semibold", children: title }), _jsx(Base.Description, { render: _jsx("div", {}), className: wide ? "" : "text-ink-muted", children: children })] }) }) })] }));
|
|
12
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/** How a result can be shown, besides as JSON. */
|
|
2
|
+
export type ResultShape = "table" | "markdown" | "media" | "json";
|
|
3
|
+
/** The most rows a table draws; the rest are counted, and the JSON has them all. */
|
|
4
|
+
export declare const MAX_TABLE_ROWS = 500;
|
|
5
|
+
type Plain = Record<string, unknown>;
|
|
6
|
+
/** The shapes a value can be shown in, the best first. JSON is always last. */
|
|
7
|
+
export declare function resultShapes(value: unknown): ResultShape[];
|
|
8
|
+
/**
|
|
9
|
+
* The columns of a table: those the schema names, in its order, then those
|
|
10
|
+
* found in the rows. Without a schema, the order the rows were stored in says
|
|
11
|
+
* little (the database keeps keys shortest first), so plain values come
|
|
12
|
+
* first, and addresses and nested values after them.
|
|
13
|
+
*/
|
|
14
|
+
export declare function columnsOf(rows: Plain[], schema?: unknown): string[];
|
|
15
|
+
export interface ResultViewProps {
|
|
16
|
+
value: unknown;
|
|
17
|
+
/** Names the block for screen readers: "Output". */
|
|
18
|
+
label: string;
|
|
19
|
+
/** The result's declared JSON Schema, if any: a table's columns follow it. */
|
|
20
|
+
schema?: unknown;
|
|
21
|
+
maxHeight?: string;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* A result shown by its shape: rows as a table, `{ markdown }` as text, an
|
|
25
|
+
* address as a link or an image, with the JSON always one click away.
|
|
26
|
+
*/
|
|
27
|
+
export declare function ResultView({ value, label, schema, maxHeight }: ResultViewProps): import("react").JSX.Element;
|
|
28
|
+
export {};
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { useState } from "react";
|
|
3
|
+
import { JsonView } from "./JsonView.js";
|
|
4
|
+
import { Markdown, safeHref } from "./Markdown.js";
|
|
5
|
+
import { SegmentedControl } from "./Pickers.js";
|
|
6
|
+
/** The most rows a table draws; the rest are counted, and the JSON has them all. */
|
|
7
|
+
export const MAX_TABLE_ROWS = 500;
|
|
8
|
+
const MAX_COLUMNS = 12;
|
|
9
|
+
function isPlain(value) {
|
|
10
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
11
|
+
}
|
|
12
|
+
const IMAGE = /\.(png|jpe?g|gif|webp|avif|svg)(\?.*)?$/i;
|
|
13
|
+
/** A single address, or `{ image }`, `{ url }` or `{ file }` with an optional name. */
|
|
14
|
+
function mediaOf(value) {
|
|
15
|
+
if (typeof value === "string") {
|
|
16
|
+
if (/^data:image\//.test(value))
|
|
17
|
+
return { url: value, image: true };
|
|
18
|
+
const href = /^https?:\/\/\S+$/i.test(value) ? safeHref(value) : undefined;
|
|
19
|
+
return href ? { url: href, image: IMAGE.test(new URL(href).pathname) } : undefined;
|
|
20
|
+
}
|
|
21
|
+
if (!isPlain(value))
|
|
22
|
+
return undefined;
|
|
23
|
+
const keys = Object.keys(value);
|
|
24
|
+
const name = typeof value.name === "string" ? value.name : undefined;
|
|
25
|
+
if (keys.some((key) => !["image", "url", "file", "name", "alt"].includes(key)))
|
|
26
|
+
return undefined;
|
|
27
|
+
if (typeof value.image === "string") {
|
|
28
|
+
const found = mediaOf(value.image);
|
|
29
|
+
if (found)
|
|
30
|
+
return { ...found, image: true, name: name ?? value.alt };
|
|
31
|
+
}
|
|
32
|
+
const address = typeof value.url === "string" ? value.url : value.file;
|
|
33
|
+
if (typeof address === "string") {
|
|
34
|
+
const found = mediaOf(address);
|
|
35
|
+
if (found)
|
|
36
|
+
return { ...found, name };
|
|
37
|
+
}
|
|
38
|
+
return undefined;
|
|
39
|
+
}
|
|
40
|
+
/** The shapes a value can be shown in, the best first. JSON is always last. */
|
|
41
|
+
export function resultShapes(value) {
|
|
42
|
+
const shapes = [];
|
|
43
|
+
if (Array.isArray(value) && value.length > 0 && value.every(isPlain))
|
|
44
|
+
shapes.push("table");
|
|
45
|
+
if (isPlain(value) && typeof value.markdown === "string")
|
|
46
|
+
shapes.push("markdown");
|
|
47
|
+
if (mediaOf(value))
|
|
48
|
+
shapes.push("media");
|
|
49
|
+
shapes.push("json");
|
|
50
|
+
return shapes;
|
|
51
|
+
}
|
|
52
|
+
const LABELS = {
|
|
53
|
+
table: "Table",
|
|
54
|
+
markdown: "Text",
|
|
55
|
+
media: "Preview",
|
|
56
|
+
json: "JSON",
|
|
57
|
+
};
|
|
58
|
+
const ADDRESS = /^https?:\/\/\S+$/i;
|
|
59
|
+
/**
|
|
60
|
+
* The columns of a table: those the schema names, in its order, then those
|
|
61
|
+
* found in the rows. Without a schema, the order the rows were stored in says
|
|
62
|
+
* little (the database keeps keys shortest first), so plain values come
|
|
63
|
+
* first, and addresses and nested values after them.
|
|
64
|
+
*/
|
|
65
|
+
export function columnsOf(rows, schema) {
|
|
66
|
+
const named = isPlain(schema) && isPlain(schema.items) && isPlain(schema.items.properties)
|
|
67
|
+
? Object.keys(schema.items.properties)
|
|
68
|
+
: [];
|
|
69
|
+
const sample = rows.slice(0, 100);
|
|
70
|
+
const found = [];
|
|
71
|
+
for (const row of sample) {
|
|
72
|
+
for (const key of Object.keys(row))
|
|
73
|
+
if (!found.includes(key))
|
|
74
|
+
found.push(key);
|
|
75
|
+
}
|
|
76
|
+
const plain = (key) => sample.every((row) => {
|
|
77
|
+
const value = row[key];
|
|
78
|
+
return value === null || value === undefined || typeof value !== "object"
|
|
79
|
+
? !(typeof value === "string" && ADDRESS.test(value))
|
|
80
|
+
: false;
|
|
81
|
+
});
|
|
82
|
+
const rest = found.filter((key) => !named.includes(key));
|
|
83
|
+
return [...named, ...rest.filter(plain), ...rest.filter((key) => !plain(key))];
|
|
84
|
+
}
|
|
85
|
+
function cell(value) {
|
|
86
|
+
if (value === null || value === undefined)
|
|
87
|
+
return _jsx("span", { className: "text-ink-muted", children: "\u2014" });
|
|
88
|
+
if (typeof value === "string") {
|
|
89
|
+
const href = ADDRESS.test(value) ? safeHref(value) : undefined;
|
|
90
|
+
if (href) {
|
|
91
|
+
return (_jsx("a", { href: href, target: "_blank", rel: "noopener noreferrer", title: value, className: "block max-w-[16rem] truncate text-action", children: value }));
|
|
92
|
+
}
|
|
93
|
+
// A code, a name or a date reads on one line; prose wraps.
|
|
94
|
+
return value.length <= 32 ? _jsx("span", { className: "whitespace-nowrap", children: value }) : value;
|
|
95
|
+
}
|
|
96
|
+
if (typeof value === "number" || typeof value === "boolean") {
|
|
97
|
+
return _jsx("span", { className: "whitespace-nowrap tabular-nums", children: String(value) });
|
|
98
|
+
}
|
|
99
|
+
const text = JSON.stringify(value);
|
|
100
|
+
return (_jsx("code", { className: "font-mono text-code-sm text-ink-muted", title: text, children: text.length > 80 ? `${text.slice(0, 79)}…` : text }));
|
|
101
|
+
}
|
|
102
|
+
function ResultTable({ rows, schema, label }) {
|
|
103
|
+
const all = columnsOf(rows, schema);
|
|
104
|
+
const columns = all.slice(0, MAX_COLUMNS);
|
|
105
|
+
const shown = rows.slice(0, MAX_TABLE_ROWS);
|
|
106
|
+
const hidden = [
|
|
107
|
+
rows.length > shown.length
|
|
108
|
+
? `${rows.length - shown.length} more row${rows.length - shown.length === 1 ? "" : "s"}`
|
|
109
|
+
: "",
|
|
110
|
+
all.length > columns.length
|
|
111
|
+
? `${all.length - columns.length} more column${all.length - columns.length === 1 ? "" : "s"}`
|
|
112
|
+
: "",
|
|
113
|
+
].filter(Boolean);
|
|
114
|
+
return (_jsxs("div", { className: "flex flex-col gap-1.5", children: [_jsx("section", { "aria-label": label,
|
|
115
|
+
// biome-ignore lint/a11y/noNoninteractiveTabindex: a block that scrolls must be reachable by keyboard
|
|
116
|
+
tabIndex: 0, className: "max-h-[24rem] overflow-auto rounded-control border border-line bg-surface", children: _jsxs("table", { className: "w-full border-collapse text-left text-body-sm", children: [_jsx("thead", { className: "sticky top-0 bg-surface", children: _jsx("tr", { children: columns.map((column) => (_jsx("th", { scope: "col", className: "border-b border-line px-3 py-1.5 font-medium whitespace-nowrap text-ink-muted", children: column }, column))) }) }), _jsx("tbody", { children: shown.map((row, at) => (_jsx("tr", { className: "border-b border-line last:border-b-0", children: columns.map((column) => (_jsx("td", { className: "max-w-[24rem] px-3 py-1.5 align-top", children: cell(row[column]) }, column))) }, at))) })] }) }), _jsxs("p", { className: "m-0 text-meta text-ink-muted", children: [rows.length, " row", rows.length === 1 ? "" : "s", hidden.length > 0 ? `; ${hidden.join(" and ")} in the JSON` : ""] })] }));
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* An image from another site is not loaded until asked for: loading it tells
|
|
120
|
+
* that site who is looking. One carried in the value itself is shown.
|
|
121
|
+
*/
|
|
122
|
+
function MediaPreview({ media }) {
|
|
123
|
+
const inline = media.url.startsWith("data:");
|
|
124
|
+
const [show, setShow] = useState(inline);
|
|
125
|
+
const name = media.name ?? (inline ? "Image" : media.url);
|
|
126
|
+
return (_jsxs("div", { className: "flex flex-col items-start gap-2 rounded-control border border-line bg-sunken p-3", children: [media.image && show ? (_jsx("img", { src: media.url, alt: media.name ?? "", className: "max-h-[20rem] max-w-full rounded-inner" })) : null, _jsxs("div", { className: "flex max-w-full flex-wrap items-center gap-2", children: [inline ? null : (_jsx("a", { href: media.url, target: "_blank", rel: "noopener noreferrer", className: "text-action break-all", children: name })), media.image && !show ? (_jsx("button", { type: "button", className: "rounded-inner border border-line bg-surface px-1.5 py-0.5 text-meta text-ink-muted hover:text-ink", onClick: () => setShow(true), children: "Show image" })) : null] })] }));
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* A result shown by its shape: rows as a table, `{ markdown }` as text, an
|
|
130
|
+
* address as a link or an image, with the JSON always one click away.
|
|
131
|
+
*/
|
|
132
|
+
export function ResultView({ value, label, schema, maxHeight = "24rem" }) {
|
|
133
|
+
const shapes = resultShapes(value);
|
|
134
|
+
const [chosen, setChosen] = useState(shapes[0]);
|
|
135
|
+
const shape = shapes.includes(chosen) ? chosen : shapes[0];
|
|
136
|
+
const media = shape === "media" ? mediaOf(value) : undefined;
|
|
137
|
+
return (_jsxs("div", { className: "flex flex-col gap-2", children: [shapes.length > 1 ? (_jsx(SegmentedControl, { label: `Show ${label.toLowerCase()} as`, size: "small", value: shape, onChange: setChosen, options: shapes.map((value) => ({ value, label: LABELS[value] })) })) : null, shape === "table" ? (_jsx(ResultTable, { rows: value, schema: schema, label: label })) : shape === "markdown" ? (_jsx("section", { "aria-label": label,
|
|
138
|
+
// biome-ignore lint/a11y/noNoninteractiveTabindex: a block that scrolls must be reachable by keyboard
|
|
139
|
+
tabIndex: 0, style: { maxHeight }, className: "overflow-auto rounded-control border border-line bg-surface p-3", children: _jsx(Markdown, { text: value.markdown }) })) : media ? (_jsx(MediaPreview, { media: media })) : (_jsx(JsonView, { label: label, value: value, maxHeight: maxHeight }))] }));
|
|
140
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A form drawn from a JSON Schema: the part of JSON Schema 2020-12 that zod's
|
|
3
|
+
* `toJSONSchema` writes. Nested objects, lists, choices, unions picked by a
|
|
4
|
+
* field, and values that may be left as nothing. A part of a schema it cannot
|
|
5
|
+
* draw is asked for as JSON, that part alone. The whole form can be switched
|
|
6
|
+
* to JSON and back.
|
|
7
|
+
*
|
|
8
|
+
* It checks what it draws itself, with no code made at run time: the pages
|
|
9
|
+
* apps are served on allow no `eval`. The server checks again, fully, and what
|
|
10
|
+
* it refuses can be passed back as `errors`.
|
|
11
|
+
*/
|
|
12
|
+
export type JsonSchema = Record<string, unknown>;
|
|
13
|
+
/** What a value starts as: the one given, else the schema's default, else nothing. */
|
|
14
|
+
export declare function startValue(schema: JsonSchema, given: unknown): unknown;
|
|
15
|
+
/** The value as it is sent: nothing left empty. JSON half typed is kept, for `checkValue` to refuse. */
|
|
16
|
+
export declare function cleaned(value: unknown): unknown;
|
|
17
|
+
/**
|
|
18
|
+
* Checks a value against a schema, as far as the form draws it. Returns a
|
|
19
|
+
* message for each place that does not pass, by JSON Pointer; an empty object
|
|
20
|
+
* when it all passes.
|
|
21
|
+
*/
|
|
22
|
+
export declare function checkValue(schema: JsonSchema, value: unknown, at?: string): Record<string, string>;
|
|
23
|
+
export interface SchemaFormProps {
|
|
24
|
+
schema: JsonSchema;
|
|
25
|
+
/** What the form starts with. Read once: give the form a new `key` to start again. */
|
|
26
|
+
value?: unknown;
|
|
27
|
+
/** Called with the checked value. While its promise is pending the button is disabled. */
|
|
28
|
+
/** Without it, the form is part of a larger one: no button, and `onChange` says what it holds. */
|
|
29
|
+
onSubmit?: (value: unknown) => void | Promise<void>;
|
|
30
|
+
submitLabel?: string;
|
|
31
|
+
/** Shown beside the submit button, for a form in a dialog. */
|
|
32
|
+
onCancel?: () => void;
|
|
33
|
+
/** Messages from the server, by JSON Pointer into the value (`/customer/email`). */
|
|
34
|
+
errors?: Record<string, string>;
|
|
35
|
+
disabled?: boolean;
|
|
36
|
+
/** Called with the value as it is edited, checked or not. */
|
|
37
|
+
onChange?: (value: unknown) => void;
|
|
38
|
+
/**
|
|
39
|
+
* Offer "Fields / JSON" to switch to the value as JSON. For people who build things, the
|
|
40
|
+
* default; a form shown to the people who use an app (a question an action asks) leaves it off.
|
|
41
|
+
*/
|
|
42
|
+
json?: boolean;
|
|
43
|
+
}
|
|
44
|
+
/** A form drawn from a JSON Schema, with a JSON view of the whole value beside it. */
|
|
45
|
+
export declare function SchemaForm({ schema, value, onSubmit, submitLabel, onCancel, errors, disabled, onChange, json, }: SchemaFormProps): import("react").JSX.Element;
|