@mdbase-dev/ui 0.2.1 → 0.2.2
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 +119 -0
- package/css/controls.css +37 -0
- package/css/date-picker.css +176 -0
- package/css/filter-builder.css +336 -0
- package/css/properties.css +398 -0
- package/dist/AnchoredPopover.d.ts +20 -0
- package/dist/AnchoredPopover.js +100 -0
- package/dist/Combo.d.ts +41 -0
- package/dist/Combo.js +82 -0
- package/dist/DatePicker.d.ts +69 -0
- package/dist/DatePicker.js +317 -0
- package/dist/Menu.d.ts +3 -1
- package/dist/Menu.js +3 -2
- package/dist/QuickCreate.js +1 -16
- package/dist/date-picker-model.d.ts +31 -0
- package/dist/date-picker-model.js +97 -0
- package/dist/filter-builder/AddFilter.d.ts +13 -0
- package/dist/filter-builder/AddFilter.js +295 -0
- package/dist/filter-builder/FilterBuilder.d.ts +36 -0
- package/dist/filter-builder/FilterBuilder.js +225 -0
- package/dist/filter-builder/FilterPill.d.ts +17 -0
- package/dist/filter-builder/FilterPill.js +494 -0
- package/dist/filter-builder/context.d.ts +40 -0
- package/dist/filter-builder/context.js +67 -0
- package/dist/filter-builder/editors.d.ts +31 -0
- package/dist/filter-builder/editors.js +294 -0
- package/dist/filter-builder/filter-ast.d.ts +13 -0
- package/dist/filter-builder/filter-ast.js +382 -0
- package/dist/filter-builder/labels.d.ts +99 -0
- package/dist/filter-builder/labels.js +555 -0
- package/dist/filter-builder/menu.d.ts +21 -0
- package/dist/filter-builder/menu.js +60 -0
- package/dist/filter-builder/operators.d.ts +30 -0
- package/dist/filter-builder/operators.js +192 -0
- package/dist/filter-builder/tree.d.ts +12 -0
- package/dist/filter-builder/tree.js +61 -0
- package/dist/filter-builder/types.d.ts +69 -0
- package/dist/filter-builder/types.js +48 -0
- package/dist/filter-builder.d.ts +13 -0
- package/dist/filter-builder.js +10 -0
- package/dist/glyphs.d.ts +32 -0
- package/dist/glyphs.js +143 -0
- package/dist/locales/de/messages.json +1 -1
- package/dist/locales/en/messages.json +1 -1
- package/dist/locales/es/messages.json +1 -1
- package/dist/locales/fr/messages.json +1 -1
- package/dist/locales/it/messages.json +1 -1
- package/dist/locales/ja/messages.json +1 -1
- package/dist/locales/ko/messages.json +1 -1
- package/dist/locales/pt/messages.json +1 -1
- package/dist/locales/ru/messages.json +1 -1
- package/dist/locales/zh/messages.json +1 -1
- package/dist/popover.d.ts +5 -0
- package/dist/popover.js +15 -0
- package/dist/properties/PropertyList.d.ts +31 -0
- package/dist/properties/PropertyList.js +233 -0
- package/dist/properties/PropertyValue.d.ts +42 -0
- package/dist/properties/PropertyValue.js +304 -0
- package/dist/properties/fields.d.ts +20 -0
- package/dist/properties/fields.js +81 -0
- package/dist/properties/labels.d.ts +29 -0
- package/dist/properties/labels.js +127 -0
- package/dist/properties/model.d.ts +52 -0
- package/dist/properties/model.js +195 -0
- package/dist/properties.d.ts +13 -0
- package/dist/properties.js +9 -0
- package/package.json +25 -2
|
@@ -0,0 +1,398 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* A record's properties (./properties): a name and its value on one line, the value edited
|
|
3
|
+
* where it stands. Its controls are in controls.css, which the host loads beside this file;
|
|
4
|
+
* the calendar a date opens comes with this one.
|
|
5
|
+
*/
|
|
6
|
+
@import "./date-picker.css";
|
|
7
|
+
|
|
8
|
+
.mdbase-properties {
|
|
9
|
+
display: flex;
|
|
10
|
+
flex-direction: column;
|
|
11
|
+
color: var(--color-text);
|
|
12
|
+
font-size: var(--control-font-size);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
.mdbase-properties,
|
|
16
|
+
.mdbase-properties *,
|
|
17
|
+
.mdbase-tokens,
|
|
18
|
+
.mdbase-tokens * {
|
|
19
|
+
box-sizing: border-box;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
.mdbase-property {
|
|
23
|
+
position: relative;
|
|
24
|
+
min-height: 30px;
|
|
25
|
+
display: grid;
|
|
26
|
+
grid-template-columns: clamp(108px, 34%, 200px) minmax(0, 1fr);
|
|
27
|
+
align-items: start;
|
|
28
|
+
gap: 4px;
|
|
29
|
+
padding-inline: 6px 2px;
|
|
30
|
+
border-radius: var(--control-radius);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
.mdbase-property-name {
|
|
34
|
+
min-width: 0;
|
|
35
|
+
height: 30px;
|
|
36
|
+
display: flex;
|
|
37
|
+
align-items: center;
|
|
38
|
+
gap: 6px;
|
|
39
|
+
color: var(--color-text-muted);
|
|
40
|
+
font-size: var(--font-size-caption);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
.mdbase-property-name > span {
|
|
44
|
+
overflow: hidden;
|
|
45
|
+
text-overflow: ellipsis;
|
|
46
|
+
white-space: nowrap;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
.mdbase-property-name > .mdbase-glyph {
|
|
50
|
+
flex: none;
|
|
51
|
+
color: var(--color-text-faint);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/* A part of a value that has parts: the property, then the part. */
|
|
55
|
+
.mdbase-property-name i {
|
|
56
|
+
color: var(--color-text-faint);
|
|
57
|
+
font-style: normal;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
.mdbase-property-name i::before {
|
|
61
|
+
content: "› ";
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/* A property that holds nothing is quieter than one that does. */
|
|
65
|
+
.mdbase-property[data-empty] > .mdbase-property-name {
|
|
66
|
+
color: var(--color-text-faint);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
.mdbase-property-value {
|
|
70
|
+
min-width: 0;
|
|
71
|
+
min-height: 30px;
|
|
72
|
+
display: flex;
|
|
73
|
+
align-items: center;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
.mdbase-property-static {
|
|
77
|
+
padding: 0 7px;
|
|
78
|
+
overflow: hidden;
|
|
79
|
+
color: var(--color-text-soft);
|
|
80
|
+
text-overflow: ellipsis;
|
|
81
|
+
white-space: nowrap;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
.mdbase-property-static.is-empty {
|
|
85
|
+
color: var(--color-text-faint);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/* A field that reads as text until it is pointed at or typed in. */
|
|
89
|
+
.mdbase-property-input,
|
|
90
|
+
.mdbase-property-combo .mdbase-field {
|
|
91
|
+
width: 100%;
|
|
92
|
+
min-width: 0;
|
|
93
|
+
min-height: 28px;
|
|
94
|
+
padding: 0 7px;
|
|
95
|
+
border: 1px solid transparent;
|
|
96
|
+
border-radius: var(--control-radius);
|
|
97
|
+
outline: 0;
|
|
98
|
+
color: var(--color-text);
|
|
99
|
+
background: transparent;
|
|
100
|
+
font: inherit;
|
|
101
|
+
transition:
|
|
102
|
+
background var(--motion-quick) var(--ease-standard),
|
|
103
|
+
border-color var(--motion-quick) var(--ease-standard);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
textarea.mdbase-property-input {
|
|
107
|
+
display: block;
|
|
108
|
+
padding-block: 5px;
|
|
109
|
+
line-height: 1.45;
|
|
110
|
+
resize: none;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
.mdbase-property-input::placeholder,
|
|
114
|
+
.mdbase-property-combo .mdbase-field::placeholder {
|
|
115
|
+
color: var(--color-text-faint);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
.mdbase-property-input:hover,
|
|
119
|
+
.mdbase-property-combo .mdbase-field:hover:not(:disabled) {
|
|
120
|
+
border-color: transparent;
|
|
121
|
+
background: var(--color-surface-subtle);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
.mdbase-property-input:focus,
|
|
125
|
+
.mdbase-property-combo .mdbase-field:focus {
|
|
126
|
+
border-color: var(--color-border-strong);
|
|
127
|
+
background: var(--color-surface);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
.mdbase-property-input:focus-visible {
|
|
131
|
+
outline: var(--focus-ring);
|
|
132
|
+
outline-offset: -1px;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
.mdbase-property-select.mdbase-select {
|
|
136
|
+
width: 100%;
|
|
137
|
+
padding: 0 7px;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
.mdbase-property-value > .mdbase-switch {
|
|
141
|
+
margin-inline-start: 7px;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/* Named with the date field's own class, so these hold wherever date-picker.css is loaded. */
|
|
145
|
+
.mdbase-property-date.mdbase-date-field {
|
|
146
|
+
width: 100%;
|
|
147
|
+
min-height: 28px;
|
|
148
|
+
padding: 0 7px;
|
|
149
|
+
border: 1px solid transparent;
|
|
150
|
+
border-radius: var(--control-radius);
|
|
151
|
+
transition: background var(--motion-quick) var(--ease-standard);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
.mdbase-property-date.mdbase-date-field:hover {
|
|
155
|
+
background: var(--color-surface-subtle);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
.mdbase-property-date.mdbase-date-field[aria-expanded="true"] {
|
|
159
|
+
border-color: var(--color-border-strong);
|
|
160
|
+
background: var(--color-surface);
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/* Takes the property off the record: shown when the row is pointed at or the button has the focus. */
|
|
164
|
+
.mdbase-property-clear {
|
|
165
|
+
position: absolute;
|
|
166
|
+
top: 5px;
|
|
167
|
+
right: 4px;
|
|
168
|
+
width: 20px;
|
|
169
|
+
height: 20px;
|
|
170
|
+
display: grid;
|
|
171
|
+
place-items: center;
|
|
172
|
+
padding: 0;
|
|
173
|
+
border: 0;
|
|
174
|
+
border-radius: 5px;
|
|
175
|
+
color: var(--color-text-muted);
|
|
176
|
+
background: var(--color-canvas);
|
|
177
|
+
opacity: 0;
|
|
178
|
+
cursor: pointer;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
.mdbase-property:hover > .mdbase-property-clear,
|
|
182
|
+
.mdbase-property-clear:focus-visible {
|
|
183
|
+
opacity: 1;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
.mdbase-property-clear:hover {
|
|
187
|
+
color: var(--color-danger);
|
|
188
|
+
background: var(--color-surface-subtle);
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
.mdbase-properties-none {
|
|
192
|
+
margin: 0;
|
|
193
|
+
padding: 6px;
|
|
194
|
+
color: var(--color-text-muted);
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/* Finding a property to add: a field the width of the list, with its matches under it. */
|
|
198
|
+
.mdbase-property-add {
|
|
199
|
+
width: auto;
|
|
200
|
+
margin: 2px 6px 0;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
.mdbase-properties-foot {
|
|
204
|
+
display: flex;
|
|
205
|
+
align-items: center;
|
|
206
|
+
justify-content: space-between;
|
|
207
|
+
gap: 8px;
|
|
208
|
+
margin-top: 2px;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
.mdbase-properties-action {
|
|
212
|
+
height: 28px;
|
|
213
|
+
display: inline-flex;
|
|
214
|
+
align-items: center;
|
|
215
|
+
gap: 6px;
|
|
216
|
+
padding: 0 8px 0 6px;
|
|
217
|
+
border: 0;
|
|
218
|
+
border-radius: var(--control-radius);
|
|
219
|
+
color: var(--color-text-muted);
|
|
220
|
+
background: none;
|
|
221
|
+
font: inherit;
|
|
222
|
+
font-size: var(--font-size-caption);
|
|
223
|
+
cursor: pointer;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
.mdbase-properties-action:hover {
|
|
227
|
+
color: var(--color-text);
|
|
228
|
+
background: var(--color-surface-subtle);
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/* How many properties hold nothing, and the way to see them. */
|
|
232
|
+
.mdbase-properties-action.is-toggle {
|
|
233
|
+
margin-inline-start: auto;
|
|
234
|
+
padding: 0 6px 0 8px;
|
|
235
|
+
color: var(--color-text-faint);
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
/* Several values in one property: short text, or links to other records. */
|
|
239
|
+
.mdbase-tokens {
|
|
240
|
+
width: 100%;
|
|
241
|
+
min-width: 0;
|
|
242
|
+
display: flex;
|
|
243
|
+
flex-wrap: wrap;
|
|
244
|
+
align-items: center;
|
|
245
|
+
gap: 4px;
|
|
246
|
+
padding: 3px 5px;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
.mdbase-tokens[data-editable] {
|
|
250
|
+
border: 1px solid transparent;
|
|
251
|
+
border-radius: var(--control-radius);
|
|
252
|
+
cursor: text;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
.mdbase-tokens[data-editable]:hover {
|
|
256
|
+
background: var(--color-surface-subtle);
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
.mdbase-tokens[data-editable]:focus-within {
|
|
260
|
+
border-color: var(--color-border-strong);
|
|
261
|
+
background: var(--color-surface);
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
.mdbase-token {
|
|
265
|
+
max-width: 100%;
|
|
266
|
+
height: 22px;
|
|
267
|
+
display: inline-flex;
|
|
268
|
+
align-items: center;
|
|
269
|
+
gap: 2px;
|
|
270
|
+
padding: 0 3px 0 8px;
|
|
271
|
+
border-radius: 5px;
|
|
272
|
+
color: var(--color-text-soft);
|
|
273
|
+
background: var(--color-surface-subtle);
|
|
274
|
+
font-size: var(--font-size-caption);
|
|
275
|
+
white-space: nowrap;
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
.mdbase-token[data-link] {
|
|
279
|
+
padding-inline-start: 0;
|
|
280
|
+
color: var(--color-accent-strong);
|
|
281
|
+
background: var(--color-accent-soft);
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
.mdbase-token[data-missing] {
|
|
285
|
+
color: var(--color-text-muted);
|
|
286
|
+
background: var(--color-surface-subtle);
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
.mdbase-token button {
|
|
290
|
+
min-width: 16px;
|
|
291
|
+
height: 16px;
|
|
292
|
+
display: grid;
|
|
293
|
+
place-items: center;
|
|
294
|
+
padding: 0;
|
|
295
|
+
border: 0;
|
|
296
|
+
border-radius: 4px;
|
|
297
|
+
color: inherit;
|
|
298
|
+
background: none;
|
|
299
|
+
font: inherit;
|
|
300
|
+
opacity: 0.6;
|
|
301
|
+
cursor: pointer;
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
.mdbase-token button:hover {
|
|
305
|
+
background: color-mix(in srgb, currentColor 14%, transparent);
|
|
306
|
+
opacity: 1;
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
.mdbase-token > .mdbase-token-open {
|
|
310
|
+
max-width: 180px;
|
|
311
|
+
height: 22px;
|
|
312
|
+
display: block;
|
|
313
|
+
padding: 0 4px 0 8px;
|
|
314
|
+
overflow: hidden;
|
|
315
|
+
line-height: 22px;
|
|
316
|
+
text-overflow: ellipsis;
|
|
317
|
+
opacity: 1;
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
.mdbase-token > button.mdbase-token-open:hover {
|
|
321
|
+
background: none;
|
|
322
|
+
text-decoration: underline;
|
|
323
|
+
text-underline-offset: 2px;
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
.mdbase-tokens-input {
|
|
327
|
+
min-width: 48px;
|
|
328
|
+
height: 22px;
|
|
329
|
+
flex: 1;
|
|
330
|
+
padding: 0 2px;
|
|
331
|
+
border: 0;
|
|
332
|
+
outline: 0;
|
|
333
|
+
color: var(--color-text);
|
|
334
|
+
background: transparent;
|
|
335
|
+
font: inherit;
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
.mdbase-tokens-input::placeholder {
|
|
339
|
+
color: var(--color-text-faint);
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
.mdbase-token-add {
|
|
343
|
+
width: 22px;
|
|
344
|
+
height: 22px;
|
|
345
|
+
display: grid;
|
|
346
|
+
place-items: center;
|
|
347
|
+
padding: 0;
|
|
348
|
+
border: 0;
|
|
349
|
+
border-radius: 5px;
|
|
350
|
+
color: var(--color-text-muted);
|
|
351
|
+
background: none;
|
|
352
|
+
cursor: pointer;
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
.mdbase-token-add:hover {
|
|
356
|
+
color: var(--color-text);
|
|
357
|
+
background: var(--color-surface-subtle);
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
.mdbase-property-link-picker {
|
|
361
|
+
flex: 1 1 100%;
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
.mdbase-property-link-picker .mdbase-field {
|
|
365
|
+
min-height: 26px;
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
@media (pointer: coarse) {
|
|
369
|
+
/* Nothing to point at with: the way to take a property off is always there. */
|
|
370
|
+
.mdbase-property-clear {
|
|
371
|
+
top: 10px;
|
|
372
|
+
opacity: 1;
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
.mdbase-property {
|
|
376
|
+
padding-inline-end: 28px;
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
.mdbase-property,
|
|
380
|
+
.mdbase-property-value {
|
|
381
|
+
min-height: 40px;
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
.mdbase-property-name {
|
|
385
|
+
height: 40px;
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
.mdbase-properties-action {
|
|
389
|
+
height: 40px;
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
/* Below 16px, iOS zooms the page when a field takes the focus. */
|
|
393
|
+
.mdbase-property-input,
|
|
394
|
+
.mdbase-tokens-input,
|
|
395
|
+
.mdbase-property-combo .mdbase-field {
|
|
396
|
+
font-size: 16px;
|
|
397
|
+
}
|
|
398
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { type JSX, type ReactNode, type RefObject } from "react";
|
|
2
|
+
export interface AnchoredPopoverProps {
|
|
3
|
+
/** The control that opened the popover: it opens beside it, and a press on it is not a press elsewhere. */
|
|
4
|
+
readonly anchor: RefObject<HTMLElement | null>;
|
|
5
|
+
/** The popover's accessible name. */
|
|
6
|
+
readonly label: string;
|
|
7
|
+
/** Escape (`refocus` is true, and the anchor has the focus again) or a press elsewhere (false). */
|
|
8
|
+
readonly onClose: (refocus: boolean) => void;
|
|
9
|
+
readonly className?: string | undefined;
|
|
10
|
+
readonly children: ReactNode;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* A small panel beside the control that opened it, for a value to be chosen or typed: a
|
|
14
|
+
* calendar, a checklist, a short form. Render it while it is wanted. It opens in the top layer,
|
|
15
|
+
* so a scrolling or clipped container does not cut it off, follows its anchor, and puts the
|
|
16
|
+
* keyboard in the element marked `data-autofocus` (failing that, in its first control). Escape
|
|
17
|
+
* closes it and returns the focus to the anchor; a press elsewhere closes it after the field
|
|
18
|
+
* being typed in has lost the focus, so that field can keep what was typed.
|
|
19
|
+
*/
|
|
20
|
+
export declare function AnchoredPopover({ anchor, label, onClose, className, children }: AnchoredPopoverProps): JSX.Element;
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { useEffect, useEffectEvent, useLayoutEffect, useRef } from "react";
|
|
3
|
+
import { createPortal } from "react-dom";
|
|
4
|
+
import { activeModalDialog, anchoredPlacement } from "./popover.js";
|
|
5
|
+
const FOCUSABLE = ':is(button, input, select, textarea, [tabindex="0"]):not(:disabled)';
|
|
6
|
+
/** A list that has the keyboard closes on Escape before the popover around it does. */
|
|
7
|
+
const OPEN_LIST = '[role="listbox"], [role="menu"]';
|
|
8
|
+
/**
|
|
9
|
+
* A small panel beside the control that opened it, for a value to be chosen or typed: a
|
|
10
|
+
* calendar, a checklist, a short form. Render it while it is wanted. It opens in the top layer,
|
|
11
|
+
* so a scrolling or clipped container does not cut it off, follows its anchor, and puts the
|
|
12
|
+
* keyboard in the element marked `data-autofocus` (failing that, in its first control). Escape
|
|
13
|
+
* closes it and returns the focus to the anchor; a press elsewhere closes it after the field
|
|
14
|
+
* being typed in has lost the focus, so that field can keep what was typed.
|
|
15
|
+
*/
|
|
16
|
+
export function AnchoredPopover({
|
|
17
|
+
anchor,
|
|
18
|
+
label,
|
|
19
|
+
onClose,
|
|
20
|
+
className,
|
|
21
|
+
children
|
|
22
|
+
}) {
|
|
23
|
+
const ref = useRef(null);
|
|
24
|
+
const close = useEffectEvent(onClose);
|
|
25
|
+
const place = useEffectEvent(() => {
|
|
26
|
+
const popover = ref.current;
|
|
27
|
+
const trigger = anchor.current;
|
|
28
|
+
if (!popover || !trigger) return;
|
|
29
|
+
const box = trigger.getBoundingClientRect();
|
|
30
|
+
popover.style.maxHeight = "";
|
|
31
|
+
const placement = anchoredPlacement(box, {
|
|
32
|
+
width: popover.offsetWidth,
|
|
33
|
+
height: popover.scrollHeight
|
|
34
|
+
}, {
|
|
35
|
+
width: window.innerWidth,
|
|
36
|
+
height: window.innerHeight
|
|
37
|
+
}, {
|
|
38
|
+
gap: 6
|
|
39
|
+
});
|
|
40
|
+
popover.style.top = `${placement.top}px`;
|
|
41
|
+
popover.style.left = `${placement.left}px`;
|
|
42
|
+
popover.style.maxHeight = `${placement.maxHeight}px`;
|
|
43
|
+
popover.style.setProperty("--overlay-origin", `${placement.top < box.top ? "bottom" : "top"} left`);
|
|
44
|
+
});
|
|
45
|
+
useLayoutEffect(() => {
|
|
46
|
+
const popover = ref.current;
|
|
47
|
+
if (!popover) return undefined;
|
|
48
|
+
// Browsers without the Popover API (and jsdom) keep it a fixed element where it is mounted.
|
|
49
|
+
if (typeof popover.showPopover === "function") popover.showPopover();
|
|
50
|
+
place();
|
|
51
|
+
(popover.querySelector("[data-autofocus]") ?? popover.querySelector(FOCUSABLE))?.focus({
|
|
52
|
+
preventScroll: true
|
|
53
|
+
});
|
|
54
|
+
// Its own content changes size, as a step or a checklist changes.
|
|
55
|
+
const resized = typeof ResizeObserver === "function" ? new ResizeObserver(() => place()) : undefined;
|
|
56
|
+
resized?.observe(popover);
|
|
57
|
+
return () => resized?.disconnect();
|
|
58
|
+
}, []);
|
|
59
|
+
// The anchor moves when the content around it changes.
|
|
60
|
+
useLayoutEffect(() => place());
|
|
61
|
+
useEffect(() => {
|
|
62
|
+
const inside = target => Boolean(target instanceof Node && ref.current?.contains(target));
|
|
63
|
+
const onPointerDown = event => {
|
|
64
|
+
if (inside(event.target) || event.target instanceof Node && anchor.current?.contains(event.target)) return;
|
|
65
|
+
const active = document.activeElement;
|
|
66
|
+
if (active instanceof HTMLElement && inside(active)) active.blur();
|
|
67
|
+
close(false);
|
|
68
|
+
};
|
|
69
|
+
const follow = event => {
|
|
70
|
+
if (!inside(event.target)) place();
|
|
71
|
+
};
|
|
72
|
+
document.addEventListener("pointerdown", onPointerDown, true);
|
|
73
|
+
document.addEventListener("scroll", follow, true);
|
|
74
|
+
window.addEventListener("resize", place);
|
|
75
|
+
return () => {
|
|
76
|
+
document.removeEventListener("pointerdown", onPointerDown, true);
|
|
77
|
+
document.removeEventListener("scroll", follow, true);
|
|
78
|
+
window.removeEventListener("resize", place);
|
|
79
|
+
};
|
|
80
|
+
}, [anchor]);
|
|
81
|
+
const onKeyDown = event => {
|
|
82
|
+
if (event.key !== "Escape" || event.target.closest(OPEN_LIST)) return;
|
|
83
|
+
event.preventDefault();
|
|
84
|
+
event.stopPropagation();
|
|
85
|
+
anchor.current?.focus({
|
|
86
|
+
preventScroll: true
|
|
87
|
+
});
|
|
88
|
+
onClose(true);
|
|
89
|
+
};
|
|
90
|
+
return createPortal(_jsx("div", {
|
|
91
|
+
ref: ref,
|
|
92
|
+
className: ["mdbase-popover mdbase-overlay", className].filter(Boolean).join(" "),
|
|
93
|
+
"data-open": true,
|
|
94
|
+
popover: "manual",
|
|
95
|
+
role: "dialog",
|
|
96
|
+
"aria-label": label,
|
|
97
|
+
onKeyDown: onKeyDown,
|
|
98
|
+
children: children
|
|
99
|
+
}), activeModalDialog() ?? document.body);
|
|
100
|
+
}
|
package/dist/Combo.d.ts
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { type JSX, type ReactNode } from "react";
|
|
2
|
+
export interface ComboOption {
|
|
3
|
+
/** What is handed back when the row is chosen. */
|
|
4
|
+
readonly value: string;
|
|
5
|
+
/** What the row says, when that is not the value. */
|
|
6
|
+
readonly label?: string | undefined;
|
|
7
|
+
/** Quiet text beside it. */
|
|
8
|
+
readonly detail?: string | undefined;
|
|
9
|
+
readonly icon?: ReactNode;
|
|
10
|
+
}
|
|
11
|
+
export interface ComboProps {
|
|
12
|
+
/** Names the field for assistive technology. */
|
|
13
|
+
readonly label: string;
|
|
14
|
+
readonly placeholder?: string | undefined;
|
|
15
|
+
/** The text the field starts with. */
|
|
16
|
+
readonly value?: string | undefined;
|
|
17
|
+
readonly options: readonly ComboOption[];
|
|
18
|
+
/**
|
|
19
|
+
* The row offered for text that is none of the options, such as "New property" or "Use": it is
|
|
20
|
+
* followed by the text in quotation marks. Without it only the options can be chosen.
|
|
21
|
+
*/
|
|
22
|
+
readonly newLabel?: string | undefined;
|
|
23
|
+
/** Whether typed text may be taken as it is. Every text may, when this is absent. */
|
|
24
|
+
readonly accepts?: ((text: string) => boolean) | undefined;
|
|
25
|
+
/** Shown when nothing matches and the text cannot be taken. */
|
|
26
|
+
readonly emptyMessage: string;
|
|
27
|
+
readonly autoFocus?: boolean | undefined;
|
|
28
|
+
readonly className?: string | undefined;
|
|
29
|
+
/** Hand over what was typed when the field is left, as a text field would. */
|
|
30
|
+
readonly commitOnBlur?: boolean | undefined;
|
|
31
|
+
/** `made` says the value is typed text, not one of the options. */
|
|
32
|
+
readonly onCommit: (value: string, made: boolean) => void;
|
|
33
|
+
/** The field was left, or Escape was pressed, with nothing chosen. */
|
|
34
|
+
readonly onCancel?: (() => void) | undefined;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* A text field with a list under it: type to narrow the list, choose a row, or take what was
|
|
38
|
+
* typed. It is the link picker, which finds or makes a record the same way, given plain values
|
|
39
|
+
* to find and make. Every string is passed in.
|
|
40
|
+
*/
|
|
41
|
+
export declare function Combo(props: ComboProps): JSX.Element;
|
package/dist/Combo.js
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { useEffect, useMemo, useRef, useState } from "react";
|
|
3
|
+
import { LinkPicker } from "./LinkPicker.js";
|
|
4
|
+
const rank = (option, needle) => {
|
|
5
|
+
const text = (option.label ?? option.value).toLowerCase();
|
|
6
|
+
return text === needle ? 0 : text.startsWith(needle) ? 1 : text.includes(needle) ? 2 : -1;
|
|
7
|
+
};
|
|
8
|
+
/**
|
|
9
|
+
* A text field with a list under it: type to narrow the list, choose a row, or take what was
|
|
10
|
+
* typed. It is the link picker, which finds or makes a record the same way, given plain values
|
|
11
|
+
* to find and make. Every string is passed in.
|
|
12
|
+
*/
|
|
13
|
+
export function Combo(props) {
|
|
14
|
+
const start = props.value ?? "";
|
|
15
|
+
const [query, setQuery] = useState(start);
|
|
16
|
+
// The value changed some other way (an undo, another editor): show what it is now.
|
|
17
|
+
useEffect(() => setQuery(start), [start]);
|
|
18
|
+
/** How the field's last use ended, so leaving it afterwards does not end it again. */
|
|
19
|
+
const ended = useRef(null);
|
|
20
|
+
const needle = query.trim().toLowerCase();
|
|
21
|
+
const options = useMemo(() => {
|
|
22
|
+
// Until something is typed over it, the text the field starts with is not a search.
|
|
23
|
+
if (!needle || query === start) return props.options.slice(0, 80);
|
|
24
|
+
return props.options.map(option => ({
|
|
25
|
+
option,
|
|
26
|
+
at: rank(option, needle)
|
|
27
|
+
})).filter(found => found.at >= 0).sort((a, b) => a.at - b.at).slice(0, 80).map(found => found.option);
|
|
28
|
+
}, [props.options, needle, query, start]);
|
|
29
|
+
const usable = Boolean(props.newLabel) && query.trim() !== "" && (props.accepts?.(query.trim()) ?? true);
|
|
30
|
+
const commit = (value, made) => {
|
|
31
|
+
ended.current = "chosen";
|
|
32
|
+
setQuery(value);
|
|
33
|
+
props.onCommit(value, made);
|
|
34
|
+
};
|
|
35
|
+
return _jsx("div", {
|
|
36
|
+
className: ["mdbase-combo", props.className].filter(Boolean).join(" "),
|
|
37
|
+
onKeyDownCapture: event => {
|
|
38
|
+
// Escape gives the field up; the picker reports it as it reports the focus leaving.
|
|
39
|
+
if (event.key === "Escape") ended.current = "cancelled";
|
|
40
|
+
},
|
|
41
|
+
children: _jsx(LinkPicker, {
|
|
42
|
+
label: props.label,
|
|
43
|
+
placeholder: props.placeholder,
|
|
44
|
+
query: query,
|
|
45
|
+
onQueryChange: text => {
|
|
46
|
+
ended.current = null;
|
|
47
|
+
setQuery(text);
|
|
48
|
+
},
|
|
49
|
+
options: options.map(option => ({
|
|
50
|
+
id: option.value,
|
|
51
|
+
title: option.label ?? option.value,
|
|
52
|
+
...(option.detail ? {
|
|
53
|
+
detail: option.detail
|
|
54
|
+
} : {}),
|
|
55
|
+
...(option.icon ? {
|
|
56
|
+
icon: option.icon
|
|
57
|
+
} : {})
|
|
58
|
+
})),
|
|
59
|
+
creates: usable ? [{
|
|
60
|
+
id: "new",
|
|
61
|
+
label: props.newLabel
|
|
62
|
+
}] : [],
|
|
63
|
+
emptyMessage: props.emptyMessage,
|
|
64
|
+
autoFocus: props.autoFocus,
|
|
65
|
+
onPick: option => commit(option.id, false),
|
|
66
|
+
onCreate: (_create, title) => commit(title.trim(), true),
|
|
67
|
+
onClose: () => {
|
|
68
|
+
const how = ended.current;
|
|
69
|
+
ended.current = null;
|
|
70
|
+
if (how === "chosen") return;
|
|
71
|
+
const typed = query.trim();
|
|
72
|
+
if (how !== "cancelled" && props.commitOnBlur && typed !== start.trim()) {
|
|
73
|
+
const known = props.options.find(option => option.value === typed || option.label === typed);
|
|
74
|
+
if (known) props.onCommit(known.value, false);else if (typed === "" || usable) props.onCommit(typed, true);else props.onCancel?.();
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
if (how === "cancelled") setQuery(start);
|
|
78
|
+
props.onCancel?.();
|
|
79
|
+
}
|
|
80
|
+
})
|
|
81
|
+
});
|
|
82
|
+
}
|