@heybejs/ui 0.0.0-stage → 1.0.3
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 +21 -0
- package/README.md +32 -2
- package/dist/hooks/mergeProps.d.ts +1 -0
- package/dist/hooks/mergeProps.js +19 -0
- package/dist/hooks/useDismiss.d.ts +6 -0
- package/dist/hooks/useDismiss.js +34 -0
- package/dist/hooks/useListKeyboard.d.ts +9 -0
- package/dist/hooks/useListKeyboard.js +56 -0
- package/dist/hooks/useMergeRefs.d.ts +2 -0
- package/dist/hooks/useMergeRefs.js +18 -0
- package/dist/hooks/usePosition.d.ts +13 -0
- package/dist/hooks/usePosition.js +76 -0
- package/dist/hooks/useTooltipDelay.d.ts +5 -0
- package/dist/hooks/useTooltipDelay.js +28 -0
- package/dist/hooks/useTopLayer.d.ts +2 -0
- package/dist/hooks/useTopLayer.js +12 -0
- package/dist/index.d.ts +25 -0
- package/dist/index.js +25 -0
- package/dist/primitives/Combobox.d.ts +18 -0
- package/dist/primitives/Combobox.js +98 -0
- package/dist/primitives/Dialog.d.ts +10 -0
- package/dist/primitives/Dialog.js +48 -0
- package/dist/primitives/Menu.d.ts +22 -0
- package/dist/primitives/Menu.js +97 -0
- package/dist/primitives/Popover.d.ts +13 -0
- package/dist/primitives/Popover.js +47 -0
- package/dist/primitives/RadioGroup.d.ts +14 -0
- package/dist/primitives/RadioGroup.js +38 -0
- package/dist/primitives/Switch.d.ts +10 -0
- package/dist/primitives/Switch.js +6 -0
- package/dist/primitives/Tabs.d.ts +16 -0
- package/dist/primitives/Tabs.js +44 -0
- package/dist/primitives/Tooltip.d.ts +6 -0
- package/dist/primitives/Tooltip.js +59 -0
- package/dist/primitives/index.d.ts +8 -0
- package/dist/primitives/index.js +8 -0
- package/dist/state/useControlledState.d.ts +1 -0
- package/dist/state/useControlledState.js +12 -0
- package/dist/state/useListState.d.ts +12 -0
- package/dist/state/useListState.js +30 -0
- package/dist/state/useOverlayState.d.ts +10 -0
- package/dist/state/useOverlayState.js +8 -0
- package/dist/style.css +2 -0
- package/dist/styled/Badge.d.ts +12 -0
- package/dist/styled/Badge.js +10 -0
- package/dist/styled/Button.d.ts +7 -0
- package/dist/styled/Button.js +21 -0
- package/dist/styled/Checkbox.d.ts +5 -0
- package/dist/styled/Checkbox.js +7 -0
- package/dist/styled/CodeArea.d.ts +32 -0
- package/dist/styled/CodeArea.js +291 -0
- package/dist/styled/Combobox.d.ts +4 -0
- package/dist/styled/Combobox.js +18 -0
- package/dist/styled/Confirm.d.ts +15 -0
- package/dist/styled/Confirm.js +29 -0
- package/dist/styled/CopyButton.d.ts +5 -0
- package/dist/styled/CopyButton.js +26 -0
- package/dist/styled/Dialog.d.ts +6 -0
- package/dist/styled/Dialog.js +27 -0
- package/dist/styled/Disclosure.d.ts +10 -0
- package/dist/styled/Disclosure.js +13 -0
- package/dist/styled/EmptyState.d.ts +6 -0
- package/dist/styled/EmptyState.js +4 -0
- package/dist/styled/FieldShell.d.ts +13 -0
- package/dist/styled/FieldShell.js +7 -0
- package/dist/styled/FileField.d.ts +9 -0
- package/dist/styled/FileField.js +32 -0
- package/dist/styled/Menu.d.ts +4 -0
- package/dist/styled/Menu.js +19 -0
- package/dist/styled/NumberField.d.ts +3 -0
- package/dist/styled/NumberField.js +10 -0
- package/dist/styled/Popover.d.ts +3 -0
- package/dist/styled/Popover.js +10 -0
- package/dist/styled/RadioGroup.d.ts +4 -0
- package/dist/styled/RadioGroup.js +9 -0
- package/dist/styled/Section.d.ts +5 -0
- package/dist/styled/Section.js +6 -0
- package/dist/styled/Select.d.ts +3 -0
- package/dist/styled/Select.js +8 -0
- package/dist/styled/Switch.d.ts +4 -0
- package/dist/styled/Switch.js +8 -0
- package/dist/styled/Table.d.ts +19 -0
- package/dist/styled/Table.js +15 -0
- package/dist/styled/Tabs.d.ts +11 -0
- package/dist/styled/Tabs.js +5 -0
- package/dist/styled/TextField.d.ts +3 -0
- package/dist/styled/TextField.js +7 -0
- package/dist/styled/Toasts.d.ts +10 -0
- package/dist/styled/Toasts.js +29 -0
- package/dist/styled/Tooltip.d.ts +3 -0
- package/dist/styled/Tooltip.js +10 -0
- package/dist/styled/field.d.ts +16 -0
- package/dist/styled/field.js +19 -0
- package/dist/styled/glass.d.ts +1 -0
- package/dist/styled/glass.js +1 -0
- package/dist/styled/tv.d.ts +1 -0
- package/dist/styled/tv.js +2 -0
- package/dist/test-clock.d.ts +1 -0
- package/dist/test-clock.js +2 -0
- package/package.json +68 -3
- package/src/theme.css +173 -0
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { type FieldProps } from "./field.js";
|
|
2
|
+
type Language = "json" | "sql" | "html" | "javascript";
|
|
3
|
+
export interface CodeSuggestion {
|
|
4
|
+
from: number;
|
|
5
|
+
options: {
|
|
6
|
+
label: string;
|
|
7
|
+
detail?: string;
|
|
8
|
+
type?: string;
|
|
9
|
+
}[];
|
|
10
|
+
}
|
|
11
|
+
export type CodeSuggest = (text: string, pos: number) => CodeSuggestion | null;
|
|
12
|
+
interface CodeAreaProps extends FieldProps {
|
|
13
|
+
value: string;
|
|
14
|
+
onChange?: (value: string) => void;
|
|
15
|
+
hideLabel?: boolean;
|
|
16
|
+
rows?: number;
|
|
17
|
+
disabled?: boolean;
|
|
18
|
+
onRun?: () => void;
|
|
19
|
+
singleLine?: boolean;
|
|
20
|
+
readOnly?: boolean;
|
|
21
|
+
placeholder?: string;
|
|
22
|
+
language?: Language;
|
|
23
|
+
completions?: string[];
|
|
24
|
+
suggest?: CodeSuggest;
|
|
25
|
+
sqlSchema?: Record<string, string[]>;
|
|
26
|
+
}
|
|
27
|
+
export interface CodeAreaHandle {
|
|
28
|
+
insert(text: string): void;
|
|
29
|
+
focus(): void;
|
|
30
|
+
}
|
|
31
|
+
export declare const CodeArea: import("react").ForwardRefExoticComponent<CodeAreaProps & import("react").RefAttributes<CodeAreaHandle>>;
|
|
32
|
+
export {};
|
|
@@ -0,0 +1,291 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { forwardRef, useImperativeHandle, useLayoutEffect, useRef, } from "react";
|
|
3
|
+
import { Annotation, Compartment, EditorState, } from "@codemirror/state";
|
|
4
|
+
import { EditorView, drawSelection, highlightActiveLine, keymap, lineNumbers, placeholder as placeholderExtension, } from "@codemirror/view";
|
|
5
|
+
import { autocompletion, closeCompletion, completionKeymap, } from "@codemirror/autocomplete";
|
|
6
|
+
import { defaultKeymap, history, historyKeymap, indentWithTab, } from "@codemirror/commands";
|
|
7
|
+
import { HighlightStyle, bracketMatching, syntaxHighlighting, } from "@codemirror/language";
|
|
8
|
+
import { tags } from "@lezer/highlight";
|
|
9
|
+
import { json } from "@codemirror/lang-json";
|
|
10
|
+
import { SQLite, schemaCompletionSource, sql } from "@codemirror/lang-sql";
|
|
11
|
+
import { html } from "@codemirror/lang-html";
|
|
12
|
+
import { javascript } from "@codemirror/lang-javascript";
|
|
13
|
+
import { useFieldIds } from "./field.js";
|
|
14
|
+
import { FieldShell } from "./FieldShell.js";
|
|
15
|
+
// lang-sql does not tokenize a leading "_", so "_su" would complete to "__superusers".
|
|
16
|
+
const withLeadingUnderscores = (source) => async (context) => {
|
|
17
|
+
const result = await source(context);
|
|
18
|
+
if (!result)
|
|
19
|
+
return result;
|
|
20
|
+
let from = result.from;
|
|
21
|
+
while (from > 0 && context.state.sliceDoc(from - 1, from) === "_")
|
|
22
|
+
from--;
|
|
23
|
+
return { ...result, from };
|
|
24
|
+
};
|
|
25
|
+
const sqlExtension = (schema) => {
|
|
26
|
+
const support = sql({ dialect: SQLite });
|
|
27
|
+
return [
|
|
28
|
+
support,
|
|
29
|
+
support.language.data.of({
|
|
30
|
+
autocomplete: withLeadingUnderscores(schemaCompletionSource({ dialect: SQLite, schema })),
|
|
31
|
+
}),
|
|
32
|
+
];
|
|
33
|
+
};
|
|
34
|
+
const languageExtension = (language, sqlSchema) => {
|
|
35
|
+
if (language === "sql")
|
|
36
|
+
return sqlExtension(sqlSchema);
|
|
37
|
+
if (language === "json")
|
|
38
|
+
return json();
|
|
39
|
+
if (language === "html")
|
|
40
|
+
return html();
|
|
41
|
+
if (language === "javascript")
|
|
42
|
+
return javascript();
|
|
43
|
+
return [];
|
|
44
|
+
};
|
|
45
|
+
const highlight = HighlightStyle.define([
|
|
46
|
+
{ tag: [tags.keyword, tags.operatorKeyword], color: "var(--cm-keyword)" },
|
|
47
|
+
{ tag: [tags.string, tags.special(tags.string)], color: "var(--cm-string)" },
|
|
48
|
+
{ tag: [tags.number, tags.bool, tags.null], color: "var(--cm-number)" },
|
|
49
|
+
{ tag: [tags.propertyName, tags.attributeName], color: "var(--cm-name)" },
|
|
50
|
+
{ tag: [tags.tagName, tags.typeName], color: "var(--cm-tag)" },
|
|
51
|
+
{ tag: [tags.comment, tags.meta], color: "var(--cm-comment)" },
|
|
52
|
+
{ tag: [tags.name, tags.variableName], color: "var(--cm-variable)" },
|
|
53
|
+
{
|
|
54
|
+
tag: [tags.function(tags.variableName), tags.standard(tags.name)],
|
|
55
|
+
color: "var(--cm-function)",
|
|
56
|
+
},
|
|
57
|
+
{
|
|
58
|
+
tag: [tags.operator, tags.compareOperator, tags.logicOperator],
|
|
59
|
+
color: "var(--cm-operator)",
|
|
60
|
+
},
|
|
61
|
+
{
|
|
62
|
+
tag: [tags.bracket, tags.punctuation, tags.angleBracket],
|
|
63
|
+
color: "var(--cm-punctuation)",
|
|
64
|
+
},
|
|
65
|
+
]);
|
|
66
|
+
const theme = EditorView.theme({
|
|
67
|
+
"&": { fontSize: "0.875rem", backgroundColor: "transparent" },
|
|
68
|
+
"&.cm-focused": { outline: "none" },
|
|
69
|
+
".cm-scroller": { fontFamily: "var(--hb-font-mono)" },
|
|
70
|
+
".cm-gutters": {
|
|
71
|
+
backgroundColor: "transparent",
|
|
72
|
+
color: "var(--cm-comment)",
|
|
73
|
+
border: "none",
|
|
74
|
+
},
|
|
75
|
+
".cm-activeLine, .cm-activeLineGutter": { backgroundColor: "transparent" },
|
|
76
|
+
".cm-selectionBackground": { backgroundColor: "var(--hb-accent-soft)" },
|
|
77
|
+
".cm-tooltip": {
|
|
78
|
+
backgroundColor: "var(--hb-surface-raised)",
|
|
79
|
+
color: "var(--hb-text)",
|
|
80
|
+
border: "1px solid var(--hb-line-strong)",
|
|
81
|
+
borderRadius: "var(--hb-radius)",
|
|
82
|
+
boxShadow: "var(--hb-shadow-overlay)",
|
|
83
|
+
overflow: "hidden",
|
|
84
|
+
},
|
|
85
|
+
".cm-tooltip.cm-tooltip-autocomplete > ul": {
|
|
86
|
+
fontFamily: "var(--hb-font-mono)",
|
|
87
|
+
padding: "4px",
|
|
88
|
+
},
|
|
89
|
+
".cm-tooltip.cm-tooltip-autocomplete > ul > li": {
|
|
90
|
+
borderRadius: "var(--hb-radius-control)",
|
|
91
|
+
padding: "3px 8px",
|
|
92
|
+
},
|
|
93
|
+
".cm-tooltip-autocomplete ul li[aria-selected]": {
|
|
94
|
+
backgroundColor: "var(--hb-accent)",
|
|
95
|
+
color: "var(--hb-accent-fg)",
|
|
96
|
+
},
|
|
97
|
+
".cm-placeholder": { color: "var(--hb-text-faint)" },
|
|
98
|
+
".cm-completionMatchedText": { textDecoration: "none", fontWeight: "600" },
|
|
99
|
+
});
|
|
100
|
+
const external = Annotation.define();
|
|
101
|
+
const lineHeightRem = 1.25;
|
|
102
|
+
export const CodeArea = forwardRef(function CodeArea({ label, value, onChange, hideLabel, error, hint, rows = 6, disabled, onRun, singleLine, readOnly, placeholder, language, completions, suggest, sqlSchema, }, ref) {
|
|
103
|
+
const ids = useFieldIds({ error, hint });
|
|
104
|
+
const host = useRef(null);
|
|
105
|
+
const view = useRef(null);
|
|
106
|
+
const latest = useRef({ onChange, onRun, completions, suggest });
|
|
107
|
+
latest.current = { onChange, onRun, completions, suggest };
|
|
108
|
+
const schemaKey = JSON.stringify(sqlSchema);
|
|
109
|
+
const compartments = useRef({
|
|
110
|
+
language: new Compartment(),
|
|
111
|
+
editable: new Compartment(),
|
|
112
|
+
attributes: new Compartment(),
|
|
113
|
+
height: new Compartment(),
|
|
114
|
+
});
|
|
115
|
+
const editable = (off, locked) => [
|
|
116
|
+
EditorView.editable.of(!off),
|
|
117
|
+
EditorState.readOnly.of(!!off || !!locked),
|
|
118
|
+
];
|
|
119
|
+
const attributes = () => EditorView.contentAttributes.of(Object.fromEntries(Object.entries({ ...ids.aria, "aria-label": label }).filter(([, v]) => v !== undefined)));
|
|
120
|
+
const suggested = (context, suggest) => {
|
|
121
|
+
const result = suggest(context.state.doc.toString(), context.pos);
|
|
122
|
+
if (!result)
|
|
123
|
+
return null;
|
|
124
|
+
const before = context.state.sliceDoc(Math.max(0, context.pos - 1), context.pos);
|
|
125
|
+
const spaced = singleLine && (before === "" || before === " ");
|
|
126
|
+
if (result.from === context.pos && !context.explicit && !spaced)
|
|
127
|
+
return null;
|
|
128
|
+
return { from: result.from, options: result.options };
|
|
129
|
+
};
|
|
130
|
+
const words = (context) => {
|
|
131
|
+
const suggest = latest.current.suggest;
|
|
132
|
+
if (suggest)
|
|
133
|
+
return suggested(context, suggest);
|
|
134
|
+
const list = latest.current.completions;
|
|
135
|
+
if (!list?.length)
|
|
136
|
+
return null;
|
|
137
|
+
const options = list.map((label) => ({ label, type: "variable" }));
|
|
138
|
+
const word = context.matchBefore(/[\w@.]+/);
|
|
139
|
+
if (word)
|
|
140
|
+
return { from: word.from, options, validFor: /^[\w@.]*$/ };
|
|
141
|
+
const operator = context.matchBefore(/[!=<>~?&|]+/);
|
|
142
|
+
if (operator)
|
|
143
|
+
return { from: operator.from, options, validFor: /^[!=<>~?&|]*$/ };
|
|
144
|
+
const before = context.state.sliceDoc(Math.max(0, context.pos - 1), context.pos);
|
|
145
|
+
if (context.explicit || (singleLine && (before === "" || before === " ")))
|
|
146
|
+
return { from: context.pos, options, validFor: /^[\w@.]*$/ };
|
|
147
|
+
return null;
|
|
148
|
+
};
|
|
149
|
+
const height = () => readOnly
|
|
150
|
+
? EditorView.theme({
|
|
151
|
+
"&": { maxHeight: "16rem" },
|
|
152
|
+
".cm-scroller": { overflow: "auto" },
|
|
153
|
+
".cm-content": { padding: "8px 0" },
|
|
154
|
+
".cm-line": { padding: "0 12px" },
|
|
155
|
+
".cm-cursor": { display: "none" },
|
|
156
|
+
})
|
|
157
|
+
: singleLine
|
|
158
|
+
? EditorView.theme({
|
|
159
|
+
".cm-content": { padding: "7px 10px", minHeight: "0" },
|
|
160
|
+
".cm-line": { padding: "0" },
|
|
161
|
+
".cm-scroller": { overflowY: "hidden" },
|
|
162
|
+
})
|
|
163
|
+
: EditorView.theme({
|
|
164
|
+
".cm-content, .cm-gutter": {
|
|
165
|
+
minHeight: `${rows * lineHeightRem}rem`,
|
|
166
|
+
},
|
|
167
|
+
});
|
|
168
|
+
useLayoutEffect(() => {
|
|
169
|
+
const c = compartments.current;
|
|
170
|
+
const instance = new EditorView({
|
|
171
|
+
parent: host.current,
|
|
172
|
+
state: EditorState.create({
|
|
173
|
+
doc: value,
|
|
174
|
+
extensions: [
|
|
175
|
+
singleLine || readOnly ? [] : lineNumbers(),
|
|
176
|
+
placeholder ? placeholderExtension(placeholder) : [],
|
|
177
|
+
history(),
|
|
178
|
+
drawSelection(),
|
|
179
|
+
highlightActiveLine(),
|
|
180
|
+
bracketMatching(),
|
|
181
|
+
autocompletion({ icons: false }),
|
|
182
|
+
EditorState.languageData.of(() => [{ autocomplete: words }]),
|
|
183
|
+
syntaxHighlighting(highlight),
|
|
184
|
+
singleLine
|
|
185
|
+
? EditorState.transactionFilter.of((tr) => {
|
|
186
|
+
if (tr.newDoc.lines === 1)
|
|
187
|
+
return tr;
|
|
188
|
+
return [
|
|
189
|
+
{
|
|
190
|
+
changes: {
|
|
191
|
+
from: 0,
|
|
192
|
+
to: tr.startState.doc.length,
|
|
193
|
+
insert: tr.newDoc.toString().replaceAll("\n", " "),
|
|
194
|
+
},
|
|
195
|
+
selection: tr.newSelection,
|
|
196
|
+
},
|
|
197
|
+
];
|
|
198
|
+
})
|
|
199
|
+
: readOnly
|
|
200
|
+
? []
|
|
201
|
+
: EditorView.lineWrapping,
|
|
202
|
+
theme,
|
|
203
|
+
keymap.of([
|
|
204
|
+
{
|
|
205
|
+
key: "Mod-Enter",
|
|
206
|
+
run: (v) => {
|
|
207
|
+
if (!latest.current.onRun)
|
|
208
|
+
return false;
|
|
209
|
+
closeCompletion(v);
|
|
210
|
+
latest.current.onRun();
|
|
211
|
+
return true;
|
|
212
|
+
},
|
|
213
|
+
},
|
|
214
|
+
...completionKeymap,
|
|
215
|
+
{
|
|
216
|
+
key: "Enter",
|
|
217
|
+
run: (v) => {
|
|
218
|
+
if (!singleLine)
|
|
219
|
+
return false;
|
|
220
|
+
// the suggestion list would cover the run's error message
|
|
221
|
+
closeCompletion(v);
|
|
222
|
+
latest.current.onRun?.();
|
|
223
|
+
return true;
|
|
224
|
+
},
|
|
225
|
+
},
|
|
226
|
+
indentWithTab,
|
|
227
|
+
...defaultKeymap,
|
|
228
|
+
...historyKeymap,
|
|
229
|
+
]),
|
|
230
|
+
c.language.of(languageExtension(language, sqlSchema)),
|
|
231
|
+
c.editable.of(editable(disabled, readOnly)),
|
|
232
|
+
c.attributes.of(attributes()),
|
|
233
|
+
c.height.of(height()),
|
|
234
|
+
EditorView.updateListener.of((u) => {
|
|
235
|
+
if (u.docChanged &&
|
|
236
|
+
!u.transactions.some((t) => t.annotation(external)))
|
|
237
|
+
latest.current.onChange?.(u.state.doc.toString());
|
|
238
|
+
}),
|
|
239
|
+
],
|
|
240
|
+
}),
|
|
241
|
+
});
|
|
242
|
+
view.current = instance;
|
|
243
|
+
return () => {
|
|
244
|
+
instance.destroy();
|
|
245
|
+
view.current = null;
|
|
246
|
+
};
|
|
247
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
248
|
+
}, []);
|
|
249
|
+
useLayoutEffect(() => {
|
|
250
|
+
const v = view.current;
|
|
251
|
+
if (!v || v.state.doc.toString() === value)
|
|
252
|
+
return;
|
|
253
|
+
v.dispatch({
|
|
254
|
+
changes: { from: 0, to: v.state.doc.length, insert: value },
|
|
255
|
+
annotations: external.of(true),
|
|
256
|
+
});
|
|
257
|
+
}, [value]);
|
|
258
|
+
useLayoutEffect(() => {
|
|
259
|
+
const c = compartments.current;
|
|
260
|
+
view.current?.dispatch({
|
|
261
|
+
effects: [
|
|
262
|
+
c.language.reconfigure(languageExtension(language, sqlSchema)),
|
|
263
|
+
c.editable.reconfigure(editable(disabled, readOnly)),
|
|
264
|
+
c.attributes.reconfigure(attributes()),
|
|
265
|
+
c.height.reconfigure(height()),
|
|
266
|
+
],
|
|
267
|
+
});
|
|
268
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
269
|
+
}, [
|
|
270
|
+
language,
|
|
271
|
+
schemaKey,
|
|
272
|
+
disabled,
|
|
273
|
+
readOnly,
|
|
274
|
+
rows,
|
|
275
|
+
singleLine,
|
|
276
|
+
label,
|
|
277
|
+
ids.aria["aria-invalid"],
|
|
278
|
+
ids.aria["aria-describedby"],
|
|
279
|
+
]);
|
|
280
|
+
useImperativeHandle(ref, () => ({
|
|
281
|
+
insert(text) {
|
|
282
|
+
const v = view.current;
|
|
283
|
+
if (!v)
|
|
284
|
+
return;
|
|
285
|
+
v.dispatch(v.state.replaceSelection(text));
|
|
286
|
+
v.focus();
|
|
287
|
+
},
|
|
288
|
+
focus: () => view.current?.focus(),
|
|
289
|
+
}));
|
|
290
|
+
return (_jsx(FieldShell, { ...ids, label: label, hideLabel: hideLabel, error: error, hint: hint, children: _jsx("div", { ref: host, className: `w-full rounded-control border border-field-line bg-field focus-within:outline-2 focus-within:outline-focus ${disabled ? "opacity-60" : ""}` }) }));
|
|
291
|
+
});
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { ComponentProps } from "react";
|
|
2
|
+
import { Combobox as ComboboxPrimitive } from "../primitives/Combobox.js";
|
|
3
|
+
export type { ComboboxOption } from "../primitives/Combobox.js";
|
|
4
|
+
export declare function Combobox({ className, listClassName, ...rest }: ComponentProps<typeof ComboboxPrimitive>): import("react").JSX.Element;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { Combobox as ComboboxPrimitive } from "../primitives/Combobox.js";
|
|
3
|
+
import { glass } from "./glass.js";
|
|
4
|
+
import { inputClass } from "./field.js";
|
|
5
|
+
import { tv } from "./tv.js";
|
|
6
|
+
const input = tv({ base: inputClass });
|
|
7
|
+
const list = tv({
|
|
8
|
+
base: [
|
|
9
|
+
glass,
|
|
10
|
+
"z-50 rounded-base p-1 text-sm text-text",
|
|
11
|
+
"[&_[role=option]]:cursor-default [&_[role=option]]:rounded-control [&_[role=option]]:px-2.5 [&_[role=option]]:py-1.5",
|
|
12
|
+
"[&_[data-active]]:bg-hover [&_[aria-selected=true]]:text-accent-text [&_[aria-disabled=true]]:opacity-50",
|
|
13
|
+
"[&_[data-empty]]:px-2.5 [&_[data-empty]]:py-1.5 [&_[data-empty]]:text-muted",
|
|
14
|
+
],
|
|
15
|
+
});
|
|
16
|
+
export function Combobox({ className, listClassName, ...rest }) {
|
|
17
|
+
return (_jsx("div", { className: "flex flex-col gap-1.5 [&>label]:text-sm [&>label]:font-medium [&>label]:text-text", children: _jsx(ComboboxPrimitive, { ...rest, className: input({ className }), listClassName: list({ className: listClassName }) }) }));
|
|
18
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { type ReactNode } from "react";
|
|
2
|
+
export interface ConfirmOptions {
|
|
3
|
+
title: string;
|
|
4
|
+
message: string;
|
|
5
|
+
confirmLabel: string;
|
|
6
|
+
cancelLabel?: string;
|
|
7
|
+
danger?: boolean;
|
|
8
|
+
typeToConfirm?: string;
|
|
9
|
+
}
|
|
10
|
+
type Confirm = (o: ConfirmOptions) => Promise<boolean>;
|
|
11
|
+
export declare function ConfirmProvider({ children }: {
|
|
12
|
+
children: ReactNode;
|
|
13
|
+
}): import("react").JSX.Element;
|
|
14
|
+
export declare function useConfirm(): Confirm;
|
|
15
|
+
export {};
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { Fragment as _Fragment, jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { createContext, useCallback, useContext, useRef, useState, } from "react";
|
|
3
|
+
import { Button } from "./Button.js";
|
|
4
|
+
import { Dialog } from "./Dialog.js";
|
|
5
|
+
import { TextField } from "./TextField.js";
|
|
6
|
+
const ConfirmContext = createContext(null);
|
|
7
|
+
export function ConfirmProvider({ children }) {
|
|
8
|
+
const [options, setOptions] = useState(null);
|
|
9
|
+
const [typed, setTyped] = useState("");
|
|
10
|
+
const resolver = useRef(() => { });
|
|
11
|
+
const confirm = useCallback((o) => new Promise((resolve) => {
|
|
12
|
+
resolver.current(false);
|
|
13
|
+
resolver.current = resolve;
|
|
14
|
+
setTyped("");
|
|
15
|
+
setOptions(o);
|
|
16
|
+
}), []);
|
|
17
|
+
const settle = (ok) => {
|
|
18
|
+
resolver.current(ok);
|
|
19
|
+
setOptions(null);
|
|
20
|
+
};
|
|
21
|
+
const blocked = !!options?.typeToConfirm && typed !== options.typeToConfirm;
|
|
22
|
+
return (_jsxs(ConfirmContext.Provider, { value: confirm, children: [children, _jsxs(Dialog, { open: !!options, onClose: () => settle(false), title: options?.title ?? "", footer: _jsxs(_Fragment, { children: [_jsx(Button, { onClick: () => settle(false), children: options?.cancelLabel ?? "Cancel" }), _jsx(Button, { variant: options?.danger ? "danger" : "primary", disabled: blocked, onClick: () => settle(true), children: options?.confirmLabel })] }), children: [_jsx("p", { className: "text-sm text-text", children: options?.message }), options?.typeToConfirm && (_jsx("div", { className: "mt-3", children: _jsx(TextField, { label: `Type "${options.typeToConfirm}" to confirm`, value: typed, onChange: (e) => setTyped(e.target.value) }) }))] })] }));
|
|
23
|
+
}
|
|
24
|
+
export function useConfirm() {
|
|
25
|
+
const confirm = useContext(ConfirmContext);
|
|
26
|
+
if (!confirm)
|
|
27
|
+
throw new Error("ConfirmProvider is missing");
|
|
28
|
+
return confirm;
|
|
29
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { Fragment as _Fragment, jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { Check, Copy, TriangleAlert } from "lucide-react";
|
|
3
|
+
import { useEffect, useRef, useState } from "react";
|
|
4
|
+
import { Button } from "./Button.js";
|
|
5
|
+
import { Tooltip } from "./Tooltip.js";
|
|
6
|
+
const message = { idle: "Copy", copied: "Copied", failed: "Could not copy" };
|
|
7
|
+
const icons = { idle: Copy, copied: Check, failed: TriangleAlert };
|
|
8
|
+
export function CopyButton({ text, label = "Copy", showLabel = false, }) {
|
|
9
|
+
const [state, setState] = useState("idle");
|
|
10
|
+
const timer = useRef(undefined);
|
|
11
|
+
useEffect(() => () => clearTimeout(timer.current), []);
|
|
12
|
+
const copy = async () => {
|
|
13
|
+
let next = "copied";
|
|
14
|
+
try {
|
|
15
|
+
await navigator.clipboard.writeText(text);
|
|
16
|
+
}
|
|
17
|
+
catch {
|
|
18
|
+
next = "failed";
|
|
19
|
+
}
|
|
20
|
+
setState(next);
|
|
21
|
+
clearTimeout(timer.current);
|
|
22
|
+
timer.current = setTimeout(() => setState("idle"), 2000);
|
|
23
|
+
};
|
|
24
|
+
const Icon = icons[state];
|
|
25
|
+
return (_jsxs(_Fragment, { children: [_jsx(Tooltip, { content: message[state], children: _jsx(Button, { variant: showLabel ? undefined : "ghost", size: showLabel ? undefined : "sm", "aria-label": showLabel ? undefined : label, onClick: copy, icon: _jsx(Icon, { size: 15, strokeWidth: 1.75, className: state === "failed" ? "text-danger-text" : undefined }), children: showLabel && label }) }), _jsx("span", { role: "status", className: "sr-only", children: state === "idle" ? "" : message[state] })] }));
|
|
26
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { ComponentProps } from "react";
|
|
2
|
+
import { Dialog as DialogPrimitive } from "../primitives/Dialog.js";
|
|
3
|
+
export declare function Dialog({ variant, size, ...rest }: Omit<ComponentProps<typeof DialogPrimitive>, "className"> & {
|
|
4
|
+
variant?: "center" | "side";
|
|
5
|
+
size?: "md" | "lg";
|
|
6
|
+
}): import("react").JSX.Element;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { X } from "lucide-react";
|
|
3
|
+
import { Dialog as DialogPrimitive } from "../primitives/Dialog.js";
|
|
4
|
+
import { tv } from "./tv.js";
|
|
5
|
+
const dialog = tv({
|
|
6
|
+
base: [
|
|
7
|
+
"border border-line bg-raised p-0 text-text shadow-overlay backdrop:bg-scrim",
|
|
8
|
+
"[&_[data-part=content]]:flex [&_[data-part=content]]:max-h-dvh [&_[data-part=content]]:flex-col",
|
|
9
|
+
"[&_[data-part=header]]:flex [&_[data-part=header]]:items-center [&_[data-part=header]]:justify-between [&_[data-part=header]]:gap-2 [&_[data-part=header]]:border-b [&_[data-part=header]]:border-line [&_[data-part=header]]:py-3 [&_[data-part=header]]:pl-4 [&_[data-part=header]]:pr-3",
|
|
10
|
+
"[&_[data-part=title]]:text-base [&_[data-part=title]]:font-semibold",
|
|
11
|
+
"[&_[data-part=close]]:inline-flex [&_[data-part=close]]:size-8 [&_[data-part=close]]:items-center [&_[data-part=close]]:justify-center [&_[data-part=close]]:rounded-control [&_[data-part=close]]:text-muted [&_[data-part=close]:hover]:bg-hover [&_[data-part=close]:hover]:text-text [&_[data-part=close]:focus-visible]:outline-2 [&_[data-part=close]:focus-visible]:outline-focus",
|
|
12
|
+
"[&_[data-part=body]]:flex-1 [&_[data-part=body]]:overflow-auto [&_[data-part=body]]:p-4",
|
|
13
|
+
"[&_[data-part=footer]]:flex [&_[data-part=footer]]:flex-wrap [&_[data-part=footer]]:justify-end [&_[data-part=footer]]:gap-2 [&_[data-part=footer]]:border-t [&_[data-part=footer]]:border-line [&_[data-part=footer]]:p-4",
|
|
14
|
+
],
|
|
15
|
+
variants: {
|
|
16
|
+
variant: {
|
|
17
|
+
center: "m-auto w-full max-w-lg rounded-base",
|
|
18
|
+
side: "ml-auto h-dvh max-h-dvh w-full max-w-xl [&_[data-part=content]]:h-full",
|
|
19
|
+
},
|
|
20
|
+
size: { md: "", lg: "" },
|
|
21
|
+
},
|
|
22
|
+
compoundVariants: [{ variant: "center", size: "lg", class: "max-w-3xl" }],
|
|
23
|
+
defaultVariants: { variant: "center", size: "md" },
|
|
24
|
+
});
|
|
25
|
+
export function Dialog({ variant, size, ...rest }) {
|
|
26
|
+
return (_jsx(DialogPrimitive, { ...rest, closeIcon: _jsx(X, { size: 15, strokeWidth: 1.75, "aria-hidden": true }), className: dialog({ variant, size }) }));
|
|
27
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { type ReactNode } from "react";
|
|
2
|
+
export declare function Disclosure({ title, icon, badge, defaultOpen, open: controlledOpen, onOpenChange, children, }: {
|
|
3
|
+
title: string;
|
|
4
|
+
icon?: ReactNode;
|
|
5
|
+
badge?: ReactNode;
|
|
6
|
+
defaultOpen?: boolean;
|
|
7
|
+
open?: boolean;
|
|
8
|
+
onOpenChange?(open: boolean): void;
|
|
9
|
+
children: ReactNode;
|
|
10
|
+
}): import("react").JSX.Element;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { ChevronRight } from "lucide-react";
|
|
3
|
+
import { useId, useState } from "react";
|
|
4
|
+
export function Disclosure({ title, icon, badge, defaultOpen = false, open: controlledOpen, onOpenChange, children, }) {
|
|
5
|
+
const [inner, setInner] = useState(defaultOpen);
|
|
6
|
+
const open = controlledOpen ?? inner;
|
|
7
|
+
const toggle = () => {
|
|
8
|
+
setInner(!open);
|
|
9
|
+
onOpenChange?.(!open);
|
|
10
|
+
};
|
|
11
|
+
const bodyId = useId();
|
|
12
|
+
return (_jsxs("div", { className: "rounded-base border border-line", children: [_jsxs("button", { type: "button", "aria-expanded": open, "aria-controls": bodyId, onClick: toggle, className: "flex w-full items-center gap-2 rounded-base px-3 py-2 text-left text-sm font-medium text-text hover:bg-hover focus-visible:outline-2 focus-visible:outline-focus", children: [_jsx(ChevronRight, { size: 15, strokeWidth: 1.75, "aria-hidden": true, className: `text-muted transition-transform duration-(--hb-duration) motion-reduce:transition-none ${open ? "rotate-90" : ""}` }), icon, _jsx("span", { children: title }), badge != null && _jsx("span", { className: "ml-auto", children: badge })] }), _jsx("div", { id: bodyId, hidden: !open, className: "border-t border-line p-3", children: children })] }));
|
|
13
|
+
}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
export function EmptyState({ title, icon, children, }) {
|
|
3
|
+
return (_jsxs("div", { className: "flex flex-col items-center gap-2 px-8 py-12 text-center text-sm text-muted", children: [icon && (_jsx("span", { className: "mb-1 flex size-9 items-center justify-center rounded-full bg-hover text-muted", children: icon })), _jsx("p", { className: "font-medium text-text", children: title }), children] }));
|
|
4
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { ReactNode } from "react";
|
|
2
|
+
import type { FieldProps } from "./field.js";
|
|
3
|
+
export declare function FieldMessages({ errorId, hintId, error, hint, }: Pick<FieldProps, "error" | "hint"> & {
|
|
4
|
+
errorId: string;
|
|
5
|
+
hintId: string;
|
|
6
|
+
}): import("react").JSX.Element;
|
|
7
|
+
export declare function FieldShell({ id, errorId, hintId, label, error, hint, hideLabel, children, }: FieldProps & {
|
|
8
|
+
hideLabel?: boolean;
|
|
9
|
+
id: string;
|
|
10
|
+
errorId: string;
|
|
11
|
+
hintId: string;
|
|
12
|
+
children: ReactNode;
|
|
13
|
+
}): import("react").JSX.Element;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { Fragment as _Fragment, jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
export function FieldMessages({ errorId, hintId, error, hint, }) {
|
|
3
|
+
return (_jsxs(_Fragment, { children: [hint && (_jsx("p", { id: hintId, className: "text-xs text-muted", children: hint })), error && (_jsx("p", { id: errorId, className: "text-xs text-danger-text", children: error }))] }));
|
|
4
|
+
}
|
|
5
|
+
export function FieldShell({ id, errorId, hintId, label, error, hint, hideLabel, children, }) {
|
|
6
|
+
return (_jsxs("div", { className: "flex flex-col gap-1", children: [_jsx("label", { htmlFor: id, className: hideLabel ? "sr-only" : "text-sm font-medium text-text", children: label }), children, _jsx(FieldMessages, { errorId: errorId, hintId: hintId, error: error, hint: hint })] }));
|
|
7
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { type FieldProps } from "./field.js";
|
|
2
|
+
export type FileFieldProps = FieldProps & {
|
|
3
|
+
accept?: string;
|
|
4
|
+
multiple?: boolean;
|
|
5
|
+
disabled?: boolean;
|
|
6
|
+
name?: string;
|
|
7
|
+
onChange: (files: File[]) => void;
|
|
8
|
+
};
|
|
9
|
+
export declare function FileField({ label, error, hint, accept, multiple, disabled, name, onChange, }: FileFieldProps): import("react").JSX.Element;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { useRef, useState } from "react";
|
|
3
|
+
import { Button } from "./Button.js";
|
|
4
|
+
import { useFieldIds } from "./field.js";
|
|
5
|
+
import { FieldShell } from "./FieldShell.js";
|
|
6
|
+
export function FileField({ label, error, hint, accept, multiple, disabled, name, onChange, }) {
|
|
7
|
+
const ids = useFieldIds({ error, hint });
|
|
8
|
+
const input = useRef(null);
|
|
9
|
+
const [names, setNames] = useState([]);
|
|
10
|
+
const [dragging, setDragging] = useState(false);
|
|
11
|
+
const pick = (files) => {
|
|
12
|
+
if (files.length === 0)
|
|
13
|
+
return;
|
|
14
|
+
setNames(files.map((f) => f.name));
|
|
15
|
+
onChange(files);
|
|
16
|
+
};
|
|
17
|
+
return (_jsx(FieldShell, { ...ids, label: label, error: error, hint: hint, children: _jsxs("div", { "data-testid": "file-drop", onDragOver: (e) => {
|
|
18
|
+
e.preventDefault();
|
|
19
|
+
if (!disabled)
|
|
20
|
+
setDragging(true);
|
|
21
|
+
}, onDragLeave: () => setDragging(false), onDrop: (e) => {
|
|
22
|
+
e.preventDefault();
|
|
23
|
+
setDragging(false);
|
|
24
|
+
if (disabled)
|
|
25
|
+
return;
|
|
26
|
+
const files = [...e.dataTransfer.files];
|
|
27
|
+
pick(multiple ? files : files.slice(0, 1));
|
|
28
|
+
}, className: `flex items-center gap-3 rounded-control border border-dashed bg-surface p-3 text-sm has-[input:focus-visible]:outline-2 has-[input:focus-visible]:outline-focus ${dragging ? "border-accent" : "border-field-line"} ${disabled ? "opacity-50" : ""}`, children: [_jsx("input", { ref: input, type: "file", name: name, accept: accept, multiple: multiple, disabled: disabled, ...ids.aria, className: "sr-only", onChange: (e) => {
|
|
29
|
+
pick([...(e.target.files ?? [])]);
|
|
30
|
+
e.target.value = "";
|
|
31
|
+
} }), _jsx(Button, { tabIndex: -1, disabled: disabled, onClick: () => input.current?.click(), children: multiple ? "Choose files" : "Choose file" }), _jsx("span", { className: "min-w-0 truncate text-muted", children: names.length > 0 ? names.join(", ") : "or drop here" })] }) }));
|
|
32
|
+
}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { ComponentProps } from "react";
|
|
2
|
+
import { Menu as MenuPrimitive } from "../primitives/Menu.js";
|
|
3
|
+
export type { MenuItem } from "../primitives/Menu.js";
|
|
4
|
+
export declare function Menu({ className, ...rest }: ComponentProps<typeof MenuPrimitive>): import("react").JSX.Element;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { Menu as MenuPrimitive } from "../primitives/Menu.js";
|
|
3
|
+
import { glass } from "./glass.js";
|
|
4
|
+
import { tv } from "./tv.js";
|
|
5
|
+
const menu = tv({
|
|
6
|
+
base: [
|
|
7
|
+
glass,
|
|
8
|
+
"z-50 min-w-44 rounded-base p-1 text-sm text-text outline-none",
|
|
9
|
+
"[&_[role^=menuitem]]:flex [&_[role^=menuitem]]:cursor-default [&_[role^=menuitem]]:items-center [&_[role^=menuitem]]:gap-2 [&_[role^=menuitem]]:rounded-control [&_[role^=menuitem]]:px-2.5 [&_[role^=menuitem]]:py-1.5 [&_[role^=menuitem]]:outline-none",
|
|
10
|
+
"[&_[role^=menuitem]>span]:flex-1 [&_kbd]:font-sans [&_kbd]:text-xs [&_kbd]:text-faint",
|
|
11
|
+
"[&_[data-active]]:bg-hover [&_[data-disabled]]:opacity-50",
|
|
12
|
+
"[&_[data-danger]]:text-danger-text [&_[data-danger][data-active]]:bg-danger/10",
|
|
13
|
+
"[&_[data-heading]]:px-2.5 [&_[data-heading]]:py-1 [&_[data-heading]]:text-xs [&_[data-heading]]:font-medium [&_[data-heading]]:text-faint",
|
|
14
|
+
"[&_[role=separator]]:my-1 [&_[role=separator]]:h-px [&_[role=separator]]:bg-line",
|
|
15
|
+
],
|
|
16
|
+
});
|
|
17
|
+
export function Menu({ className, ...rest }) {
|
|
18
|
+
return _jsx(MenuPrimitive, { ...rest, className: menu({ className }) });
|
|
19
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { inputClass, useFieldIds } from "./field.js";
|
|
3
|
+
import { FieldShell } from "./FieldShell.js";
|
|
4
|
+
export function NumberField({ label, error, hint, ...rest }) {
|
|
5
|
+
const ids = useFieldIds({ error, hint });
|
|
6
|
+
const describedBy = [ids.aria["aria-describedby"], rest["aria-describedby"]]
|
|
7
|
+
.filter(Boolean)
|
|
8
|
+
.join(" ") || undefined;
|
|
9
|
+
return (_jsx(FieldShell, { ...ids, label: label, error: error, hint: hint, children: _jsx("input", { type: "number", ...rest, ...ids.aria, "aria-invalid": ids.aria["aria-invalid"] ?? rest["aria-invalid"], "aria-describedby": describedBy, className: inputClass }) }));
|
|
10
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { Popover as PopoverPrimitive } from "../primitives/Popover.js";
|
|
3
|
+
import { glass } from "./glass.js";
|
|
4
|
+
import { tv } from "./tv.js";
|
|
5
|
+
const popover = tv({
|
|
6
|
+
base: `${glass} z-50 rounded-base p-3 text-sm text-text outline-none`,
|
|
7
|
+
});
|
|
8
|
+
export function Popover({ className, ...rest }) {
|
|
9
|
+
return _jsx(PopoverPrimitive, { ...rest, className: popover({ className }) });
|
|
10
|
+
}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { ComponentProps } from "react";
|
|
2
|
+
import { RadioGroup as RadioGroupPrimitive } from "../primitives/RadioGroup.js";
|
|
3
|
+
import { type FieldProps } from "./field.js";
|
|
4
|
+
export declare function RadioGroup({ label, error, hint, ...rest }: FieldProps & Omit<ComponentProps<typeof RadioGroupPrimitive>, "className">): import("react").JSX.Element;
|