@monster-orange/den-ui 0.7.5 → 0.8.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 +240 -0
- package/package.json +15 -1
- package/src/bulk-bar.css +96 -0
- package/src/bulk-bar.d.ts +45 -0
- package/src/bulk-bar.js +147 -0
- package/src/button.css +59 -0
- package/src/button.d.ts +27 -0
- package/src/button.js +81 -0
- package/src/filter-bar.css +184 -0
- package/src/filter-bar.d.ts +67 -0
- package/src/filter-bar.js +307 -0
- package/src/icons.js +10 -4
- package/src/kit.d.ts +49 -0
- package/src/kit.js +70 -0
- package/src/panel-fold.css +150 -0
- package/src/panel-fold.d.ts +67 -0
- package/src/panel-fold.js +152 -0
- package/src/savestate.css +75 -0
- package/src/savestate.d.ts +73 -0
- package/src/savestate.js +294 -0
- package/src/setting-row.css +56 -0
- package/src/setting-row.d.ts +75 -0
- package/src/setting-row.js +260 -0
- package/src/shell-client.d.ts +13 -0
- package/src/shell-client.js +49 -2
- package/src/shell-html.d.ts +52 -0
- package/src/shell-html.js +104 -5
- package/src/shell.css +37 -0
- package/src/strings.js +89 -0
- package/src/switch.css +88 -0
- package/src/switch.d.ts +32 -0
- package/src/switch.js +85 -0
package/src/button.js
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The general button (issue 556): `.den-btn` from button.css, for any page,
|
|
3
|
+
* inside the frame or not. Pure (no DOM): it runs in Node and in the page.
|
|
4
|
+
*
|
|
5
|
+
* buttonHtml({
|
|
6
|
+
* label, the host's words (escaped)
|
|
7
|
+
* variant: "secondary" | "primary", default secondary (the base)
|
|
8
|
+
* size: "md" | "sm", default md
|
|
9
|
+
* href, a link drawn as a button
|
|
10
|
+
* type: "button" | "submit" | "reset", default button
|
|
11
|
+
* disabled, a disabled button; a link with no href
|
|
12
|
+
* and aria-disabled
|
|
13
|
+
* id,
|
|
14
|
+
* attrs: { "data-x": "…", "aria-…": "…" }, data- and aria- only
|
|
15
|
+
* })
|
|
16
|
+
*
|
|
17
|
+
* Any other variant, size or attribute is dropped. Link button.css after
|
|
18
|
+
* tokens.css.
|
|
19
|
+
*/
|
|
20
|
+
import { esc } from "./strings.js";
|
|
21
|
+
|
|
22
|
+
const VARIANTS = { secondary: "", primary: "primary" };
|
|
23
|
+
const SIZES = { md: "", sm: "sm" };
|
|
24
|
+
const TYPES = ["button", "submit", "reset"];
|
|
25
|
+
|
|
26
|
+
/** The extra data-/aria- attributes, escaped; anything else is dropped. */
|
|
27
|
+
function extra(attrs) {
|
|
28
|
+
if (!attrs || typeof attrs !== "object") return "";
|
|
29
|
+
let s = "";
|
|
30
|
+
for (const [k, v] of Object.entries(attrs))
|
|
31
|
+
if (/^(data|aria)-[a-z0-9-]+$/.test(k) && v != null)
|
|
32
|
+
s += " " + k + '="' + esc(v) + '"';
|
|
33
|
+
return s;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** The class list for a variant and size: "den-btn primary sm". */
|
|
37
|
+
export function buttonClass(variant, size) {
|
|
38
|
+
return ["den-btn", VARIANTS[variant] || "", SIZES[size] || ""]
|
|
39
|
+
.filter(Boolean)
|
|
40
|
+
.join(" ");
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** A `.den-btn`: a button, or a link drawn as one (see the top). */
|
|
44
|
+
export function buttonHtml({
|
|
45
|
+
label,
|
|
46
|
+
variant,
|
|
47
|
+
size,
|
|
48
|
+
href,
|
|
49
|
+
type,
|
|
50
|
+
disabled,
|
|
51
|
+
id,
|
|
52
|
+
attrs,
|
|
53
|
+
} = {}) {
|
|
54
|
+
const cls = ' class="' + buttonClass(variant, size) + '"';
|
|
55
|
+
const idA = id ? ' id="' + esc(id) + '"' : "";
|
|
56
|
+
if (href != null)
|
|
57
|
+
return (
|
|
58
|
+
"<a" +
|
|
59
|
+
cls +
|
|
60
|
+
(disabled
|
|
61
|
+
? ' role="link" aria-disabled="true"'
|
|
62
|
+
: ' href="' + esc(href) + '"') +
|
|
63
|
+
idA +
|
|
64
|
+
extra(attrs) +
|
|
65
|
+
">" +
|
|
66
|
+
esc(label) +
|
|
67
|
+
"</a>"
|
|
68
|
+
);
|
|
69
|
+
return (
|
|
70
|
+
'<button type="' +
|
|
71
|
+
(TYPES.includes(type) ? type : "button") +
|
|
72
|
+
'"' +
|
|
73
|
+
cls +
|
|
74
|
+
idA +
|
|
75
|
+
extra(attrs) +
|
|
76
|
+
(disabled ? " disabled" : "") +
|
|
77
|
+
">" +
|
|
78
|
+
esc(label) +
|
|
79
|
+
"</button>"
|
|
80
|
+
);
|
|
81
|
+
}
|
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
/* den-ui: the filter bar's look (filter-bar.js, issue 552). Tokens only:
|
|
2
|
+
no colour of its own. Linked, never injected: clean under style-src
|
|
3
|
+
'self'. The row (the host's start node, Filters, the host's end nodes),
|
|
4
|
+
the chosen chips under it, the facet panel under them. */
|
|
5
|
+
.den-fb {
|
|
6
|
+
display: grid;
|
|
7
|
+
grid-template-columns: minmax(0, 1fr);
|
|
8
|
+
gap: var(--space-3);
|
|
9
|
+
font-family: var(--sans);
|
|
10
|
+
font-size: var(--fs-s);
|
|
11
|
+
color: var(--text);
|
|
12
|
+
}
|
|
13
|
+
.den-fb-row {
|
|
14
|
+
display: flex;
|
|
15
|
+
flex-wrap: wrap;
|
|
16
|
+
align-items: center;
|
|
17
|
+
gap: var(--space-3);
|
|
18
|
+
min-width: 0;
|
|
19
|
+
}
|
|
20
|
+
.den-fb-start {
|
|
21
|
+
flex: 1 1 18rem;
|
|
22
|
+
min-width: 0;
|
|
23
|
+
}
|
|
24
|
+
.den-fb-toggle,
|
|
25
|
+
.den-fb-end {
|
|
26
|
+
flex: none;
|
|
27
|
+
}
|
|
28
|
+
/* Filters, a chip, Clear all, a facet's Clear and an option: one button. */
|
|
29
|
+
.den-fb-toggle,
|
|
30
|
+
.den-fb-chip,
|
|
31
|
+
.den-fb-clear-all,
|
|
32
|
+
.den-fb-facet-clear,
|
|
33
|
+
.den-fb-opt {
|
|
34
|
+
display: inline-flex;
|
|
35
|
+
align-items: center;
|
|
36
|
+
gap: var(--space-1);
|
|
37
|
+
box-sizing: border-box;
|
|
38
|
+
min-height: var(--tap-min);
|
|
39
|
+
margin: 0;
|
|
40
|
+
padding: 0 var(--space-3);
|
|
41
|
+
font: inherit;
|
|
42
|
+
font-size: var(--fs-s);
|
|
43
|
+
line-height: 1.2;
|
|
44
|
+
white-space: nowrap;
|
|
45
|
+
color: var(--text);
|
|
46
|
+
background: var(--surface);
|
|
47
|
+
border: 1px solid var(--border-strong);
|
|
48
|
+
border-radius: var(--radius-button);
|
|
49
|
+
cursor: pointer;
|
|
50
|
+
}
|
|
51
|
+
.den-fb-toggle[aria-expanded="true"] {
|
|
52
|
+
background: var(--sunken);
|
|
53
|
+
}
|
|
54
|
+
.den-fb-chip,
|
|
55
|
+
.den-fb-opt {
|
|
56
|
+
border-radius: 999px;
|
|
57
|
+
}
|
|
58
|
+
/* A chosen value: an accent edge; the facet's name muted before it. */
|
|
59
|
+
.den-fb-chip {
|
|
60
|
+
border-color: var(--accent);
|
|
61
|
+
}
|
|
62
|
+
.den-fb-chip-facet,
|
|
63
|
+
.den-fb-n {
|
|
64
|
+
color: var(--muted);
|
|
65
|
+
}
|
|
66
|
+
.den-fb-x {
|
|
67
|
+
color: var(--muted);
|
|
68
|
+
}
|
|
69
|
+
/* Clear all and a facet's Clear: quiet, link-like buttons. */
|
|
70
|
+
.den-fb-clear-all,
|
|
71
|
+
.den-fb-facet-clear {
|
|
72
|
+
border-color: transparent;
|
|
73
|
+
background: transparent;
|
|
74
|
+
color: var(--accent-text);
|
|
75
|
+
}
|
|
76
|
+
.den-fb-toggle:hover,
|
|
77
|
+
.den-fb-chip:hover,
|
|
78
|
+
.den-fb-opt:hover:not([disabled]) {
|
|
79
|
+
border-color: var(--accent);
|
|
80
|
+
}
|
|
81
|
+
.den-fb-clear-all:hover,
|
|
82
|
+
.den-fb-facet-clear:hover {
|
|
83
|
+
text-decoration: underline;
|
|
84
|
+
}
|
|
85
|
+
.den-fb-toggle:focus-visible,
|
|
86
|
+
.den-fb-chip:focus-visible,
|
|
87
|
+
.den-fb-clear-all:focus-visible,
|
|
88
|
+
.den-fb-facet-clear:focus-visible,
|
|
89
|
+
.den-fb-opt:focus-visible {
|
|
90
|
+
outline: 2px solid var(--accent);
|
|
91
|
+
outline-offset: 2px;
|
|
92
|
+
}
|
|
93
|
+
/* The chips: a list in a row of their own, wrapping on a wide page. */
|
|
94
|
+
.den-fb-chips {
|
|
95
|
+
display: flex;
|
|
96
|
+
flex-wrap: wrap;
|
|
97
|
+
align-items: center;
|
|
98
|
+
gap: var(--space-2);
|
|
99
|
+
min-width: 0;
|
|
100
|
+
margin: 0;
|
|
101
|
+
padding: 0;
|
|
102
|
+
list-style: none;
|
|
103
|
+
}
|
|
104
|
+
.den-fb-chips > li {
|
|
105
|
+
flex: none;
|
|
106
|
+
}
|
|
107
|
+
/* The panel: a facet per column, as many as fit. */
|
|
108
|
+
.den-fb-panel {
|
|
109
|
+
display: grid;
|
|
110
|
+
grid-template-columns: repeat(auto-fit, minmax(14rem, 1fr));
|
|
111
|
+
gap: var(--space-3) var(--space-5);
|
|
112
|
+
padding: var(--space-4);
|
|
113
|
+
background: var(--surface);
|
|
114
|
+
border: 1px solid var(--border);
|
|
115
|
+
border-radius: var(--radius);
|
|
116
|
+
}
|
|
117
|
+
.den-fb-panel[hidden] {
|
|
118
|
+
display: none;
|
|
119
|
+
}
|
|
120
|
+
.den-fb-facet {
|
|
121
|
+
display: grid;
|
|
122
|
+
gap: var(--space-2);
|
|
123
|
+
align-content: start;
|
|
124
|
+
min-width: 0;
|
|
125
|
+
}
|
|
126
|
+
.den-fb-facet-head {
|
|
127
|
+
display: flex;
|
|
128
|
+
align-items: center;
|
|
129
|
+
gap: var(--space-2);
|
|
130
|
+
min-height: var(--tap-min);
|
|
131
|
+
}
|
|
132
|
+
.den-fb-facet-name {
|
|
133
|
+
font-family: var(--mono);
|
|
134
|
+
font-size: var(--fs-xs);
|
|
135
|
+
text-transform: uppercase;
|
|
136
|
+
letter-spacing: 0.07em;
|
|
137
|
+
color: var(--muted);
|
|
138
|
+
}
|
|
139
|
+
.den-fb-facet-clear {
|
|
140
|
+
padding: 0 var(--space-2);
|
|
141
|
+
}
|
|
142
|
+
.den-fb-values {
|
|
143
|
+
display: flex;
|
|
144
|
+
flex-wrap: wrap;
|
|
145
|
+
gap: var(--space-2);
|
|
146
|
+
}
|
|
147
|
+
.den-fb-opt[aria-pressed="true"] {
|
|
148
|
+
border-color: var(--accent);
|
|
149
|
+
background: var(--sunken);
|
|
150
|
+
font-weight: 500;
|
|
151
|
+
}
|
|
152
|
+
.den-fb-opt[disabled] {
|
|
153
|
+
cursor: not-allowed;
|
|
154
|
+
border-style: dashed;
|
|
155
|
+
color: var(--muted);
|
|
156
|
+
}
|
|
157
|
+
/* A phone or a narrow pane: the start node (a find box) on its own line,
|
|
158
|
+
Filters fills the next; the chips scroll in their own row. */
|
|
159
|
+
@media (max-width: 720px) {
|
|
160
|
+
.den-fb-start {
|
|
161
|
+
flex-basis: 100%;
|
|
162
|
+
}
|
|
163
|
+
.den-fb-toggle {
|
|
164
|
+
flex: 1 1 auto;
|
|
165
|
+
justify-content: center;
|
|
166
|
+
}
|
|
167
|
+
.den-fb-chips {
|
|
168
|
+
flex-wrap: nowrap;
|
|
169
|
+
overflow-x: auto;
|
|
170
|
+
overscroll-behavior-x: contain;
|
|
171
|
+
padding-bottom: 2px;
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
/* Touch and narrow: every control a 44px target. */
|
|
175
|
+
@media (max-width: 720px), (pointer: coarse) {
|
|
176
|
+
.den-fb-toggle,
|
|
177
|
+
.den-fb-chip,
|
|
178
|
+
.den-fb-clear-all,
|
|
179
|
+
.den-fb-facet-clear,
|
|
180
|
+
.den-fb-opt,
|
|
181
|
+
.den-fb-facet-head {
|
|
182
|
+
min-height: var(--tap);
|
|
183
|
+
}
|
|
184
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/** One option of a facet: the host's value and label, and its count. */
|
|
2
|
+
export interface FilterOption {
|
|
3
|
+
value: string;
|
|
4
|
+
/** The host's own word for it (data, as a rail page's label is). */
|
|
5
|
+
label: string;
|
|
6
|
+
/** How many choosing it would give; 0 can't be chosen unless chosen. */
|
|
7
|
+
count?: number;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
/** One facet: the host's id and label, and its options. */
|
|
11
|
+
export interface FilterFacet {
|
|
12
|
+
id: string;
|
|
13
|
+
label: string;
|
|
14
|
+
options: FilterOption[];
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/** What is chosen: each facet's chosen values. */
|
|
18
|
+
export type FilterSelected = Record<string, string[]>;
|
|
19
|
+
|
|
20
|
+
/** What changed: an option or a chip, a facet's Clear, or Clear all (null). */
|
|
21
|
+
export type FilterChange =
|
|
22
|
+
| { facet: string; value: string; on: boolean }
|
|
23
|
+
| { clear: string | null };
|
|
24
|
+
|
|
25
|
+
export interface FilterBarData {
|
|
26
|
+
/** The bar's id; the panel is `<id>-panel` (default "den-fb"). */
|
|
27
|
+
id?: string;
|
|
28
|
+
facets?: FilterFacet[];
|
|
29
|
+
selected?: FilterSelected;
|
|
30
|
+
/** Drawn with the panel open. */
|
|
31
|
+
open?: boolean;
|
|
32
|
+
lang?: string;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export interface FilterBarOptions extends Omit<FilterBarData, "lang"> {
|
|
36
|
+
/** Called after each change, with what is chosen now. */
|
|
37
|
+
onChange?: (selected: FilterSelected, change: FilterChange) => void;
|
|
38
|
+
/** The host's nodes before Filters in the row (a find box). */
|
|
39
|
+
start?: Element | Element[] | null;
|
|
40
|
+
/** The host's nodes after Filters in the row (Sort, the page's buttons). */
|
|
41
|
+
end?: Element | Element[] | null;
|
|
42
|
+
/** The words' language (default: <html lang>). */
|
|
43
|
+
lang?: string | (() => string);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export interface FilterBar {
|
|
47
|
+
/** New facets (counts) or choices from the host; focus stays put. */
|
|
48
|
+
update(data: { facets?: FilterFacet[]; selected?: FilterSelected }): void;
|
|
49
|
+
selected(): FilterSelected;
|
|
50
|
+
open(): void;
|
|
51
|
+
close(): void;
|
|
52
|
+
destroy(): void;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** How many values are chosen, across every facet. */
|
|
56
|
+
export declare function chosenCount(
|
|
57
|
+
selected: FilterSelected | null | undefined,
|
|
58
|
+
): number;
|
|
59
|
+
|
|
60
|
+
/** The bar as markup, in the kit's words. Pure: runs in Node. */
|
|
61
|
+
export declare function filterBarHtml(data?: FilterBarData): string;
|
|
62
|
+
|
|
63
|
+
/** The filter bar in `el`: Filters, the chosen chips, the facet panel. */
|
|
64
|
+
export declare function mountFilterBar(
|
|
65
|
+
el: HTMLElement,
|
|
66
|
+
opts: FilterBarOptions,
|
|
67
|
+
): FilterBar;
|
|
@@ -0,0 +1,307 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The filter bar (issue 552): one "Filters (n)" button that opens a panel
|
|
3
|
+
* of facets, and what is chosen as chips under the row, each removable,
|
|
4
|
+
* then Clear all. Framework-free and data-only: the host passes its facets,
|
|
5
|
+
* their options with counts, what is chosen and a callback; the kit draws
|
|
6
|
+
* the markup and owns every word (kit.filter.* in strings.js).
|
|
7
|
+
*
|
|
8
|
+
* filterBarHtml({ id, facets, selected, open, lang }) → string (pure)
|
|
9
|
+
* const bar = mountFilterBar(el, {
|
|
10
|
+
* id: "docs-filter", ids: <id>-panel, <id>-f-<n>
|
|
11
|
+
* facets: [{ id, label, options: [{ value, label, count? }] }],
|
|
12
|
+
* selected: { [facetId]: [value, …] },
|
|
13
|
+
* onChange: (selected, change) => …,
|
|
14
|
+
* change: { facet, value, on } (an option or a chip),
|
|
15
|
+
* { clear: facetId } (a facet's Clear),
|
|
16
|
+
* { clear: null } (Clear all)
|
|
17
|
+
* start, end, the host's own nodes for the row (a find
|
|
18
|
+
* box before Filters, Sort and the page's
|
|
19
|
+
* buttons after it): moved in, kept
|
|
20
|
+
* open: false, the panel drawn open
|
|
21
|
+
* lang: "en" | () => "en", default: <html lang>
|
|
22
|
+
* })
|
|
23
|
+
* bar.update({ facets?, selected? }) new counts or choices from the host;
|
|
24
|
+
* focus stays where it was
|
|
25
|
+
* bar.selected() bar.open() bar.close() bar.destroy()
|
|
26
|
+
*
|
|
27
|
+
* A facet's label and an option's label are the host's own words (its
|
|
28
|
+
* data, as a rail page's label is); everything else is the kit's. An
|
|
29
|
+
* option with a count of 0 can't be chosen, unless it is chosen. A value
|
|
30
|
+
* chosen that has no option shows as itself.
|
|
31
|
+
*
|
|
32
|
+
* Keys: Filters is a disclosure (aria-expanded, aria-controls); Esc in
|
|
33
|
+
* the panel or on Filters closes it, focus on Filters. Removing a chip
|
|
34
|
+
* focuses the next chip, else the one before, else Filters; Clear all
|
|
35
|
+
* focuses Filters; a facet's Clear focuses its first option.
|
|
36
|
+
*
|
|
37
|
+
* Its look is filter-bar.css (link it after tokens.css). Under 720px the
|
|
38
|
+
* start node takes its own line and the chips scroll in their own row;
|
|
39
|
+
* there, and on a touch screen, every control is 44px tall.
|
|
40
|
+
*/
|
|
41
|
+
import { esc, kitT } from "./strings.js";
|
|
42
|
+
|
|
43
|
+
/** How many values are chosen, across every facet. */
|
|
44
|
+
export function chosenCount(selected) {
|
|
45
|
+
let n = 0;
|
|
46
|
+
for (const v of Object.values(selected || {}))
|
|
47
|
+
if (Array.isArray(v)) n += v.length;
|
|
48
|
+
return n;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const numberIn = (lang, n) => {
|
|
52
|
+
try {
|
|
53
|
+
return new Intl.NumberFormat(lang).format(n);
|
|
54
|
+
} catch {
|
|
55
|
+
return String(n);
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
const chosenOf = (selected, facet) =>
|
|
60
|
+
Array.isArray(selected?.[facet]) ? selected[facet] : [];
|
|
61
|
+
|
|
62
|
+
/** The chips: one per chosen value, in the facets' order, then Clear all. */
|
|
63
|
+
function chipsHtml(facets, selected, lang) {
|
|
64
|
+
const items = [];
|
|
65
|
+
for (const f of facets) {
|
|
66
|
+
for (const value of chosenOf(selected, f.id)) {
|
|
67
|
+
const opt = (f.options || []).find((o) => o.value === value);
|
|
68
|
+
const label = opt ? opt.label : value;
|
|
69
|
+
items.push(
|
|
70
|
+
`<li><button type="button" class="den-fb-chip" data-facet="${esc(f.id)}" data-value="${esc(value)}"` +
|
|
71
|
+
` aria-label="${esc(kitT(lang, "kit.filter.remove", { facet: f.label, value: label }))}">` +
|
|
72
|
+
`<span class="den-fb-chip-facet">${esc(kitT(lang, "kit.filter.chip", { facet: f.label }))}</span>` +
|
|
73
|
+
` <span class="den-fb-chip-value">${esc(label)}</span>` +
|
|
74
|
+
` <span class="den-fb-x" aria-hidden="true">×</span></button></li>`,
|
|
75
|
+
);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
if (!items.length) return "";
|
|
79
|
+
items.push(
|
|
80
|
+
`<li><button type="button" class="den-fb-clear-all">${esc(kitT(lang, "kit.filter.clearAll"))}</button></li>`,
|
|
81
|
+
);
|
|
82
|
+
return `<ul class="den-fb-chips" aria-label="${esc(kitT(lang, "kit.filter.chosen"))}">${items.join("")}</ul>`;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** The panel's insides: a named group per facet, its options toggles. */
|
|
86
|
+
function facetsHtml(id, facets, selected, lang) {
|
|
87
|
+
return facets
|
|
88
|
+
.map((f, i) => {
|
|
89
|
+
const on = chosenOf(selected, f.id);
|
|
90
|
+
const head = `${id}-f-${i}`;
|
|
91
|
+
const clear = on.length
|
|
92
|
+
? `<button type="button" class="den-fb-facet-clear" data-facet="${esc(f.id)}"` +
|
|
93
|
+
` aria-label="${esc(kitT(lang, "kit.filter.clearFacet", { facet: f.label }))}">` +
|
|
94
|
+
`${esc(kitT(lang, "kit.filter.clear"))}</button>`
|
|
95
|
+
: "";
|
|
96
|
+
const opts = (f.options || [])
|
|
97
|
+
.map((o) => {
|
|
98
|
+
const pressed = on.includes(o.value);
|
|
99
|
+
const n = typeof o.count === "number" ? o.count : null;
|
|
100
|
+
const off = !pressed && n === 0;
|
|
101
|
+
const count =
|
|
102
|
+
n == null
|
|
103
|
+
? ""
|
|
104
|
+
: ` <span class="den-fb-n">${esc(numberIn(lang, n))}</span>`;
|
|
105
|
+
return (
|
|
106
|
+
`<button type="button" class="den-fb-opt" data-facet="${esc(f.id)}" data-value="${esc(o.value)}"` +
|
|
107
|
+
` aria-pressed="${pressed}"${off ? " disabled" : ""}>` +
|
|
108
|
+
`<span class="den-fb-opt-label">${esc(o.label)}</span>${count}</button>`
|
|
109
|
+
);
|
|
110
|
+
})
|
|
111
|
+
.join("");
|
|
112
|
+
return (
|
|
113
|
+
`<div class="den-fb-facet" role="group" aria-labelledby="${esc(head)}" data-facet="${esc(f.id)}">` +
|
|
114
|
+
`<div class="den-fb-facet-head"><span class="den-fb-facet-name" id="${esc(head)}">${esc(f.label)}</span>${clear}</div>` +
|
|
115
|
+
`<div class="den-fb-values">${opts}</div></div>`
|
|
116
|
+
);
|
|
117
|
+
})
|
|
118
|
+
.join("");
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
const toggleWords = (selected, lang) => {
|
|
122
|
+
const n = chosenCount(selected);
|
|
123
|
+
return n > 0
|
|
124
|
+
? kitT(lang, "kit.filter.buttonN", { n })
|
|
125
|
+
: kitT(lang, "kit.filter.button");
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
/** The whole bar as markup (see the top of this file). Pure: runs in Node. */
|
|
129
|
+
export function filterBarHtml({
|
|
130
|
+
id = "den-fb",
|
|
131
|
+
facets = [],
|
|
132
|
+
selected = {},
|
|
133
|
+
open = false,
|
|
134
|
+
lang = "en",
|
|
135
|
+
} = {}) {
|
|
136
|
+
const panel = `${id}-panel`;
|
|
137
|
+
return (
|
|
138
|
+
`<div class="den-fb" id="${esc(id)}">` +
|
|
139
|
+
`<div class="den-fb-row"><button type="button" class="den-fb-toggle" aria-expanded="${!!open}"` +
|
|
140
|
+
` aria-controls="${esc(panel)}">${esc(toggleWords(selected, lang))}</button></div>` +
|
|
141
|
+
chipsHtml(facets, selected, lang) +
|
|
142
|
+
`<div class="den-fb-panel" id="${esc(panel)}" role="group"` +
|
|
143
|
+
` aria-label="${esc(kitT(lang, "kit.filter.button"))}"${open ? "" : " hidden"}>` +
|
|
144
|
+
facetsHtml(id, facets, selected, lang) +
|
|
145
|
+
`</div></div>`
|
|
146
|
+
);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/** A copy of `selected` with only non-empty lists. */
|
|
150
|
+
function tidy(selected) {
|
|
151
|
+
const out = {};
|
|
152
|
+
for (const [k, v] of Object.entries(selected || {}))
|
|
153
|
+
if (Array.isArray(v) && v.length) out[k] = [...v];
|
|
154
|
+
return out;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/** The filter bar in `el` (see the top of this file). */
|
|
158
|
+
export function mountFilterBar(el, opts = {}) {
|
|
159
|
+
let facets = opts.facets || [];
|
|
160
|
+
let selected = tidy(opts.selected);
|
|
161
|
+
let isOpen = !!opts.open;
|
|
162
|
+
const id = opts.id || el.id || "den-fb";
|
|
163
|
+
const lang = () => {
|
|
164
|
+
const l = typeof opts.lang === "function" ? opts.lang() : opts.lang;
|
|
165
|
+
return l || document.documentElement.lang?.slice(0, 2) || "en";
|
|
166
|
+
};
|
|
167
|
+
|
|
168
|
+
el.innerHTML = filterBarHtml({
|
|
169
|
+
id,
|
|
170
|
+
facets,
|
|
171
|
+
selected,
|
|
172
|
+
open: isOpen,
|
|
173
|
+
lang: lang(),
|
|
174
|
+
});
|
|
175
|
+
const root = el.firstElementChild;
|
|
176
|
+
const row = root.querySelector(".den-fb-row");
|
|
177
|
+
const toggle = row.querySelector(".den-fb-toggle");
|
|
178
|
+
const panel = root.querySelector(".den-fb-panel");
|
|
179
|
+
const nodes = (x) => (x == null ? [] : Array.isArray(x) ? x : [x]);
|
|
180
|
+
for (const n of nodes(opts.start)) {
|
|
181
|
+
n.classList?.add("den-fb-start");
|
|
182
|
+
row.insertBefore(n, toggle);
|
|
183
|
+
}
|
|
184
|
+
for (const n of nodes(opts.end)) {
|
|
185
|
+
n.classList?.add("den-fb-end");
|
|
186
|
+
row.append(n);
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/** What has focus, as something that survives a redraw. */
|
|
190
|
+
function focusKey() {
|
|
191
|
+
const a = document.activeElement;
|
|
192
|
+
if (!a || !root.contains(a)) return null;
|
|
193
|
+
if (a.classList.contains("den-fb-opt"))
|
|
194
|
+
return `.den-fb-opt[data-facet="${CSS.escape(a.dataset.facet)}"][data-value="${CSS.escape(a.dataset.value)}"]`;
|
|
195
|
+
if (a.classList.contains("den-fb-chip"))
|
|
196
|
+
return `.den-fb-chip[data-facet="${CSS.escape(a.dataset.facet)}"][data-value="${CSS.escape(a.dataset.value)}"]`;
|
|
197
|
+
if (a.classList.contains("den-fb-clear-all")) return ".den-fb-clear-all";
|
|
198
|
+
if (a.classList.contains("den-fb-facet-clear"))
|
|
199
|
+
return `.den-fb-facet-clear[data-facet="${CSS.escape(a.dataset.facet)}"]`;
|
|
200
|
+
return null;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
function paint(focus) {
|
|
204
|
+
const l = lang();
|
|
205
|
+
const keep = focus === undefined ? focusKey() : null;
|
|
206
|
+
toggle.textContent = toggleWords(selected, l);
|
|
207
|
+
toggle.setAttribute("aria-expanded", String(isOpen));
|
|
208
|
+
panel.hidden = !isOpen;
|
|
209
|
+
const old = root.querySelector(".den-fb-chips");
|
|
210
|
+
const chips = chipsHtml(facets, selected, l);
|
|
211
|
+
if (old) old.remove();
|
|
212
|
+
if (chips) panel.insertAdjacentHTML("beforebegin", chips);
|
|
213
|
+
panel.innerHTML = facetsHtml(id, facets, selected, l);
|
|
214
|
+
const target =
|
|
215
|
+
typeof focus === "function"
|
|
216
|
+
? focus()
|
|
217
|
+
: keep
|
|
218
|
+
? root.querySelector(keep)
|
|
219
|
+
: null;
|
|
220
|
+
if (target && !target.disabled) target.focus({ preventScroll: true });
|
|
221
|
+
else if (focus) toggle.focus({ preventScroll: true });
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
function changed(change) {
|
|
225
|
+
opts.onChange?.(tidy(selected), change);
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
function setOpen(on, focusToggle) {
|
|
229
|
+
isOpen = on;
|
|
230
|
+
toggle.setAttribute("aria-expanded", String(on));
|
|
231
|
+
panel.hidden = !on;
|
|
232
|
+
if (focusToggle) toggle.focus({ preventScroll: true });
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
const onClick = (e) => {
|
|
236
|
+
const b = e.target.closest?.("button");
|
|
237
|
+
if (!b || !root.contains(b) || b.disabled) return;
|
|
238
|
+
if (b === toggle) return setOpen(!isOpen, false);
|
|
239
|
+
if (b.classList.contains("den-fb-opt")) {
|
|
240
|
+
const { facet, value } = b.dataset;
|
|
241
|
+
const on = chosenOf(selected, facet);
|
|
242
|
+
const now = !on.includes(value);
|
|
243
|
+
selected = tidy({
|
|
244
|
+
...selected,
|
|
245
|
+
[facet]: now ? [...on, value] : on.filter((v) => v !== value),
|
|
246
|
+
});
|
|
247
|
+
paint();
|
|
248
|
+
return changed({ facet, value, on: now });
|
|
249
|
+
}
|
|
250
|
+
if (b.classList.contains("den-fb-chip")) {
|
|
251
|
+
const { facet, value } = b.dataset;
|
|
252
|
+
const all = [...root.querySelectorAll(".den-fb-chip")];
|
|
253
|
+
const at = all.indexOf(b);
|
|
254
|
+
selected = tidy({
|
|
255
|
+
...selected,
|
|
256
|
+
[facet]: chosenOf(selected, facet).filter((v) => v !== value),
|
|
257
|
+
});
|
|
258
|
+
paint(() => {
|
|
259
|
+
const left = root.querySelectorAll(".den-fb-chip");
|
|
260
|
+
return left[at] || left[at - 1] || toggle;
|
|
261
|
+
});
|
|
262
|
+
return changed({ facet, value, on: false });
|
|
263
|
+
}
|
|
264
|
+
if (b.classList.contains("den-fb-clear-all")) {
|
|
265
|
+
selected = {};
|
|
266
|
+
paint(() => toggle);
|
|
267
|
+
return changed({ clear: null });
|
|
268
|
+
}
|
|
269
|
+
if (b.classList.contains("den-fb-facet-clear")) {
|
|
270
|
+
const { facet } = b.dataset;
|
|
271
|
+
selected = tidy({ ...selected, [facet]: [] });
|
|
272
|
+
paint(
|
|
273
|
+
() =>
|
|
274
|
+
[
|
|
275
|
+
...panel.querySelectorAll(
|
|
276
|
+
`.den-fb-opt[data-facet="${CSS.escape(facet)}"]`,
|
|
277
|
+
),
|
|
278
|
+
].find((o) => !o.disabled) || toggle,
|
|
279
|
+
);
|
|
280
|
+
return changed({ clear: facet });
|
|
281
|
+
}
|
|
282
|
+
};
|
|
283
|
+
const onKey = (e) => {
|
|
284
|
+
if (e.key !== "Escape" || !isOpen) return;
|
|
285
|
+
const t = e.target;
|
|
286
|
+
if (t !== toggle && !panel.contains(t)) return;
|
|
287
|
+
e.preventDefault();
|
|
288
|
+
setOpen(false, true);
|
|
289
|
+
};
|
|
290
|
+
root.addEventListener("click", onClick);
|
|
291
|
+
root.addEventListener("keydown", onKey);
|
|
292
|
+
|
|
293
|
+
return {
|
|
294
|
+
update(data = {}) {
|
|
295
|
+
if (data.facets) facets = data.facets;
|
|
296
|
+
if (data.selected) selected = tidy(data.selected);
|
|
297
|
+
paint();
|
|
298
|
+
},
|
|
299
|
+
selected: () => tidy(selected),
|
|
300
|
+
open: () => setOpen(true, false),
|
|
301
|
+
close: () => setOpen(false, false),
|
|
302
|
+
destroy() {
|
|
303
|
+
root.removeEventListener("click", onClick);
|
|
304
|
+
root.removeEventListener("keydown", onKey);
|
|
305
|
+
},
|
|
306
|
+
};
|
|
307
|
+
}
|
package/src/icons.js
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* den-ui icons: the
|
|
3
|
-
* button). icon(name) gives an
|
|
4
|
-
* markup starting with "<" (a
|
|
5
|
-
* else the page icon. Pure: it
|
|
2
|
+
* den-ui icons: the kit's 24px line drawings (the rail, the Report
|
|
3
|
+
* button, the panel fold toggle and its edge strip). icon(name) gives an
|
|
4
|
+
* <svg> string; a name it doesn't know, or markup starting with "<" (a
|
|
5
|
+
* product's own drawing), is used as given, else the page icon. Pure: it
|
|
6
|
+
* runs in Node too.
|
|
6
7
|
*/
|
|
7
8
|
export const ICONS = {
|
|
8
9
|
// FB (den#81): the flat speech bubble with "!" (the Report button).
|
|
@@ -34,6 +35,11 @@ export const ICONS = {
|
|
|
34
35
|
// SEC-2a: who this browser is (the rail footer's identity chip).
|
|
35
36
|
person:
|
|
36
37
|
'<circle cx="12" cy="8" r="3.5"/><path d="M5 20c.8-3.6 3.6-5.5 7-5.5s6.2 1.9 7 5.5"/>',
|
|
38
|
+
// Issue 548: a window with its side column (the panel fold toggle).
|
|
39
|
+
panel:
|
|
40
|
+
'<rect x="3" y="5" width="18" height="14" rx="2"/><path d="M15 5v14"/>',
|
|
41
|
+
// Issue 549: an arrow out of the edge (the folded panel's Show panel).
|
|
42
|
+
panelShow: '<path d="M10 6l-6 6 6 6M4 12h11M19 5v14"/>',
|
|
37
43
|
};
|
|
38
44
|
|
|
39
45
|
/** An icon's <svg>, aria-hidden (the label is beside it). */
|