@keystone-sites/marketing-design-system 1.0.12 → 1.0.13
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/index.css +1 -0
- package/lib/use-popover.ts +30 -0
- package/package.json +4 -1
- package/primitives/disclosure.css +86 -1
- package/primitives/disclosure.tsx +135 -55
- package/primitives/month-range.css +80 -0
- package/primitives/month-range.tsx +183 -0
- package/primitives/table.css +2 -1
package/index.css
CHANGED
|
@@ -60,6 +60,7 @@
|
|
|
60
60
|
@import "./primitives/modal.css" layer(primitives);
|
|
61
61
|
@import "./primitives/table.css" layer(primitives);
|
|
62
62
|
@import "./primitives/disclosure.css" layer(primitives);
|
|
63
|
+
@import "./primitives/month-range.css" layer(primitives);
|
|
63
64
|
@import "./primitives/strip.css" layer(primitives);
|
|
64
65
|
|
|
65
66
|
/* Shared: the landing kit's frame, which its sections refine. */
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
/** The open state of a toolbar popover (a filter checklist, a range
|
|
4
|
+
* picker): it closes on a pointer down outside its root and on Escape.
|
|
5
|
+
* The owner renders the trigger and the panel inside `rootRef`. */
|
|
6
|
+
|
|
7
|
+
import { useEffect, useRef, useState } from "react";
|
|
8
|
+
|
|
9
|
+
export function usePopover<Root extends HTMLElement>() {
|
|
10
|
+
const [open, setOpen] = useState(false);
|
|
11
|
+
const rootRef = useRef<Root>(null);
|
|
12
|
+
|
|
13
|
+
useEffect(() => {
|
|
14
|
+
if (!open) return;
|
|
15
|
+
const onDown = (event: PointerEvent) => {
|
|
16
|
+
if (!rootRef.current?.contains(event.target as Node)) setOpen(false);
|
|
17
|
+
};
|
|
18
|
+
const onKey = (event: KeyboardEvent) => {
|
|
19
|
+
if (event.key === "Escape") setOpen(false);
|
|
20
|
+
};
|
|
21
|
+
document.addEventListener("pointerdown", onDown);
|
|
22
|
+
document.addEventListener("keydown", onKey);
|
|
23
|
+
return () => {
|
|
24
|
+
document.removeEventListener("pointerdown", onDown);
|
|
25
|
+
document.removeEventListener("keydown", onKey);
|
|
26
|
+
};
|
|
27
|
+
}, [open]);
|
|
28
|
+
|
|
29
|
+
return { open, setOpen, rootRef };
|
|
30
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@keystone-sites/marketing-design-system",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.13",
|
|
4
4
|
"description": "Keystone's marketing design system: tokens, the tick grid, type, primitives, site chrome, and the landing kit. Source-shipped CSS and TSX for Next.js consumers.",
|
|
5
5
|
"license": "UNLICENSED",
|
|
6
6
|
"repository": {
|
|
@@ -126,6 +126,8 @@
|
|
|
126
126
|
"./primitives/grader": "./primitives/grader.tsx",
|
|
127
127
|
"./primitives/modal.css": "./primitives/modal.css",
|
|
128
128
|
"./primitives/modal": "./primitives/modal.tsx",
|
|
129
|
+
"./primitives/month-range.css": "./primitives/month-range.css",
|
|
130
|
+
"./primitives/month-range": "./primitives/month-range.tsx",
|
|
129
131
|
"./primitives/notice.css": "./primitives/notice.css",
|
|
130
132
|
"./primitives/notice": "./primitives/notice.tsx",
|
|
131
133
|
"./primitives/persona-card.css": "./primitives/persona-card.css",
|
|
@@ -203,6 +205,7 @@
|
|
|
203
205
|
"./lib/use-active-section": "./lib/use-active-section.ts",
|
|
204
206
|
"./lib/use-count-up": "./lib/use-count-up.ts",
|
|
205
207
|
"./lib/use-grader-search": "./lib/use-grader-search.ts",
|
|
208
|
+
"./lib/use-popover": "./lib/use-popover.ts",
|
|
206
209
|
"./lib/use-video-carousel": "./lib/use-video-carousel.ts",
|
|
207
210
|
"./themes/inverted.css": "./themes/inverted.css"
|
|
208
211
|
}
|
|
@@ -1,7 +1,14 @@
|
|
|
1
1
|
/* Disclosure: a hairline row that opens a body beneath it. Stacked
|
|
2
2
|
disclosures share dividers through .rows. The filter is a toolbar
|
|
3
3
|
trigger with a popover checklist; `.check` is the bare checkbox both
|
|
4
|
-
the checklist and dense tables use.
|
|
4
|
+
the checklist and dense tables use. The trigger and the popover frame
|
|
5
|
+
(`.filter-trigger`, `.filter-menu`) are shared with the month range. */
|
|
6
|
+
|
|
7
|
+
/* Designed constants for this file. */
|
|
8
|
+
:root {
|
|
9
|
+
/* The checklist scrolls past this rather than leaving the page. */
|
|
10
|
+
--filter-options-h: 20rem;
|
|
11
|
+
}
|
|
5
12
|
|
|
6
13
|
.disclosure-head {
|
|
7
14
|
appearance: none;
|
|
@@ -194,6 +201,79 @@
|
|
|
194
201
|
transform: none;
|
|
195
202
|
}
|
|
196
203
|
}
|
|
204
|
+
.filter-search {
|
|
205
|
+
display: flex;
|
|
206
|
+
align-items: center;
|
|
207
|
+
gap: var(--space-sm);
|
|
208
|
+
padding: var(--space-xs) var(--space-md);
|
|
209
|
+
border-bottom: 1px solid var(--color-border-050);
|
|
210
|
+
color: var(--color-text-400);
|
|
211
|
+
}
|
|
212
|
+
.filter-search-glyph {
|
|
213
|
+
flex: none;
|
|
214
|
+
}
|
|
215
|
+
.filter-search-input {
|
|
216
|
+
flex: 1;
|
|
217
|
+
min-width: 0;
|
|
218
|
+
height: var(--field-h-sm, 40px);
|
|
219
|
+
margin: 0;
|
|
220
|
+
padding: 0;
|
|
221
|
+
border: 0;
|
|
222
|
+
background: none;
|
|
223
|
+
outline: none;
|
|
224
|
+
color: var(--color-text-100);
|
|
225
|
+
}
|
|
226
|
+
.filter-search-input::placeholder {
|
|
227
|
+
color: var(--color-text-400);
|
|
228
|
+
}
|
|
229
|
+
.filter-search-input::-webkit-search-cancel-button {
|
|
230
|
+
appearance: none;
|
|
231
|
+
}
|
|
232
|
+
.filter-actions {
|
|
233
|
+
display: flex;
|
|
234
|
+
align-items: center;
|
|
235
|
+
gap: var(--space-xs);
|
|
236
|
+
padding: var(--space-xs) var(--space-sm);
|
|
237
|
+
}
|
|
238
|
+
.filter-action {
|
|
239
|
+
appearance: none;
|
|
240
|
+
margin: 0;
|
|
241
|
+
padding: var(--space-xs) var(--space-sm);
|
|
242
|
+
border: 0;
|
|
243
|
+
border-radius: var(--radius-sm);
|
|
244
|
+
background: none;
|
|
245
|
+
color: var(--color-teal-600);
|
|
246
|
+
cursor: pointer;
|
|
247
|
+
transition:
|
|
248
|
+
background var(--motion-hover-duration) var(--motion-hover-ease),
|
|
249
|
+
color var(--motion-hover-duration) var(--motion-hover-ease);
|
|
250
|
+
}
|
|
251
|
+
@media (hover: hover) {
|
|
252
|
+
.filter-action:not(:disabled):hover {
|
|
253
|
+
background: var(--color-bg-300);
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
.filter-action:focus-visible {
|
|
257
|
+
outline: none;
|
|
258
|
+
box-shadow: 0 0 0 var(--ring-w) var(--color-teal-100);
|
|
259
|
+
}
|
|
260
|
+
.filter-action:disabled {
|
|
261
|
+
color: var(--color-text-500);
|
|
262
|
+
cursor: default;
|
|
263
|
+
}
|
|
264
|
+
.filter-tally {
|
|
265
|
+
margin-left: auto;
|
|
266
|
+
padding-inline: var(--space-sm);
|
|
267
|
+
color: var(--color-text-400);
|
|
268
|
+
font-variant-numeric: tabular-nums;
|
|
269
|
+
}
|
|
270
|
+
.filter-options {
|
|
271
|
+
display: flex;
|
|
272
|
+
flex-direction: column;
|
|
273
|
+
max-height: var(--filter-options-h);
|
|
274
|
+
overflow-y: auto;
|
|
275
|
+
scrollbar-width: thin;
|
|
276
|
+
}
|
|
197
277
|
.filter-option {
|
|
198
278
|
display: flex;
|
|
199
279
|
align-items: center;
|
|
@@ -208,3 +288,8 @@
|
|
|
208
288
|
background: var(--color-bg-300);
|
|
209
289
|
}
|
|
210
290
|
}
|
|
291
|
+
.filter-empty {
|
|
292
|
+
margin: 0;
|
|
293
|
+
padding: var(--space-sm) var(--space-md);
|
|
294
|
+
color: var(--color-text-400);
|
|
295
|
+
}
|
|
@@ -3,8 +3,9 @@
|
|
|
3
3
|
/** Disclosure: a row that opens a body beneath it, and a popover menu of
|
|
4
4
|
* checkboxes for toolbar filters. Styles live in disclosure.css. */
|
|
5
5
|
|
|
6
|
-
import {
|
|
7
|
-
import { IconChevronDownSmall } from "../icons";
|
|
6
|
+
import { useId, useState, type ReactNode } from "react";
|
|
7
|
+
import { IconChevronDownSmall, IconSearchGlyph } from "../icons";
|
|
8
|
+
import { usePopover } from "../lib/use-popover";
|
|
8
9
|
|
|
9
10
|
export function Disclosure({
|
|
10
11
|
title,
|
|
@@ -66,7 +67,50 @@ export interface FilterOption<T extends string> {
|
|
|
66
67
|
label: string;
|
|
67
68
|
}
|
|
68
69
|
|
|
69
|
-
/** A
|
|
70
|
+
/** A list this long is scanned through a search box; shorter ones by eye. */
|
|
71
|
+
const SEARCH_FROM = 8;
|
|
72
|
+
|
|
73
|
+
/** The trigger of a toolbar popover: its label, an optional count and the
|
|
74
|
+
* tick that turns when it is open. */
|
|
75
|
+
export function FilterTrigger({
|
|
76
|
+
label,
|
|
77
|
+
name,
|
|
78
|
+
count,
|
|
79
|
+
open,
|
|
80
|
+
menuId,
|
|
81
|
+
onToggle,
|
|
82
|
+
}: {
|
|
83
|
+
label: string;
|
|
84
|
+
/** What the trigger is called when its label is the value it shows. */
|
|
85
|
+
name?: string;
|
|
86
|
+
/** A number beside the label when the choice is narrower than everything. */
|
|
87
|
+
count?: number;
|
|
88
|
+
open: boolean;
|
|
89
|
+
menuId: string;
|
|
90
|
+
onToggle: () => void;
|
|
91
|
+
}) {
|
|
92
|
+
return (
|
|
93
|
+
<button
|
|
94
|
+
type="button"
|
|
95
|
+
className="type type-fixed ts-text-sm-regular filter-trigger"
|
|
96
|
+
aria-expanded={open}
|
|
97
|
+
aria-controls={menuId}
|
|
98
|
+
aria-haspopup="true"
|
|
99
|
+
aria-label={name}
|
|
100
|
+
onClick={onToggle}
|
|
101
|
+
>
|
|
102
|
+
<span>{label}</span>
|
|
103
|
+
{count !== undefined && (
|
|
104
|
+
<span className="type type-fixed ts-text-2xs-medium filter-count">{count}</span>
|
|
105
|
+
)}
|
|
106
|
+
<IconChevronDownSmall className="filter-tick" />
|
|
107
|
+
</button>
|
|
108
|
+
);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/** A button that opens a checklist; the consumer owns the selection. The
|
|
112
|
+
* list can be checked or cleared whole and, past a handful of options,
|
|
113
|
+
* searched; checking from a search touches only the options it shows. */
|
|
70
114
|
export function FilterMenu<T extends string>({
|
|
71
115
|
label,
|
|
72
116
|
options,
|
|
@@ -78,65 +122,101 @@ export function FilterMenu<T extends string>({
|
|
|
78
122
|
selected: readonly T[];
|
|
79
123
|
onChange: (next: T[]) => void;
|
|
80
124
|
}) {
|
|
81
|
-
const
|
|
82
|
-
const
|
|
125
|
+
const { open, setOpen, rootRef } = usePopover<HTMLDivElement>();
|
|
126
|
+
const [query, setQuery] = useState("");
|
|
83
127
|
const menuId = useId();
|
|
84
|
-
const
|
|
128
|
+
const searchId = useId();
|
|
129
|
+
const partial = selected.length < options.length;
|
|
130
|
+
const searchable = options.length >= SEARCH_FROM;
|
|
131
|
+
const needle = query.trim().toLowerCase();
|
|
132
|
+
const shown = needle ? options.filter((o) => o.label.toLowerCase().includes(needle)) : options;
|
|
133
|
+
const shownValues = shown.map((o) => o.value);
|
|
134
|
+
const allShown = shown.length > 0 && shown.every((o) => selected.includes(o.value));
|
|
135
|
+
const noneShown = shown.every((o) => !selected.includes(o.value));
|
|
85
136
|
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
if (event.key === "Escape") setOpen(false);
|
|
93
|
-
};
|
|
94
|
-
document.addEventListener("pointerdown", onDown);
|
|
95
|
-
document.addEventListener("keydown", onKey);
|
|
96
|
-
return () => {
|
|
97
|
-
document.removeEventListener("pointerdown", onDown);
|
|
98
|
-
document.removeEventListener("keydown", onKey);
|
|
99
|
-
};
|
|
100
|
-
}, [open]);
|
|
137
|
+
const toggle = () => {
|
|
138
|
+
setQuery("");
|
|
139
|
+
setOpen((v) => !v);
|
|
140
|
+
};
|
|
141
|
+
const checkAll = () => onChange([...new Set([...selected, ...shownValues])]);
|
|
142
|
+
const checkNone = () => onChange(selected.filter((v) => !shownValues.includes(v)));
|
|
101
143
|
|
|
102
144
|
return (
|
|
103
145
|
<div className="filter" ref={rootRef}>
|
|
104
|
-
<
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
>
|
|
112
|
-
<span>{label}</span>
|
|
113
|
-
{partial && (
|
|
114
|
-
<span className="type type-fixed ts-text-2xs-medium filter-count">{selected.length}</span>
|
|
115
|
-
)}
|
|
116
|
-
<IconChevronDownSmall className="filter-tick" />
|
|
117
|
-
</button>
|
|
146
|
+
<FilterTrigger
|
|
147
|
+
label={label}
|
|
148
|
+
count={partial ? selected.length : undefined}
|
|
149
|
+
open={open}
|
|
150
|
+
menuId={menuId}
|
|
151
|
+
onToggle={toggle}
|
|
152
|
+
/>
|
|
118
153
|
{open && (
|
|
119
154
|
<div className="filter-menu" id={menuId} role="group" aria-label={label}>
|
|
120
|
-
{
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
<
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
155
|
+
{searchable && (
|
|
156
|
+
<div className="filter-search">
|
|
157
|
+
<IconSearchGlyph className="filter-search-glyph" />
|
|
158
|
+
<input
|
|
159
|
+
id={searchId}
|
|
160
|
+
className="type type-fixed ts-text-sm-light filter-search-input"
|
|
161
|
+
type="search"
|
|
162
|
+
autoComplete="off"
|
|
163
|
+
placeholder="Search…"
|
|
164
|
+
aria-label={`Search ${label.toLowerCase()}`}
|
|
165
|
+
value={query}
|
|
166
|
+
onChange={(event) => setQuery(event.target.value)}
|
|
167
|
+
autoFocus
|
|
168
|
+
/>
|
|
169
|
+
</div>
|
|
170
|
+
)}
|
|
171
|
+
<div className="filter-actions">
|
|
172
|
+
<button
|
|
173
|
+
type="button"
|
|
174
|
+
className="type type-fixed ts-text-xs-medium filter-action"
|
|
175
|
+
disabled={allShown}
|
|
176
|
+
onClick={checkAll}
|
|
177
|
+
>
|
|
178
|
+
All
|
|
179
|
+
</button>
|
|
180
|
+
<button
|
|
181
|
+
type="button"
|
|
182
|
+
className="type type-fixed ts-text-xs-medium filter-action"
|
|
183
|
+
disabled={noneShown}
|
|
184
|
+
onClick={checkNone}
|
|
185
|
+
>
|
|
186
|
+
None
|
|
187
|
+
</button>
|
|
188
|
+
<span className="type type-fixed ts-text-xs-light filter-tally">
|
|
189
|
+
{selected.length} / {options.length}
|
|
190
|
+
</span>
|
|
191
|
+
</div>
|
|
192
|
+
<div className="filter-options">
|
|
193
|
+
{shown.map((option) => {
|
|
194
|
+
const checked = selected.includes(option.value);
|
|
195
|
+
return (
|
|
196
|
+
<label
|
|
197
|
+
key={option.value}
|
|
198
|
+
className="type type-fixed ts-text-sm-light filter-option"
|
|
199
|
+
>
|
|
200
|
+
<input
|
|
201
|
+
type="checkbox"
|
|
202
|
+
className="check"
|
|
203
|
+
checked={checked}
|
|
204
|
+
onChange={() =>
|
|
205
|
+
onChange(
|
|
206
|
+
checked
|
|
207
|
+
? selected.filter((v) => v !== option.value)
|
|
208
|
+
: [...selected, option.value],
|
|
209
|
+
)
|
|
210
|
+
}
|
|
211
|
+
/>
|
|
212
|
+
<span>{option.label}</span>
|
|
213
|
+
</label>
|
|
214
|
+
);
|
|
215
|
+
})}
|
|
216
|
+
{shown.length === 0 && (
|
|
217
|
+
<p className="type type-fixed ts-text-sm-light filter-empty">No matches</p>
|
|
218
|
+
)}
|
|
219
|
+
</div>
|
|
140
220
|
</div>
|
|
141
221
|
)}
|
|
142
222
|
</div>
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
/* MonthRange: the filter's trigger and popover frame around a year of
|
|
2
|
+
months. The stepper is the pager's round button; a chosen end is inked solid and the months between are
|
|
3
|
+
tinted; the staged range reads beneath before Cancel / Apply. */
|
|
4
|
+
|
|
5
|
+
/* Designed constants for this file. */
|
|
6
|
+
:root {
|
|
7
|
+
/* A month cell: wide enough for `Sep` at the control type. */
|
|
8
|
+
--mr-cell-w: 4.25rem;
|
|
9
|
+
--mr-cell-h: 36px;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
.mr-menu {
|
|
13
|
+
gap: var(--space-md);
|
|
14
|
+
padding: var(--space-lg);
|
|
15
|
+
}
|
|
16
|
+
.mr-year {
|
|
17
|
+
display: flex;
|
|
18
|
+
align-items: center;
|
|
19
|
+
justify-content: space-between;
|
|
20
|
+
gap: var(--space-md);
|
|
21
|
+
}
|
|
22
|
+
.mr-year-label {
|
|
23
|
+
color: var(--color-text-100);
|
|
24
|
+
font-variant-numeric: tabular-nums;
|
|
25
|
+
}
|
|
26
|
+
.mr-grid {
|
|
27
|
+
display: grid;
|
|
28
|
+
grid-template-columns: repeat(3, var(--mr-cell-w));
|
|
29
|
+
gap: var(--space-xs);
|
|
30
|
+
}
|
|
31
|
+
.mr-month {
|
|
32
|
+
appearance: none;
|
|
33
|
+
box-sizing: border-box;
|
|
34
|
+
height: var(--mr-cell-h);
|
|
35
|
+
margin: 0;
|
|
36
|
+
padding: 0;
|
|
37
|
+
border: 0;
|
|
38
|
+
border-radius: var(--radius-sm);
|
|
39
|
+
background: transparent;
|
|
40
|
+
color: var(--color-text-200);
|
|
41
|
+
cursor: pointer;
|
|
42
|
+
transition:
|
|
43
|
+
background var(--motion-hover-duration) var(--motion-hover-ease),
|
|
44
|
+
color var(--motion-hover-duration) var(--motion-hover-ease);
|
|
45
|
+
}
|
|
46
|
+
@media (hover: hover) {
|
|
47
|
+
.mr-month:not(:disabled):hover {
|
|
48
|
+
background: var(--color-bg-300);
|
|
49
|
+
color: var(--color-text-100);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
.mr-month:focus-visible {
|
|
53
|
+
outline: none;
|
|
54
|
+
box-shadow: 0 0 0 var(--ring-w) var(--color-teal-100);
|
|
55
|
+
}
|
|
56
|
+
.mr-month:disabled {
|
|
57
|
+
color: var(--color-text-500);
|
|
58
|
+
cursor: default;
|
|
59
|
+
}
|
|
60
|
+
.mr-month[data-state="in"] {
|
|
61
|
+
background: var(--color-teal-100);
|
|
62
|
+
color: var(--color-teal-700);
|
|
63
|
+
}
|
|
64
|
+
.mr-month[data-state="from"],
|
|
65
|
+
.mr-month[data-state="to"] {
|
|
66
|
+
background: var(--color-teal-500);
|
|
67
|
+
color: var(--color-bg-000);
|
|
68
|
+
}
|
|
69
|
+
.mr-staged {
|
|
70
|
+
margin: 0;
|
|
71
|
+
color: var(--color-text-400);
|
|
72
|
+
text-align: center;
|
|
73
|
+
}
|
|
74
|
+
.mr-actions {
|
|
75
|
+
display: flex;
|
|
76
|
+
justify-content: flex-end;
|
|
77
|
+
gap: var(--space-sm);
|
|
78
|
+
padding-top: var(--space-xs);
|
|
79
|
+
border-top: 1px solid var(--color-border-050);
|
|
80
|
+
}
|
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
/** MonthRange: a toolbar trigger that opens a picker of whole months,
|
|
4
|
+
* for series reported by the month. A year of months is stepped through;
|
|
5
|
+
* the first month tapped opens the range and the second closes it. The
|
|
6
|
+
* choice is staged until Apply, as the console's date picker does, so a
|
|
7
|
+
* half-made range never reaches the consumer. Styles live in
|
|
8
|
+
* month-range.css; the trigger and the popover frame are the filter's. */
|
|
9
|
+
|
|
10
|
+
import { useId, useState } from "react";
|
|
11
|
+
import { IconChevronLeftSm, IconChevronRightSm } from "../icons";
|
|
12
|
+
import { usePopover } from "../lib/use-popover";
|
|
13
|
+
import { ButtonFill, ButtonGhost } from "./buttons";
|
|
14
|
+
import { FilterTrigger } from "./disclosure";
|
|
15
|
+
|
|
16
|
+
/** A month as `YYYY-MM`. */
|
|
17
|
+
export type Month = string;
|
|
18
|
+
|
|
19
|
+
/** A span of months, either end open: `from` null begins where the data
|
|
20
|
+
* does, `to` null runs to its latest month. */
|
|
21
|
+
export interface MonthRangeValue {
|
|
22
|
+
from: Month | null;
|
|
23
|
+
to: Month | null;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const MONTHS_IN_YEAR = 12;
|
|
27
|
+
|
|
28
|
+
const parse = (month: Month): { year: number; index: number } => {
|
|
29
|
+
const [y, m] = month.split("-").map(Number);
|
|
30
|
+
return { year: y, index: m - 1 };
|
|
31
|
+
};
|
|
32
|
+
const key = (year: number, index: number): Month => `${year}-${String(index + 1).padStart(2, "0")}`;
|
|
33
|
+
const toDate = (month: Month) => {
|
|
34
|
+
const { year, index } = parse(month);
|
|
35
|
+
return new Date(Date.UTC(year, index, 1));
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
/** `Jul 2026`, in the reader's language. */
|
|
39
|
+
export function formatMonth(month: Month): string {
|
|
40
|
+
return toDate(month).toLocaleDateString(undefined, {
|
|
41
|
+
month: "short",
|
|
42
|
+
year: "numeric",
|
|
43
|
+
timeZone: "UTC",
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** `Jul 2026 – Oct 2026`, `From Jul 2026`, `To Oct 2026`, or `All months`. */
|
|
48
|
+
export function formatMonthRange(value: MonthRangeValue): string {
|
|
49
|
+
if (value.from && value.to) {
|
|
50
|
+
return value.from === value.to
|
|
51
|
+
? formatMonth(value.from)
|
|
52
|
+
: `${formatMonth(value.from)} – ${formatMonth(value.to)}`;
|
|
53
|
+
}
|
|
54
|
+
if (value.from) return `From ${formatMonth(value.from)}`;
|
|
55
|
+
if (value.to) return `To ${formatMonth(value.to)}`;
|
|
56
|
+
return "All months";
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const same = (a: MonthRangeValue, b: MonthRangeValue) => a.from === b.from && a.to === b.to;
|
|
60
|
+
|
|
61
|
+
export function MonthRange({
|
|
62
|
+
label,
|
|
63
|
+
value,
|
|
64
|
+
onChange,
|
|
65
|
+
min,
|
|
66
|
+
max,
|
|
67
|
+
}: {
|
|
68
|
+
/** Names the control for assistive technology. */
|
|
69
|
+
label: string;
|
|
70
|
+
value: MonthRangeValue;
|
|
71
|
+
onChange: (next: MonthRangeValue) => void;
|
|
72
|
+
/** The first and last months on offer; the data's own span. */
|
|
73
|
+
min: Month;
|
|
74
|
+
max: Month;
|
|
75
|
+
}) {
|
|
76
|
+
const { open, setOpen, rootRef } = usePopover<HTMLDivElement>();
|
|
77
|
+
const menuId = useId();
|
|
78
|
+
const [staged, setStaged] = useState<MonthRangeValue>(value);
|
|
79
|
+
const [year, setYear] = useState(() => parse(value.from ?? max).year);
|
|
80
|
+
const { year: minYear } = parse(min);
|
|
81
|
+
const { year: maxYear } = parse(max);
|
|
82
|
+
|
|
83
|
+
const toggle = () => {
|
|
84
|
+
if (!open) {
|
|
85
|
+
setStaged(value);
|
|
86
|
+
setYear(parse(value.from ?? max).year);
|
|
87
|
+
}
|
|
88
|
+
setOpen((v) => !v);
|
|
89
|
+
};
|
|
90
|
+
const close = () => setOpen(false);
|
|
91
|
+
const apply = () => {
|
|
92
|
+
onChange(staged);
|
|
93
|
+
close();
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
/* The first tap opens a range, the second closes it (either order);
|
|
97
|
+
a third starts over. */
|
|
98
|
+
const pick = (month: Month) => {
|
|
99
|
+
if (staged.from && !staged.to) {
|
|
100
|
+
setStaged(month < staged.from ? { from: month, to: staged.from } : { ...staged, to: month });
|
|
101
|
+
} else {
|
|
102
|
+
setStaged({ from: month, to: null });
|
|
103
|
+
}
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
const text = formatMonthRange(value);
|
|
107
|
+
const monthNames = Array.from({ length: MONTHS_IN_YEAR }, (_, index) =>
|
|
108
|
+
toDate(key(year, index)).toLocaleDateString(undefined, { month: "short", timeZone: "UTC" }),
|
|
109
|
+
);
|
|
110
|
+
|
|
111
|
+
const state = (month: Month): "from" | "to" | "in" | undefined => {
|
|
112
|
+
if (month === staged.from) return "from";
|
|
113
|
+
if (month === staged.to) return "to";
|
|
114
|
+
const after = !staged.from || month > staged.from;
|
|
115
|
+
const before = !staged.to || month < staged.to;
|
|
116
|
+
return (staged.from || staged.to) && after && before ? "in" : undefined;
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
return (
|
|
120
|
+
<div className="filter mr" ref={rootRef}>
|
|
121
|
+
<FilterTrigger
|
|
122
|
+
label={text}
|
|
123
|
+
name={`${label}: ${text}`}
|
|
124
|
+
open={open}
|
|
125
|
+
menuId={menuId}
|
|
126
|
+
onToggle={toggle}
|
|
127
|
+
/>
|
|
128
|
+
{open && (
|
|
129
|
+
<div className="filter-menu mr-menu" id={menuId} role="group" aria-label={label}>
|
|
130
|
+
<div className="mr-year">
|
|
131
|
+
<button
|
|
132
|
+
type="button"
|
|
133
|
+
className="pager-btn"
|
|
134
|
+
aria-label="Previous year"
|
|
135
|
+
disabled={year <= minYear}
|
|
136
|
+
onClick={() => setYear((y) => y - 1)}
|
|
137
|
+
>
|
|
138
|
+
<IconChevronLeftSm />
|
|
139
|
+
</button>
|
|
140
|
+
<span className="type type-fixed ts-text-sm-medium mr-year-label">{year}</span>
|
|
141
|
+
<button
|
|
142
|
+
type="button"
|
|
143
|
+
className="pager-btn"
|
|
144
|
+
aria-label="Next year"
|
|
145
|
+
disabled={year >= maxYear}
|
|
146
|
+
onClick={() => setYear((y) => y + 1)}
|
|
147
|
+
>
|
|
148
|
+
<IconChevronRightSm />
|
|
149
|
+
</button>
|
|
150
|
+
</div>
|
|
151
|
+
<div className="mr-grid">
|
|
152
|
+
{monthNames.map((name, index) => {
|
|
153
|
+
const month = key(year, index);
|
|
154
|
+
return (
|
|
155
|
+
<button
|
|
156
|
+
key={month}
|
|
157
|
+
type="button"
|
|
158
|
+
className="type type-fixed ts-text-sm-regular mr-month"
|
|
159
|
+
data-state={state(month)}
|
|
160
|
+
aria-pressed={month === staged.from || month === staged.to}
|
|
161
|
+
aria-label={formatMonth(month)}
|
|
162
|
+
disabled={month < min || month > max}
|
|
163
|
+
onClick={() => pick(month)}
|
|
164
|
+
>
|
|
165
|
+
{name}
|
|
166
|
+
</button>
|
|
167
|
+
);
|
|
168
|
+
})}
|
|
169
|
+
</div>
|
|
170
|
+
<p className="type type-fixed ts-text-xs-light mr-staged">{formatMonthRange(staged)}</p>
|
|
171
|
+
<div className="mr-actions">
|
|
172
|
+
<ButtonGhost size="sm" color="gray" icon={null} onClick={close}>
|
|
173
|
+
Cancel
|
|
174
|
+
</ButtonGhost>
|
|
175
|
+
<ButtonFill size="sm" shape="box" onClick={apply} disabled={same(staged, value)}>
|
|
176
|
+
Apply
|
|
177
|
+
</ButtonFill>
|
|
178
|
+
</div>
|
|
179
|
+
</div>
|
|
180
|
+
)}
|
|
181
|
+
</div>
|
|
182
|
+
);
|
|
183
|
+
}
|
package/primitives/table.css
CHANGED
|
@@ -3,7 +3,8 @@
|
|
|
3
3
|
tick when active). The wrap scrolls sideways so columns keep their
|
|
4
4
|
widths from the tablet band; below it, marked columns drop instead.
|
|
5
5
|
The body's type is the table's ts- step; cells restate only the
|
|
6
|
-
step that differs (head, primary, secondary, mono).
|
|
6
|
+
step that differs (head, primary, secondary, mono). `.pager-btn` is
|
|
7
|
+
the round step button the month range's year stepper shares. */
|
|
7
8
|
|
|
8
9
|
/* Designed constants for this file. */
|
|
9
10
|
:root {
|