@makoai/app-sdk 2.19.0 → 2.20.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 +89 -11
- package/index.js +1 -1
- package/menu/checkbox.tsx +36 -0
- package/menu/dropdown-menu.tsx +205 -0
- package/menu/filter-menu.tsx +312 -0
- package/menu/index.ts +10 -0
- package/menu/utils.ts +7 -0
- package/package.json +31 -3
- package/ui-core.js +165 -0
- package/ui.css +16 -1
- package/ui.d.ts +92 -5
- package/ui.js +207 -73
|
@@ -0,0 +1,312 @@
|
|
|
1
|
+
// FilterMenu: one filter dropdown for flat and nested option lists, on stock
|
|
2
|
+
// shadcn DropdownMenu. Ported 1:1 from the apps' NestedFilterMenu
|
|
3
|
+
// (web-performance, hasura-performance, quarterly-requalification-rate):
|
|
4
|
+
// "All …" first, then the options; a group opens as a hover submenu (or with
|
|
5
|
+
// the arrow key) that starts with its own "All <group>" row. Each row has a
|
|
6
|
+
// checkbox (a tick when every leaf under it is picked, a dash when some are)
|
|
7
|
+
// and an "Only" action on hover. The menu stays open across toggles. Rows
|
|
8
|
+
// carry no hover descriptions: definitions live in the filter's info note.
|
|
9
|
+
//
|
|
10
|
+
// The selection model is the kit's (2.20, `filter*` in ui-core.js): a
|
|
11
|
+
// selection is a list of leaf values, [] means All, and "nothing ticked" is
|
|
12
|
+
// a draft that emits nothing until the next tick.
|
|
13
|
+
import * as React from "react";
|
|
14
|
+
import { ChevronDown, ChevronRight } from "lucide-react";
|
|
15
|
+
import { DropdownMenu as DropdownMenuPrimitive } from "radix-ui";
|
|
16
|
+
import {
|
|
17
|
+
closePopovers,
|
|
18
|
+
filterAllTick,
|
|
19
|
+
filterLeaves,
|
|
20
|
+
filterLeavesOf,
|
|
21
|
+
filterNormalize,
|
|
22
|
+
filterText,
|
|
23
|
+
filterTick,
|
|
24
|
+
filterToggle,
|
|
25
|
+
filterToggleAll,
|
|
26
|
+
type FilterTick,
|
|
27
|
+
} from "../ui.js";
|
|
28
|
+
import { Checkbox } from "./checkbox";
|
|
29
|
+
import {
|
|
30
|
+
DropdownMenu,
|
|
31
|
+
DropdownMenuContent,
|
|
32
|
+
DropdownMenuSeparator,
|
|
33
|
+
DropdownMenuSub,
|
|
34
|
+
DropdownMenuSubContent,
|
|
35
|
+
DropdownMenuSubTrigger,
|
|
36
|
+
DropdownMenuTrigger,
|
|
37
|
+
} from "./dropdown-menu";
|
|
38
|
+
|
|
39
|
+
export interface FilterMenuOption {
|
|
40
|
+
value: string;
|
|
41
|
+
label: string;
|
|
42
|
+
/** A second, muted line under a leaf's label. */
|
|
43
|
+
description?: string;
|
|
44
|
+
/** What the option covers. Not shown on the row: definitions live in the filter's info note. */
|
|
45
|
+
title?: string;
|
|
46
|
+
/** A group: a hover submenu that starts with "All <label>". */
|
|
47
|
+
children?: FilterMenuOption[];
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const ROW =
|
|
51
|
+
"mk-fm-row relative flex cursor-pointer items-center gap-2 rounded-sm py-1.5 pr-2 pl-2 text-sm outline-hidden select-none focus:bg-accent focus:text-accent-foreground";
|
|
52
|
+
|
|
53
|
+
const asChecked = (s: FilterTick) => (s === "on" ? true : s === "mixed" ? ("indeterminate" as const) : false);
|
|
54
|
+
|
|
55
|
+
/** The row's checkbox, drawn only: the row (or its wrapper) takes the click. */
|
|
56
|
+
function Tick(props: { state: FilterTick }) {
|
|
57
|
+
return <Checkbox checked={asChecked(props.state)} tabIndex={-1} aria-hidden className="pointer-events-none" />;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export interface FilterMenuItemsProps {
|
|
61
|
+
options: FilterMenuOption[];
|
|
62
|
+
/** Picked leaf values; [] (or every leaf) = All. `value` is an alias. */
|
|
63
|
+
selected?: readonly string[];
|
|
64
|
+
value?: readonly string[];
|
|
65
|
+
onChange: (selected: string[]) => void;
|
|
66
|
+
/** The first row ("All traffic"). */
|
|
67
|
+
allLabel: string;
|
|
68
|
+
/** What All emits: [] (default) or every leaf value (false). */
|
|
69
|
+
emptyMeansAll?: boolean;
|
|
70
|
+
/** The "nothing ticked" draft, when the owner shows it (FilterMenu's trigger). Uncontrolled otherwise. */
|
|
71
|
+
none?: boolean;
|
|
72
|
+
onNoneChange?: (none: boolean) => void;
|
|
73
|
+
/**
|
|
74
|
+
* The phone Filters menu, where a submenu has no room beside its parent: a
|
|
75
|
+
* group row opens in place instead (`drill.open(node)`), and `root` then
|
|
76
|
+
* lists that group's rows, "All <group>" first.
|
|
77
|
+
*/
|
|
78
|
+
drill?: { open: (node: FilterMenuOption) => void };
|
|
79
|
+
root?: FilterMenuOption;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* The menu's rows ("All …", a separator, the options and their submenus), for
|
|
84
|
+
* FilterMenu's own dropdown and for an app's combined toolbar menu, where
|
|
85
|
+
* they fill one filter's submenu.
|
|
86
|
+
*/
|
|
87
|
+
export function FilterMenuItems(props: FilterMenuItemsProps) {
|
|
88
|
+
const { options, drill, emptyMeansAll = true } = props;
|
|
89
|
+
const selected = filterNormalize(options, props.selected ?? props.value ?? []);
|
|
90
|
+
const [ownNone, setOwnNone] = React.useState(false);
|
|
91
|
+
const none = props.none ?? ownNone;
|
|
92
|
+
const setNone = props.onNoneChange ?? setOwnNone;
|
|
93
|
+
|
|
94
|
+
const emit = (next: string[]) => {
|
|
95
|
+
setNone(false);
|
|
96
|
+
props.onChange(next.length ? next : emptyMeansAll ? [] : filterLeaves(options));
|
|
97
|
+
};
|
|
98
|
+
const apply = (r: { none: boolean; selected: string[] }) => (r.none ? setNone(true) : emit(r.selected));
|
|
99
|
+
const flip = (node: FilterMenuOption) => apply(filterToggle(options, selected, node, none));
|
|
100
|
+
const tick = (node: FilterMenuOption) => filterTick(node, selected, none);
|
|
101
|
+
|
|
102
|
+
const only = (node: FilterMenuOption) => (e: React.MouseEvent) => {
|
|
103
|
+
e.preventDefault();
|
|
104
|
+
e.stopPropagation();
|
|
105
|
+
emit(filterNormalize(options, filterLeavesOf(node)));
|
|
106
|
+
};
|
|
107
|
+
// A group with no children yet picks nothing: no Only, and its box is inert.
|
|
108
|
+
const empty = (node: FilterMenuOption) => filterLeavesOf(node).length === 0;
|
|
109
|
+
const onlyButton = (node: FilterMenuOption) =>
|
|
110
|
+
empty(node) ? null : (
|
|
111
|
+
<span role="button" tabIndex={-1} className="mk-fm-only" onClick={only(node)} onPointerUp={e => e.stopPropagation()}>
|
|
112
|
+
Only
|
|
113
|
+
</span>
|
|
114
|
+
);
|
|
115
|
+
|
|
116
|
+
const leafRow = (node: FilterMenuOption) => {
|
|
117
|
+
const st = tick(node);
|
|
118
|
+
return (
|
|
119
|
+
<DropdownMenuPrimitive.CheckboxItem
|
|
120
|
+
key={node.value}
|
|
121
|
+
className={ROW}
|
|
122
|
+
checked={asChecked(st)}
|
|
123
|
+
onSelect={e => {
|
|
124
|
+
e.preventDefault();
|
|
125
|
+
flip(node);
|
|
126
|
+
}}
|
|
127
|
+
>
|
|
128
|
+
<Tick state={st} />
|
|
129
|
+
{node.description ? (
|
|
130
|
+
<span className="min-w-0 flex-1">
|
|
131
|
+
<span className="block truncate">{node.label}</span>
|
|
132
|
+
<span className="block text-xs text-muted-foreground">{node.description}</span>
|
|
133
|
+
</span>
|
|
134
|
+
) : (
|
|
135
|
+
<span className="flex-1 truncate">{node.label}</span>
|
|
136
|
+
)}
|
|
137
|
+
{onlyButton(node)}
|
|
138
|
+
</DropdownMenuPrimitive.CheckboxItem>
|
|
139
|
+
);
|
|
140
|
+
};
|
|
141
|
+
|
|
142
|
+
/** A group's box: toggles the whole group; the rest of the row opens it. */
|
|
143
|
+
const groupBox = (node: FilterMenuOption, st: FilterTick) => (
|
|
144
|
+
<span
|
|
145
|
+
role="checkbox"
|
|
146
|
+
aria-checked={st === "on" ? true : st === "mixed" ? "mixed" : false}
|
|
147
|
+
aria-label={`All ${node.label}`}
|
|
148
|
+
aria-disabled={empty(node) || undefined}
|
|
149
|
+
className="flex"
|
|
150
|
+
onClick={e => {
|
|
151
|
+
e.preventDefault();
|
|
152
|
+
e.stopPropagation();
|
|
153
|
+
if (!empty(node)) flip(node);
|
|
154
|
+
}}
|
|
155
|
+
onPointerUp={e => e.stopPropagation()}
|
|
156
|
+
>
|
|
157
|
+
<Tick state={st} />
|
|
158
|
+
</span>
|
|
159
|
+
);
|
|
160
|
+
|
|
161
|
+
/** "All <group>", first in a group's own list. */
|
|
162
|
+
const groupAll = (node: FilterMenuOption, st: FilterTick) => (
|
|
163
|
+
<DropdownMenuPrimitive.CheckboxItem
|
|
164
|
+
className={ROW}
|
|
165
|
+
checked={asChecked(st)}
|
|
166
|
+
onSelect={e => {
|
|
167
|
+
e.preventDefault();
|
|
168
|
+
flip(node);
|
|
169
|
+
}}
|
|
170
|
+
>
|
|
171
|
+
<Tick state={st} />
|
|
172
|
+
<span className="flex-1">All {node.label}</span>
|
|
173
|
+
</DropdownMenuPrimitive.CheckboxItem>
|
|
174
|
+
);
|
|
175
|
+
|
|
176
|
+
const level = (list: FilterMenuOption[]): React.ReactNode[] =>
|
|
177
|
+
list.map(node => {
|
|
178
|
+
if (!node.children) return leafRow(node);
|
|
179
|
+
const st = tick(node);
|
|
180
|
+
if (drill)
|
|
181
|
+
return (
|
|
182
|
+
<DropdownMenuPrimitive.Item
|
|
183
|
+
key={node.value}
|
|
184
|
+
className={`${ROW} mk-fm-drill`}
|
|
185
|
+
onSelect={e => {
|
|
186
|
+
e.preventDefault();
|
|
187
|
+
drill.open(node);
|
|
188
|
+
}}
|
|
189
|
+
onKeyDown={e => {
|
|
190
|
+
if (e.key === "ArrowRight") {
|
|
191
|
+
e.preventDefault();
|
|
192
|
+
drill.open(node);
|
|
193
|
+
}
|
|
194
|
+
}}
|
|
195
|
+
>
|
|
196
|
+
{groupBox(node, st)}
|
|
197
|
+
<span className="flex-1 truncate">{node.label}</span>
|
|
198
|
+
{onlyButton(node)}
|
|
199
|
+
<ChevronRight className="size-4 text-muted-foreground" aria-hidden />
|
|
200
|
+
</DropdownMenuPrimitive.Item>
|
|
201
|
+
);
|
|
202
|
+
return (
|
|
203
|
+
<DropdownMenuSub key={node.value}>
|
|
204
|
+
<DropdownMenuSubTrigger className="mk-fm-row cursor-pointer gap-2 pl-2">
|
|
205
|
+
{groupBox(node, st)}
|
|
206
|
+
<span className="flex-1 truncate">{node.label}</span>
|
|
207
|
+
{onlyButton(node)}
|
|
208
|
+
</DropdownMenuSubTrigger>
|
|
209
|
+
<DropdownMenuSubContent className="max-h-[70vh] min-w-48 overflow-y-auto">
|
|
210
|
+
{groupAll(node, st)}
|
|
211
|
+
<DropdownMenuSeparator />
|
|
212
|
+
{level(node.children)}
|
|
213
|
+
</DropdownMenuSubContent>
|
|
214
|
+
</DropdownMenuSub>
|
|
215
|
+
);
|
|
216
|
+
});
|
|
217
|
+
|
|
218
|
+
if (props.root?.children)
|
|
219
|
+
return (
|
|
220
|
+
<>
|
|
221
|
+
{groupAll(props.root, tick(props.root))}
|
|
222
|
+
<DropdownMenuSeparator />
|
|
223
|
+
{level(props.root.children)}
|
|
224
|
+
</>
|
|
225
|
+
);
|
|
226
|
+
const allSt = filterAllTick(selected, none);
|
|
227
|
+
return (
|
|
228
|
+
<>
|
|
229
|
+
<DropdownMenuPrimitive.CheckboxItem
|
|
230
|
+
className={ROW}
|
|
231
|
+
checked={asChecked(allSt)}
|
|
232
|
+
onSelect={e => {
|
|
233
|
+
e.preventDefault();
|
|
234
|
+
apply(filterToggleAll(selected, none));
|
|
235
|
+
}}
|
|
236
|
+
>
|
|
237
|
+
<Tick state={allSt} />
|
|
238
|
+
<span className="flex-1">{props.allLabel}</span>
|
|
239
|
+
</DropdownMenuPrimitive.CheckboxItem>
|
|
240
|
+
<DropdownMenuSeparator />
|
|
241
|
+
{level(options)}
|
|
242
|
+
</>
|
|
243
|
+
);
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
export interface FilterMenuProps extends Omit<FilterMenuItemsProps, "none" | "onNoneChange" | "drill" | "root"> {
|
|
247
|
+
/** The accessible name of the trigger ("Traffic"). */
|
|
248
|
+
label: string;
|
|
249
|
+
/** Fixed-width trigger class (no layout shift). */
|
|
250
|
+
triggerClassName?: string;
|
|
251
|
+
/** Trigger text; default filterText(): All, the one picked label, or the picked parts ("Pages + Assets: Images"). */
|
|
252
|
+
summary?: string;
|
|
253
|
+
/** "N selected" for a flat list. */
|
|
254
|
+
formatMany?: (n: number) => string;
|
|
255
|
+
/** The trigger while nothing is ticked (the draft). Default "None". */
|
|
256
|
+
noneLabel?: string;
|
|
257
|
+
/**
|
|
258
|
+
* More texts the trigger is sized to, besides allLabel and the top-level
|
|
259
|
+
* labels (e.g. its default pick, "Pages + Data"). A longer pick ends in an
|
|
260
|
+
* ellipsis inside the trigger, whole in its title.
|
|
261
|
+
*/
|
|
262
|
+
probes?: readonly string[];
|
|
263
|
+
/** Replaces allLabel and the top-level labels as what the trigger is sized to. */
|
|
264
|
+
sizeTo?: readonly string[];
|
|
265
|
+
/** Menu alignment against the trigger. Default "start". */
|
|
266
|
+
align?: "start" | "center" | "end";
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
/** The filter dropdown: the kit's trigger (`mk-ms-trigger`) over FilterMenuItems. */
|
|
270
|
+
export function FilterMenu(props: FilterMenuProps) {
|
|
271
|
+
const { options, allLabel, noneLabel = "None" } = props;
|
|
272
|
+
const selected = filterNormalize(options, props.selected ?? props.value ?? []);
|
|
273
|
+
const [none, setNone] = React.useState(false);
|
|
274
|
+
const text = none ? noneLabel : (props.summary ?? filterText(options, selected, allLabel, props.formatMany));
|
|
275
|
+
const probes = [...(props.sizeTo ?? [allLabel, ...options.map(o => o.label)]), ...(props.probes ?? [])];
|
|
276
|
+
return (
|
|
277
|
+
<DropdownMenu
|
|
278
|
+
onOpenChange={open => {
|
|
279
|
+
if (open) closePopovers();
|
|
280
|
+
else setNone(false);
|
|
281
|
+
}}
|
|
282
|
+
>
|
|
283
|
+
<DropdownMenuTrigger asChild>
|
|
284
|
+
<button type="button" className={`mk-ms-trigger ${props.triggerClassName ?? ""}`} aria-label={props.label} title={text}>
|
|
285
|
+
<span className="mk-ms-value">
|
|
286
|
+
<span className="mk-ms-fit">
|
|
287
|
+
{/* The text first: a grid's baseline is its first item's, and the probes are 0 px tall. */}
|
|
288
|
+
<span className="mk-ms-text">{text}</span>
|
|
289
|
+
{probes.map((p, i) => (
|
|
290
|
+
<span key={i} className="mk-ms-probe" aria-hidden>
|
|
291
|
+
{p}
|
|
292
|
+
</span>
|
|
293
|
+
))}
|
|
294
|
+
</span>
|
|
295
|
+
</span>
|
|
296
|
+
<ChevronDown className="mk-ms-caret" size={16} />
|
|
297
|
+
</button>
|
|
298
|
+
</DropdownMenuTrigger>
|
|
299
|
+
<DropdownMenuContent align={props.align ?? "start"} className="max-h-[70vh] min-w-52 overflow-y-auto">
|
|
300
|
+
<FilterMenuItems
|
|
301
|
+
options={options}
|
|
302
|
+
selected={selected}
|
|
303
|
+
onChange={props.onChange}
|
|
304
|
+
allLabel={allLabel}
|
|
305
|
+
emptyMeansAll={props.emptyMeansAll}
|
|
306
|
+
none={none}
|
|
307
|
+
onNoneChange={setNone}
|
|
308
|
+
/>
|
|
309
|
+
</DropdownMenuContent>
|
|
310
|
+
</DropdownMenu>
|
|
311
|
+
);
|
|
312
|
+
}
|
package/menu/index.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
// @makoai/app-sdk/menu (2.20): stock shadcn/ui DropdownMenu and Checkbox
|
|
2
|
+
// (new-york, as shipped; the Checkbox adds the indeterminate dash), and the
|
|
3
|
+
// FilterMenu built on them. TypeScript source: the app's Vite compiles it.
|
|
4
|
+
// Needs the app's Tailwind to see these classes: `@source
|
|
5
|
+
// "../node_modules/@makoai/app-sdk/menu";` in its CSS (README, "Menus").
|
|
6
|
+
export * from "./dropdown-menu";
|
|
7
|
+
export { Checkbox } from "./checkbox";
|
|
8
|
+
export { cn } from "./utils";
|
|
9
|
+
export { FilterMenu, FilterMenuItems } from "./filter-menu";
|
|
10
|
+
export type { FilterMenuItemsProps, FilterMenuOption, FilterMenuProps } from "./filter-menu";
|
package/menu/utils.ts
ADDED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@makoai/app-sdk",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.20.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": {
|
|
@@ -36,6 +36,10 @@
|
|
|
36
36
|
"types": "./ui.d.ts",
|
|
37
37
|
"default": "./ui.js"
|
|
38
38
|
},
|
|
39
|
+
"./menu": {
|
|
40
|
+
"types": "./menu/index.ts",
|
|
41
|
+
"default": "./menu/index.ts"
|
|
42
|
+
},
|
|
39
43
|
"./ui.css": "./ui.css"
|
|
40
44
|
},
|
|
41
45
|
"files": [
|
|
@@ -51,11 +55,30 @@
|
|
|
51
55
|
"ui-core.js",
|
|
52
56
|
"ui.d.ts",
|
|
53
57
|
"ui.css",
|
|
58
|
+
"menu/",
|
|
54
59
|
"README.md"
|
|
55
60
|
],
|
|
56
61
|
"peerDependencies": {
|
|
62
|
+
"clsx": ">=2",
|
|
63
|
+
"lucide-react": ">=0.400",
|
|
64
|
+
"radix-ui": ">=1.4",
|
|
57
65
|
"react": ">=18",
|
|
58
|
-
"react-dom": ">=18"
|
|
66
|
+
"react-dom": ">=18",
|
|
67
|
+
"tailwind-merge": ">=2"
|
|
68
|
+
},
|
|
69
|
+
"peerDependenciesMeta": {
|
|
70
|
+
"clsx": {
|
|
71
|
+
"optional": true
|
|
72
|
+
},
|
|
73
|
+
"lucide-react": {
|
|
74
|
+
"optional": true
|
|
75
|
+
},
|
|
76
|
+
"radix-ui": {
|
|
77
|
+
"optional": true
|
|
78
|
+
},
|
|
79
|
+
"tailwind-merge": {
|
|
80
|
+
"optional": true
|
|
81
|
+
}
|
|
59
82
|
},
|
|
60
83
|
"publishConfig": {
|
|
61
84
|
"access": "public"
|
|
@@ -65,6 +88,11 @@
|
|
|
65
88
|
},
|
|
66
89
|
"devDependencies": {
|
|
67
90
|
"apache-arrow": "17.0.0",
|
|
68
|
-
"
|
|
91
|
+
"clsx": "^2.1.1",
|
|
92
|
+
"esbuild": "^0.28.2",
|
|
93
|
+
"jsdom": "^30.1.2",
|
|
94
|
+
"lucide-react": "^1.61.0",
|
|
95
|
+
"radix-ui": "^1.8.1",
|
|
96
|
+
"tailwind-merge": "^3.7.0"
|
|
69
97
|
}
|
|
70
98
|
}
|
package/ui-core.js
CHANGED
|
@@ -369,6 +369,171 @@ export function multiSelectText(value, options, allLabel = "All", many = n => `$
|
|
|
369
369
|
return many(selected.length);
|
|
370
370
|
}
|
|
371
371
|
|
|
372
|
+
// ---------------------------------------------------------------------------
|
|
373
|
+
// Filter trees (2.20): the model behind FilterItems and MultiSelect
|
|
374
|
+
// ---------------------------------------------------------------------------
|
|
375
|
+
// Options form a tree ({ value, label, children? }); a flat list is a one-level
|
|
376
|
+
// tree. A selection is a list of leaf values, and [] means every leaf (All).
|
|
377
|
+
// Groups never sit in a selection: a group is ticked when all its leaves are.
|
|
378
|
+
// `none` is the open menu's "nothing ticked" draft: a click on a full All
|
|
379
|
+
// unticks every row. It emits nothing (an empty filter still means
|
|
380
|
+
// everything): the data keeps its selection until a row is ticked.
|
|
381
|
+
|
|
382
|
+
/** Every leaf value under `options`, in menu order. */
|
|
383
|
+
export function filterLeaves(options) {
|
|
384
|
+
return options.flatMap(o => (o.children ? filterLeaves(o.children) : [o.value]));
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
/** The leaves under a node (a leaf is its own). */
|
|
388
|
+
export function filterLeavesOf(node) {
|
|
389
|
+
return node.children ? filterLeaves(node.children) : [node.value];
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
/** The node with `value` anywhere in the tree. */
|
|
393
|
+
export function filterFind(options, value) {
|
|
394
|
+
for (const o of options) {
|
|
395
|
+
if (o.value === value) return o;
|
|
396
|
+
const hit = o.children && filterFind(o.children, value);
|
|
397
|
+
if (hit) return hit;
|
|
398
|
+
}
|
|
399
|
+
return undefined;
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
/**
|
|
403
|
+
* A selection in menu order; [] when it holds every leaf. Values the options
|
|
404
|
+
* don't list (a URL pick before its options load, or one the current date
|
|
405
|
+
* range lacks) are kept at the end, so the trigger never reads "All" over
|
|
406
|
+
* filtered data and the next tick does not drop them.
|
|
407
|
+
*/
|
|
408
|
+
export function filterNormalize(options, picked) {
|
|
409
|
+
const set = new Set(picked);
|
|
410
|
+
const leaves = filterLeaves(options);
|
|
411
|
+
const known = new Set(leaves);
|
|
412
|
+
const out = leaves.filter(v => set.has(v));
|
|
413
|
+
const unknown = [...set].filter(v => !known.has(v));
|
|
414
|
+
if (leaves.length > 0 && out.length === leaves.length && !unknown.length) return [];
|
|
415
|
+
return [...out, ...unknown];
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
/** A node's tick: "on" when every leaf under it is picked, "mixed" when some are, "off" when none (or in the draft). */
|
|
419
|
+
export function filterTick(node, selected, none = false) {
|
|
420
|
+
const ls = filterLeavesOf(node);
|
|
421
|
+
// A group with no children yet (still loading) picks nothing: never "on".
|
|
422
|
+
if (none || !ls.length) return "off";
|
|
423
|
+
if (!selected.length) return "on";
|
|
424
|
+
const on = ls.filter(v => selected.includes(v)).length;
|
|
425
|
+
return on === ls.length ? "on" : on ? "mixed" : "off";
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
/** The All row's tick: "on" with every leaf picked, "mixed" with some, "off" in the draft. */
|
|
429
|
+
export function filterAllTick(selected, none = false) {
|
|
430
|
+
return none ? "off" : selected.length ? "mixed" : "on";
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
/**
|
|
434
|
+
* A click on All. Full All goes to the draft ({ none: true }); a partial All
|
|
435
|
+
* or the draft ticks every row ({ selected: [] }).
|
|
436
|
+
*/
|
|
437
|
+
export function filterToggleAll(selected, none = false) {
|
|
438
|
+
return filterAllTick(selected, none) === "on" ? { none: true, selected } : { none: false, selected: [] };
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
/**
|
|
442
|
+
* A click on a node: off when all its leaves are ticked, else on. From the
|
|
443
|
+
* draft a tick picks that node alone. When the click leaves nothing ticked
|
|
444
|
+
* the result is the draft ({ none: true }) and the selection is unchanged.
|
|
445
|
+
*/
|
|
446
|
+
export function filterToggle(options, selected, node, none = false) {
|
|
447
|
+
const ls = filterLeavesOf(node);
|
|
448
|
+
if (none) return { none: false, selected: filterNormalize(options, ls) };
|
|
449
|
+
const current = selected.length ? selected : filterLeaves(options);
|
|
450
|
+
const next = filterTick(node, selected) === "on" ? current.filter(v => !ls.includes(v)) : [...current, ...ls];
|
|
451
|
+
return next.length ? { none: false, selected: filterNormalize(options, next) } : { none: true, selected };
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
/** The one node whose leaves are exactly the pick (a leaf, or a whole group), else undefined. */
|
|
455
|
+
export function filterPickedNode(options, selected) {
|
|
456
|
+
const want = new Set(selected);
|
|
457
|
+
const walk = list => {
|
|
458
|
+
for (const o of list) {
|
|
459
|
+
const ls = filterLeavesOf(o);
|
|
460
|
+
if (ls.length === want.size && ls.every(v => want.has(v))) return o;
|
|
461
|
+
if (o.children && ls.some(v => want.has(v))) return walk(o.children);
|
|
462
|
+
}
|
|
463
|
+
return undefined;
|
|
464
|
+
};
|
|
465
|
+
return walk(options);
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
/**
|
|
469
|
+
* The trigger text: the All label; the one picked node's label ("404 Not
|
|
470
|
+
* found", a whole group "OpenAI"); for a flat list "N selected" (`many(n)`);
|
|
471
|
+
* for a tree its picked parts ("Pages + Assets: Images").
|
|
472
|
+
*/
|
|
473
|
+
export function filterText(options, selected, allLabel = "All", many = n => `${n} selected`) {
|
|
474
|
+
if (!selected.length) return allLabel;
|
|
475
|
+
const one = filterPickedNode(options, selected);
|
|
476
|
+
if (one) return one.label;
|
|
477
|
+
if (selected.length === 1) return selected[0]; // one value the options don't list (yet)
|
|
478
|
+
if (!options.some(o => o.children)) return many(selected.length);
|
|
479
|
+
const parts = list =>
|
|
480
|
+
list.flatMap(o => {
|
|
481
|
+
const st = filterTick(o, selected);
|
|
482
|
+
if (st === "on") return [o.label];
|
|
483
|
+
return st === "mixed" && o.children ? parts(o.children) : [];
|
|
484
|
+
});
|
|
485
|
+
// A value the options don't list (yet) reads as itself, never as All or "".
|
|
486
|
+
const unknown = selected.filter(v => !filterFind(options, v));
|
|
487
|
+
return [
|
|
488
|
+
...options.flatMap(o => {
|
|
489
|
+
const st = filterTick(o, selected);
|
|
490
|
+
if (st === "on") return [o.label];
|
|
491
|
+
if (st === "mixed" && o.children) return [`${o.label}: ${parts(o.children).join(", ")}`];
|
|
492
|
+
return [];
|
|
493
|
+
}),
|
|
494
|
+
...unknown,
|
|
495
|
+
].join(" + ");
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
const sameSet = (a, b) => a.length === b.length && a.every(v => b.includes(v));
|
|
499
|
+
|
|
500
|
+
/**
|
|
501
|
+
* The URL value of a selection: "" for the filter's default, "all" for every
|
|
502
|
+
* leaf (when the default is narrower), else a whole group by its own value
|
|
503
|
+
* and a partial one by its picked children, comma-separated.
|
|
504
|
+
*/
|
|
505
|
+
export function filterToParam(options, selectedIn, defaultsIn = []) {
|
|
506
|
+
// All emitted as every leaf (emptyMeansAll false) compares as [].
|
|
507
|
+
const selected = filterNormalize(options, selectedIn);
|
|
508
|
+
const defaults = filterNormalize(options, defaultsIn);
|
|
509
|
+
if (sameSet(selected, defaults)) return "";
|
|
510
|
+
if (!selected.length) return "all";
|
|
511
|
+
const walk = list =>
|
|
512
|
+
list.flatMap(o => {
|
|
513
|
+
const st = filterTick(o, selected);
|
|
514
|
+
if (st === "on") return [o.value];
|
|
515
|
+
return st === "mixed" && o.children ? walk(o.children) : [];
|
|
516
|
+
});
|
|
517
|
+
const unknown = selected.filter(v => !filterFind(options, v));
|
|
518
|
+
return [...walk(options), ...unknown].join(",");
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
/**
|
|
522
|
+
* A URL value back to a selection. `legacy` maps former values onto node
|
|
523
|
+
* values first. Values the options don't list are kept (a URL pick read
|
|
524
|
+
* before its options load stays a pick). Missing or empty: the default.
|
|
525
|
+
*/
|
|
526
|
+
export function filterFromParam(options, raw, defaults = [], legacy = {}) {
|
|
527
|
+
if (raw == null || raw === "") return defaults;
|
|
528
|
+
if (raw === "all") return [];
|
|
529
|
+
const picked = raw.split(",").flatMap(v => {
|
|
530
|
+
const node = filterFind(options, legacy[v] ?? v);
|
|
531
|
+
return node ? filterLeavesOf(node) : [v];
|
|
532
|
+
});
|
|
533
|
+
if (!picked.length) return defaults;
|
|
534
|
+
return filterNormalize(options, picked);
|
|
535
|
+
}
|
|
536
|
+
|
|
372
537
|
// ---------------------------------------------------------------------------
|
|
373
538
|
// One open popover at a time
|
|
374
539
|
// ---------------------------------------------------------------------------
|
package/ui.css
CHANGED
|
@@ -286,6 +286,11 @@ button, input, select, textarea { font-family: inherit; }
|
|
|
286
286
|
.mk-ms-value { display: flex; align-items: center; gap: 6px; min-width: 0; overflow: hidden; }
|
|
287
287
|
/* The trigger reads "All", the one selected label, or "N selected". */
|
|
288
288
|
.mk-ms-text { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
|
|
289
|
+
/* The trigger is as wide as its longest probe (every label it can show): picks never move the bar. */
|
|
290
|
+
.mk-ms-fit { display: inline-grid; grid-template-columns: minmax(0, auto); min-width: 0; text-align: left; }
|
|
291
|
+
.mk-ms-fit > * { grid-area: 1 / 1; white-space: nowrap; }
|
|
292
|
+
.mk-ms-probe { visibility: hidden; height: 0; overflow: hidden; }
|
|
293
|
+
.mk-ms-fit > .mk-ms-text { width: 0; min-width: 100%; }
|
|
289
294
|
.mk-ms-caret { color: var(--muted-foreground); flex-shrink: 0; transition: transform .15s ease; }
|
|
290
295
|
.mk-ms-trigger.open .mk-ms-caret { transform: rotate(180deg); }
|
|
291
296
|
.mk-ms-menu {
|
|
@@ -301,7 +306,11 @@ button, input, select, textarea { font-family: inherit; }
|
|
|
301
306
|
}
|
|
302
307
|
.mk-ms-opt:hover, .mk-ms-opt:focus-visible { background: var(--accent); outline: none; }
|
|
303
308
|
.mk-ms-opt.active { font-weight: 500; }
|
|
304
|
-
.mk-ms-label { flex: 1; }
|
|
309
|
+
.mk-ms-label { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; }
|
|
310
|
+
/* FilterItems: the rows of one filter, in a MultiSelect or an app's own menu. */
|
|
311
|
+
.mk-ms-items { display: flex; flex-direction: column; gap: 1px; }
|
|
312
|
+
.mk-ms-all { font-weight: 500; }
|
|
313
|
+
.mk-ms-group .mk-ms-label { font-weight: 500; }
|
|
305
314
|
/* The kit's one checkbox (Checkbox, MultiSelect rows): theme tokens only. */
|
|
306
315
|
.mk-ms-box {
|
|
307
316
|
display: inline-flex; align-items: center; justify-content: center;
|
|
@@ -319,6 +328,12 @@ button, input, select, textarea { font-family: inherit; }
|
|
|
319
328
|
}
|
|
320
329
|
.mk-ms-opt:hover .mk-ms-only, .mk-ms-opt:focus-within .mk-ms-only { visibility: visible; }
|
|
321
330
|
.mk-ms-only:hover { color: var(--foreground); background: var(--muted); }
|
|
331
|
+
/* FilterMenu (@makoai/app-sdk/menu): the "Only" action shows on the hovered or highlighted row, as in the apps' NestedFilterMenu. */
|
|
332
|
+
.mk-fm-only { visibility: hidden; font-size: 12px; color: var(--muted-foreground); padding: 0 4px; border-radius: 4px; cursor: pointer; }
|
|
333
|
+
.mk-fm-only:hover { color: var(--foreground); background: var(--muted); }
|
|
334
|
+
.mk-fm-row:hover .mk-fm-only, .mk-fm-row:focus .mk-fm-only, .mk-fm-row[data-highlighted] .mk-fm-only { visibility: visible; }
|
|
335
|
+
/* The phone Filters menu: a group row that opens in place (drill) keeps its chevron at the end. */
|
|
336
|
+
.mk-fm-drill { padding-right: 4px; }
|
|
322
337
|
@media (max-width: 640px) {
|
|
323
338
|
.mk-ms-trigger { height: 38px; }
|
|
324
339
|
.mk-ms-opt { padding: 10px 8px; }
|