@makoai/app-sdk 2.16.0 → 2.18.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +46 -4
- package/index.js +1 -1
- package/package.json +3 -2
- package/ui-core.js +76 -0
- package/ui.css +19 -10
- package/ui.d.ts +42 -3
- package/ui.js +101 -45
package/README.md
CHANGED
|
@@ -388,10 +388,9 @@ import {
|
|
|
388
388
|
<MultiSelect options={["CH", "ES", "IT"]} value={countries} onChange={setCountries} allLabel="All countries" />
|
|
389
389
|
```
|
|
390
390
|
|
|
391
|
-
- `MultiSelect` — controlled (`value` / `onChange`).
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
button per row. On phones the menu is a bottom sheet.
|
|
391
|
+
- `MultiSelect` — controlled (`value` / `onChange`). Options are strings or
|
|
392
|
+
`{ value, label, color }`; `showOnly` adds an "Only" button per row. On
|
|
393
|
+
phones the menu is a bottom sheet. See "MultiSelect: the All rule" below.
|
|
395
394
|
- `FreshnessBadge` — presentational: the app decides what fresh means and
|
|
396
395
|
passes `tone` (`ok` / `warn` / `bad`, or `green` / `amber` / `red`), a
|
|
397
396
|
`headline`, and per-source rows; `action` is a callout slot (e.g. a "Load
|
|
@@ -421,6 +420,49 @@ import {
|
|
|
421
420
|
and declare it once (`src/vite-env.d.ts`):
|
|
422
421
|
`declare const __APP_BINDING_NAMES__: string[];`
|
|
423
422
|
|
|
423
|
+
### MultiSelect: the All rule and one open popover (2.17+)
|
|
424
|
+
|
|
425
|
+
**All.** Nothing is filtered when `value` is empty or holds every option. The
|
|
426
|
+
All row is then ticked and every option row unticked, whichever of the two the
|
|
427
|
+
app passes. A click on an option under All selects only that option.
|
|
428
|
+
Unticking the last option, or ticking the last missing one, returns to All.
|
|
429
|
+
`emptyMeansAll` (default `true`) only sets what All emits: `[]`, or every
|
|
430
|
+
option's value when `false`.
|
|
431
|
+
|
|
432
|
+
**Trigger.** It reads the All label, the one selected label, or "3 selected".
|
|
433
|
+
`formatMany={n => `${n} markets`}` changes the last one. The trigger's `title`
|
|
434
|
+
lists the selected labels. Chips and `maxChips` are gone.
|
|
435
|
+
|
|
436
|
+
**Checkbox.** `Checkbox` (`checked`: `true` / `false` / `"mixed"`) is the
|
|
437
|
+
kit's one checkbox. From 2.18 it ticks white on `--brand` (`--primary` when a
|
|
438
|
+
theme has no `--brand`), unticked boxes use `--input`. Draw the rows of an
|
|
439
|
+
app's own Radix menu with it, so every menu on the page ticks alike.
|
|
440
|
+
|
|
441
|
+
**One open at a time.** Opening a kit popover (`MultiSelect`,
|
|
442
|
+
`FreshnessBadge`) closes any other. Escape, a pointerdown outside and focus
|
|
443
|
+
leaving it close it too. The kit listens for pointerdown in the capture phase,
|
|
444
|
+
because Radix triggers cancel the mouse events of their pointerdown. When an
|
|
445
|
+
app's own menu opens, call `closePopovers()`:
|
|
446
|
+
|
|
447
|
+
```tsx
|
|
448
|
+
<DropdownMenu modal={false} onOpenChange={o => o && closePopovers()}>
|
|
449
|
+
```
|
|
450
|
+
|
|
451
|
+
**Inside a `<label>` (2.18+).** A label clicks its control when you click
|
|
452
|
+
plain content inside it. In 2.17 a click on an option row reached the label,
|
|
453
|
+
the label clicked the trigger, and the menu closed. Kit popovers now cancel
|
|
454
|
+
that click, so a `MultiSelect` or `FreshnessBadge` can sit in a label.
|
|
455
|
+
|
|
456
|
+
**Cursor (2.18+).** `ui.css` gives a pointer to every enabled button, summary,
|
|
457
|
+
select, checkbox, radio, tab, option and Radix menu item (`menuitem`,
|
|
458
|
+
`menuitemradio`, `menuitemcheckbox`). The rule is unlayered, so it beats
|
|
459
|
+
shadcn's `cursor-default` on menu items. Disabled ones (`:disabled`,
|
|
460
|
+
`aria-disabled="true"`, `data-disabled`) show `not-allowed`.
|
|
461
|
+
|
|
462
|
+
Pure helpers for app-side menus: `multiSelectState(value, optionValues)`,
|
|
463
|
+
`multiSelectToggle(value, optionValues, v, emptyMeansAll)`,
|
|
464
|
+
`multiSelectText(value, options, allLabel, formatMany)`.
|
|
465
|
+
|
|
424
466
|
### Chart tooltips: `TableTooltip` (2.10+, every series from 2.15)
|
|
425
467
|
|
|
426
468
|
Every chart tooltip is a table, never recharts' coloured `name : value` list:
|
package/index.js
CHANGED
|
@@ -37,7 +37,7 @@ import {
|
|
|
37
37
|
* in a page (or this export) says which SDK a deployed app actually runs.
|
|
38
38
|
* Kept in step with package.json by snapshot.test.mjs.
|
|
39
39
|
*/
|
|
40
|
-
export const SDK_VERSION = "2.
|
|
40
|
+
export const SDK_VERSION = "2.18.0";
|
|
41
41
|
if (typeof window !== "undefined") {
|
|
42
42
|
try {
|
|
43
43
|
window.__MAKO_APP_SDK__ = SDK_VERSION;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@makoai/app-sdk",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.18.0",
|
|
4
4
|
"description": "Mako app SDK: data bindings (useQuery/useDuckDB), URL state, theme, and the house dashboard kit (/ui).",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -64,6 +64,7 @@
|
|
|
64
64
|
"test": "node --test"
|
|
65
65
|
},
|
|
66
66
|
"devDependencies": {
|
|
67
|
-
"apache-arrow": "17.0.0"
|
|
67
|
+
"apache-arrow": "17.0.0",
|
|
68
|
+
"jsdom": "^30.1.2"
|
|
68
69
|
}
|
|
69
70
|
}
|
package/ui-core.js
CHANGED
|
@@ -322,3 +322,79 @@ export function csvToTsv(text) {
|
|
|
322
322
|
.map(r => r.map(v => v.replace(/[\t\r\n]+/g, " ")).join("\t"))
|
|
323
323
|
.join("\n");
|
|
324
324
|
}
|
|
325
|
+
|
|
326
|
+
// ---------------------------------------------------------------------------
|
|
327
|
+
// MultiSelect: the All rule and the trigger text
|
|
328
|
+
// ---------------------------------------------------------------------------
|
|
329
|
+
|
|
330
|
+
/**
|
|
331
|
+
* Where a MultiSelect stands. Nothing is filtered when the value is empty or
|
|
332
|
+
* holds every option: `all` is true and `selected` is empty, so the All row
|
|
333
|
+
* is ticked and every option row unticked. Otherwise `selected` is the value
|
|
334
|
+
* in option order (values the options don't list yet are kept at the end).
|
|
335
|
+
*/
|
|
336
|
+
export function multiSelectState(value, optionValues) {
|
|
337
|
+
const picked = [...new Set(value ?? [])];
|
|
338
|
+
const covers = optionValues.length > 0 && optionValues.every(v => picked.includes(v));
|
|
339
|
+
if (picked.length === 0 || covers) return { all: true, selected: [] };
|
|
340
|
+
const known = optionValues.filter(v => picked.includes(v));
|
|
341
|
+
return { all: false, selected: [...known, ...picked.filter(v => !optionValues.includes(v))] };
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
/** The value that means "All": [] by default, every option when `emptyMeansAll` is false. */
|
|
345
|
+
export function multiSelectAll(optionValues, emptyMeansAll = true) {
|
|
346
|
+
return emptyMeansAll ? [] : [...optionValues];
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
/**
|
|
350
|
+
* The next value after a click on option `v`. Under All, the click selects
|
|
351
|
+
* only `v`. Otherwise it toggles `v`; unticking the last option or ticking
|
|
352
|
+
* the last missing one returns to All.
|
|
353
|
+
*/
|
|
354
|
+
export function multiSelectToggle(value, optionValues, v, emptyMeansAll = true) {
|
|
355
|
+
const { all, selected } = multiSelectState(value, optionValues);
|
|
356
|
+
if (all) return optionValues.length === 1 ? multiSelectAll(optionValues, emptyMeansAll) : [v];
|
|
357
|
+
const next = selected.includes(v) ? selected.filter(x => x !== v) : [...selected, v];
|
|
358
|
+
return multiSelectState(next, optionValues).all ? multiSelectAll(optionValues, emptyMeansAll) : next;
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
/**
|
|
362
|
+
* The trigger text: the All label, the one selected option's label, or
|
|
363
|
+
* "N selected" (or `many(n)`).
|
|
364
|
+
*/
|
|
365
|
+
export function multiSelectText(value, options, allLabel = "All", many = n => `${n} selected`) {
|
|
366
|
+
const { all, selected } = multiSelectState(value, options.map(o => o.value));
|
|
367
|
+
if (all) return allLabel;
|
|
368
|
+
if (selected.length === 1) return options.find(o => o.value === selected[0])?.label ?? selected[0];
|
|
369
|
+
return many(selected.length);
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
// ---------------------------------------------------------------------------
|
|
373
|
+
// One open popover at a time
|
|
374
|
+
// ---------------------------------------------------------------------------
|
|
375
|
+
let openPopoverClose = null;
|
|
376
|
+
|
|
377
|
+
/**
|
|
378
|
+
* Register the popover that just opened: `close` is how to close it. Any other
|
|
379
|
+
* kit popover that is open closes first.
|
|
380
|
+
*/
|
|
381
|
+
export function claimPopover(close) {
|
|
382
|
+
const prev = openPopoverClose;
|
|
383
|
+
openPopoverClose = close;
|
|
384
|
+
if (prev && prev !== close) prev();
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
/** Forget `close` if it is still the open popover (on close or unmount). */
|
|
388
|
+
export function releasePopover(close) {
|
|
389
|
+
if (openPopoverClose === close) openPopoverClose = null;
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
/**
|
|
393
|
+
* Close whichever kit popover is open. Call it when a popover the kit doesn't
|
|
394
|
+
* own opens (a Radix menu's onOpenChange), so one dropdown is open at a time.
|
|
395
|
+
*/
|
|
396
|
+
export function closePopovers() {
|
|
397
|
+
const close = openPopoverClose;
|
|
398
|
+
openPopoverClose = null;
|
|
399
|
+
if (close) close();
|
|
400
|
+
}
|
package/ui.css
CHANGED
|
@@ -40,7 +40,19 @@ body {
|
|
|
40
40
|
-webkit-font-smoothing: antialiased;
|
|
41
41
|
}
|
|
42
42
|
button, input, select, textarea { font-family: inherit; }
|
|
43
|
-
|
|
43
|
+
/* A hand on everything clickable, the kit's controls and the shadcn/Radix
|
|
44
|
+
* menus apps vendor. shadcn gives menu items `cursor-default`, so items
|
|
45
|
+
* showed an arrow. Unlayered on purpose: it beats Tailwind v4's layered
|
|
46
|
+
* utilities. Disabled items (disabled, aria-disabled, Radix data-disabled)
|
|
47
|
+
* keep not-allowed. */
|
|
48
|
+
:is(button, summary, select, input[type="checkbox"], input[type="radio"], [role="button"], [role="tab"],
|
|
49
|
+
[role="option"], [role="menuitem"], [role="menuitemradio"], [role="menuitemcheckbox"]):not(:disabled, [aria-disabled="true"], [data-disabled]) {
|
|
50
|
+
cursor: pointer;
|
|
51
|
+
}
|
|
52
|
+
:is(button, select, input[type="checkbox"], input[type="radio"], [role="button"], [role="tab"],
|
|
53
|
+
[role="option"], [role="menuitem"], [role="menuitemradio"], [role="menuitemcheckbox"]):is(:disabled, [aria-disabled="true"], [data-disabled]) {
|
|
54
|
+
cursor: not-allowed;
|
|
55
|
+
}
|
|
44
56
|
@media (prefers-reduced-motion: reduce) {
|
|
45
57
|
*, *::before, *::after { animation-duration: 0.01ms !important; transition-duration: 0.01ms !important; }
|
|
46
58
|
}
|
|
@@ -271,14 +283,9 @@ button:not(:disabled), [role="button"]:not(:disabled) { cursor: pointer; }
|
|
|
271
283
|
.mk-ms-trigger:hover { background: var(--accent); }
|
|
272
284
|
.mk-ms-trigger:focus-visible { outline: none; }
|
|
273
285
|
.mk-ms-trigger.open, .mk-ms-trigger:focus-visible { border-color: var(--ring); box-shadow: var(--mk-ring-focus); }
|
|
274
|
-
.mk-ms-value { display: flex; align-items: center; min-width: 0; overflow: hidden; }
|
|
275
|
-
|
|
276
|
-
.mk-ms-
|
|
277
|
-
display: inline-flex; align-items: center; gap: 5px; height: 20px; padding: 0 8px;
|
|
278
|
-
font-size: 12px; font-weight: 500; border-radius: 999px; white-space: nowrap;
|
|
279
|
-
background: var(--secondary); color: var(--secondary-foreground);
|
|
280
|
-
}
|
|
281
|
-
.mk-ms-more { color: var(--muted-foreground); }
|
|
286
|
+
.mk-ms-value { display: flex; align-items: center; gap: 6px; min-width: 0; overflow: hidden; }
|
|
287
|
+
/* The trigger reads "All", the one selected label, or "N selected". */
|
|
288
|
+
.mk-ms-text { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
|
|
282
289
|
.mk-ms-caret { color: var(--muted-foreground); flex-shrink: 0; transition: transform .15s ease; }
|
|
283
290
|
.mk-ms-trigger.open .mk-ms-caret { transform: rotate(180deg); }
|
|
284
291
|
.mk-ms-menu {
|
|
@@ -295,13 +302,15 @@ button:not(:disabled), [role="button"]:not(:disabled) { cursor: pointer; }
|
|
|
295
302
|
.mk-ms-opt:hover, .mk-ms-opt:focus-visible { background: var(--accent); outline: none; }
|
|
296
303
|
.mk-ms-opt.active { font-weight: 500; }
|
|
297
304
|
.mk-ms-label { flex: 1; }
|
|
305
|
+
/* The kit's one checkbox (Checkbox, MultiSelect rows): theme tokens only. */
|
|
298
306
|
.mk-ms-box {
|
|
299
307
|
display: inline-flex; align-items: center; justify-content: center;
|
|
300
308
|
width: 16px; height: 16px; flex-shrink: 0; border-radius: var(--mk-radius-sm);
|
|
301
309
|
border: 1px solid var(--input); background: var(--background); color: transparent; box-shadow: var(--mk-shadow-xs);
|
|
302
310
|
transition: background .12s, border-color .12s, color .12s;
|
|
303
311
|
}
|
|
304
|
-
|
|
312
|
+
/* Ticked in the brand blue (white tick), --primary where a theme has no --brand. */
|
|
313
|
+
.mk-ms-box.on { background: var(--brand, var(--primary)); border-color: var(--brand, var(--primary)); color: #fff; }
|
|
305
314
|
.mk-ms-swatch { width: 8px; height: 8px; border-radius: 999px; flex-shrink: 0; }
|
|
306
315
|
.mk-ms-sep { height: 1px; background: var(--border); margin: 4px -4px; flex: none; }
|
|
307
316
|
.mk-ms-only {
|
package/ui.d.ts
CHANGED
|
@@ -73,17 +73,25 @@ export interface MultiSelectOption<V extends string = string> {
|
|
|
73
73
|
|
|
74
74
|
export interface MultiSelectProps<V extends string = string> {
|
|
75
75
|
options: ReadonlyArray<V | MultiSelectOption<V>>;
|
|
76
|
+
/**
|
|
77
|
+
* The selected values. Empty, or every option, means "All": the All row is
|
|
78
|
+
* ticked and every option row unticked. Under All a click on an option
|
|
79
|
+
* selects only that option; unticking the last option, or ticking the last
|
|
80
|
+
* missing one, returns to All.
|
|
81
|
+
*/
|
|
76
82
|
value: readonly V[];
|
|
77
83
|
onChange: (next: V[]) => void;
|
|
78
84
|
allLabel?: string;
|
|
79
85
|
/**
|
|
80
|
-
* true (default):
|
|
81
|
-
*
|
|
86
|
+
* What All emits. true (default): []. false: every option's value.
|
|
87
|
+
* Both spellings display as All either way.
|
|
82
88
|
*/
|
|
83
89
|
emptyMeansAll?: boolean;
|
|
84
90
|
/** An "Only" button on each row that selects just that value. */
|
|
85
91
|
showOnly?: boolean;
|
|
86
|
-
/**
|
|
92
|
+
/** Trigger text for several values. Default `${n} selected`. */
|
|
93
|
+
formatMany?: (n: number) => string;
|
|
94
|
+
/** @deprecated 2.17: the trigger reads "All", the one label, or "N selected". Ignored. */
|
|
87
95
|
maxChips?: number;
|
|
88
96
|
/** Which edge the menu aligns to before flipping. Default "start". */
|
|
89
97
|
align?: "start" | "end";
|
|
@@ -92,6 +100,37 @@ export interface MultiSelectProps<V extends string = string> {
|
|
|
92
100
|
}
|
|
93
101
|
export function MultiSelect<V extends string = string>(props: MultiSelectProps<V>): React.ReactElement;
|
|
94
102
|
|
|
103
|
+
/**
|
|
104
|
+
* The kit's one checkbox, drawn only (the row around it takes the click),
|
|
105
|
+
* coloured from theme tokens. Use it for the rows of an app's own menus.
|
|
106
|
+
*/
|
|
107
|
+
export function Checkbox(props: { checked?: boolean | "mixed"; className?: string }): React.ReactElement;
|
|
108
|
+
|
|
109
|
+
/** Where a MultiSelect stands: `all` when the value is empty or holds every option (then `selected` is empty). */
|
|
110
|
+
export function multiSelectState<V extends string>(
|
|
111
|
+
value: readonly V[] | null | undefined,
|
|
112
|
+
optionValues: readonly V[],
|
|
113
|
+
): { all: boolean; selected: V[] };
|
|
114
|
+
/** The next value after a click on `v`, by the All rule. */
|
|
115
|
+
export function multiSelectToggle<V extends string>(
|
|
116
|
+
value: readonly V[],
|
|
117
|
+
optionValues: readonly V[],
|
|
118
|
+
v: V,
|
|
119
|
+
emptyMeansAll?: boolean,
|
|
120
|
+
): V[];
|
|
121
|
+
/** The trigger text: the All label, the one label, or "N selected" (`many(n)`). */
|
|
122
|
+
export function multiSelectText<V extends string>(
|
|
123
|
+
value: readonly V[],
|
|
124
|
+
options: ReadonlyArray<{ value: V; label: string }>,
|
|
125
|
+
allLabel?: string,
|
|
126
|
+
many?: (n: number) => string,
|
|
127
|
+
): string;
|
|
128
|
+
/**
|
|
129
|
+
* Close whichever kit popover (MultiSelect, FreshnessBadge) is open. Call it
|
|
130
|
+
* when an app's own menu opens: `onOpenChange={o => o && closePopovers()}`.
|
|
131
|
+
*/
|
|
132
|
+
export function closePopovers(): void;
|
|
133
|
+
|
|
95
134
|
export interface FreshnessSource {
|
|
96
135
|
key?: string;
|
|
97
136
|
label: React.ReactNode;
|
package/ui.js
CHANGED
|
@@ -12,6 +12,8 @@ import * as React from "react";
|
|
|
12
12
|
import { createPortal } from "react-dom";
|
|
13
13
|
import { bindingPhaseLabel, refreshBinding, useBindingProgress } from "./index.js";
|
|
14
14
|
import {
|
|
15
|
+
claimPopover,
|
|
16
|
+
closePopovers,
|
|
15
17
|
COLUMN_WIDTHS,
|
|
16
18
|
columnLayout,
|
|
17
19
|
compareValues,
|
|
@@ -20,11 +22,16 @@ import {
|
|
|
20
22
|
csvToTsv,
|
|
21
23
|
defaultColumnWidth,
|
|
22
24
|
formatSeconds as fmtSecs,
|
|
25
|
+
multiSelectAll,
|
|
26
|
+
multiSelectState,
|
|
27
|
+
multiSelectText,
|
|
28
|
+
multiSelectToggle,
|
|
23
29
|
nextSort,
|
|
24
30
|
normalizeTone,
|
|
25
31
|
pageSlice,
|
|
26
32
|
pageToSync,
|
|
27
33
|
refreshOrder,
|
|
34
|
+
releasePopover,
|
|
28
35
|
serializeResetKey,
|
|
29
36
|
sortRows,
|
|
30
37
|
toCsv,
|
|
@@ -34,6 +41,7 @@ import {
|
|
|
34
41
|
} from "./ui-core.js";
|
|
35
42
|
|
|
36
43
|
export {
|
|
44
|
+
closePopovers,
|
|
37
45
|
COLUMN_WIDTHS,
|
|
38
46
|
columnLayout,
|
|
39
47
|
compareValues,
|
|
@@ -41,6 +49,9 @@ export {
|
|
|
41
49
|
csvDate,
|
|
42
50
|
csvToTsv,
|
|
43
51
|
defaultColumnWidth,
|
|
52
|
+
multiSelectState,
|
|
53
|
+
multiSelectText,
|
|
54
|
+
multiSelectToggle,
|
|
44
55
|
nextSort,
|
|
45
56
|
normalizeTone,
|
|
46
57
|
pageSlice,
|
|
@@ -84,6 +95,8 @@ function svg(size, strokeWidth, children, className) {
|
|
|
84
95
|
}
|
|
85
96
|
const IconCheck = ({ size = 11 }) =>
|
|
86
97
|
svg(size, 3.2, [h("polyline", { key: "p", points: "20 6 9 17 4 12" })]);
|
|
98
|
+
const IconMinus = ({ size = 11 }) =>
|
|
99
|
+
svg(size, 3.2, [h("line", { key: "l", x1: 6, y1: 12, x2: 18, y2: 12 })]);
|
|
87
100
|
const IconChevron = ({ className }) =>
|
|
88
101
|
svg(12, 2.2, [h("polyline", { key: "p", points: "6 9 12 15 18 9" })], className);
|
|
89
102
|
const IconX = ({ size = 16 }) =>
|
|
@@ -102,25 +115,36 @@ const IconRefresh = () =>
|
|
|
102
115
|
const PHONE = "(max-width: 640px)";
|
|
103
116
|
|
|
104
117
|
/**
|
|
105
|
-
*
|
|
106
|
-
*
|
|
107
|
-
*
|
|
108
|
-
*
|
|
109
|
-
*
|
|
118
|
+
* One kit popover is open at a time: opening this one closes any other (a
|
|
119
|
+
* module-level registry, see claimPopover). It closes on Escape, on a
|
|
120
|
+
* pointerdown outside it, and when focus moves outside it. Pointerdown, in
|
|
121
|
+
* the capture phase, because Radix menu triggers cancel the mouse events of
|
|
122
|
+
* their pointerdown, so an outside mousedown/click listener never hears them.
|
|
123
|
+
*
|
|
124
|
+
* It also keeps the `.mk-pop` inside `wrapRef` within the viewport: it opens
|
|
125
|
+
* under its trigger aligned to `align`, flips to the other edge when that
|
|
126
|
+
* would overflow, and as a last resort is pinned to the viewport margin. On
|
|
127
|
+
* phones the popover is a bottom sheet (CSS) and is left alone.
|
|
110
128
|
*/
|
|
111
129
|
function usePopover(open, setOpen, wrapRef, align) {
|
|
112
130
|
React.useEffect(() => {
|
|
113
131
|
if (!open) return undefined;
|
|
114
|
-
const
|
|
115
|
-
|
|
132
|
+
const close = () => setOpen(false);
|
|
133
|
+
claimPopover(close);
|
|
134
|
+
const outside = e => {
|
|
135
|
+
const wrap = wrapRef.current;
|
|
136
|
+
if (wrap && e.target instanceof Node && !wrap.contains(e.target)) close();
|
|
116
137
|
};
|
|
117
138
|
const onKey = e => {
|
|
118
|
-
if (e.key === "Escape")
|
|
139
|
+
if (e.key === "Escape") close();
|
|
119
140
|
};
|
|
120
|
-
document.addEventListener("
|
|
141
|
+
document.addEventListener("pointerdown", outside, true);
|
|
142
|
+
document.addEventListener("focusin", outside, true);
|
|
121
143
|
document.addEventListener("keydown", onKey);
|
|
122
144
|
return () => {
|
|
123
|
-
|
|
145
|
+
releasePopover(close);
|
|
146
|
+
document.removeEventListener("pointerdown", outside, true);
|
|
147
|
+
document.removeEventListener("focusin", outside, true);
|
|
124
148
|
document.removeEventListener("keydown", onKey);
|
|
125
149
|
};
|
|
126
150
|
}, [open, setOpen, wrapRef]);
|
|
@@ -156,6 +180,20 @@ function usePopover(open, setOpen, wrapRef, align) {
|
|
|
156
180
|
}, [open, wrapRef, align]);
|
|
157
181
|
}
|
|
158
182
|
|
|
183
|
+
/**
|
|
184
|
+
* onClick for a popover panel. An app may wrap a kit control in a <label>
|
|
185
|
+
* (a natural form-field wrapper). A click on plain content inside the label,
|
|
186
|
+
* like a MultiSelect option row, runs the label's activation behaviour: the
|
|
187
|
+
* browser clicks the labelled control, the popover's trigger, which closed
|
|
188
|
+
* the menu. Cancelling the click stops that. Clicks on links, buttons and
|
|
189
|
+
* form fields keep their default, and the label ignores those anyway.
|
|
190
|
+
*/
|
|
191
|
+
const LABEL_SAFE = "a[href],button,input,select,textarea,summary";
|
|
192
|
+
function cancelLabelActivation(e) {
|
|
193
|
+
const t = e.target;
|
|
194
|
+
if (!(t instanceof Element) || !t.closest(LABEL_SAFE)) e.preventDefault();
|
|
195
|
+
}
|
|
196
|
+
|
|
159
197
|
// ---------------------------------------------------------------------------
|
|
160
198
|
// Page blocks
|
|
161
199
|
// ---------------------------------------------------------------------------
|
|
@@ -621,19 +659,39 @@ export function DataTable({
|
|
|
621
659
|
}
|
|
622
660
|
|
|
623
661
|
// ---------------------------------------------------------------------------
|
|
624
|
-
// MultiSelect
|
|
662
|
+
// Checkbox + MultiSelect
|
|
625
663
|
// ---------------------------------------------------------------------------
|
|
626
664
|
function toOption(o) {
|
|
627
665
|
return typeof o === "string" ? { value: o, label: o } : o;
|
|
628
666
|
}
|
|
629
667
|
|
|
668
|
+
/**
|
|
669
|
+
* The kit's one checkbox, drawn only (the row around it takes the click).
|
|
670
|
+
* `checked`: true, false or "mixed". Colours come from the theme tokens
|
|
671
|
+
* (--brand with a white tick when ticked, --input border when not), so every
|
|
672
|
+
* menu on a page, the kit's or the app's own Radix menus, ticks the same way.
|
|
673
|
+
*/
|
|
674
|
+
export function Checkbox({ checked = false, className }) {
|
|
675
|
+
const on = checked === true || checked === "mixed";
|
|
676
|
+
return h(
|
|
677
|
+
"span",
|
|
678
|
+
{ className: cx("mk-ms-box", on && "on", className), "aria-hidden": "true" },
|
|
679
|
+
checked === "mixed" ? h(IconMinus) : on ? h(IconCheck) : null,
|
|
680
|
+
);
|
|
681
|
+
}
|
|
682
|
+
|
|
630
683
|
/**
|
|
631
684
|
* Multi-select filter (shadcn combobox look). Controlled: `value` is the
|
|
632
685
|
* selected values, `onChange` gets the next array.
|
|
633
686
|
*
|
|
634
|
-
*
|
|
635
|
-
*
|
|
636
|
-
*
|
|
687
|
+
* The All rule: nothing is filtered when `value` is empty or holds every
|
|
688
|
+
* option. The All row is then ticked and every option row unticked. A click
|
|
689
|
+
* on an option under All selects only that option. Unticking the last
|
|
690
|
+
* option, or ticking the last missing one, returns to All. The trigger reads
|
|
691
|
+
* the All label, the one selected label, or "N selected".
|
|
692
|
+
*
|
|
693
|
+
* `emptyMeansAll` (default true) only picks what All emits: [] by default,
|
|
694
|
+
* every option's value when false.
|
|
637
695
|
*/
|
|
638
696
|
export function MultiSelect({
|
|
639
697
|
options,
|
|
@@ -642,7 +700,7 @@ export function MultiSelect({
|
|
|
642
700
|
allLabel = "All",
|
|
643
701
|
emptyMeansAll = true,
|
|
644
702
|
showOnly = false,
|
|
645
|
-
|
|
703
|
+
formatMany,
|
|
646
704
|
align = "start",
|
|
647
705
|
className,
|
|
648
706
|
"aria-label": ariaLabel,
|
|
@@ -651,16 +709,15 @@ export function MultiSelect({
|
|
|
651
709
|
const ref = React.useRef(null);
|
|
652
710
|
usePopover(open, setOpen, ref, align);
|
|
653
711
|
const opts = options.map(toOption);
|
|
654
|
-
const
|
|
655
|
-
const
|
|
656
|
-
|
|
657
|
-
const
|
|
658
|
-
onChange(value.includes(v) ? value.filter(x => x !== v) : [...value, v]);
|
|
659
|
-
const all = () => onChange(emptyMeansAll ? [] : opts.map(o => o.value));
|
|
712
|
+
const values = opts.map(o => o.value);
|
|
713
|
+
const { all: allOn, selected } = multiSelectState(value, values);
|
|
714
|
+
const text = multiSelectText(value, opts, allLabel, formatMany);
|
|
715
|
+
const single = selected.length === 1 ? opts.find(o => o.value === selected[0]) : null;
|
|
660
716
|
|
|
661
|
-
const
|
|
717
|
+
const toggle = v => onChange(multiSelectToggle(value, values, v, emptyMeansAll));
|
|
718
|
+
const all = () => onChange(multiSelectAll(values, emptyMeansAll));
|
|
662
719
|
const dot = o =>
|
|
663
|
-
o
|
|
720
|
+
o?.color ? h("span", { className: "mk-ms-swatch", style: { background: o.color } }) : null;
|
|
664
721
|
|
|
665
722
|
return h(
|
|
666
723
|
"div",
|
|
@@ -673,38 +730,32 @@ export function MultiSelect({
|
|
|
673
730
|
"aria-haspopup": "listbox",
|
|
674
731
|
"aria-expanded": open,
|
|
675
732
|
"aria-label": ariaLabel,
|
|
733
|
+
title: selected.length > 1
|
|
734
|
+
? selected.map(v => opts.find(o => o.value === v)?.label ?? v).join(", ")
|
|
735
|
+
: undefined,
|
|
676
736
|
onClick: () => setOpen(o => !o),
|
|
677
737
|
},
|
|
678
|
-
h(
|
|
679
|
-
"span",
|
|
680
|
-
{ className: "mk-ms-value" },
|
|
681
|
-
allOn || chosen.length === 0
|
|
682
|
-
? h("span", null, allLabel)
|
|
683
|
-
: h(
|
|
684
|
-
"span",
|
|
685
|
-
{ className: "mk-ms-chips" },
|
|
686
|
-
...chosen.slice(0, maxChips).map(o =>
|
|
687
|
-
h("span", { key: o.value, className: "mk-ms-chip" }, dot(o), o.label),
|
|
688
|
-
),
|
|
689
|
-
chosen.length > maxChips &&
|
|
690
|
-
h("span", { key: "+", className: "mk-ms-chip mk-ms-more" }, `+${chosen.length - maxChips}`),
|
|
691
|
-
),
|
|
692
|
-
),
|
|
738
|
+
h("span", { className: "mk-ms-value" }, dot(single), h("span", { className: "mk-ms-text" }, text)),
|
|
693
739
|
h(IconChevron, { className: "mk-ms-caret" }),
|
|
694
740
|
),
|
|
695
741
|
open &&
|
|
696
742
|
h(
|
|
697
743
|
"div",
|
|
698
|
-
{
|
|
744
|
+
{
|
|
745
|
+
className: "mk-pop mk-ms-menu",
|
|
746
|
+
role: "listbox",
|
|
747
|
+
"aria-multiselectable": true,
|
|
748
|
+
onClick: cancelLabelActivation,
|
|
749
|
+
},
|
|
699
750
|
h(
|
|
700
751
|
"button",
|
|
701
|
-
{ type: "button", className: cx("mk-ms-opt", allOn && "active"), onClick: all },
|
|
702
|
-
|
|
752
|
+
{ type: "button", className: cx("mk-ms-opt", allOn && "active"), "aria-pressed": allOn, onClick: all },
|
|
753
|
+
h(Checkbox, { checked: allOn }),
|
|
703
754
|
h("span", { className: "mk-ms-label" }, allLabel),
|
|
704
755
|
),
|
|
705
756
|
h("div", { className: "mk-ms-sep" }),
|
|
706
757
|
...opts.map(o => {
|
|
707
|
-
const on =
|
|
758
|
+
const on = selected.includes(o.value);
|
|
708
759
|
return h(
|
|
709
760
|
"div",
|
|
710
761
|
{
|
|
@@ -721,7 +772,7 @@ export function MultiSelect({
|
|
|
721
772
|
}
|
|
722
773
|
},
|
|
723
774
|
},
|
|
724
|
-
|
|
775
|
+
h(Checkbox, { checked: on }),
|
|
725
776
|
dot(o),
|
|
726
777
|
h("span", { className: "mk-ms-label" }, o.label),
|
|
727
778
|
showOnly &&
|
|
@@ -732,7 +783,7 @@ export function MultiSelect({
|
|
|
732
783
|
className: "mk-ms-only",
|
|
733
784
|
onClick: e => {
|
|
734
785
|
e.stopPropagation();
|
|
735
|
-
onChange([o.value
|
|
786
|
+
onChange(multiSelectToggle([], values, o.value, emptyMeansAll));
|
|
736
787
|
},
|
|
737
788
|
},
|
|
738
789
|
"Only",
|
|
@@ -795,7 +846,12 @@ export function FreshnessBadge({
|
|
|
795
846
|
hasPanel &&
|
|
796
847
|
h(
|
|
797
848
|
"div",
|
|
798
|
-
{
|
|
849
|
+
{
|
|
850
|
+
className: "mk-pop mk-fresh-pop",
|
|
851
|
+
role: "dialog",
|
|
852
|
+
"aria-label": "Data freshness",
|
|
853
|
+
onClick: cancelLabelActivation,
|
|
854
|
+
},
|
|
799
855
|
(title != null || meta != null) &&
|
|
800
856
|
h(
|
|
801
857
|
"div",
|