@mdbase-dev/ui 0.1.34 → 0.1.37
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/README.md +56 -0
- package/css/controls.css +140 -0
- package/dist/LinkPicker.d.ts +34 -0
- package/dist/LinkPicker.js +193 -0
- package/dist/QuickCreate.d.ts +29 -0
- package/dist/QuickCreate.js +156 -0
- package/dist/link-picker-model.d.ts +31 -0
- package/dist/link-picker-model.js +28 -0
- package/dist/popover.d.ts +7 -2
- package/dist/popover.js +9 -4
- package/package.json +10 -2
package/README.md
CHANGED
|
@@ -201,6 +201,62 @@ and `showAll` values as stable IDs. Group order, browsing limits and search matc
|
|
|
201
201
|
still use those IDs; translated headings do not change matching or merge groups.
|
|
202
202
|
Re-render with the current locale's callback to update the presentation.
|
|
203
203
|
|
|
204
|
+
## Link picker
|
|
205
|
+
|
|
206
|
+
`LinkPicker` from `@mdbase-dev/ui/link-picker` is the field for linking one
|
|
207
|
+
record to another: type to find a record, or make a new one of a given kind
|
|
208
|
+
from what was typed. Include `controls.css`.
|
|
209
|
+
|
|
210
|
+
```tsx
|
|
211
|
+
<LinkPicker
|
|
212
|
+
label={t`Author`}
|
|
213
|
+
query={query}
|
|
214
|
+
onQueryChange={setQuery}
|
|
215
|
+
options={matches.map((person) => ({ id: person.path, title: person.name, detail: t`Person` }))}
|
|
216
|
+
creates={[{ id: "person", label: t`New person` }]}
|
|
217
|
+
emptyMessage={t`No matching people.`}
|
|
218
|
+
onPick={(option) => link(option.id)}
|
|
219
|
+
onCreate={(create, title) => createAndLink(create.id, title)}
|
|
220
|
+
/>
|
|
221
|
+
```
|
|
222
|
+
|
|
223
|
+
The app matches and ranks `options` for the current `query`; the picker does not
|
|
224
|
+
filter. `creates` are listed after the matches once something is typed, unless a
|
|
225
|
+
match already carries exactly that title. Every string is passed in, so the
|
|
226
|
+
picker adds nothing to the shared catalogue. Focus stays in the text field: the
|
|
227
|
+
first Escape closes the list, the second calls `onClose`, as does leaving the
|
|
228
|
+
field. `linkPickerRows(query, options, creates)` gives the same rows to a surface
|
|
229
|
+
that draws its own list, such as an editor's completion popup.
|
|
230
|
+
|
|
231
|
+
## Quick create
|
|
232
|
+
|
|
233
|
+
`QuickCreate` from `@mdbase-dev/ui/quick-create` is a short form for a record
|
|
234
|
+
made in passing, such as one chosen from a link picker's create row when its
|
|
235
|
+
type needs more than a name. Include `controls.css`.
|
|
236
|
+
|
|
237
|
+
```tsx
|
|
238
|
+
{creating && <QuickCreate
|
|
239
|
+
title={t`New book`}
|
|
240
|
+
name={creating.title}
|
|
241
|
+
nameLabel={t`Title`}
|
|
242
|
+
onNameChange={(title) => setCreating({ ...creating, title })}
|
|
243
|
+
anchor={() => field.current?.getBoundingClientRect()}
|
|
244
|
+
submitLabel={t`Create and link`}
|
|
245
|
+
blocker={creating.isbn ? undefined : t`Give the ISBN.`}
|
|
246
|
+
onSubmit={createAndLink}
|
|
247
|
+
onClose={(refocus) => { setCreating(undefined); if (refocus) field.current?.focus(); }}
|
|
248
|
+
>
|
|
249
|
+
<IsbnField value={creating.isbn} onChange={(isbn) => setCreating({ ...creating, isbn })} />
|
|
250
|
+
</QuickCreate>}
|
|
251
|
+
```
|
|
252
|
+
|
|
253
|
+
Render it while the form is wanted. It opens in the top layer beside `anchor`
|
|
254
|
+
(called again on scroll and resize; return nothing once that place has gone),
|
|
255
|
+
puts the keyboard in the first field after the name and keeps Tab inside. The
|
|
256
|
+
app supplies the fields as children and every string except Cancel. Escape and
|
|
257
|
+
Cancel call `onClose(true)`; a press elsewhere calls `onClose(false)`, because
|
|
258
|
+
the focus has already moved. A list open inside the form takes the first Escape.
|
|
259
|
+
|
|
204
260
|
## Live preview
|
|
205
261
|
|
|
206
262
|
`livePreview()` from `@mdbase-dev/ui/live-preview` renders Markdown in a
|
package/css/controls.css
CHANGED
|
@@ -280,6 +280,146 @@ textarea.mdbase-field {
|
|
|
280
280
|
cursor: default;
|
|
281
281
|
}
|
|
282
282
|
|
|
283
|
+
/* Link picker: a text field whose list links to a record or makes one (LinkPicker). */
|
|
284
|
+
.mdbase-link-picker {
|
|
285
|
+
min-width: 0;
|
|
286
|
+
display: flex;
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
.mdbase-link-picker-list {
|
|
290
|
+
min-width: 240px;
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
.mdbase-link-option {
|
|
294
|
+
gap: 8px;
|
|
295
|
+
padding-right: 9px;
|
|
296
|
+
/* The title and detail truncate themselves; the row lets its divider show above it. */
|
|
297
|
+
overflow: visible;
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
/* The active row is the one Enter takes; nothing here is a held selection. */
|
|
301
|
+
.mdbase-link-option[aria-selected="true"] {
|
|
302
|
+
background: var(--color-surface-subtle);
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
.mdbase-link-option[aria-selected="true"]::after {
|
|
306
|
+
content: none;
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
.mdbase-link-option-icon {
|
|
310
|
+
width: 16px;
|
|
311
|
+
height: 16px;
|
|
312
|
+
flex: 0 0 16px;
|
|
313
|
+
display: grid;
|
|
314
|
+
place-items: center;
|
|
315
|
+
color: var(--color-text-muted);
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
.mdbase-link-option-title {
|
|
319
|
+
min-width: 0;
|
|
320
|
+
overflow: hidden;
|
|
321
|
+
color: var(--color-text);
|
|
322
|
+
text-overflow: ellipsis;
|
|
323
|
+
white-space: nowrap;
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
.mdbase-link-option-detail {
|
|
327
|
+
min-width: 0;
|
|
328
|
+
flex: 1 10 auto;
|
|
329
|
+
overflow: hidden;
|
|
330
|
+
color: var(--color-text-muted);
|
|
331
|
+
font-size: var(--font-size-caption);
|
|
332
|
+
text-align: right;
|
|
333
|
+
text-overflow: ellipsis;
|
|
334
|
+
white-space: nowrap;
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
.mdbase-link-option.is-create .mdbase-link-option-title {
|
|
338
|
+
color: var(--color-text-soft);
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
/* Making a record is a different act from picking one: a rule sets the first create row apart. */
|
|
342
|
+
.mdbase-link-option:not(.is-create) + .mdbase-link-option.is-create {
|
|
343
|
+
margin-top: var(--space-1);
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
.mdbase-link-option:not(.is-create) + .mdbase-link-option.is-create::before {
|
|
347
|
+
position: absolute;
|
|
348
|
+
top: calc(-0.5 * var(--space-1) - 0.5px);
|
|
349
|
+
right: 0;
|
|
350
|
+
left: 0;
|
|
351
|
+
border-top: 1px solid var(--color-border);
|
|
352
|
+
content: "";
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
.mdbase-link-picker-empty {
|
|
356
|
+
padding: 7px 9px;
|
|
357
|
+
color: var(--color-text-muted);
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
/* Quick create: a short form beside the place a record is made from (QuickCreate). */
|
|
361
|
+
.mdbase-quick-create {
|
|
362
|
+
position: fixed;
|
|
363
|
+
inset: auto;
|
|
364
|
+
box-sizing: border-box;
|
|
365
|
+
width: min(360px, calc(100vw - 16px));
|
|
366
|
+
margin: 0;
|
|
367
|
+
padding: 0;
|
|
368
|
+
overflow: auto;
|
|
369
|
+
overscroll-behavior: contain;
|
|
370
|
+
border: 1px solid var(--color-border-strong);
|
|
371
|
+
border-radius: var(--radius-md);
|
|
372
|
+
color: var(--color-text);
|
|
373
|
+
background: var(--color-surface-overlay);
|
|
374
|
+
font-size: var(--control-font-size);
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
.mdbase-quick-create-header {
|
|
378
|
+
display: grid;
|
|
379
|
+
gap: 2px;
|
|
380
|
+
padding: var(--space-3) var(--space-3) var(--space-2);
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
.mdbase-quick-create-title {
|
|
384
|
+
display: inline-flex;
|
|
385
|
+
align-items: center;
|
|
386
|
+
gap: 6px;
|
|
387
|
+
color: var(--color-text-muted);
|
|
388
|
+
font-size: var(--font-size-caption);
|
|
389
|
+
font-weight: var(--font-weight-medium);
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
.mdbase-quick-create-title svg {
|
|
393
|
+
width: 13px;
|
|
394
|
+
height: 13px;
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
/* The name reads as the record's title, not as one more field. */
|
|
398
|
+
.mdbase-quick-create-name {
|
|
399
|
+
width: 100%;
|
|
400
|
+
min-width: 0;
|
|
401
|
+
padding: 2px 0;
|
|
402
|
+
border: 0;
|
|
403
|
+
outline: 0;
|
|
404
|
+
color: inherit;
|
|
405
|
+
background: none;
|
|
406
|
+
font: inherit;
|
|
407
|
+
font-size: 15px;
|
|
408
|
+
font-weight: var(--font-weight-semibold);
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
.mdbase-quick-create-fields {
|
|
412
|
+
padding-inline: var(--space-3);
|
|
413
|
+
border-top: 1px solid var(--color-border);
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
.mdbase-quick-create-actions {
|
|
417
|
+
display: flex;
|
|
418
|
+
justify-content: flex-end;
|
|
419
|
+
gap: var(--space-2);
|
|
420
|
+
padding: var(--space-2) var(--space-3) var(--space-3);
|
|
421
|
+
}
|
|
422
|
+
|
|
283
423
|
/* Menus anchored to a trigger (useMenuPopover). */
|
|
284
424
|
.mdbase-menu {
|
|
285
425
|
position: fixed;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { type JSX, type Ref } from "react";
|
|
2
|
+
import { type LinkPickerCreate, type LinkPickerOption } from "./link-picker-model.js";
|
|
3
|
+
export { linkPickerRows } from "./link-picker-model.js";
|
|
4
|
+
export type { LinkPickerCreate, LinkPickerOption, LinkPickerRow } from "./link-picker-model.js";
|
|
5
|
+
export interface LinkPickerProps {
|
|
6
|
+
/** The field's accessible name. */
|
|
7
|
+
readonly label: string;
|
|
8
|
+
readonly placeholder?: string | undefined;
|
|
9
|
+
readonly query: string;
|
|
10
|
+
readonly onQueryChange: (query: string) => void;
|
|
11
|
+
/** The records matching `query`, best first. The app matches; the picker does not filter. */
|
|
12
|
+
readonly options: readonly LinkPickerOption[];
|
|
13
|
+
/** Kinds of record that can be made from the typed title, listed after the matches. */
|
|
14
|
+
readonly creates?: readonly LinkPickerCreate[] | undefined;
|
|
15
|
+
/** A create row's text. By default the kind's label, then the title in quotation marks. */
|
|
16
|
+
readonly createLabel?: ((create: LinkPickerCreate, title: string) => string) | undefined;
|
|
17
|
+
/** Shown in the open list when there is nothing to pick or create. */
|
|
18
|
+
readonly emptyMessage: string;
|
|
19
|
+
readonly disabled?: boolean | undefined;
|
|
20
|
+
readonly autoFocus?: boolean | undefined;
|
|
21
|
+
readonly className?: string | undefined;
|
|
22
|
+
readonly onPick: (option: LinkPickerOption) => void;
|
|
23
|
+
readonly onCreate?: ((create: LinkPickerCreate, title: string) => void) | undefined;
|
|
24
|
+
/** Escape with the list already closed, or focus leaving the field. */
|
|
25
|
+
readonly onClose?: (() => void) | undefined;
|
|
26
|
+
/** The text field, for focusing the picker from elsewhere. */
|
|
27
|
+
readonly ref?: Ref<HTMLInputElement> | undefined;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* A text field that links to a record: type to find one, or make a new one of a given kind from
|
|
31
|
+
* what was typed. Focus stays in the field (the ARIA editable combobox pattern) and the list
|
|
32
|
+
* opens in the top layer, so side panels and scrolling rows never clip it.
|
|
33
|
+
*/
|
|
34
|
+
export declare function LinkPicker({ label, placeholder, query, onQueryChange, options, creates, createLabel, emptyMessage, disabled, autoFocus, className, onPick, onCreate, onClose, ref, }: LinkPickerProps): JSX.Element;
|
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { useId, useLayoutEffect, useRef, useState } from "react";
|
|
3
|
+
import { linkPickerRowId, linkPickerRows } from "./link-picker-model.js";
|
|
4
|
+
import { useOverlay } from "./overlay.js";
|
|
5
|
+
import { useSelectPopover } from "./popover.js";
|
|
6
|
+
export { linkPickerRows } from "./link-picker-model.js";
|
|
7
|
+
/**
|
|
8
|
+
* A text field that links to a record: type to find one, or make a new one of a given kind from
|
|
9
|
+
* what was typed. Focus stays in the field (the ARIA editable combobox pattern) and the list
|
|
10
|
+
* opens in the top layer, so side panels and scrolling rows never clip it.
|
|
11
|
+
*/
|
|
12
|
+
export function LinkPicker({
|
|
13
|
+
label,
|
|
14
|
+
placeholder,
|
|
15
|
+
query,
|
|
16
|
+
onQueryChange,
|
|
17
|
+
options,
|
|
18
|
+
creates = [],
|
|
19
|
+
createLabel = quotedCreateLabel,
|
|
20
|
+
emptyMessage,
|
|
21
|
+
disabled,
|
|
22
|
+
autoFocus,
|
|
23
|
+
className,
|
|
24
|
+
onPick,
|
|
25
|
+
onCreate,
|
|
26
|
+
onClose,
|
|
27
|
+
ref
|
|
28
|
+
}) {
|
|
29
|
+
const id = useId();
|
|
30
|
+
const inputRef = useRef(null);
|
|
31
|
+
const [open, setOpen] = useState(false);
|
|
32
|
+
const [active, setActive] = useState(0);
|
|
33
|
+
const rows = linkPickerRows(query, options, onCreate ? creates : []);
|
|
34
|
+
const rowKey = rows.map(linkPickerRowId).join("\n");
|
|
35
|
+
const {
|
|
36
|
+
ref: listRef,
|
|
37
|
+
present
|
|
38
|
+
} = useOverlay(open);
|
|
39
|
+
useSelectPopover(open, inputRef, listRef, () => setOpen(false), {
|
|
40
|
+
focusList: false,
|
|
41
|
+
contentKey: rowKey
|
|
42
|
+
});
|
|
43
|
+
// The best match is one Enter away; a row that moved under the old index is not.
|
|
44
|
+
useLayoutEffect(() => setActive(0), [rowKey]);
|
|
45
|
+
useLayoutEffect(() => {
|
|
46
|
+
if (open && listRef.current) {
|
|
47
|
+
revealRow(listRef.current, active);
|
|
48
|
+
}
|
|
49
|
+
}, [active, open, listRef]);
|
|
50
|
+
const choose = row => {
|
|
51
|
+
if (!row) {
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
setOpen(false);
|
|
55
|
+
if (row.kind === "option") {
|
|
56
|
+
onPick(row.option);
|
|
57
|
+
} else {
|
|
58
|
+
onCreate?.(row.create, row.title);
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
const onKeyDown = event => {
|
|
62
|
+
if (event.key === "ArrowDown" || event.key === "ArrowUp") {
|
|
63
|
+
event.preventDefault();
|
|
64
|
+
if (!open) {
|
|
65
|
+
setOpen(true);
|
|
66
|
+
} else if (rows.length) {
|
|
67
|
+
const step = event.key === "ArrowDown" ? 1 : -1;
|
|
68
|
+
setActive((active + step + rows.length) % rows.length);
|
|
69
|
+
}
|
|
70
|
+
} else if (event.key === "Enter") {
|
|
71
|
+
if (open && rows[active]) {
|
|
72
|
+
event.preventDefault();
|
|
73
|
+
choose(rows[active]);
|
|
74
|
+
}
|
|
75
|
+
} else if (event.key === "Escape") {
|
|
76
|
+
// Inside a dialog or panel, the first Escape closes only the list.
|
|
77
|
+
event.stopPropagation();
|
|
78
|
+
if (open) {
|
|
79
|
+
event.preventDefault();
|
|
80
|
+
setOpen(false);
|
|
81
|
+
} else {
|
|
82
|
+
onClose?.();
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
};
|
|
86
|
+
return _jsxs("div", {
|
|
87
|
+
className: ["mdbase-link-picker", className].filter(Boolean).join(" "),
|
|
88
|
+
children: [_jsx("input", {
|
|
89
|
+
ref: node => {
|
|
90
|
+
inputRef.current = node;
|
|
91
|
+
if (typeof ref === "function") ref(node);else if (ref) ref.current = node;
|
|
92
|
+
},
|
|
93
|
+
className: "mdbase-field mdbase-link-picker-input",
|
|
94
|
+
type: "text",
|
|
95
|
+
role: "combobox",
|
|
96
|
+
"aria-label": label,
|
|
97
|
+
"aria-autocomplete": "list",
|
|
98
|
+
"aria-expanded": open,
|
|
99
|
+
"aria-controls": `${id}-list`,
|
|
100
|
+
"aria-activedescendant": open && rows[active] ? `${id}-row-${String(active)}` : undefined,
|
|
101
|
+
autoComplete: "off",
|
|
102
|
+
spellCheck: false,
|
|
103
|
+
// eslint-disable-next-line jsx-a11y/no-autofocus -- the picker opens because the person asked to add a link.
|
|
104
|
+
autoFocus: autoFocus,
|
|
105
|
+
disabled: disabled,
|
|
106
|
+
placeholder: placeholder,
|
|
107
|
+
value: query,
|
|
108
|
+
onChange: event => {
|
|
109
|
+
onQueryChange(event.target.value);
|
|
110
|
+
setOpen(true);
|
|
111
|
+
},
|
|
112
|
+
onFocus: () => setOpen(true),
|
|
113
|
+
onClick: () => setOpen(true),
|
|
114
|
+
onBlur: () => {
|
|
115
|
+
setOpen(false);
|
|
116
|
+
onClose?.();
|
|
117
|
+
},
|
|
118
|
+
onKeyDown: onKeyDown
|
|
119
|
+
}), present && _jsxs("div", {
|
|
120
|
+
ref: listRef,
|
|
121
|
+
id: `${id}-list`,
|
|
122
|
+
className: "mdbase-select-list mdbase-link-picker-list mdbase-overlay",
|
|
123
|
+
"data-open": open,
|
|
124
|
+
popover: "manual",
|
|
125
|
+
role: "listbox",
|
|
126
|
+
"aria-label": label,
|
|
127
|
+
// A press in the list must not take the focus (and the list) away from the field.
|
|
128
|
+
onPointerDown: event => event.preventDefault(),
|
|
129
|
+
onClick: event => event.stopPropagation(),
|
|
130
|
+
children: [rows.map((row, index) => _jsxs("div", {
|
|
131
|
+
id: `${id}-row-${String(index)}`,
|
|
132
|
+
"data-index": index,
|
|
133
|
+
role: "option",
|
|
134
|
+
"aria-selected": index === active,
|
|
135
|
+
className: rowClassName(row, index === active),
|
|
136
|
+
onPointerMove: () => {
|
|
137
|
+
if (index !== active) {
|
|
138
|
+
setActive(index);
|
|
139
|
+
}
|
|
140
|
+
},
|
|
141
|
+
onClick: () => choose(row),
|
|
142
|
+
children: [_jsx("span", {
|
|
143
|
+
className: "mdbase-link-option-icon",
|
|
144
|
+
"aria-hidden": "true",
|
|
145
|
+
children: row.kind === "option" ? row.option.icon : row.create.icon ?? _jsx(PlusGlyph, {})
|
|
146
|
+
}), _jsx("span", {
|
|
147
|
+
className: "mdbase-link-option-title",
|
|
148
|
+
children: row.kind === "option" ? row.option.title : createLabel(row.create, row.title)
|
|
149
|
+
}), row.kind === "option" && row.option.detail && _jsx("span", {
|
|
150
|
+
className: "mdbase-link-option-detail",
|
|
151
|
+
children: row.option.detail
|
|
152
|
+
})]
|
|
153
|
+
}, linkPickerRowId(row))), !rows.length && _jsx("div", {
|
|
154
|
+
className: "mdbase-link-picker-empty",
|
|
155
|
+
children: emptyMessage
|
|
156
|
+
})]
|
|
157
|
+
})]
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
function quotedCreateLabel(create, title) {
|
|
161
|
+
return `${create.label} “${title}”`;
|
|
162
|
+
}
|
|
163
|
+
function rowClassName(row, active) {
|
|
164
|
+
return ["mdbase-select-option", "mdbase-link-option", row.kind === "create" ? "is-create" : "", active ? "is-active" : ""].filter(Boolean).join(" ");
|
|
165
|
+
}
|
|
166
|
+
function PlusGlyph() {
|
|
167
|
+
return _jsx("svg", {
|
|
168
|
+
viewBox: "0 0 16 16",
|
|
169
|
+
width: "14",
|
|
170
|
+
height: "14",
|
|
171
|
+
fill: "none",
|
|
172
|
+
stroke: "currentColor",
|
|
173
|
+
strokeWidth: "1.5",
|
|
174
|
+
strokeLinecap: "round",
|
|
175
|
+
children: _jsx("path", {
|
|
176
|
+
d: "M8 3.5v9M3.5 8h9"
|
|
177
|
+
})
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
/** Scrolls only the list; scrollIntoView would move the panels the field sits in. */
|
|
181
|
+
function revealRow(list, index) {
|
|
182
|
+
const row = list.querySelector(`[data-index="${String(index)}"]`);
|
|
183
|
+
if (!row) {
|
|
184
|
+
return;
|
|
185
|
+
}
|
|
186
|
+
const top = row.offsetTop;
|
|
187
|
+
const bottom = top + row.offsetHeight;
|
|
188
|
+
if (top < list.scrollTop) {
|
|
189
|
+
list.scrollTop = top;
|
|
190
|
+
} else if (bottom > list.scrollTop + list.clientHeight) {
|
|
191
|
+
list.scrollTop = bottom - list.clientHeight;
|
|
192
|
+
}
|
|
193
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { type JSX, type ReactNode } from "react";
|
|
2
|
+
export interface QuickCreateProps {
|
|
3
|
+
/** What is being made ("New book"): the heading and the form's accessible name. */
|
|
4
|
+
readonly title: string;
|
|
5
|
+
readonly icon?: ReactNode;
|
|
6
|
+
readonly name: string;
|
|
7
|
+
/** The name field's accessible name and placeholder. */
|
|
8
|
+
readonly nameLabel: string;
|
|
9
|
+
readonly onNameChange: (name: string) => void;
|
|
10
|
+
/** Where on screen the record is being made from; nothing once that place has gone. */
|
|
11
|
+
readonly anchor: () => DOMRect | undefined;
|
|
12
|
+
readonly submitLabel: string;
|
|
13
|
+
readonly cancelLabel?: string | undefined;
|
|
14
|
+
/** Why the form cannot be submitted yet. The submit button is disabled and says so. */
|
|
15
|
+
readonly blocker?: string | undefined;
|
|
16
|
+
readonly onSubmit: () => void;
|
|
17
|
+
/** `refocus` is false when a press elsewhere has already moved the focus. */
|
|
18
|
+
readonly onClose: (refocus: boolean) => void;
|
|
19
|
+
readonly className?: string | undefined;
|
|
20
|
+
/** The fields the record needs besides its name. */
|
|
21
|
+
readonly children?: ReactNode;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* A short form for a record made in passing, beside the place it is made from: its name, then
|
|
25
|
+
* the fields the app asks for. It opens in the top layer and takes the keyboard until it is
|
|
26
|
+
* submitted or dismissed. Escape and Cancel dismiss it, as does a press anywhere else. Render it
|
|
27
|
+
* while the form is wanted; the app closes it by no longer rendering it.
|
|
28
|
+
*/
|
|
29
|
+
export declare function QuickCreate({ title, icon, name, nameLabel, onNameChange, anchor, submitLabel, cancelLabel, blocker, onSubmit, onClose, className, children, }: QuickCreateProps): JSX.Element;
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { i18n as _i18n } from "@lingui/core";
|
|
3
|
+
import { useEffect, useEffectEvent, useLayoutEffect, useRef } from "react";
|
|
4
|
+
import { useLocaleRevision } from "./i18n.js";
|
|
5
|
+
import { anchoredPlacement } from "./popover.js";
|
|
6
|
+
const FOCUSABLE = ':is(button, input, select, textarea, [tabindex="0"]):not(:disabled)';
|
|
7
|
+
const OPEN_LIST = '[aria-expanded="true"], [role="listbox"], [role="menu"]';
|
|
8
|
+
/**
|
|
9
|
+
* A short form for a record made in passing, beside the place it is made from: its name, then
|
|
10
|
+
* the fields the app asks for. It opens in the top layer and takes the keyboard until it is
|
|
11
|
+
* submitted or dismissed. Escape and Cancel dismiss it, as does a press anywhere else. Render it
|
|
12
|
+
* while the form is wanted; the app closes it by no longer rendering it.
|
|
13
|
+
*/
|
|
14
|
+
export function QuickCreate({
|
|
15
|
+
title,
|
|
16
|
+
icon,
|
|
17
|
+
name,
|
|
18
|
+
nameLabel,
|
|
19
|
+
onNameChange,
|
|
20
|
+
anchor,
|
|
21
|
+
submitLabel,
|
|
22
|
+
cancelLabel = _i18n._(
|
|
23
|
+
/** i18n */
|
|
24
|
+
{
|
|
25
|
+
id: "dEgA5A",
|
|
26
|
+
message: "Cancel"
|
|
27
|
+
}),
|
|
28
|
+
blocker,
|
|
29
|
+
onSubmit,
|
|
30
|
+
onClose,
|
|
31
|
+
className,
|
|
32
|
+
children
|
|
33
|
+
}) {
|
|
34
|
+
useLocaleRevision();
|
|
35
|
+
const ref = useRef(null);
|
|
36
|
+
const fields = useRef(null);
|
|
37
|
+
const place = useEffectEvent(() => {
|
|
38
|
+
const popover = ref.current;
|
|
39
|
+
const box = anchor();
|
|
40
|
+
if (!popover || !box) return;
|
|
41
|
+
popover.style.maxHeight = "";
|
|
42
|
+
const placement = anchoredPlacement(box, {
|
|
43
|
+
width: popover.offsetWidth,
|
|
44
|
+
height: popover.scrollHeight
|
|
45
|
+
}, {
|
|
46
|
+
width: window.innerWidth,
|
|
47
|
+
height: window.innerHeight
|
|
48
|
+
}, {
|
|
49
|
+
gap: 6
|
|
50
|
+
});
|
|
51
|
+
popover.style.top = `${placement.top}px`;
|
|
52
|
+
popover.style.left = `${placement.left}px`;
|
|
53
|
+
popover.style.maxHeight = `${placement.maxHeight}px`;
|
|
54
|
+
popover.style.setProperty("--overlay-origin", `${placement.top < box.top ? "bottom" : "top"} left`);
|
|
55
|
+
});
|
|
56
|
+
const close = useEffectEvent(onClose);
|
|
57
|
+
useLayoutEffect(() => {
|
|
58
|
+
const popover = ref.current;
|
|
59
|
+
if (!popover) return undefined;
|
|
60
|
+
if (typeof popover.showPopover === "function") popover.showPopover();
|
|
61
|
+
place();
|
|
62
|
+
// The name is usually typed already: the first field still to fill in takes the keyboard.
|
|
63
|
+
const first = fields.current?.querySelector(FOCUSABLE) ?? popover.querySelector(FOCUSABLE);
|
|
64
|
+
first?.focus({
|
|
65
|
+
preventScroll: true
|
|
66
|
+
});
|
|
67
|
+
// The form grows as lists gain items and errors appear.
|
|
68
|
+
const resized = new ResizeObserver(() => place());
|
|
69
|
+
resized.observe(popover);
|
|
70
|
+
return () => resized.disconnect();
|
|
71
|
+
}, []);
|
|
72
|
+
useEffect(() => {
|
|
73
|
+
const inside = target => Boolean(target instanceof Node && ref.current?.contains(target));
|
|
74
|
+
const onPointerDown = event => {
|
|
75
|
+
if (!inside(event.target)) close(false);
|
|
76
|
+
};
|
|
77
|
+
const onScroll = event => {
|
|
78
|
+
if (!inside(event.target)) place();
|
|
79
|
+
};
|
|
80
|
+
const onKeyDown = event => {
|
|
81
|
+
const popover = ref.current;
|
|
82
|
+
if (!popover) return;
|
|
83
|
+
if (event.key === "Escape") {
|
|
84
|
+
// A list open inside the form (a choice, a link picker) closes first.
|
|
85
|
+
if (inside(event.target) && event.target.closest(OPEN_LIST)) return;
|
|
86
|
+
event.preventDefault();
|
|
87
|
+
event.stopPropagation();
|
|
88
|
+
close(true);
|
|
89
|
+
} else if (event.key === "Tab") {
|
|
90
|
+
// Tab stays in the form: behind it is the page the record is being made from.
|
|
91
|
+
const controls = [...popover.querySelectorAll(FOCUSABLE)];
|
|
92
|
+
const edge = event.shiftKey ? controls[0] : controls.at(-1);
|
|
93
|
+
const active = document.activeElement;
|
|
94
|
+
if (!controls.length || inside(active) && active !== edge) return;
|
|
95
|
+
event.preventDefault();
|
|
96
|
+
(event.shiftKey ? controls.at(-1) : controls[0])?.focus();
|
|
97
|
+
}
|
|
98
|
+
};
|
|
99
|
+
document.addEventListener("pointerdown", onPointerDown, true);
|
|
100
|
+
document.addEventListener("scroll", onScroll, true);
|
|
101
|
+
document.addEventListener("keydown", onKeyDown, true);
|
|
102
|
+
window.addEventListener("resize", place);
|
|
103
|
+
return () => {
|
|
104
|
+
document.removeEventListener("pointerdown", onPointerDown, true);
|
|
105
|
+
document.removeEventListener("scroll", onScroll, true);
|
|
106
|
+
document.removeEventListener("keydown", onKeyDown, true);
|
|
107
|
+
window.removeEventListener("resize", place);
|
|
108
|
+
};
|
|
109
|
+
}, []);
|
|
110
|
+
return _jsx("div", {
|
|
111
|
+
ref: ref,
|
|
112
|
+
className: `mdbase-quick-create mdbase-overlay${className ? ` ${className}` : ""}`,
|
|
113
|
+
"data-open": true,
|
|
114
|
+
popover: "manual",
|
|
115
|
+
role: "dialog",
|
|
116
|
+
"aria-label": title,
|
|
117
|
+
children: _jsxs("form", {
|
|
118
|
+
onSubmit: event => {
|
|
119
|
+
event.preventDefault();
|
|
120
|
+
if (!blocker) onSubmit();
|
|
121
|
+
},
|
|
122
|
+
children: [_jsxs("header", {
|
|
123
|
+
className: "mdbase-quick-create-header",
|
|
124
|
+
children: [_jsxs("span", {
|
|
125
|
+
className: "mdbase-quick-create-title",
|
|
126
|
+
children: [icon, title]
|
|
127
|
+
}), _jsx("input", {
|
|
128
|
+
className: "mdbase-quick-create-name",
|
|
129
|
+
"aria-label": nameLabel,
|
|
130
|
+
placeholder: nameLabel,
|
|
131
|
+
value: name,
|
|
132
|
+
autoComplete: "off",
|
|
133
|
+
onChange: event => onNameChange(event.target.value)
|
|
134
|
+
})]
|
|
135
|
+
}), children !== undefined && children !== null && _jsx("div", {
|
|
136
|
+
ref: fields,
|
|
137
|
+
className: "mdbase-quick-create-fields",
|
|
138
|
+
children: children
|
|
139
|
+
}), _jsxs("footer", {
|
|
140
|
+
className: "mdbase-quick-create-actions",
|
|
141
|
+
children: [_jsx("button", {
|
|
142
|
+
type: "button",
|
|
143
|
+
className: "mdbase-button is-tertiary",
|
|
144
|
+
onClick: () => onClose(true),
|
|
145
|
+
children: cancelLabel
|
|
146
|
+
}), _jsx("button", {
|
|
147
|
+
type: "submit",
|
|
148
|
+
className: "mdbase-button is-primary",
|
|
149
|
+
disabled: Boolean(blocker),
|
|
150
|
+
title: blocker,
|
|
151
|
+
children: submitLabel
|
|
152
|
+
})]
|
|
153
|
+
})]
|
|
154
|
+
})
|
|
155
|
+
});
|
|
156
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { ReactNode } from "react";
|
|
2
|
+
/** A record that can be linked to. The app matches and ranks; the picker only shows. */
|
|
3
|
+
export interface LinkPickerOption {
|
|
4
|
+
readonly id: string;
|
|
5
|
+
readonly title: string;
|
|
6
|
+
/** Quiet text beside the title: a type name or a path. */
|
|
7
|
+
readonly detail?: string | undefined;
|
|
8
|
+
readonly icon?: ReactNode;
|
|
9
|
+
}
|
|
10
|
+
/** A kind of record the picker can make from what was typed: a type, or a plain note. */
|
|
11
|
+
export interface LinkPickerCreate {
|
|
12
|
+
readonly id: string;
|
|
13
|
+
/** The action without the title, such as "New person". */
|
|
14
|
+
readonly label: string;
|
|
15
|
+
readonly icon?: ReactNode;
|
|
16
|
+
}
|
|
17
|
+
export type LinkPickerRow = {
|
|
18
|
+
readonly kind: "option";
|
|
19
|
+
readonly option: LinkPickerOption;
|
|
20
|
+
} | {
|
|
21
|
+
readonly kind: "create";
|
|
22
|
+
readonly create: LinkPickerCreate;
|
|
23
|
+
readonly title: string;
|
|
24
|
+
};
|
|
25
|
+
/**
|
|
26
|
+
* What the picker lists for a query: the app's matches, then one row per kind of record that
|
|
27
|
+
* can be made with the typed title. Nothing is offered to create while the field is empty or
|
|
28
|
+
* when a match already carries exactly that title.
|
|
29
|
+
*/
|
|
30
|
+
export declare function linkPickerRows(query: string, options: readonly LinkPickerOption[], creates: readonly LinkPickerCreate[]): readonly LinkPickerRow[];
|
|
31
|
+
export declare function linkPickerRowId(row: LinkPickerRow): string;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What the picker lists for a query: the app's matches, then one row per kind of record that
|
|
3
|
+
* can be made with the typed title. Nothing is offered to create while the field is empty or
|
|
4
|
+
* when a match already carries exactly that title.
|
|
5
|
+
*/
|
|
6
|
+
export function linkPickerRows(query, options, creates) {
|
|
7
|
+
const title = query.trim().replace(/\s+/gu, " ");
|
|
8
|
+
const rows = options.map(option => ({
|
|
9
|
+
kind: "option",
|
|
10
|
+
option
|
|
11
|
+
}));
|
|
12
|
+
if (!title || options.some(option => sameTitle(option.title, title))) {
|
|
13
|
+
return rows;
|
|
14
|
+
}
|
|
15
|
+
return [...rows, ...creates.map(create => ({
|
|
16
|
+
kind: "create",
|
|
17
|
+
create,
|
|
18
|
+
title
|
|
19
|
+
}))];
|
|
20
|
+
}
|
|
21
|
+
export function linkPickerRowId(row) {
|
|
22
|
+
return row.kind === "option" ? `option:${row.option.id}` : `create:${row.create.id}`;
|
|
23
|
+
}
|
|
24
|
+
function sameTitle(left, right) {
|
|
25
|
+
return left.trim().localeCompare(right, undefined, {
|
|
26
|
+
sensitivity: "accent"
|
|
27
|
+
}) === 0;
|
|
28
|
+
}
|
package/dist/popover.d.ts
CHANGED
|
@@ -24,9 +24,14 @@ export declare function anchoredPlacement(trigger: Box, popover: Size, viewport:
|
|
|
24
24
|
};
|
|
25
25
|
/**
|
|
26
26
|
* Shows a select's list in the top layer beside its trigger, focuses it, and dismisses it when
|
|
27
|
-
* the pointer lands elsewhere or the page scrolls or resizes underneath it.
|
|
27
|
+
* the pointer lands elsewhere or the page scrolls or resizes underneath it. A list driven from
|
|
28
|
+
* a text field (`focusList: false`) leaves the focus where the typing is, and is placed again
|
|
29
|
+
* whenever `contentKey` changes, since its rows follow the text.
|
|
28
30
|
*/
|
|
29
|
-
export declare function useSelectPopover(open: boolean, triggerRef: RefObject<HTMLElement | null>, listRef: RefObject<HTMLElement | null>, onDismiss: () => void
|
|
31
|
+
export declare function useSelectPopover(open: boolean, triggerRef: RefObject<HTMLElement | null>, listRef: RefObject<HTMLElement | null>, onDismiss: () => void, { focusList, contentKey }?: {
|
|
32
|
+
readonly focusList?: boolean;
|
|
33
|
+
readonly contentKey?: string;
|
|
34
|
+
}): void;
|
|
30
35
|
export interface MenuPopoverOptions {
|
|
31
36
|
readonly point?: {
|
|
32
37
|
readonly left: number;
|
package/dist/popover.js
CHANGED
|
@@ -49,9 +49,14 @@ function onScreen(element) {
|
|
|
49
49
|
}
|
|
50
50
|
/**
|
|
51
51
|
* Shows a select's list in the top layer beside its trigger, focuses it, and dismisses it when
|
|
52
|
-
* the pointer lands elsewhere or the page scrolls or resizes underneath it.
|
|
52
|
+
* the pointer lands elsewhere or the page scrolls or resizes underneath it. A list driven from
|
|
53
|
+
* a text field (`focusList: false`) leaves the focus where the typing is, and is placed again
|
|
54
|
+
* whenever `contentKey` changes, since its rows follow the text.
|
|
53
55
|
*/
|
|
54
|
-
export function useSelectPopover(open, triggerRef, listRef, onDismiss
|
|
56
|
+
export function useSelectPopover(open, triggerRef, listRef, onDismiss, {
|
|
57
|
+
focusList = true,
|
|
58
|
+
contentKey
|
|
59
|
+
} = {}) {
|
|
55
60
|
const dismiss = useEffectEvent(onDismiss);
|
|
56
61
|
useLayoutEffect(() => {
|
|
57
62
|
const list = listRef.current;
|
|
@@ -61,10 +66,10 @@ export function useSelectPopover(open, triggerRef, listRef, onDismiss) {
|
|
|
61
66
|
place(trigger, list, {
|
|
62
67
|
matchWidth: true
|
|
63
68
|
});
|
|
64
|
-
list.focus({
|
|
69
|
+
if (focusList) list.focus({
|
|
65
70
|
preventScroll: true
|
|
66
71
|
});
|
|
67
|
-
}, [open, triggerRef, listRef]);
|
|
72
|
+
}, [open, triggerRef, listRef, focusList, contentKey]);
|
|
68
73
|
useEffect(() => {
|
|
69
74
|
const list = listRef.current;
|
|
70
75
|
const trigger = triggerRef.current;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mdbase-dev/ui",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.37",
|
|
4
4
|
"private": false,
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -84,6 +84,10 @@
|
|
|
84
84
|
"types": "./dist/ConfirmDialog.d.ts",
|
|
85
85
|
"import": "./dist/ConfirmDialog.js"
|
|
86
86
|
},
|
|
87
|
+
"./link-picker": {
|
|
88
|
+
"types": "./dist/LinkPicker.d.ts",
|
|
89
|
+
"import": "./dist/LinkPicker.js"
|
|
90
|
+
},
|
|
87
91
|
"./menu": {
|
|
88
92
|
"types": "./dist/Menu.d.ts",
|
|
89
93
|
"import": "./dist/Menu.js"
|
|
@@ -112,6 +116,10 @@
|
|
|
112
116
|
"types": "./dist/popover.d.ts",
|
|
113
117
|
"import": "./dist/popover.js"
|
|
114
118
|
},
|
|
119
|
+
"./quick-create": {
|
|
120
|
+
"types": "./dist/QuickCreate.d.ts",
|
|
121
|
+
"import": "./dist/QuickCreate.js"
|
|
122
|
+
},
|
|
115
123
|
"./save-notice": {
|
|
116
124
|
"types": "./dist/SaveNotice.d.ts",
|
|
117
125
|
"import": "./dist/SaveNotice.js"
|
|
@@ -250,6 +258,6 @@
|
|
|
250
258
|
"typecheck": "node scripts/compile-catalogues.mjs && tsc -p tsconfig.json --noEmit"
|
|
251
259
|
},
|
|
252
260
|
"mdbaseRelease": {
|
|
253
|
-
"commit": "
|
|
261
|
+
"commit": "d3b89b09816d1b43ff6c6b6f3abc5b00c10d1e50"
|
|
254
262
|
}
|
|
255
263
|
}
|