@openartifactsai/ui 0.0.0-stage → 0.23.0-rc.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/NOTICE +5 -0
- package/README.md +5 -2
- package/package.json +82 -4
- package/src/button.d.ts +30 -0
- package/src/button.js +29 -0
- package/src/clicks.d.ts +63 -0
- package/src/clicks.js +75 -0
- package/src/cn.d.ts +3 -0
- package/src/cn.js +5 -0
- package/src/color-picker.d.ts +98 -0
- package/src/color-picker.js +395 -0
- package/src/color.d.ts +150 -0
- package/src/color.js +543 -0
- package/src/crash-boundary.d.ts +29 -0
- package/src/crash-boundary.js +32 -0
- package/src/data/google-fonts.json +1775 -0
- package/src/drag-cover.d.ts +48 -0
- package/src/drag-cover.js +41 -0
- package/src/effect-editors.d.ts +48 -0
- package/src/effect-editors.js +230 -0
- package/src/effects.d.ts +118 -0
- package/src/effects.js +425 -0
- package/src/escape-to-popup.d.ts +53 -0
- package/src/escape-to-popup.js +78 -0
- package/src/field-announce.d.ts +28 -0
- package/src/field-announce.js +43 -0
- package/src/fields.d.ts +176 -0
- package/src/fields.js +377 -0
- package/src/focusables.d.ts +50 -0
- package/src/focusables.js +61 -0
- package/src/font-picker.d.ts +62 -0
- package/src/font-picker.js +242 -0
- package/src/fonts.d.ts +121 -0
- package/src/fonts.js +273 -0
- package/src/history-diff.d.ts +64 -0
- package/src/history-diff.js +151 -0
- package/src/history.d.ts +95 -0
- package/src/history.js +160 -0
- package/src/index.d.ts +38 -0
- package/src/index.js +38 -0
- package/src/key-chord.d.ts +74 -0
- package/src/key-chord.js +228 -0
- package/src/keys.d.ts +142 -0
- package/src/keys.js +172 -0
- package/src/layout.d.ts +74 -0
- package/src/layout.js +173 -0
- package/src/link-card-view.d.ts +97 -0
- package/src/link-card-view.js +175 -0
- package/src/link-card.d.ts +136 -0
- package/src/link-card.js +158 -0
- package/src/menu-shortcuts.d.ts +31 -0
- package/src/menu-shortcuts.js +91 -0
- package/src/menu.d.ts +120 -0
- package/src/menu.js +100 -0
- package/src/number-entry.d.ts +140 -0
- package/src/number-entry.js +202 -0
- package/src/number-limits.d.ts +2 -0
- package/src/number-limits.js +2 -0
- package/src/number-wheel.d.ts +29 -0
- package/src/number-wheel.js +27 -0
- package/src/numbers.d.ts +28 -0
- package/src/numbers.js +124 -0
- package/src/overlays.d.ts +71 -0
- package/src/overlays.js +131 -0
- package/src/pressed.d.ts +5 -0
- package/src/pressed.js +10 -0
- package/src/scroll.d.ts +24 -0
- package/src/scroll.js +111 -0
- package/src/scrub-number-field.d.ts +79 -0
- package/src/scrub-number-field.js +254 -0
- package/src/select-announce.d.ts +50 -0
- package/src/select-announce.js +71 -0
- package/src/slider-session.d.ts +43 -0
- package/src/slider-session.js +39 -0
- package/src/styles.css +96 -0
- package/src/tab-strip-layout.d.ts +76 -0
- package/src/tab-strip-layout.js +111 -0
- package/src/tab-strip.d.ts +73 -0
- package/src/tab-strip.js +498 -0
- package/src/text-area-view.d.ts +35 -0
- package/src/text-area-view.js +142 -0
- package/src/text-area.d.ts +38 -0
- package/src/text-area.js +51 -0
- package/src/theme.css +34 -0
- package/src/toast-fit.d.ts +21 -0
- package/src/toast-fit.js +30 -0
- package/src/toast-tone.d.ts +27 -0
- package/src/toast-tone.js +22 -0
- package/src/tooltip.d.ts +23 -0
- package/src/tooltip.js +18 -0
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
/**
|
|
3
|
+
* Multi-line text: `TextArea`, a box that can grow with its text, and
|
|
4
|
+
* `FieldTextArea`, the same with a label, a hint and an error under it.
|
|
5
|
+
*/
|
|
6
|
+
import { forwardRef, useCallback, useEffect, useId, useLayoutEffect, useRef } from 'react';
|
|
7
|
+
import { cn } from "./cn.js";
|
|
8
|
+
import { lineHeightPx, textAreaFit, typedDirection } from "./text-area.js";
|
|
9
|
+
const SIZE_CLASS = {
|
|
10
|
+
xs: 'text-[11px] leading-4',
|
|
11
|
+
sm: 'text-xs leading-4',
|
|
12
|
+
md: 'text-[13px] leading-5',
|
|
13
|
+
};
|
|
14
|
+
function pixels(value) {
|
|
15
|
+
return Number.parseFloat(value) || 0;
|
|
16
|
+
}
|
|
17
|
+
/** Sets the field's height to fit its text (see {@link textAreaFit}). */
|
|
18
|
+
function fitHeight(field, rows) {
|
|
19
|
+
const view = field.ownerDocument.defaultView;
|
|
20
|
+
if (!view)
|
|
21
|
+
return;
|
|
22
|
+
const style = view.getComputedStyle(field);
|
|
23
|
+
field.style.height = 'auto';
|
|
24
|
+
const fit = textAreaFit({
|
|
25
|
+
scrollHeight: field.scrollHeight,
|
|
26
|
+
lineHeight: lineHeightPx(style.lineHeight, style.fontSize),
|
|
27
|
+
padding: pixels(style.paddingTop) + pixels(style.paddingBottom),
|
|
28
|
+
border: pixels(style.borderTopWidth) + pixels(style.borderBottomWidth),
|
|
29
|
+
borderBox: style.boxSizing === 'border-box',
|
|
30
|
+
maxHeight: style.maxHeight === 'none' ? Infinity : pixels(style.maxHeight),
|
|
31
|
+
}, rows);
|
|
32
|
+
field.style.height = `${fit.height}px`;
|
|
33
|
+
field.style.overflowY = fit.scrolls ? 'auto' : 'hidden';
|
|
34
|
+
}
|
|
35
|
+
/** On a right-to-left page, the field runs the way its text starts (unless `dir` is given). */
|
|
36
|
+
function followTypedDirection(field) {
|
|
37
|
+
const direction = field.ownerDocument.documentElement.dir === 'rtl' ? typedDirection(field.value) : undefined;
|
|
38
|
+
if (direction)
|
|
39
|
+
field.setAttribute('dir', direction);
|
|
40
|
+
else
|
|
41
|
+
field.removeAttribute('dir');
|
|
42
|
+
}
|
|
43
|
+
function assignRef(ref, value) {
|
|
44
|
+
if (typeof ref === 'function')
|
|
45
|
+
ref(value);
|
|
46
|
+
else if (ref)
|
|
47
|
+
ref.current = value;
|
|
48
|
+
}
|
|
49
|
+
/** A multi-line text box. */
|
|
50
|
+
export const TextArea = forwardRef(function TextArea({ rows = 3, resize, autosize = false, size = 'md', fullWidth = true, bare = false, dir, className, value, onInput, ...rest }, ref) {
|
|
51
|
+
const own = useRef(null);
|
|
52
|
+
const setRef = useCallback((node) => {
|
|
53
|
+
own.current = node;
|
|
54
|
+
assignRef(ref, node);
|
|
55
|
+
}, [ref]);
|
|
56
|
+
const refresh = useCallback(() => {
|
|
57
|
+
const field = own.current;
|
|
58
|
+
if (!field)
|
|
59
|
+
return;
|
|
60
|
+
if (dir === undefined)
|
|
61
|
+
followTypedDirection(field);
|
|
62
|
+
if (autosize)
|
|
63
|
+
fitHeight(field, rows);
|
|
64
|
+
}, [autosize, rows, dir]);
|
|
65
|
+
// A value set from outside (or a prop that changes the measure).
|
|
66
|
+
useLayoutEffect(refresh, [refresh, value]);
|
|
67
|
+
useEffect(() => {
|
|
68
|
+
const field = own.current;
|
|
69
|
+
const view = field?.ownerDocument.defaultView;
|
|
70
|
+
if (!field || !view)
|
|
71
|
+
return undefined;
|
|
72
|
+
let frame = 0;
|
|
73
|
+
const later = () => {
|
|
74
|
+
view.cancelAnimationFrame(frame);
|
|
75
|
+
frame = view.requestAnimationFrame(refresh);
|
|
76
|
+
};
|
|
77
|
+
// A form reset puts the text back after the event.
|
|
78
|
+
const form = field.form;
|
|
79
|
+
form?.addEventListener('reset', later);
|
|
80
|
+
// The page's direction can change while the field is up.
|
|
81
|
+
const pageDirection = new view.MutationObserver(refresh);
|
|
82
|
+
pageDirection.observe(field.ownerDocument.documentElement, { attributes: true, attributeFilter: ['dir'] });
|
|
83
|
+
let width = field.offsetWidth;
|
|
84
|
+
const resized = new view.ResizeObserver(() => {
|
|
85
|
+
if (field.offsetWidth === width)
|
|
86
|
+
return;
|
|
87
|
+
width = field.offsetWidth;
|
|
88
|
+
later();
|
|
89
|
+
});
|
|
90
|
+
if (autosize)
|
|
91
|
+
resized.observe(field);
|
|
92
|
+
const fonts = field.ownerDocument.fonts;
|
|
93
|
+
if (autosize) {
|
|
94
|
+
fonts?.addEventListener('loadingdone', later);
|
|
95
|
+
void fonts?.ready.then(later);
|
|
96
|
+
}
|
|
97
|
+
return () => {
|
|
98
|
+
view.cancelAnimationFrame(frame);
|
|
99
|
+
form?.removeEventListener('reset', later);
|
|
100
|
+
pageDirection.disconnect();
|
|
101
|
+
resized.disconnect();
|
|
102
|
+
fonts?.removeEventListener('loadingdone', later);
|
|
103
|
+
};
|
|
104
|
+
}, [refresh, autosize]);
|
|
105
|
+
return (_jsx("textarea", { ref: setRef, "data-ui-field": "", rows: rows, dir: dir, value: value, className: cn('min-w-0 text-fg outline-none placeholder:text-fg-subtle', bare ? 'border-0 bg-transparent p-0' : 'rounded-control border border-line bg-surface-2 px-2 py-1 focus:border-accent focus:bg-surface', fullWidth ? 'w-full' : 'w-auto', (resize ?? (autosize ? 'none' : 'y')) === 'none' ? 'resize-none' : 'resize-y', SIZE_CLASS[size], className), onInput: (event) => {
|
|
106
|
+
onInput?.(event);
|
|
107
|
+
refresh();
|
|
108
|
+
}, ...rest }));
|
|
109
|
+
});
|
|
110
|
+
/** A labelled multi-line text box with an optional hint and error. */
|
|
111
|
+
export const FieldTextArea = forwardRef(function FieldTextArea({ label, hint, error, reserveErrorLines = 0, busy = false, id, disabled, onFocus, onBlur, ...rest }, ref) {
|
|
112
|
+
const made = useId();
|
|
113
|
+
const fieldId = id ?? made;
|
|
114
|
+
const hintId = `${fieldId}-hint`;
|
|
115
|
+
const errorId = `${fieldId}-error`;
|
|
116
|
+
const field = useRef(null);
|
|
117
|
+
const setRef = useCallback((node) => {
|
|
118
|
+
field.current = node;
|
|
119
|
+
assignRef(ref, node);
|
|
120
|
+
}, [ref]);
|
|
121
|
+
// Whether the field had the focus; a blur caused by going busy does not count.
|
|
122
|
+
const focused = useRef(false);
|
|
123
|
+
const busyNow = useRef(busy);
|
|
124
|
+
busyNow.current = busy;
|
|
125
|
+
useEffect(() => {
|
|
126
|
+
const node = field.current;
|
|
127
|
+
if (busy || !node || !focused.current)
|
|
128
|
+
return;
|
|
129
|
+
const active = node.ownerDocument.activeElement;
|
|
130
|
+
if (active === null || active === node.ownerDocument.body)
|
|
131
|
+
node.focus();
|
|
132
|
+
}, [busy]);
|
|
133
|
+
const described = [hint ? hintId : null, error ? errorId : null].filter(Boolean).join(' ');
|
|
134
|
+
return (_jsxs("div", { className: "flex min-w-0 flex-col gap-1", children: [_jsx("label", { htmlFor: fieldId, className: "text-xs text-fg-muted", children: label }), hint ? (_jsx("p", { id: hintId, className: "text-xs text-fg-subtle", children: hint })) : null, _jsx(TextArea, { ref: setRef, id: fieldId, disabled: disabled || busy, "aria-busy": busy || undefined, "aria-invalid": error ? true : undefined, "aria-describedby": described || undefined, onFocus: (event) => {
|
|
135
|
+
focused.current = true;
|
|
136
|
+
onFocus?.(event);
|
|
137
|
+
}, onBlur: (event) => {
|
|
138
|
+
if (!busyNow.current)
|
|
139
|
+
focused.current = false;
|
|
140
|
+
onBlur?.(event);
|
|
141
|
+
}, ...rest }), error || reserveErrorLines > 0 ? (_jsx("p", { id: errorId, className: "text-xs leading-4 text-danger", style: reserveErrorLines > 0 ? { minHeight: `${reserveErrorLines}rem` } : undefined, children: error })) : null] }));
|
|
142
|
+
});
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The rules behind the multi-line text field: which way typed text runs on a
|
|
3
|
+
* right-to-left page, and how tall a field that grows with its text is.
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* The direction typed text takes on a right-to-left page: that of its first
|
|
7
|
+
* letter, or of a direction mark (LRM, RLM, ALM) met before any letter.
|
|
8
|
+
* `undefined` while there is none, so the page's own direction holds.
|
|
9
|
+
*/
|
|
10
|
+
export declare function typedDirection(text: string): 'rtl' | 'ltr' | undefined;
|
|
11
|
+
/** A field's measures, px, as the browser reports them. */
|
|
12
|
+
export interface TextAreaMeasure {
|
|
13
|
+
/** The text's full height with the field's padding (`scrollHeight` with the height set to `auto`). */
|
|
14
|
+
scrollHeight: number;
|
|
15
|
+
lineHeight: number;
|
|
16
|
+
/** Top plus bottom padding. */
|
|
17
|
+
padding: number;
|
|
18
|
+
/** Top plus bottom border. */
|
|
19
|
+
border: number;
|
|
20
|
+
/** `box-sizing: border-box`: the height counts padding and border. */
|
|
21
|
+
borderBox: boolean;
|
|
22
|
+
/** The field's `max-height` (Infinity when none). */
|
|
23
|
+
maxHeight: number;
|
|
24
|
+
}
|
|
25
|
+
/** How tall a growing field is, and whether its text scrolls. */
|
|
26
|
+
export interface TextAreaFit {
|
|
27
|
+
/** The CSS height, px. */
|
|
28
|
+
height: number;
|
|
29
|
+
/** The text is taller than the field may grow: let it scroll. */
|
|
30
|
+
scrolls: boolean;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* The height of a field that grows with its text: never less than `rows`
|
|
34
|
+
* lines, never more than its `max-height`.
|
|
35
|
+
*/
|
|
36
|
+
export declare function textAreaFit(measure: TextAreaMeasure, rows: number): TextAreaFit;
|
|
37
|
+
/** A computed `line-height` in px; `normal` counts as 1.2 × the font size. */
|
|
38
|
+
export declare function lineHeightPx(lineHeight: string, fontSize: string): number;
|
package/src/text-area.js
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The rules behind the multi-line text field: which way typed text runs on a
|
|
3
|
+
* right-to-left page, and how tall a field that grows with its text is.
|
|
4
|
+
*/
|
|
5
|
+
/** Letters of the scripts written right to left (Hebrew through Arabic, their presentation forms and the RTL planes). */
|
|
6
|
+
const RTL_RANGES = [
|
|
7
|
+
[0x0590, 0x08ff],
|
|
8
|
+
[0xfb1d, 0xfdff],
|
|
9
|
+
[0xfe70, 0xfefc],
|
|
10
|
+
[0x10800, 0x10fff],
|
|
11
|
+
[0x1e800, 0x1efff],
|
|
12
|
+
];
|
|
13
|
+
const LEFT_TO_RIGHT_MARK = 0x200e;
|
|
14
|
+
const RIGHT_TO_LEFT_MARK = 0x200f;
|
|
15
|
+
const ARABIC_LETTER_MARK = 0x061c;
|
|
16
|
+
function isRtlLetter(code) {
|
|
17
|
+
return RTL_RANGES.some(([from, to]) => code >= from && code <= to);
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* The direction typed text takes on a right-to-left page: that of its first
|
|
21
|
+
* letter, or of a direction mark (LRM, RLM, ALM) met before any letter.
|
|
22
|
+
* `undefined` while there is none, so the page's own direction holds.
|
|
23
|
+
*/
|
|
24
|
+
export function typedDirection(text) {
|
|
25
|
+
for (const char of text) {
|
|
26
|
+
const code = char.codePointAt(0);
|
|
27
|
+
if (code === RIGHT_TO_LEFT_MARK || code === ARABIC_LETTER_MARK)
|
|
28
|
+
return 'rtl';
|
|
29
|
+
if (code === LEFT_TO_RIGHT_MARK)
|
|
30
|
+
return 'ltr';
|
|
31
|
+
if (/\p{L}/u.test(char))
|
|
32
|
+
return isRtlLetter(code) ? 'rtl' : 'ltr';
|
|
33
|
+
}
|
|
34
|
+
return undefined;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* The height of a field that grows with its text: never less than `rows`
|
|
38
|
+
* lines, never more than its `max-height`.
|
|
39
|
+
*/
|
|
40
|
+
export function textAreaFit(measure, rows) {
|
|
41
|
+
const frame = measure.borderBox ? measure.border : 0;
|
|
42
|
+
const floor = Math.max(1, rows) * measure.lineHeight + (measure.borderBox ? measure.padding + measure.border : 0);
|
|
43
|
+
const wanted = Math.max(floor, measure.scrollHeight - (measure.borderBox ? 0 : measure.padding) + frame);
|
|
44
|
+
const height = Math.min(wanted, measure.maxHeight);
|
|
45
|
+
return { height, scrolls: wanted > measure.maxHeight };
|
|
46
|
+
}
|
|
47
|
+
/** A computed `line-height` in px; `normal` counts as 1.2 × the font size. */
|
|
48
|
+
export function lineHeightPx(lineHeight, fontSize) {
|
|
49
|
+
const value = Number.parseFloat(lineHeight);
|
|
50
|
+
return Number.isFinite(value) ? value : 1.2 * (Number.parseFloat(fontSize) || 16);
|
|
51
|
+
}
|
package/src/theme.css
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Open Artifacts UI tokens for Tailwind CSS v4: the `dark` variant and the semantic names the components use
|
|
3
|
+
* (`bg-surface`, `text-fg-muted`, `border-line`…), each read from an `--open-artifacts-*` variable.
|
|
4
|
+
*
|
|
5
|
+
* `styles.css` imports it with Tailwind and gives the variables their light and dark values. An app with styles of
|
|
6
|
+
* its own that only builds the utilities the kit's components use imports this file instead and sets the variables
|
|
7
|
+
* itself.
|
|
8
|
+
*/
|
|
9
|
+
@custom-variant dark (&:where([data-theme="dark"], [data-theme="dark"] *));
|
|
10
|
+
|
|
11
|
+
@theme {
|
|
12
|
+
--font-sans: "Inter", ui-sans-serif, system-ui, -apple-system, "Segoe UI", sans-serif;
|
|
13
|
+
--font-mono: "JetBrains Mono", ui-monospace, SFMono-Regular, Menlo, monospace;
|
|
14
|
+
|
|
15
|
+
--color-canvas: var(--open-artifacts-canvas);
|
|
16
|
+
--color-surface: var(--open-artifacts-surface);
|
|
17
|
+
--color-surface-2: var(--open-artifacts-surface-2);
|
|
18
|
+
--color-hover: var(--open-artifacts-hover);
|
|
19
|
+
--color-line: var(--open-artifacts-line);
|
|
20
|
+
--color-line-strong: var(--open-artifacts-line-strong);
|
|
21
|
+
--color-fg: var(--open-artifacts-fg);
|
|
22
|
+
--color-fg-muted: var(--open-artifacts-fg-muted);
|
|
23
|
+
--color-fg-subtle: var(--open-artifacts-fg-subtle);
|
|
24
|
+
--color-accent: var(--open-artifacts-accent);
|
|
25
|
+
--color-accent-fg: var(--open-artifacts-accent-fg);
|
|
26
|
+
--color-accent-soft: var(--open-artifacts-accent-soft);
|
|
27
|
+
--color-danger: var(--open-artifacts-danger);
|
|
28
|
+
--color-warning: var(--open-artifacts-warning);
|
|
29
|
+
--color-success: var(--open-artifacts-success);
|
|
30
|
+
--color-select: var(--open-artifacts-select);
|
|
31
|
+
|
|
32
|
+
--radius-control: 6px;
|
|
33
|
+
--shadow-popup: 0 8px 24px -6px rgb(0 0 0 / 0.18), 0 0 0 1px var(--open-artifacts-line);
|
|
34
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* How tall a toast's text may grow so the whole column of toasts stays on
|
|
3
|
+
* screen. Short toasts keep their full height; the room they leave is
|
|
4
|
+
* shared out evenly among the long ones, whose text then scrolls.
|
|
5
|
+
*/
|
|
6
|
+
/** Room kept free between the top toast and the top of the window, px. */
|
|
7
|
+
export declare const TOAST_TOP_GAP_PX = 8;
|
|
8
|
+
/** The smallest height a toast's text is held to, px (3rem). */
|
|
9
|
+
export declare const TOAST_BODY_FLOOR_PX = 48;
|
|
10
|
+
/** One toast in the column, px. */
|
|
11
|
+
export interface ToastExtent {
|
|
12
|
+
/** The card around the text: padding and border. */
|
|
13
|
+
chrome: number;
|
|
14
|
+
/** The full height of the text, unscrolled. */
|
|
15
|
+
content: number;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* The height every toast's text is held to, px, or null when the column
|
|
19
|
+
* fits within `room` as it is. `between` is the space between two toasts.
|
|
20
|
+
*/
|
|
21
|
+
export declare function toastBodyLimit(toasts: readonly ToastExtent[], room: number, between: number, floor?: number): number | null;
|
package/src/toast-fit.js
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* How tall a toast's text may grow so the whole column of toasts stays on
|
|
3
|
+
* screen. Short toasts keep their full height; the room they leave is
|
|
4
|
+
* shared out evenly among the long ones, whose text then scrolls.
|
|
5
|
+
*/
|
|
6
|
+
/** Room kept free between the top toast and the top of the window, px. */
|
|
7
|
+
export const TOAST_TOP_GAP_PX = 8;
|
|
8
|
+
/** The smallest height a toast's text is held to, px (3rem). */
|
|
9
|
+
export const TOAST_BODY_FLOOR_PX = 48;
|
|
10
|
+
/**
|
|
11
|
+
* The height every toast's text is held to, px, or null when the column
|
|
12
|
+
* fits within `room` as it is. `between` is the space between two toasts.
|
|
13
|
+
*/
|
|
14
|
+
export function toastBodyLimit(toasts, room, between, floor = TOAST_BODY_FLOOR_PX) {
|
|
15
|
+
if (toasts.length === 0)
|
|
16
|
+
return null;
|
|
17
|
+
let left = room - between * (toasts.length - 1);
|
|
18
|
+
for (const toast of toasts)
|
|
19
|
+
left -= toast.chrome;
|
|
20
|
+
const contents = toasts.map((toast) => toast.content).sort((a, b) => a - b);
|
|
21
|
+
let uncapped = contents.length;
|
|
22
|
+
for (const content of contents) {
|
|
23
|
+
const share = left / uncapped;
|
|
24
|
+
if (content > share)
|
|
25
|
+
return Math.max(floor, Math.floor(share));
|
|
26
|
+
left -= content;
|
|
27
|
+
uncapped -= 1;
|
|
28
|
+
}
|
|
29
|
+
return null;
|
|
30
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The toasts that report a failure or a warning: which sign they carry and
|
|
3
|
+
* how long they stay.
|
|
4
|
+
*/
|
|
5
|
+
/** What such a toast says: one line, or a title with more under it. */
|
|
6
|
+
export type ToastWords = string | {
|
|
7
|
+
title: string;
|
|
8
|
+
description?: string;
|
|
9
|
+
};
|
|
10
|
+
/** The `type` of a toast that reports trouble. */
|
|
11
|
+
export type TroubleTone = 'error' | 'warning';
|
|
12
|
+
/** What the toast manager is given for one such toast. */
|
|
13
|
+
export interface TroubleToast {
|
|
14
|
+
title: string;
|
|
15
|
+
description?: string;
|
|
16
|
+
type: TroubleTone;
|
|
17
|
+
/** How long it stays, ms (0: until dismissed); absent: the usual time. */
|
|
18
|
+
timeout?: number;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* A failure that stays until dismissed (`durationMs` 0) waits on the person
|
|
22
|
+
* rather than reporting something that just went wrong, so it takes the
|
|
23
|
+
* warning sign; any other failure the error sign.
|
|
24
|
+
*/
|
|
25
|
+
export declare function failureTone(durationMs?: number): TroubleTone;
|
|
26
|
+
/** The toast manager's options for `words` in `tone`. */
|
|
27
|
+
export declare function troubleToast(words: ToastWords, tone: TroubleTone, durationMs?: number): TroubleToast;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The toasts that report a failure or a warning: which sign they carry and
|
|
3
|
+
* how long they stay.
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* A failure that stays until dismissed (`durationMs` 0) waits on the person
|
|
7
|
+
* rather than reporting something that just went wrong, so it takes the
|
|
8
|
+
* warning sign; any other failure the error sign.
|
|
9
|
+
*/
|
|
10
|
+
export function failureTone(durationMs) {
|
|
11
|
+
return durationMs === 0 ? 'warning' : 'error';
|
|
12
|
+
}
|
|
13
|
+
/** The toast manager's options for `words` in `tone`. */
|
|
14
|
+
export function troubleToast(words, tone, durationMs) {
|
|
15
|
+
const text = typeof words === 'string' ? { title: words } : words;
|
|
16
|
+
return {
|
|
17
|
+
title: text.title,
|
|
18
|
+
...(text.description !== undefined ? { description: text.description } : {}),
|
|
19
|
+
type: tone,
|
|
20
|
+
...(durationMs !== undefined ? { timeout: durationMs } : {}),
|
|
21
|
+
};
|
|
22
|
+
}
|
package/src/tooltip.d.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { MouseEvent, ReactElement, ReactNode } from 'react';
|
|
2
|
+
/** Props of {@link Tooltip}. */
|
|
3
|
+
export interface TooltipProps {
|
|
4
|
+
/** Tooltip text. */
|
|
5
|
+
label: ReactNode;
|
|
6
|
+
/** Optional keyboard shortcut, shown dimmed after the label. */
|
|
7
|
+
shortcut?: string;
|
|
8
|
+
/** Where the tooltip opens relative to its trigger. */
|
|
9
|
+
side?: 'top' | 'bottom' | 'left' | 'right';
|
|
10
|
+
/** The trigger element; it must accept a ref and DOM props. */
|
|
11
|
+
children: ReactElement<Record<string, unknown>>;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* A press on a tooltip leaves focus where it was: the tip floats over a
|
|
15
|
+
* field or text being edited, and a stray click on it must not end that.
|
|
16
|
+
*/
|
|
17
|
+
export declare function keepFocusOnTipPress(event: MouseEvent<HTMLElement>): void;
|
|
18
|
+
/** Wraps any element with a hover tooltip. Put one {@link TooltipProvider} near the app root. */
|
|
19
|
+
export declare function Tooltip({ label, shortcut, side, children }: TooltipProps): import("react/jsx-runtime").JSX.Element;
|
|
20
|
+
/** Shares hover delays between neighbouring tooltips. */
|
|
21
|
+
export declare function TooltipProvider({ children }: {
|
|
22
|
+
children: ReactNode;
|
|
23
|
+
}): import("react/jsx-runtime").JSX.Element;
|
package/src/tooltip.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { Tooltip as BaseTooltip } from '@base-ui/react/tooltip';
|
|
3
|
+
import { onApplePlatform, spokenShortcut } from "./key-chord.js";
|
|
4
|
+
/**
|
|
5
|
+
* A press on a tooltip leaves focus where it was: the tip floats over a
|
|
6
|
+
* field or text being edited, and a stray click on it must not end that.
|
|
7
|
+
*/
|
|
8
|
+
export function keepFocusOnTipPress(event) {
|
|
9
|
+
event.preventDefault();
|
|
10
|
+
}
|
|
11
|
+
/** Wraps any element with a hover tooltip. Put one {@link TooltipProvider} near the app root. */
|
|
12
|
+
export function Tooltip({ label, shortcut, side = 'bottom', children }) {
|
|
13
|
+
return (_jsxs(BaseTooltip.Root, { children: [_jsx(BaseTooltip.Trigger, { render: children }), _jsx(BaseTooltip.Portal, { children: _jsx(BaseTooltip.Positioner, { side: side, sideOffset: 6, className: "z-[1000]", children: _jsxs(BaseTooltip.Popup, { onMouseDown: keepFocusOnTipPress, className: "rounded-md bg-fg px-2 py-1 text-xs text-surface shadow-popup data-[starting-style]:opacity-0 data-[ending-style]:opacity-0 transition-opacity", children: [label, shortcut ? (_jsxs(_Fragment, { children: [_jsx("span", { "aria-hidden": "true", className: "ml-2 opacity-60", children: shortcut }), _jsx("span", { className: "sr-only", children: spokenShortcut(shortcut, onApplePlatform()) })] })) : null] }) }) })] }));
|
|
14
|
+
}
|
|
15
|
+
/** Shares hover delays between neighbouring tooltips. */
|
|
16
|
+
export function TooltipProvider({ children }) {
|
|
17
|
+
return (_jsx(BaseTooltip.Provider, { delay: 500, closeDelay: 0, children: children }));
|
|
18
|
+
}
|