@open-artifact/ui 0.11.0 → 0.12.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@open-artifact/ui",
3
- "version": "0.11.0",
3
+ "version": "0.12.0",
4
4
  "description": "Shared editor UI kit: Base UI primitives styled with Tailwind CSS, lucide icons, light and dark themes.",
5
5
  "type": "module",
6
6
  "exports": {
@@ -0,0 +1,29 @@
1
+ /**
2
+ * {@link CrashBoundary}: keeps a render error in an editor from blanking the
3
+ * page. The subtree below it is replaced by the editor's `fallback` (what
4
+ * happened, how to recover); the error still reaches the console, and any
5
+ * `window.onerror` listener, through `reportError`.
6
+ */
7
+ import { Component, type ReactNode } from 'react';
8
+ /** Props of {@link CrashBoundary}. */
9
+ export interface CrashBoundaryProps {
10
+ children: ReactNode;
11
+ /** Shown in place of `children` after they failed to render. */
12
+ fallback: ReactNode;
13
+ }
14
+ export declare class CrashBoundary extends Component<CrashBoundaryProps, {
15
+ crashed: boolean;
16
+ }> {
17
+ state: {
18
+ crashed: boolean;
19
+ };
20
+ static getDerivedStateFromError(): {
21
+ crashed: boolean;
22
+ };
23
+ /** New children after a crash (a remote change, a page switch) get another try. */
24
+ componentDidUpdate(previous: CrashBoundaryProps, previousState: {
25
+ crashed: boolean;
26
+ }): void;
27
+ componentDidCatch(error: unknown): void;
28
+ render(): ReactNode;
29
+ }
@@ -0,0 +1,32 @@
1
+ /**
2
+ * {@link CrashBoundary}: keeps a render error in an editor from blanking the
3
+ * page. The subtree below it is replaced by the editor's `fallback` (what
4
+ * happened, how to recover); the error still reaches the console, and any
5
+ * `window.onerror` listener, through `reportError`.
6
+ */
7
+ import { Component } from 'react';
8
+ export class CrashBoundary extends Component {
9
+ state = { crashed: false };
10
+ static getDerivedStateFromError() {
11
+ return { crashed: true };
12
+ }
13
+ /** New children after a crash (a remote change, a page switch) get another try. */
14
+ componentDidUpdate(previous, previousState) {
15
+ if (previousState.crashed && this.state.crashed && previous.children !== this.props.children)
16
+ this.setState({ crashed: false });
17
+ }
18
+ componentDidCatch(error) {
19
+ try {
20
+ if (typeof reportError === 'function')
21
+ reportError(error);
22
+ else
23
+ console.error(error);
24
+ }
25
+ catch {
26
+ // Reporting must never throw out of the boundary.
27
+ }
28
+ }
29
+ render() {
30
+ return this.state.crashed ? this.props.fallback : this.props.children;
31
+ }
32
+ }
package/src/fields.d.ts CHANGED
@@ -1,5 +1,8 @@
1
1
  import { type InputHTMLAttributes, type ReactNode } from 'react';
2
- /** Shared look of single-line inputs. */
2
+ /**
3
+ * Shared look of single-line inputs. Every field's box also carries `data-ui-field`, so a page can give the fields
4
+ * of one part of it another look (a compact inspector, say).
5
+ */
3
6
  export declare const INPUT_CLASS = "h-7 w-full min-w-0 rounded-control border border-line bg-surface-2 px-2 text-[13px] text-fg outline-none placeholder:text-fg-subtle focus:border-accent focus:bg-surface";
4
7
  /** A plain text input. */
5
8
  export declare const TextInput: import("react").ForwardRefExoticComponent<InputHTMLAttributes<HTMLInputElement> & import("react").RefAttributes<HTMLInputElement>>;
@@ -59,11 +62,16 @@ export interface SegmentedProps<T extends string> {
59
62
  value: T | null;
60
63
  options: Option<T>[];
61
64
  onChange: (value: T) => void;
65
+ /**
66
+ * Called with the picked option when it is pressed again (to let the pick go, say). Without it, that press does
67
+ * nothing and the pick stays.
68
+ */
69
+ onRepick?: (value: T) => void;
62
70
  className?: string;
63
71
  'aria-label'?: string;
64
72
  }
65
73
  /** A segmented control (one of a few options, all visible). */
66
- export declare function Segmented<T extends string>({ value, options, onChange, className, ...aria }: SegmentedProps<T>): import("react/jsx-runtime").JSX.Element;
74
+ export declare function Segmented<T extends string>({ value, options, onChange, onRepick, className, ...aria }: SegmentedProps<T>): import("react/jsx-runtime").JSX.Element;
67
75
  /** Props of {@link SwitchInput}. */
68
76
  export interface SwitchInputProps {
69
77
  checked: boolean;
package/src/fields.js CHANGED
@@ -14,11 +14,14 @@ import { forwardRef, useEffect, useRef, useState } from 'react';
14
14
  import { cn } from "./cn.js";
15
15
  import { isComposingKey, preventRepeatedActivation } from "./keys.js";
16
16
  import { POPUP_CLASS } from "./menu.js";
17
- /** Shared look of single-line inputs. */
17
+ /**
18
+ * Shared look of single-line inputs. Every field's box also carries `data-ui-field`, so a page can give the fields
19
+ * of one part of it another look (a compact inspector, say).
20
+ */
18
21
  export const INPUT_CLASS = 'h-7 w-full min-w-0 rounded-control border border-line bg-surface-2 px-2 text-[13px] text-fg outline-none placeholder:text-fg-subtle focus:border-accent focus:bg-surface';
19
22
  /** A plain text input. */
20
23
  export const TextInput = forwardRef(function TextInput({ className, ...rest }, ref) {
21
- return _jsx("input", { ref: ref, className: cn(INPUT_CLASS, className), ...rest });
24
+ return _jsx("input", { ref: ref, "data-ui-field": "", className: cn(INPUT_CLASS, className), ...rest });
22
25
  });
23
26
  /**
24
27
  * A text input that keeps a local draft and commits on Enter or blur; Escape
@@ -40,7 +43,7 @@ export function CommitInput({ value, onCommit, onDraftChange, className, ...rest
40
43
  onDraftChange?.(value);
41
44
  }
42
45
  };
43
- return (_jsx("input", { className: cn(INPUT_CLASS, className), value: draft, onChange: (event) => {
46
+ return (_jsx("input", { "data-ui-field": "", className: cn(INPUT_CLASS, className), value: draft, onChange: (event) => {
44
47
  setDraft(event.target.value);
45
48
  onDraftChange?.(event.target.value);
46
49
  }, onBlur: commit, onKeyDown: (event) => {
@@ -63,19 +66,25 @@ export function CommitInput({ value, onCommit, onDraftChange, className, ...rest
63
66
  }
64
67
  /** A number field whose label can be dragged horizontally to scrub the value. */
65
68
  export function NumberInput({ value, onChange, onCommit, label, unit, min, max, step = 1, placeholder, className, ...aria }) {
66
- return (_jsxs(BaseNumberField.Root, { value: value, min: min, max: max, step: step, onValueChange: (next) => onChange?.(next), onValueCommitted: (next) => onCommit(next), className: cn('flex h-7 min-w-0 items-center rounded-control border border-line bg-surface-2 focus-within:border-accent', className), children: [label !== undefined ? (_jsxs(BaseNumberField.ScrubArea, { className: "flex h-full w-6 shrink-0 cursor-ew-resize items-center justify-center text-[11px] text-fg-subtle", children: [label, _jsx(BaseNumberField.ScrubAreaCursor, {})] })) : null, _jsx(BaseNumberField.Input, { placeholder: placeholder, "aria-label": aria['aria-label'], className: cn('h-full w-full min-w-0 bg-transparent text-[13px] text-fg outline-none', label === undefined && 'pl-2') }), unit ? _jsx("span", { className: "pr-2 text-[11px] text-fg-subtle", children: unit }) : null] }));
69
+ return (_jsxs(BaseNumberField.Root, { value: value, min: min, max: max, step: step, onValueChange: (next) => onChange?.(next), onValueCommitted: (next) => onCommit(next), "data-ui-field": "", className: cn('flex h-7 min-w-0 items-center rounded-control border border-line bg-surface-2 focus-within:border-accent', className), children: [label !== undefined ? (_jsxs(BaseNumberField.ScrubArea, { className: "flex h-full w-6 shrink-0 cursor-ew-resize items-center justify-center text-[11px] text-fg-subtle", children: [label, _jsx(BaseNumberField.ScrubAreaCursor, {})] })) : null, _jsx(BaseNumberField.Input, { placeholder: placeholder, "aria-label": aria['aria-label'], onKeyDown: (event) => {
70
+ if (event.key === 'Enter')
71
+ event.currentTarget.blur();
72
+ }, className: cn('h-full w-full min-w-0 bg-transparent text-[13px] text-fg outline-none', label === undefined && 'pl-2') }), unit ? _jsx("span", { className: "pr-2 text-[11px] text-fg-subtle", children: unit }) : null] }));
67
73
  }
68
74
  /** A dropdown select. */
69
75
  export function SelectInput({ value, options, onChange, placeholder = '—', className, ...aria }) {
70
- return (_jsxs(BaseSelect.Root, { value: value, onValueChange: (next) => next !== null && onChange(next), children: [_jsxs(BaseSelect.Trigger, { "aria-label": aria['aria-label'], className: cn(INPUT_CLASS, 'flex items-center justify-between gap-1 text-left', className), children: [_jsx(BaseSelect.Value, { children: (current) => options.find((option) => option.value === current)?.label ?? _jsx("span", { className: "text-fg-subtle", children: placeholder }) }), _jsx(BaseSelect.Icon, { children: _jsx(ChevronDown, { className: "size-3.5 text-fg-subtle" }) })] }), _jsx(BaseSelect.Portal, { children: _jsx(BaseSelect.Positioner, { sideOffset: 4, className: "z-[1000]", alignItemWithTrigger: false, children: _jsx(BaseSelect.Popup, { className: cn(POPUP_CLASS, 'max-h-80 overflow-auto'), children: _jsx(BaseSelect.List, { children: options.map((option) => (_jsxs(BaseSelect.Item, { value: option.value, className: "flex h-7 min-w-32 cursor-default select-none items-center gap-2 rounded-md px-2 outline-none data-[highlighted]:bg-hover", children: [_jsx("span", { className: "flex size-4 items-center justify-center", children: _jsx(BaseSelect.ItemIndicator, { children: _jsx(Check, { className: "size-3.5" }) }) }), _jsx(BaseSelect.ItemText, { children: option.label })] }, option.value))) }) }) }) })] }));
76
+ return (_jsxs(BaseSelect.Root, { value: value, onValueChange: (next) => next !== null && onChange(next), children: [_jsxs(BaseSelect.Trigger, { "aria-label": aria['aria-label'], "data-ui-field": "", className: cn(INPUT_CLASS, 'flex items-center justify-between gap-1 text-left', className), children: [_jsx(BaseSelect.Value, { children: (current) => options.find((option) => option.value === current)?.label ?? _jsx("span", { className: "text-fg-subtle", children: placeholder }) }), _jsx(BaseSelect.Icon, { children: _jsx(ChevronDown, { className: "size-3.5 text-fg-subtle" }) })] }), _jsx(BaseSelect.Portal, { children: _jsx(BaseSelect.Positioner, { sideOffset: 4, className: "z-[1000]", alignItemWithTrigger: false, children: _jsx(BaseSelect.Popup, { className: cn(POPUP_CLASS, 'max-h-80 overflow-auto'), children: _jsx(BaseSelect.List, { children: options.map((option) => (_jsxs(BaseSelect.Item, { value: option.value, className: "flex h-7 min-w-32 cursor-default select-none items-center gap-2 rounded-md px-2 outline-none data-[highlighted]:bg-hover", children: [_jsx("span", { className: "flex size-4 items-center justify-center", children: _jsx(BaseSelect.ItemIndicator, { children: _jsx(Check, { className: "size-3.5" }) }) }), _jsx(BaseSelect.ItemText, { children: option.label })] }, option.value))) }) }) }) })] }));
71
77
  }
72
78
  /** A segmented control (one of a few options, all visible). */
73
- export function Segmented({ value, options, onChange, className, ...aria }) {
79
+ export function Segmented({ value, options, onChange, onRepick, className, ...aria }) {
74
80
  return (_jsx(ToggleGroup, { value: value === null ? [] : [value], onValueChange: (next) => {
75
81
  const picked = next[0];
82
+ // Pressing the pressed option again leaves the group empty.
76
83
  if (picked !== undefined)
77
84
  onChange(picked);
78
- }, "aria-label": aria['aria-label'], className: cn('flex h-7 items-center gap-0.5 rounded-control bg-surface-2 p-0.5', className), children: options.map((option) => (_jsx(Toggle, { value: option.value, "aria-label": option.title, title: option.title, className: "flex h-full flex-1 items-center justify-center rounded-[4px] px-1.5 text-fg-muted outline-none hover:text-fg data-[pressed]:bg-surface data-[pressed]:text-fg data-[pressed]:shadow-sm focus-visible:outline-2 focus-visible:outline-accent", children: option.label }, option.value))) }));
85
+ else if (value !== null)
86
+ onRepick?.(value);
87
+ }, "aria-label": aria['aria-label'], "data-ui-field": "", className: cn('flex h-7 items-center gap-0.5 rounded-control bg-surface-2 p-0.5', className), children: options.map((option) => (_jsx(Toggle, { value: option.value, "aria-label": option.title, title: option.title, className: "flex h-full flex-1 items-center justify-center whitespace-nowrap rounded-[4px] px-1.5 text-fg-muted outline-none hover:text-fg data-[pressed]:bg-surface data-[pressed]:text-fg data-[pressed]:shadow-sm focus-visible:outline-2 focus-visible:outline-accent", children: option.label }, option.value))) }));
79
88
  }
80
89
  /** An on/off switch. A held Enter flips it once. */
81
90
  export function SwitchInput({ checked, onChange, disabled, ...aria }) {
package/src/index.d.ts CHANGED
@@ -16,6 +16,9 @@ export * from './clicks.ts';
16
16
  export * from './focusables.ts';
17
17
  export * from './scroll.tsx';
18
18
  export * from './history.tsx';
19
+ export * from './crash-boundary.tsx';
20
+ export * from './link-card.ts';
21
+ export * from './link-card-view.tsx';
19
22
  export * from './color.ts';
20
23
  export * from './color-picker.tsx';
21
24
  export * from './effects.ts';
package/src/index.js CHANGED
@@ -16,6 +16,9 @@ export * from "./clicks.js";
16
16
  export * from "./focusables.js";
17
17
  export * from "./scroll.js";
18
18
  export * from "./history.js";
19
+ export * from "./crash-boundary.js";
20
+ export * from "./link-card.js";
21
+ export * from "./link-card-view.js";
19
22
  export * from "./color.js";
20
23
  export * from "./color-picker.js";
21
24
  export * from "./effects.js";
package/src/layout.d.ts CHANGED
@@ -4,6 +4,8 @@ export interface TabItem<T extends string> {
4
4
  value: T;
5
5
  label: ReactNode;
6
6
  content: ReactNode;
7
+ /** Kept rendered, hidden, while another tab is on show (its content is unmounted otherwise). */
8
+ keepMounted?: boolean;
7
9
  }
8
10
  /** Props of {@link Tabs}. */
9
11
  export interface TabsProps<T extends string> {
@@ -13,9 +15,11 @@ export interface TabsProps<T extends string> {
13
15
  className?: string;
14
16
  /** Extra controls shown at the end of the tab bar. */
15
17
  trailing?: ReactNode;
18
+ /** The tab list's accessible name. */
19
+ label?: string;
16
20
  }
17
21
  /** A tab strip with panels. */
18
- export declare function Tabs<T extends string>({ value, onChange, items, className, trailing }: TabsProps<T>): import("react/jsx-runtime").JSX.Element;
22
+ export declare function Tabs<T extends string>({ value, onChange, items, className, trailing, label }: TabsProps<T>): import("react/jsx-runtime").JSX.Element;
19
23
  /** Props of {@link Section}. */
20
24
  export interface SectionProps {
21
25
  title: ReactNode;
@@ -25,9 +29,9 @@ export interface SectionProps {
25
29
  /** Collapsed state is uncontrolled; start open unless `defaultOpen` is false. */
26
30
  defaultOpen?: boolean;
27
31
  }
28
- /** A collapsible group of inspector rows. */
32
+ /** A collapsible group of inspector rows. Carries `data-ui-section` (its heading `data-ui-section-head`) for page styles. */
29
33
  export declare function Section({ title, children, actions, defaultOpen }: SectionProps): import("react/jsx-runtime").JSX.Element;
30
- /** A label + control row inside a {@link Section}. */
34
+ /** A label + control row inside a {@link Section}. Carries `data-ui-row` (its label `data-ui-row-label`) for page styles. */
31
35
  export declare function Row({ label, children, className }: {
32
36
  label?: ReactNode;
33
37
  children: ReactNode;
package/src/layout.js CHANGED
@@ -10,16 +10,16 @@ import { ChevronDown, X } from 'lucide-react';
10
10
  import { cn } from "./cn.js";
11
11
  import { guardHeldActivation } from "./keys.js";
12
12
  /** A tab strip with panels. */
13
- export function Tabs({ value, onChange, items, className, trailing }) {
14
- return (_jsxs(BaseTabs.Root, { value: value, onValueChange: (next) => onChange(next), className: cn('flex min-h-0 flex-col', className), children: [_jsxs("div", { className: "flex h-9 shrink-0 items-center gap-1 border-b border-line px-2", children: [_jsx(BaseTabs.List, { className: "relative flex items-center gap-1", children: items.map((item) => (_jsx(BaseTabs.Tab, { value: item.value, className: "h-7 rounded-md px-2 text-[13px] font-medium text-fg-muted outline-none hover:text-fg data-[active]:bg-hover data-[active]:text-fg focus-visible:outline-2 focus-visible:outline-accent", children: item.label }, item.value))) }), _jsx("div", { className: "ml-auto flex items-center gap-1", children: trailing })] }), items.map((item) => (_jsx(BaseTabs.Panel, { value: item.value, className: "min-h-0 flex-1 overflow-auto outline-none", children: item.content }, item.value)))] }));
13
+ export function Tabs({ value, onChange, items, className, trailing, label }) {
14
+ return (_jsxs(BaseTabs.Root, { value: value, onValueChange: (next) => onChange(next), className: cn('flex min-h-0 flex-col', className), children: [_jsxs("div", { className: "flex h-9 shrink-0 items-center gap-1 border-b border-line px-2", "data-ui-tabs-bar": "", children: [_jsx(BaseTabs.List, { "aria-label": label, className: "relative flex items-center gap-1", children: items.map((item) => (_jsx(BaseTabs.Tab, { value: item.value, className: "h-7 rounded-md px-2 text-[13px] font-medium text-fg-muted outline-none hover:text-fg data-[active]:bg-hover data-[active]:text-fg focus-visible:outline-2 focus-visible:outline-accent", children: item.label }, item.value))) }), _jsx("div", { className: "ml-auto flex items-center gap-1", children: trailing })] }), items.map((item) => (_jsx(BaseTabs.Panel, { value: item.value, keepMounted: item.keepMounted, className: "min-h-0 flex-1 overflow-auto outline-none", children: item.content }, item.value)))] }));
15
15
  }
16
- /** A collapsible group of inspector rows. */
16
+ /** A collapsible group of inspector rows. Carries `data-ui-section` (its heading `data-ui-section-head`) for page styles. */
17
17
  export function Section({ title, children, actions, defaultOpen = true }) {
18
- return (_jsxs(Collapsible.Root, { defaultOpen: defaultOpen, className: "border-b border-line px-3 py-2", children: [_jsxs("div", { className: "flex h-7 items-center gap-1", children: [_jsxs(Collapsible.Trigger, { className: "group flex flex-1 items-center gap-1 text-left text-xs font-semibold text-fg outline-none", children: [_jsx(ChevronDown, { className: "size-3 text-fg-subtle transition-transform group-data-[panel-open]:rotate-0 -rotate-90" }), title] }), actions] }), _jsx(Collapsible.Panel, { className: "flex flex-col gap-1.5 pb-1 pt-1", children: children })] }));
18
+ return (_jsxs(Collapsible.Root, { defaultOpen: defaultOpen, className: "border-b border-line px-3 py-2", "data-ui-section": "", children: [_jsxs("div", { className: "flex h-7 items-center gap-1", "data-ui-section-head": "", children: [_jsxs(Collapsible.Trigger, { className: "group flex flex-1 items-center gap-1 text-left text-xs font-semibold text-fg outline-none", children: [_jsx(ChevronDown, { className: "size-3 text-fg-subtle transition-transform group-data-[panel-open]:rotate-0 -rotate-90" }), title] }), actions] }), _jsx(Collapsible.Panel, { className: "flex flex-col gap-1.5 pb-1 pt-1", children: children })] }));
19
19
  }
20
- /** A label + control row inside a {@link Section}. */
20
+ /** A label + control row inside a {@link Section}. Carries `data-ui-row` (its label `data-ui-row-label`) for page styles. */
21
21
  export function Row({ label, children, className }) {
22
- return (_jsxs("div", { className: cn('flex min-w-0 items-center gap-2', className), children: [label !== undefined ? _jsx("div", { className: "w-16 shrink-0 truncate text-xs text-fg-muted", children: label }) : null, _jsx("div", { className: "flex min-w-0 flex-1 items-center gap-1.5", children: children })] }));
22
+ return (_jsxs("div", { className: cn('flex min-w-0 items-center gap-2', className), "data-ui-row": "", children: [label !== undefined ? (_jsx("div", { className: "w-16 shrink-0 truncate text-xs text-fg-muted", "data-ui-row-label": "", children: label })) : null, _jsx("div", { className: "flex min-w-0 flex-1 items-center gap-1.5", children: children })] }));
23
23
  }
24
24
  /** A floating bar (toolbars, zoom controls). */
25
25
  export function Bar({ className, ...rest }) {
@@ -0,0 +1,30 @@
1
+ /**
2
+ * {@link LinkCard}: the card under a link in rich text being edited. It shows
3
+ * the address, and Open, Copy link, Edit and Remove. Editing the address
4
+ * saves on Enter and puts it back on Escape; Escape on the card gives the
5
+ * focus back to the text.
6
+ *
7
+ * The editor brings what differs: what a typed address becomes
8
+ * (`normalize`), how it is copied and how the link changes.
9
+ */
10
+ /** Props of {@link LinkCard}. */
11
+ export interface LinkCardProps {
12
+ /** The link in the text; the card sits under it. */
13
+ link: HTMLElement;
14
+ /** The address as the document writes it: shown, copied and edited. */
15
+ target: string;
16
+ /** The address resolved, which Open goes to. */
17
+ href: string;
18
+ /** What a typed address becomes, or null when it cannot be one. */
19
+ normalize(typed: string): string | null;
20
+ /** Shown under an address `normalize` refused. */
21
+ invalidMessage?: string;
22
+ onCopy(target: string): void;
23
+ /** Writes a new address into the link; returns false when it could not. */
24
+ onEdit(target: string): boolean;
25
+ onRemove(): void;
26
+ /** Gives focus back to the text, with the caret in the link. */
27
+ onReturn(): void;
28
+ className?: string;
29
+ }
30
+ export declare function LinkCard({ link, target, href, normalize, invalidMessage, onCopy, onEdit, onRemove, onReturn, className }: LinkCardProps): import("react/jsx-runtime").JSX.Element;
@@ -0,0 +1,76 @@
1
+ import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
2
+ /**
3
+ * {@link LinkCard}: the card under a link in rich text being edited. It shows
4
+ * the address, and Open, Copy link, Edit and Remove. Editing the address
5
+ * saves on Enter and puts it back on Escape; Escape on the card gives the
6
+ * focus back to the text.
7
+ *
8
+ * The editor brings what differs: what a typed address becomes
9
+ * (`normalize`), how it is copied and how the link changes.
10
+ */
11
+ import { Copy, ExternalLink, Pencil, Unlink } from 'lucide-react';
12
+ import { useLayoutEffect, useRef, useState } from 'react';
13
+ import { IconButton } from "./button.js";
14
+ import { cn } from "./cn.js";
15
+ import { INPUT_CLASS } from "./fields.js";
16
+ import { isComposingKey } from "./keys.js";
17
+ import { canOpenLink, placeLinkCard, shownLink } from "./link-card.js";
18
+ const keepTextFocus = (event) => event.preventDefault();
19
+ export function LinkCard({ link, target, href, normalize, invalidMessage = 'Not a link address', onCopy, onEdit, onRemove, onReturn, className }) {
20
+ const card = useRef(null);
21
+ const [editing, setEditing] = useState(false);
22
+ const [draft, setDraft] = useState('');
23
+ const [invalid, setInvalid] = useState(false);
24
+ const [place, setPlace] = useState(null);
25
+ const shown = shownLink(target);
26
+ const openable = canOpenLink(href);
27
+ useLayoutEffect(() => {
28
+ const measure = () => {
29
+ if (!card.current || !link.isConnected)
30
+ return;
31
+ const box = card.current.getBoundingClientRect();
32
+ setPlace(placeLinkCard(link.getBoundingClientRect(), box, { width: window.innerWidth, height: window.innerHeight }));
33
+ };
34
+ measure();
35
+ window.addEventListener('scroll', measure, true);
36
+ window.addEventListener('resize', measure);
37
+ return () => {
38
+ window.removeEventListener('scroll', measure, true);
39
+ window.removeEventListener('resize', measure);
40
+ };
41
+ }, [link, editing]);
42
+ const startEdit = () => {
43
+ setDraft(target);
44
+ setInvalid(false);
45
+ setEditing(true);
46
+ };
47
+ const save = () => {
48
+ const next = normalize(draft);
49
+ if (next === null) {
50
+ setInvalid(true);
51
+ return;
52
+ }
53
+ if (next !== target && !onEdit(next))
54
+ return;
55
+ setEditing(false);
56
+ if (next === target)
57
+ onReturn();
58
+ };
59
+ return (_jsx("div", { ref: card, role: "dialog", "aria-label": "Link", "data-link-card": "", className: cn('fixed z-50 flex max-w-[min(420px,calc(100vw-12px))] items-center gap-1 rounded-control border border-line bg-surface p-1 text-[12px] shadow-lg', className), style: place ? { top: place.top, left: place.left } : { top: -9999, left: -9999 }, onKeyDown: (event) => {
60
+ if (event.key === 'Escape' && !isComposingKey(event.nativeEvent)) {
61
+ event.preventDefault();
62
+ event.stopPropagation();
63
+ if (editing)
64
+ setEditing(false);
65
+ onReturn();
66
+ }
67
+ }, children: editing ? (_jsxs(_Fragment, { children: [_jsx("input", { autoFocus: true, "aria-label": "Link address", "aria-invalid": invalid || undefined, value: draft, spellCheck: false, className: cn(INPUT_CLASS, 'h-7 w-72 font-mono text-[12px]', invalid && 'border-danger'), onChange: (event) => {
68
+ setDraft(event.target.value);
69
+ setInvalid(false);
70
+ }, onKeyDown: (event) => {
71
+ if (event.key !== 'Enter' || isComposingKey(event.nativeEvent))
72
+ return;
73
+ event.preventDefault();
74
+ save();
75
+ } }), invalid ? _jsx("span", { role: "alert", className: "px-1 text-danger", children: invalidMessage }) : null] })) : (_jsxs(_Fragment, { children: [_jsxs("span", { className: "min-w-0 flex-1 truncate px-1.5", title: target, children: [_jsx("span", { className: "text-fg", children: shown.site }), _jsx("span", { className: "text-fg-subtle", children: shown.rest })] }), openable ? (_jsx(IconButton, { size: "sm", label: "Open link", icon: _jsx(ExternalLink, { className: "size-3.5" }), onMouseDown: keepTextFocus, onClick: () => window.open(href, '_blank', 'noopener,noreferrer') })) : null, _jsx(IconButton, { size: "sm", label: "Copy link", icon: _jsx(Copy, { className: "size-3.5" }), onMouseDown: keepTextFocus, onClick: () => onCopy(target) }), _jsx(IconButton, { size: "sm", label: "Edit link", icon: _jsx(Pencil, { className: "size-3.5" }), onClick: startEdit }), _jsx(IconButton, { size: "sm", label: "Remove link", icon: _jsx(Unlink, { className: "size-3.5" }), onMouseDown: keepTextFocus, onClick: onRemove })] })) }));
76
+ }
@@ -0,0 +1,26 @@
1
+ /**
2
+ * How the link card shows an address, what it may open and where it goes.
3
+ * What a typed address becomes is the editor's own rule (see {@link LinkCard}).
4
+ */
5
+ /** The address split for display: the site in front, the rest after it. */
6
+ export declare function shownLink(target: string): {
7
+ site: string;
8
+ rest: string;
9
+ };
10
+ /** Whether a resolved address may be opened from the card. */
11
+ export declare function canOpenLink(resolved: string): boolean;
12
+ /** Where the card goes: below the link, or above it when below would leave the window. */
13
+ export declare function placeLinkCard(link: {
14
+ top: number;
15
+ bottom: number;
16
+ left: number;
17
+ }, card: {
18
+ width: number;
19
+ height: number;
20
+ }, window: {
21
+ width: number;
22
+ height: number;
23
+ }, gap?: number): {
24
+ top: number;
25
+ left: number;
26
+ };
@@ -0,0 +1,37 @@
1
+ /**
2
+ * How the link card shows an address, what it may open and where it goes.
3
+ * What a typed address becomes is the editor's own rule (see {@link LinkCard}).
4
+ */
5
+ /** The address split for display: the site in front, the rest after it. */
6
+ export function shownLink(target) {
7
+ const web = /^https?:\/\/([^/?#]+)(.*)$/i.exec(target);
8
+ if (web)
9
+ return { site: web[1].replace(/^www\./i, ''), rest: web[2] === '/' ? '' : web[2] };
10
+ const mail = /^mailto:(.*)$/i.exec(target);
11
+ if (mail) {
12
+ try {
13
+ return { site: decodeURIComponent(mail[1]), rest: '' };
14
+ }
15
+ catch {
16
+ return { site: mail[1], rest: '' };
17
+ }
18
+ }
19
+ return { site: target, rest: '' };
20
+ }
21
+ /** Whether a resolved address may be opened from the card. */
22
+ export function canOpenLink(resolved) {
23
+ try {
24
+ return ['http:', 'https:', 'mailto:'].includes(new URL(resolved).protocol);
25
+ }
26
+ catch {
27
+ return false;
28
+ }
29
+ }
30
+ /** Where the card goes: below the link, or above it when below would leave the window. */
31
+ export function placeLinkCard(link, card, window, gap = 6) {
32
+ const below = link.bottom + gap;
33
+ const above = link.top - gap - card.height;
34
+ const top = below + card.height <= window.height - gap || above < gap ? below : above;
35
+ const left = Math.max(gap, Math.min(link.left, window.width - card.width - gap));
36
+ return { top, left };
37
+ }
@@ -194,7 +194,7 @@ export function ScrubNumberField({ value, mixed = false, onChange, onCommit, lab
194
194
  // eslint-disable-next-line react-hooks/exhaustive-deps
195
195
  }, [scrubbing]);
196
196
  const numeric = value !== null && !mixed ? roundTo(value, decimals) : undefined;
197
- return (_jsxs("div", { className: cn('flex h-7 min-w-0 items-center rounded-control border border-line bg-surface-2 text-[13px] text-fg focus-within:border-accent focus-within:bg-surface', disabled && 'opacity-50', scrubbing && 'border-accent', className), children: [label !== undefined ? (_jsx("span", { "aria-hidden": "true", "data-scrub-for": inputId, title: title ?? 'Drag to change', onPointerDown: onPointerDown, onPointerMove: onPointerMove, onPointerUp: (event) => endScrub(event.currentTarget, true), onPointerCancel: (event) => endScrub(event.currentTarget, false), onLostPointerCapture: (event) => scrub.current && endScrub(event.currentTarget, true), className: cn('flex h-full min-w-6 shrink-0 select-none items-center justify-center px-1 text-[11px] text-fg-subtle touch-none', disabled ? 'cursor-default' : 'cursor-ew-resize hover:text-fg'), children: label })) : null, _jsx("input", { ref: inputRef, id: inputId, role: "spinbutton", inputMode: "decimal", autoComplete: "off", spellCheck: false, "aria-label": aria['aria-label'], "aria-valuenow": numeric, "aria-valuemin": min, "aria-valuemax": max, "aria-valuetext": mixed ? 'Mixed' : numeric !== undefined ? `${formatNumber(numeric, decimals)}${unit ?? ''}` : undefined, disabled: disabled, value: draft, placeholder: mixed ? 'Mixed' : placeholder, onFocus: (event) => event.currentTarget.select(), onChange: (event) => {
197
+ return (_jsxs("div", { "data-ui-field": "", className: cn('flex h-7 min-w-0 items-center rounded-control border border-line bg-surface-2 text-[13px] text-fg focus-within:border-accent focus-within:bg-surface', disabled && 'opacity-50', scrubbing && 'border-accent', className), children: [label !== undefined ? (_jsx("span", { "aria-hidden": "true", "data-scrub-for": inputId, title: title ?? 'Drag to change', onPointerDown: onPointerDown, onPointerMove: onPointerMove, onPointerUp: (event) => endScrub(event.currentTarget, true), onPointerCancel: (event) => endScrub(event.currentTarget, false), onLostPointerCapture: (event) => scrub.current && endScrub(event.currentTarget, true), className: cn('flex h-full min-w-6 shrink-0 select-none items-center justify-center px-1 text-[11px] text-fg-subtle touch-none', disabled ? 'cursor-default' : 'cursor-ew-resize hover:text-fg'), children: label })) : null, _jsx("input", { ref: inputRef, id: inputId, role: "spinbutton", inputMode: "decimal", autoComplete: "off", spellCheck: false, "aria-label": aria['aria-label'], "aria-valuenow": numeric, "aria-valuemin": min, "aria-valuemax": max, "aria-valuetext": mixed ? 'Mixed' : numeric !== undefined ? `${formatNumber(numeric, decimals)}${unit ?? ''}` : undefined, disabled: disabled, value: draft, placeholder: mixed ? 'Mixed' : placeholder, onFocus: (event) => event.currentTarget.select(), onChange: (event) => {
198
198
  editing.current = true;
199
199
  setDraft(event.target.value);
200
200
  }, onBlur: commitText, onKeyDown: onKeyDown, className: cn('h-full w-full min-w-0 bg-transparent tabular-nums outline-none placeholder:text-fg-subtle', label === undefined && 'pl-2', !unit && 'pr-2') }), unit ? _jsx("span", { className: "shrink-0 pr-2 text-[11px] text-fg-subtle", children: unit }) : null] }));