@finstack-com-public/design 0.7.1 → 0.9.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/CHANGELOG.md +51 -0
- package/dist/css/calendar.css +266 -0
- package/dist/css/calendar.md +301 -0
- package/dist/css/input.css +4 -2
- package/dist/css/input.md +3 -1
- package/dist/css/popover.css +7 -0
- package/dist/css/popover.md +2 -0
- package/dist/css-text/calendar.d.ts +2 -0
- package/dist/css-text/calendar.js +1 -0
- package/dist/css-text/input.js +1 -1
- package/dist/css-text/kit.js +278 -2
- package/dist/css-text/popover.js +1 -1
- package/dist/dom/calendar-dates.d.ts +45 -0
- package/dist/dom/calendar-dates.js +129 -0
- package/dist/dom/calendar.d.ts +124 -0
- package/dist/dom/calendar.js +612 -0
- package/dist/dom/index.d.ts +1 -0
- package/dist/dom/index.js +1 -0
- package/dist/kit.css +278 -2
- package/dist/react/inputs.d.ts +41 -2
- package/dist/react/inputs.js +129 -2
- package/dist/react/overlays.d.ts +15 -15
- package/package.json +1 -1
|
@@ -0,0 +1,612 @@
|
|
|
1
|
+
// Calendar: year, month and day views (spec 02 B, 03 §3.2, 06 §4; D42). The second helper that
|
|
2
|
+
// builds its own markup, after the toaster: the cells change with every view and page. Dates are
|
|
3
|
+
// ISO strings (`calendar-dates.ts`); every name and word is passed in. `mode: 'range'` picks a start
|
|
4
|
+
// and an end with the same grid (T85), and `months: 2` shows two pages of the lowest view side by side.
|
|
5
|
+
import { createIcon } from "../icons/index.js";
|
|
6
|
+
import { addDays, addMonths, addYears, clampIso, daysInMonth, formatDay, formatMonth, localToday, monthGrid, parseDay, parseMonth, weekday, yearPage, } from "./calendar-dates.js";
|
|
7
|
+
const VIEWS = {
|
|
8
|
+
day: ['year', 'month', 'day'],
|
|
9
|
+
month: ['year', 'month'],
|
|
10
|
+
};
|
|
11
|
+
/** How many characters of an ISO value a view's cell reads: `YYYY-MM-DD`, `YYYY-MM`, `YYYY`. */
|
|
12
|
+
const WIDTH = { day: 10, month: 7, year: 4 };
|
|
13
|
+
const mod = (a, b) => ((a % b) + b) % b;
|
|
14
|
+
let seq = 0;
|
|
15
|
+
export function createCalendar(root, options) {
|
|
16
|
+
const doc = root.ownerDocument;
|
|
17
|
+
const idBase = root.id || `fs-calendar-${++seq}`;
|
|
18
|
+
const headingId = `${idBase}-heading`;
|
|
19
|
+
const captionId = `${idBase}-caption`;
|
|
20
|
+
const isRange = options.mode === 'range';
|
|
21
|
+
let opts = check({ granularity: 'day', weekStart: 1, ...options });
|
|
22
|
+
/** Single mode's value. */
|
|
23
|
+
let value = null;
|
|
24
|
+
/** Range mode's ends; `end` is null while the second pick is pending. */
|
|
25
|
+
let start = null;
|
|
26
|
+
let end = null;
|
|
27
|
+
/** The lowest-view cell under the pointer, for the range preview. */
|
|
28
|
+
let hoverIso = null;
|
|
29
|
+
/** Whether a cell has focus, for the range preview. */
|
|
30
|
+
let gridFocused = false;
|
|
31
|
+
let min;
|
|
32
|
+
let max;
|
|
33
|
+
let today = '';
|
|
34
|
+
let view = opts.granularity ?? 'day';
|
|
35
|
+
/** The cell with the tab stop, always at the granularity, whatever the view. */
|
|
36
|
+
let focusIso = '';
|
|
37
|
+
/** A value on the first page shown; the others follow it. Empty: the focus's page. */
|
|
38
|
+
let pageStart = '';
|
|
39
|
+
let lastHeading = '';
|
|
40
|
+
let cellValues = new Map();
|
|
41
|
+
const lowest = () => opts.granularity;
|
|
42
|
+
const rootNode = () => root.getRootNode();
|
|
43
|
+
/** Reads an ISO day or month at the granularity; `edge` says where a month falls in a day calendar. */
|
|
44
|
+
function read(iso, edge = 'first') {
|
|
45
|
+
if (!iso)
|
|
46
|
+
return undefined;
|
|
47
|
+
if (lowest() === 'month') {
|
|
48
|
+
const m = parseMonth(iso.slice(0, 7));
|
|
49
|
+
return m && (iso.length === 7 || parseDay(iso)) ? formatMonth(m.year, m.month) : undefined;
|
|
50
|
+
}
|
|
51
|
+
if (parseDay(iso))
|
|
52
|
+
return iso;
|
|
53
|
+
const m = parseMonth(iso);
|
|
54
|
+
if (!m)
|
|
55
|
+
return undefined;
|
|
56
|
+
return formatDay(m.year, m.month, edge === 'first' ? 1 : daysInMonth(m.year, m.month));
|
|
57
|
+
}
|
|
58
|
+
/** A range's ends at the granularity, in order; no start, no range. */
|
|
59
|
+
function readRange(v) {
|
|
60
|
+
const r = v && typeof v === 'object' ? v : null;
|
|
61
|
+
const s = read(r?.start, 'first') ?? null;
|
|
62
|
+
const e = read(r?.end, 'last') ?? null;
|
|
63
|
+
if (s === null)
|
|
64
|
+
return [null, null];
|
|
65
|
+
return e !== null && e < s ? [e, s] : [s, e];
|
|
66
|
+
}
|
|
67
|
+
function readAll(next, first) {
|
|
68
|
+
const granularityChanged = !first && next.granularity !== undefined && next.granularity !== lowest();
|
|
69
|
+
opts = check({ ...opts, ...next });
|
|
70
|
+
if (isRange && !(opts.labels.rangeStart && opts.labels.rangeEnd)) {
|
|
71
|
+
throw new TypeError('[fs] createCalendar: a range needs labels.rangeStart and labels.rangeEnd');
|
|
72
|
+
}
|
|
73
|
+
min = read(opts.min, 'first');
|
|
74
|
+
max = read(opts.max, 'last');
|
|
75
|
+
today = read(opts.today) ?? localToday(lowest());
|
|
76
|
+
if (first || granularityChanged || 'value' in next) {
|
|
77
|
+
if (isRange)
|
|
78
|
+
[start, end] = readRange(opts.value);
|
|
79
|
+
else
|
|
80
|
+
value = read(opts.value) ?? null;
|
|
81
|
+
hoverIso = null;
|
|
82
|
+
}
|
|
83
|
+
if (first || granularityChanged)
|
|
84
|
+
view = lowest();
|
|
85
|
+
if (first || granularityChanged || 'value' in next || !focusIso) {
|
|
86
|
+
focusIso = clampIso((isRange ? start : value) ?? today, min, max);
|
|
87
|
+
pageStart = '';
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
// The header, built once: its buttons keep focus across pages.
|
|
91
|
+
const header = doc.createElement('div');
|
|
92
|
+
header.className = 'fs-calendar__header';
|
|
93
|
+
const viewButton = button('fs-calendar__view');
|
|
94
|
+
const heading = doc.createElement('span');
|
|
95
|
+
heading.className = 'fs-calendar__heading';
|
|
96
|
+
heading.id = headingId;
|
|
97
|
+
const viewIcon = createIcon('chevron-down', { size: 16, className: 'fs-calendar__view-icon', document: doc });
|
|
98
|
+
viewButton.append(heading, viewIcon);
|
|
99
|
+
const prev = button('fs-icon-button fs-calendar__prev');
|
|
100
|
+
prev.append(createIcon('chevron-left', { size: 16, document: doc }));
|
|
101
|
+
const next = button('fs-icon-button fs-calendar__next');
|
|
102
|
+
next.append(createIcon('chevron-right', { size: 16, document: doc }));
|
|
103
|
+
header.append(viewButton, prev, next);
|
|
104
|
+
/** The second page's heading, over its grid (two months, lowest view only). Not a control. */
|
|
105
|
+
const caption = doc.createElement('span');
|
|
106
|
+
caption.className = 'fs-calendar__caption';
|
|
107
|
+
caption.id = captionId;
|
|
108
|
+
const makeGrid = (labelledBy) => {
|
|
109
|
+
const g = doc.createElement('div');
|
|
110
|
+
g.setAttribute('role', 'grid');
|
|
111
|
+
g.setAttribute('aria-labelledby', labelledBy);
|
|
112
|
+
return g;
|
|
113
|
+
};
|
|
114
|
+
const grids = [makeGrid(headingId), makeGrid(captionId)];
|
|
115
|
+
/** Holds the grids when `months: 2`, so the calendar keeps its width in every view. */
|
|
116
|
+
const monthsEl = doc.createElement('div');
|
|
117
|
+
monthsEl.className = 'fs-calendar__months';
|
|
118
|
+
/** The fallback live region, when the host passes no `announcer` (spec 06 §5). */
|
|
119
|
+
const live = doc.createElement('span');
|
|
120
|
+
live.className = 'fs-visually-hidden fs-calendar__live';
|
|
121
|
+
live.setAttribute('aria-live', 'polite');
|
|
122
|
+
function button(className) {
|
|
123
|
+
const b = doc.createElement('button');
|
|
124
|
+
b.type = 'button';
|
|
125
|
+
b.className = className;
|
|
126
|
+
return b;
|
|
127
|
+
}
|
|
128
|
+
function pageOf(v, iso) {
|
|
129
|
+
const year = Number(iso.slice(0, 4));
|
|
130
|
+
if (v === 'day')
|
|
131
|
+
return { view: 'day', year, month: Number(iso.slice(5, 7)) };
|
|
132
|
+
if (v === 'month')
|
|
133
|
+
return { view: 'month', year };
|
|
134
|
+
return { view: 'year', ...yearPage(year) };
|
|
135
|
+
}
|
|
136
|
+
/** The first and last value a page holds, at the granularity. */
|
|
137
|
+
function pageRange(page) {
|
|
138
|
+
const [fy, fm, ly, lm] = page.view === 'day'
|
|
139
|
+
? [page.year, page.month, page.year, page.month]
|
|
140
|
+
: page.view === 'month'
|
|
141
|
+
? [page.year, 1, page.year, 12]
|
|
142
|
+
: [Math.max(page.from, 1), 1, page.to, 12];
|
|
143
|
+
if (lowest() === 'month')
|
|
144
|
+
return [formatMonth(fy, fm), formatMonth(ly, lm)];
|
|
145
|
+
return [formatDay(fy, fm, 1), formatDay(ly, lm, daysInMonth(ly, lm))];
|
|
146
|
+
}
|
|
147
|
+
/** Whether any value on the page is pickable. */
|
|
148
|
+
function pageOpen(page) {
|
|
149
|
+
const [first, last] = pageRange(page);
|
|
150
|
+
return !(max !== undefined && first > max) && !(min !== undefined && last < min);
|
|
151
|
+
}
|
|
152
|
+
/** A cell is enabled when its span meets [min, max]: compared on the cell's own width. */
|
|
153
|
+
function enabled(v, iso) {
|
|
154
|
+
const n = WIDTH[v];
|
|
155
|
+
return !(min !== undefined && iso < min.slice(0, n)) && !(max !== undefined && iso > max.slice(0, n));
|
|
156
|
+
}
|
|
157
|
+
/** Paging: one month, one year or twelve years. */
|
|
158
|
+
function page(v, iso, dir) {
|
|
159
|
+
return v === 'day' ? addMonths(iso, dir) : addYears(iso, v === 'month' ? dir : 12 * dir);
|
|
160
|
+
}
|
|
161
|
+
/** A move that lands on a page with nothing pickable stops at min or max instead. */
|
|
162
|
+
function settle(iso) {
|
|
163
|
+
return pageOpen(pageOf(view, iso)) ? iso : clampIso(iso, min, max);
|
|
164
|
+
}
|
|
165
|
+
/** How many pages the current view shows. */
|
|
166
|
+
const pages = () => (view === lowest() && opts.months === 2 ? 2 : 1);
|
|
167
|
+
/** Keeps the focus on a page shown: pages forward or back only when it leaves them. */
|
|
168
|
+
function follow() {
|
|
169
|
+
const n = pages();
|
|
170
|
+
if (n === 1 || !pageStart) {
|
|
171
|
+
pageStart = focusIso;
|
|
172
|
+
return;
|
|
173
|
+
}
|
|
174
|
+
const key = (iso) => pageRange(pageOf(view, iso))[0];
|
|
175
|
+
const f = key(focusIso);
|
|
176
|
+
if (f < key(pageStart))
|
|
177
|
+
pageStart = focusIso;
|
|
178
|
+
else if (f > key(page(view, pageStart, n - 1)))
|
|
179
|
+
pageStart = page(view, focusIso, 1 - n);
|
|
180
|
+
}
|
|
181
|
+
/** A cell's place in the range, compared on the cell's own width. */
|
|
182
|
+
function rangeRole(n, iso) {
|
|
183
|
+
if (!isRange || start === null)
|
|
184
|
+
return undefined;
|
|
185
|
+
const s = start.slice(0, n);
|
|
186
|
+
const e = (end ?? start).slice(0, n);
|
|
187
|
+
if (iso < s || iso > e)
|
|
188
|
+
return undefined;
|
|
189
|
+
if (s === e)
|
|
190
|
+
return end === null ? 'start' : 'both';
|
|
191
|
+
return iso === s ? 'start' : iso === e ? 'end' : 'inside';
|
|
192
|
+
}
|
|
193
|
+
/**
|
|
194
|
+
* A cell button; only a day cell gets an aria-label (its full date), the others are named by their
|
|
195
|
+
* text. In range mode the start and end words (`labels.rangeStart`, `rangeEnd`) follow the name: in
|
|
196
|
+
* a day's aria-label, else in a visually hidden span after the visible text, so the name always
|
|
197
|
+
* holds the text (WCAG 2.5.3) and a range is never colour alone.
|
|
198
|
+
*/
|
|
199
|
+
function makeCell(v, iso, text, label) {
|
|
200
|
+
const el = button('fs-calendar__cell');
|
|
201
|
+
el.setAttribute('role', 'gridcell');
|
|
202
|
+
el.textContent = text;
|
|
203
|
+
const n = WIDTH[v];
|
|
204
|
+
const role = rangeRole(n, iso);
|
|
205
|
+
const { rangeStart, rangeEnd } = opts.labels;
|
|
206
|
+
const said = role === 'start' ? [rangeStart] : role === 'end' ? [rangeEnd] : role === 'both' ? [rangeStart, rangeEnd] : [];
|
|
207
|
+
const suffix = said.map((w) => `, ${w}`).join('');
|
|
208
|
+
if (label !== undefined)
|
|
209
|
+
el.setAttribute('aria-label', label + suffix);
|
|
210
|
+
else if (suffix) {
|
|
211
|
+
const hidden = doc.createElement('span');
|
|
212
|
+
hidden.className = 'fs-visually-hidden';
|
|
213
|
+
hidden.textContent = suffix;
|
|
214
|
+
el.append(hidden);
|
|
215
|
+
}
|
|
216
|
+
el.tabIndex = focusIso.slice(0, n) === iso ? 0 : -1;
|
|
217
|
+
if (isRange ? role !== undefined : value !== null && value.slice(0, n) === iso) {
|
|
218
|
+
el.setAttribute('aria-selected', 'true');
|
|
219
|
+
}
|
|
220
|
+
// The band's shape: square where a cell meets the next one in the range.
|
|
221
|
+
if (role === 'inside')
|
|
222
|
+
el.classList.add('fs-calendar__cell--range');
|
|
223
|
+
else if (role === 'start' && end !== null)
|
|
224
|
+
el.classList.add('fs-calendar__cell--range-start');
|
|
225
|
+
else if (role === 'end')
|
|
226
|
+
el.classList.add('fs-calendar__cell--range-end');
|
|
227
|
+
if (today.slice(0, n) === iso)
|
|
228
|
+
el.setAttribute('aria-current', 'date');
|
|
229
|
+
if (!enabled(v, iso))
|
|
230
|
+
el.setAttribute('aria-disabled', 'true');
|
|
231
|
+
cellValues.set(el, iso);
|
|
232
|
+
return el;
|
|
233
|
+
}
|
|
234
|
+
function blank() {
|
|
235
|
+
const el = doc.createElement('span');
|
|
236
|
+
el.className = 'fs-calendar__blank';
|
|
237
|
+
el.setAttribute('role', 'gridcell');
|
|
238
|
+
return el;
|
|
239
|
+
}
|
|
240
|
+
function row(children) {
|
|
241
|
+
const el = doc.createElement('div');
|
|
242
|
+
el.className = 'fs-calendar__row';
|
|
243
|
+
el.setAttribute('role', 'row');
|
|
244
|
+
el.append(...children);
|
|
245
|
+
return el;
|
|
246
|
+
}
|
|
247
|
+
/** The rows of one page. */
|
|
248
|
+
function rowsOf(current) {
|
|
249
|
+
const rows = [];
|
|
250
|
+
if (current.view === 'day') {
|
|
251
|
+
const { year, month } = current;
|
|
252
|
+
const ws = opts.weekStart;
|
|
253
|
+
rows.push(row(Array.from({ length: 7 }, (_, i) => {
|
|
254
|
+
const th = doc.createElement('span');
|
|
255
|
+
th.className = 'fs-calendar__weekday';
|
|
256
|
+
th.setAttribute('role', 'columnheader');
|
|
257
|
+
th.textContent = opts.weekdayNames[mod(ws + i, 7)];
|
|
258
|
+
return th;
|
|
259
|
+
})));
|
|
260
|
+
for (const week of monthGrid(year, month, ws)) {
|
|
261
|
+
rows.push(row(week.map((day) => {
|
|
262
|
+
if (day === null)
|
|
263
|
+
return blank();
|
|
264
|
+
const iso = formatDay(year, month, day);
|
|
265
|
+
const label = opts.cellLabel({ value: iso, year, month, day, weekday: weekday(year, month, day) });
|
|
266
|
+
return makeCell('day', iso, String(day), label);
|
|
267
|
+
})));
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
else {
|
|
271
|
+
const units = current.view === 'month'
|
|
272
|
+
? Array.from({ length: 12 }, (_, i) => {
|
|
273
|
+
return makeCell('month', formatMonth(current.year, i + 1), opts.monthNames[i]);
|
|
274
|
+
})
|
|
275
|
+
: Array.from({ length: 12 }, (_, i) => {
|
|
276
|
+
const year = current.from + i;
|
|
277
|
+
if (year < 1)
|
|
278
|
+
return blank();
|
|
279
|
+
const iso = String(year).padStart(4, '0');
|
|
280
|
+
return makeCell('year', iso, String(year));
|
|
281
|
+
});
|
|
282
|
+
for (let i = 0; i < 12; i += 3)
|
|
283
|
+
rows.push(row(units.slice(i, i + 3)));
|
|
284
|
+
}
|
|
285
|
+
return rows;
|
|
286
|
+
}
|
|
287
|
+
/** Puts `children` into `parent` in order, touching nothing already in place. */
|
|
288
|
+
function place(parent, children) {
|
|
289
|
+
if (children.length !== parent.children.length || children.some((c, i) => parent.children[i] !== c)) {
|
|
290
|
+
parent.replaceChildren(...children);
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
function render(focusGrid = false) {
|
|
294
|
+
const hadFocus = focusGrid || grids.some((g) => g.contains(rootNode().activeElement));
|
|
295
|
+
follow();
|
|
296
|
+
const n = pages();
|
|
297
|
+
const shown = Array.from({ length: n }, (_, i) => pageOf(view, page(view, pageStart, i)));
|
|
298
|
+
cellValues = new Map();
|
|
299
|
+
shown.forEach((current, i) => {
|
|
300
|
+
grids[i].className = `fs-calendar__grid fs-calendar__grid--${view}`;
|
|
301
|
+
grids[i].replaceChildren(...rowsOf(current));
|
|
302
|
+
});
|
|
303
|
+
// One page: the grid straight after the header. Two: both in __months, which stays in every view.
|
|
304
|
+
if (opts.months === 2) {
|
|
305
|
+
grids[0].remove();
|
|
306
|
+
place(monthsEl, grids.slice(0, n));
|
|
307
|
+
if (header.nextElementSibling !== monthsEl)
|
|
308
|
+
header.after(monthsEl);
|
|
309
|
+
}
|
|
310
|
+
else {
|
|
311
|
+
monthsEl.remove();
|
|
312
|
+
if (header.nextElementSibling !== grids[0])
|
|
313
|
+
header.after(grids[0]);
|
|
314
|
+
}
|
|
315
|
+
if (n < 2)
|
|
316
|
+
grids[1].remove();
|
|
317
|
+
const texts = shown.map((p) => opts.heading(p));
|
|
318
|
+
heading.textContent = texts[0];
|
|
319
|
+
if (n === 2) {
|
|
320
|
+
caption.textContent = texts[1];
|
|
321
|
+
if (viewButton.nextElementSibling !== caption)
|
|
322
|
+
viewButton.after(caption);
|
|
323
|
+
}
|
|
324
|
+
else
|
|
325
|
+
caption.remove();
|
|
326
|
+
const views = VIEWS[lowest()];
|
|
327
|
+
const top = view === views[0];
|
|
328
|
+
const up = view === 'year' ? undefined : opts.labels.up?.[view];
|
|
329
|
+
if (up)
|
|
330
|
+
viewButton.setAttribute('aria-label', `${texts[0]}, ${up}`);
|
|
331
|
+
else
|
|
332
|
+
viewButton.removeAttribute('aria-label');
|
|
333
|
+
toggle(viewButton, 'aria-disabled', top);
|
|
334
|
+
if (top)
|
|
335
|
+
viewIcon.remove();
|
|
336
|
+
else
|
|
337
|
+
viewButton.append(viewIcon);
|
|
338
|
+
prev.setAttribute('aria-label', opts.labels.prev[view]);
|
|
339
|
+
next.setAttribute('aria-label', opts.labels.next[view]);
|
|
340
|
+
const last = page(view, pageStart, n - 1);
|
|
341
|
+
toggle(prev, 'aria-disabled', !pageOpen(pageOf(view, page(view, pageStart, -1))) || isEdge(pageStart, -1));
|
|
342
|
+
toggle(next, 'aria-disabled', !pageOpen(pageOf(view, page(view, last, 1))) || isEdge(last, 1));
|
|
343
|
+
// Announce a new heading (paging, stepping), never the first one. Not from inside the view button:
|
|
344
|
+
// a live region in a focused button whose name changes is read twice.
|
|
345
|
+
const text = texts.join(' – ');
|
|
346
|
+
const out = opts.announcer ?? live;
|
|
347
|
+
if (opts.announcer)
|
|
348
|
+
live.remove();
|
|
349
|
+
else if (live.parentElement !== root)
|
|
350
|
+
root.append(live);
|
|
351
|
+
if (lastHeading && text !== lastHeading)
|
|
352
|
+
out.textContent = text;
|
|
353
|
+
lastHeading = text;
|
|
354
|
+
if (hadFocus) {
|
|
355
|
+
gridFocused = true;
|
|
356
|
+
stopCell()?.focus();
|
|
357
|
+
}
|
|
358
|
+
paintPreview();
|
|
359
|
+
}
|
|
360
|
+
/**
|
|
361
|
+
* The range preview: while only the start is picked, the cells from it to the hovered cell (else
|
|
362
|
+
* the focused one, while the grid has focus) take `__cell--preview`. Classes only, no re-render.
|
|
363
|
+
*/
|
|
364
|
+
function paintPreview() {
|
|
365
|
+
if (!isRange)
|
|
366
|
+
return;
|
|
367
|
+
let target = start !== null && end === null && view === lowest() ? (hoverIso ?? (gridFocused ? focusIso : null)) : null;
|
|
368
|
+
if (target !== null && !enabled(view, target))
|
|
369
|
+
target = null;
|
|
370
|
+
const [lo, hi] = target === null ? ['', ''] : target < start ? [target, start] : [start, target];
|
|
371
|
+
for (const [el, iso] of cellValues) {
|
|
372
|
+
el.classList.toggle('fs-calendar__cell--preview', target !== null && iso !== start && iso >= lo && iso <= hi);
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
/** At year 1 or 9999 paging goes nowhere. */
|
|
376
|
+
function isEdge(iso, dir) {
|
|
377
|
+
return JSON.stringify(pageOf(view, page(view, iso, dir))) === JSON.stringify(pageOf(view, iso));
|
|
378
|
+
}
|
|
379
|
+
/** Changes the view; the pages start again from the focus. */
|
|
380
|
+
function setViewTo(v) {
|
|
381
|
+
view = v;
|
|
382
|
+
pageStart = '';
|
|
383
|
+
}
|
|
384
|
+
function toggle(el, attr, on) {
|
|
385
|
+
if (on)
|
|
386
|
+
el.setAttribute(attr, 'true');
|
|
387
|
+
else
|
|
388
|
+
el.removeAttribute(attr);
|
|
389
|
+
}
|
|
390
|
+
const stopCell = () => [...cellValues.keys()].find((el) => el.tabIndex === 0);
|
|
391
|
+
/** Puts the focus value on a cell of the current view, keeping the finer parts it doesn't show. */
|
|
392
|
+
function onto(v, iso) {
|
|
393
|
+
if (v === 'year')
|
|
394
|
+
return addYears(focusIso, Number(iso) - Number(focusIso.slice(0, 4)));
|
|
395
|
+
if (v === 'month') {
|
|
396
|
+
const [y, m] = [Number(iso.slice(0, 4)), Number(iso.slice(5, 7))];
|
|
397
|
+
if (lowest() === 'month')
|
|
398
|
+
return iso;
|
|
399
|
+
return formatDay(y, m, Math.min(Number(focusIso.slice(8, 10)), daysInMonth(y, m)));
|
|
400
|
+
}
|
|
401
|
+
return iso;
|
|
402
|
+
}
|
|
403
|
+
/** Moves the focus; `pageTo` pages the pages shown too, so a pair pages together. */
|
|
404
|
+
function moveTo(iso, pageTo) {
|
|
405
|
+
focusIso = settle(iso);
|
|
406
|
+
if (pageTo && pageStart)
|
|
407
|
+
pageStart = pageTo;
|
|
408
|
+
hoverIso = null; // the keyboard leads the preview now
|
|
409
|
+
render(true);
|
|
410
|
+
}
|
|
411
|
+
function onKeydown(e) {
|
|
412
|
+
const target = e.target;
|
|
413
|
+
if (!cellValues.has(target) || e.altKey || e.ctrlKey || e.metaKey)
|
|
414
|
+
return;
|
|
415
|
+
const rtl = root.ownerDocument.defaultView?.getComputedStyle(root).direction === 'rtl';
|
|
416
|
+
const across = view === 'day' ? 7 : 3;
|
|
417
|
+
const step = (n) => view === 'day' ? addDays(focusIso, n) : view === 'month' ? addMonths(focusIso, n) : addYears(focusIso, n);
|
|
418
|
+
/** The focus value's column in its row, and the row's length. */
|
|
419
|
+
const column = () => {
|
|
420
|
+
if (view === 'day') {
|
|
421
|
+
const [y, m, d] = [Number(focusIso.slice(0, 4)), Number(focusIso.slice(5, 7)), Number(focusIso.slice(8, 10))];
|
|
422
|
+
const col = mod(weekday(y, m, d) - opts.weekStart, 7);
|
|
423
|
+
return { back: Math.min(col, d - 1), ahead: Math.min(6 - col, daysInMonth(y, m) - d) };
|
|
424
|
+
}
|
|
425
|
+
const index = view === 'month'
|
|
426
|
+
? Number(focusIso.slice(5, 7)) - 1
|
|
427
|
+
: Number(focusIso.slice(0, 4)) - yearPage(Number(focusIso.slice(0, 4))).from;
|
|
428
|
+
return { back: index % 3, ahead: 2 - (index % 3) };
|
|
429
|
+
};
|
|
430
|
+
let to;
|
|
431
|
+
let pageTo;
|
|
432
|
+
switch (e.key) {
|
|
433
|
+
case 'ArrowRight':
|
|
434
|
+
to = step(rtl ? -1 : 1);
|
|
435
|
+
break;
|
|
436
|
+
case 'ArrowLeft':
|
|
437
|
+
to = step(rtl ? 1 : -1);
|
|
438
|
+
break;
|
|
439
|
+
case 'ArrowDown':
|
|
440
|
+
to = step(across);
|
|
441
|
+
break;
|
|
442
|
+
case 'ArrowUp':
|
|
443
|
+
to = step(-across);
|
|
444
|
+
break;
|
|
445
|
+
case 'Home':
|
|
446
|
+
to = step(-column().back);
|
|
447
|
+
break;
|
|
448
|
+
case 'End':
|
|
449
|
+
to = step(column().ahead);
|
|
450
|
+
break;
|
|
451
|
+
case 'PageUp':
|
|
452
|
+
case 'PageDown': {
|
|
453
|
+
const dir = e.key === 'PageUp' ? -1 : 1;
|
|
454
|
+
const shift = (iso) => (e.shiftKey && view === 'day' ? addYears(iso, dir) : page(view, iso, dir));
|
|
455
|
+
to = shift(focusIso);
|
|
456
|
+
if (pageStart)
|
|
457
|
+
pageTo = shift(pageStart);
|
|
458
|
+
break;
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
if (to === undefined)
|
|
462
|
+
return;
|
|
463
|
+
e.preventDefault();
|
|
464
|
+
moveTo(to, pageTo);
|
|
465
|
+
}
|
|
466
|
+
function onClick(e) {
|
|
467
|
+
const target = e.target;
|
|
468
|
+
if (viewButton.contains(target)) {
|
|
469
|
+
const views = VIEWS[lowest()];
|
|
470
|
+
const i = views.indexOf(view);
|
|
471
|
+
if (i > 0) {
|
|
472
|
+
setViewTo(views[i - 1]);
|
|
473
|
+
render();
|
|
474
|
+
}
|
|
475
|
+
return;
|
|
476
|
+
}
|
|
477
|
+
for (const [b, dir] of [
|
|
478
|
+
[prev, -1],
|
|
479
|
+
[next, 1],
|
|
480
|
+
]) {
|
|
481
|
+
if (b.contains(target)) {
|
|
482
|
+
if (b.getAttribute('aria-disabled') !== 'true') {
|
|
483
|
+
focusIso = settle(page(view, focusIso, dir));
|
|
484
|
+
if (pageStart)
|
|
485
|
+
pageStart = page(view, pageStart, dir);
|
|
486
|
+
render();
|
|
487
|
+
}
|
|
488
|
+
return;
|
|
489
|
+
}
|
|
490
|
+
}
|
|
491
|
+
const cell = target.closest('.fs-calendar__cell');
|
|
492
|
+
const iso = cell && cellValues.get(cell);
|
|
493
|
+
if (!cell || !iso || cell.getAttribute('aria-disabled') === 'true')
|
|
494
|
+
return;
|
|
495
|
+
if (view === lowest() && isRange) {
|
|
496
|
+
focusIso = iso;
|
|
497
|
+
// The first pick (or one after a complete range) is the start; the second the end, in order.
|
|
498
|
+
const completes = start !== null && end === null;
|
|
499
|
+
if (completes)
|
|
500
|
+
[start, end] = iso < start ? [iso, start] : [start, iso];
|
|
501
|
+
else
|
|
502
|
+
[start, end] = [iso, null];
|
|
503
|
+
render(true);
|
|
504
|
+
if (completes) {
|
|
505
|
+
const detail = { start: start, end: end };
|
|
506
|
+
root.dispatchEvent(new CustomEvent('fs-change', { bubbles: true, detail }));
|
|
507
|
+
}
|
|
508
|
+
return;
|
|
509
|
+
}
|
|
510
|
+
if (view === lowest()) {
|
|
511
|
+
value = iso;
|
|
512
|
+
focusIso = iso;
|
|
513
|
+
render(true);
|
|
514
|
+
root.dispatchEvent(new CustomEvent('fs-change', { bubbles: true, detail: { value: iso } }));
|
|
515
|
+
return;
|
|
516
|
+
}
|
|
517
|
+
focusIso = clampIso(onto(view, iso), min, max);
|
|
518
|
+
const views = VIEWS[lowest()];
|
|
519
|
+
setViewTo(views[views.indexOf(view) + 1]);
|
|
520
|
+
render(true);
|
|
521
|
+
}
|
|
522
|
+
function onFocusin(e) {
|
|
523
|
+
const iso = cellValues.get(e.target);
|
|
524
|
+
if (iso === undefined)
|
|
525
|
+
return;
|
|
526
|
+
gridFocused = true;
|
|
527
|
+
if (iso !== focusIso.slice(0, WIDTH[view])) {
|
|
528
|
+
focusIso = onto(view, iso);
|
|
529
|
+
for (const el of cellValues.keys())
|
|
530
|
+
el.tabIndex = el === e.target ? 0 : -1;
|
|
531
|
+
}
|
|
532
|
+
paintPreview();
|
|
533
|
+
}
|
|
534
|
+
function onFocusout(e) {
|
|
535
|
+
if (!cellValues.has(e.target) || cellValues.has(e.relatedTarget))
|
|
536
|
+
return;
|
|
537
|
+
gridFocused = false;
|
|
538
|
+
paintPreview();
|
|
539
|
+
}
|
|
540
|
+
function onMouseover(e) {
|
|
541
|
+
const cell = e.target.closest?.('.fs-calendar__cell');
|
|
542
|
+
const iso = cell ? cellValues.get(cell) : undefined;
|
|
543
|
+
const over = iso !== undefined && view === lowest() ? iso : null;
|
|
544
|
+
if (over === hoverIso)
|
|
545
|
+
return;
|
|
546
|
+
hoverIso = over;
|
|
547
|
+
paintPreview();
|
|
548
|
+
}
|
|
549
|
+
function onMouseleave() {
|
|
550
|
+
if (hoverIso === null)
|
|
551
|
+
return;
|
|
552
|
+
hoverIso = null;
|
|
553
|
+
paintPreview();
|
|
554
|
+
}
|
|
555
|
+
const listeners = [
|
|
556
|
+
['keydown', onKeydown],
|
|
557
|
+
['click', onClick],
|
|
558
|
+
['focusin', onFocusin],
|
|
559
|
+
['focusout', onFocusout],
|
|
560
|
+
['mouseover', onMouseover],
|
|
561
|
+
['mouseleave', onMouseleave],
|
|
562
|
+
];
|
|
563
|
+
readAll(options, true);
|
|
564
|
+
root.replaceChildren(header);
|
|
565
|
+
render();
|
|
566
|
+
for (const [type, fn] of listeners)
|
|
567
|
+
root.addEventListener(type, fn);
|
|
568
|
+
const controller = {
|
|
569
|
+
get value() {
|
|
570
|
+
return value;
|
|
571
|
+
},
|
|
572
|
+
get view() {
|
|
573
|
+
return view;
|
|
574
|
+
},
|
|
575
|
+
setValue(next) {
|
|
576
|
+
readAll({ value: next }, false);
|
|
577
|
+
render();
|
|
578
|
+
},
|
|
579
|
+
setView(next) {
|
|
580
|
+
if (!VIEWS[lowest()].includes(next))
|
|
581
|
+
return;
|
|
582
|
+
setViewTo(next);
|
|
583
|
+
render();
|
|
584
|
+
},
|
|
585
|
+
focus() {
|
|
586
|
+
stopCell()?.focus();
|
|
587
|
+
},
|
|
588
|
+
update(next) {
|
|
589
|
+
readAll(next, false);
|
|
590
|
+
render();
|
|
591
|
+
},
|
|
592
|
+
destroy() {
|
|
593
|
+
for (const [type, fn] of listeners)
|
|
594
|
+
root.removeEventListener(type, fn);
|
|
595
|
+
root.replaceChildren();
|
|
596
|
+
cellValues = new Map();
|
|
597
|
+
},
|
|
598
|
+
};
|
|
599
|
+
if (!isRange)
|
|
600
|
+
return controller;
|
|
601
|
+
// Range mode: the same controller, with the range as its value.
|
|
602
|
+
return Object.defineProperty(controller, 'value', {
|
|
603
|
+
get: () => (start === null ? null : { start, end }),
|
|
604
|
+
});
|
|
605
|
+
}
|
|
606
|
+
function check(o) {
|
|
607
|
+
if (o.monthNames?.length !== 12)
|
|
608
|
+
throw new TypeError('[fs] createCalendar: monthNames needs 12 names');
|
|
609
|
+
if (o.weekdayNames?.length !== 7)
|
|
610
|
+
throw new TypeError('[fs] createCalendar: weekdayNames needs 7 names');
|
|
611
|
+
return o;
|
|
612
|
+
}
|
package/dist/dom/index.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
export { type Calendar, type CalendarChangeDetail, type CalendarDay, type CalendarLabels, type CalendarOptions, type CalendarPage, type CalendarRange, type CalendarRangeChangeDetail, type CalendarRangeLabels, type CalendarView, createCalendar, type Granularity, type RangeCalendar, type RangeCalendarOptions, } from './calendar.ts';
|
|
1
2
|
export { createDialog, createSheet, type Dialog, type DialogCloseDetail, type DialogOptions } from './dialog.ts';
|
|
2
3
|
export { createListbox, type Listbox, type ListboxOptions, type ListboxSelectDetail } from './listbox.ts';
|
|
3
4
|
export { createLoader, type Loader, type LoaderOptions } from './loader.ts';
|
package/dist/dom/index.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
// The plain-DOM behaviour helpers (D13; spec 03 §3). Each is also importable on its own:
|
|
2
2
|
// `@finstack-com-public/design/dom/<helper>`.
|
|
3
|
+
export { createCalendar, } from "./calendar.js";
|
|
3
4
|
export { createDialog, createSheet } from "./dialog.js";
|
|
4
5
|
export { createListbox } from "./listbox.js";
|
|
5
6
|
export { createLoader } from "./loader.js";
|