@finstack-com-public/design 0.7.1 → 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/CHANGELOG.md +26 -0
- package/dist/css/calendar.css +172 -0
- package/dist/css/calendar.md +169 -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 +184 -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 +79 -0
- package/dist/dom/calendar.js +396 -0
- package/dist/dom/index.d.ts +1 -0
- package/dist/dom/index.js +1 -0
- package/dist/kit.css +184 -2
- package/dist/react/inputs.d.ts +23 -2
- package/dist/react/inputs.js +55 -2
- package/dist/react/overlays.d.ts +15 -15
- package/package.json +1 -1
package/dist/css-text/popover.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export default "/* Popover (spec 02 D). Usage: popover.md. Behaviour: createPopover. */\n@layer fs.components {\n .fs-popover {\n box-sizing: border-box;\n width: min(var(--fs-size-popover), calc(100vw - 16px));\n inset: auto;\n z-index: var(--fs-z-dropdown);\n margin: 0;\n padding: var(--fs-space-4);\n font-size: var(--fs-text-body-size);\n line-height: var(--fs-text-body-line);\n color: var(--fs-ink);\n background: var(--fs-bg-surface);\n border: var(--fs-line-width) solid var(--fs-line-strong);\n border-radius: var(--fs-radius-md);\n box-shadow: var(--fs-shadow-2);\n }\n .fs-popover__kicker {\n display: block;\n font-size: var(--fs-text-body-size);\n line-height: var(--fs-text-body-line);\n font-weight: var(--fs-weight-semibold);\n letter-spacing: var(--fs-tracking-caps);\n text-transform: uppercase;\n color: var(--fs-ink-2);\n }\n .fs-popover__title {\n display: flex;\n align-items: center;\n gap: var(--fs-space-2);\n margin: var(--fs-space-1) 0 var(--fs-space-2);\n font-size: var(--fs-text-body-size);\n line-height: var(--fs-text-body-line);\n font-weight: var(--fs-weight-semibold);\n color: var(--fs-ink-2);\n }\n .fs-popover__body {\n display: grid;\n gap: var(--fs-space-2);\n color: var(--fs-ink-2);\n }\n .fs-popover__actions {\n display: flex;\n justify-content: flex-end;\n gap: var(--fs-space-2);\n margin-top: var(--fs-space-3);\n }\n\n /* The ring recipe (D15), when the panel itself takes focus. */\n .fs-popover:focus-visible {\n outline: var(--fs-ring-width) solid var(--fs-ring);\n outline-offset: var(--fs-ring-halo-width);\n box-shadow: 0 0 0 var(--fs-ring-halo-width) var(--fs-ring-halo);\n }\n}\n";
|
|
1
|
+
export default "/* Popover (spec 02 D). Usage: popover.md. Behaviour: createPopover. */\n@layer fs.components {\n .fs-popover {\n box-sizing: border-box;\n width: min(var(--fs-size-popover), calc(100vw - 16px));\n inset: auto;\n z-index: var(--fs-z-dropdown);\n margin: 0;\n padding: var(--fs-space-4);\n font-size: var(--fs-text-body-size);\n line-height: var(--fs-text-body-line);\n color: var(--fs-ink);\n background: var(--fs-bg-surface);\n border: var(--fs-line-width) solid var(--fs-line-strong);\n border-radius: var(--fs-radius-md);\n box-shadow: var(--fs-shadow-2);\n }\n .fs-popover__kicker {\n display: block;\n font-size: var(--fs-text-body-size);\n line-height: var(--fs-text-body-line);\n font-weight: var(--fs-weight-semibold);\n letter-spacing: var(--fs-tracking-caps);\n text-transform: uppercase;\n color: var(--fs-ink-2);\n }\n .fs-popover__title {\n display: flex;\n align-items: center;\n gap: var(--fs-space-2);\n margin: var(--fs-space-1) 0 var(--fs-space-2);\n font-size: var(--fs-text-body-size);\n line-height: var(--fs-text-body-line);\n font-weight: var(--fs-weight-semibold);\n color: var(--fs-ink-2);\n }\n .fs-popover__body {\n display: grid;\n gap: var(--fs-space-2);\n color: var(--fs-ink-2);\n }\n .fs-popover__actions {\n display: flex;\n justify-content: flex-end;\n gap: var(--fs-space-2);\n margin-top: var(--fs-space-3);\n }\n\n /* Fits its content and leaves the padding to it: a picker's calendar (T84). */\n .fs-popover--fit {\n width: max-content;\n max-width: calc(100vw - 16px);\n padding: 0;\n }\n\n /* The ring recipe (D15), when the panel itself takes focus. */\n .fs-popover:focus-visible {\n outline: var(--fs-ring-width) solid var(--fs-ring);\n outline-offset: var(--fs-ring-halo-width);\n box-shadow: 0 0 0 var(--fs-ring-halo-width) var(--fs-ring-halo);\n }\n}\n";
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
export type Granularity = 'month' | 'day';
|
|
2
|
+
export interface YMD {
|
|
3
|
+
year: number;
|
|
4
|
+
month: number;
|
|
5
|
+
day: number;
|
|
6
|
+
}
|
|
7
|
+
export declare function isLeapYear(year: number): boolean;
|
|
8
|
+
/** Days in a month (1–12). */
|
|
9
|
+
export declare function daysInMonth(year: number, month: number): number;
|
|
10
|
+
export declare const formatMonth: (year: number, month: number) => string;
|
|
11
|
+
export declare const formatDay: (year: number, month: number, day: number) => string;
|
|
12
|
+
/** `YYYY-MM` to its parts, or null when malformed or out of range. */
|
|
13
|
+
export declare function parseMonth(iso: string): {
|
|
14
|
+
year: number;
|
|
15
|
+
month: number;
|
|
16
|
+
} | null;
|
|
17
|
+
/** `YYYY-MM-DD` to its parts, or null when malformed or not a real day. */
|
|
18
|
+
export declare function parseDay(iso: string): YMD | null;
|
|
19
|
+
/** The weekday of a day, 0 = Sunday. */
|
|
20
|
+
export declare function weekday(year: number, month: number, day: number): number;
|
|
21
|
+
/** A day `n` days on (or back), held inside years 1–9999. */
|
|
22
|
+
export declare function addDays(iso: string, n: number): string;
|
|
23
|
+
/** A month or day `n` months on; a day keeps its number, clamped to the month's length. */
|
|
24
|
+
export declare function addMonths(iso: string, n: number): string;
|
|
25
|
+
export declare const addYears: (iso: string, n: number) => string;
|
|
26
|
+
/**
|
|
27
|
+
* Holds `iso` between `min` and `max` (either may be missing). Same-form ISO strings sort as
|
|
28
|
+
* text, because years are always four digits.
|
|
29
|
+
*/
|
|
30
|
+
export declare function clampIso(iso: string, min?: string, max?: string): string;
|
|
31
|
+
/**
|
|
32
|
+
* A month in weeks of seven, starting on `weekStart` (0 = Sunday): each entry is the day number,
|
|
33
|
+
* or null for a day of a neighbouring month.
|
|
34
|
+
*/
|
|
35
|
+
export declare function monthGrid(year: number, month: number, weekStart: number): (number | null)[][];
|
|
36
|
+
/** The page of twelve years that holds `year`: pages start at multiples of 12 (2016–2027). */
|
|
37
|
+
export declare function yearPage(year: number): {
|
|
38
|
+
from: number;
|
|
39
|
+
to: number;
|
|
40
|
+
};
|
|
41
|
+
/**
|
|
42
|
+
* Today in the user's own time zone, as a month or a day. The one place the calendar reads the
|
|
43
|
+
* clock, and only when the product passes no `today`.
|
|
44
|
+
*/
|
|
45
|
+
export declare function localToday(granularity: Granularity, now?: Date): string;
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
// Date arithmetic for the calendar (D42; spec 03 §3.2), on ISO strings: `YYYY-MM` for a month,
|
|
2
|
+
// `YYYY-MM-DD` for a day. No `Date` is ever made from a value, so no time zone can move one by a
|
|
3
|
+
// day. Months are 1–12, weekdays 0 (Sunday) to 6, years 1–9999.
|
|
4
|
+
const MIN_YEAR = 1;
|
|
5
|
+
const MAX_YEAR = 9999;
|
|
6
|
+
const pad = (n, width) => String(n).padStart(width, '0');
|
|
7
|
+
const mod = (a, b) => ((a % b) + b) % b;
|
|
8
|
+
export function isLeapYear(year) {
|
|
9
|
+
return (year % 4 === 0 && year % 100 !== 0) || year % 400 === 0;
|
|
10
|
+
}
|
|
11
|
+
/** Days in a month (1–12). */
|
|
12
|
+
export function daysInMonth(year, month) {
|
|
13
|
+
return month === 2 ? (isLeapYear(year) ? 29 : 28) : [4, 6, 9, 11].includes(month) ? 30 : 31;
|
|
14
|
+
}
|
|
15
|
+
export const formatMonth = (year, month) => `${pad(year, 4)}-${pad(month, 2)}`;
|
|
16
|
+
export const formatDay = (year, month, day) => `${formatMonth(year, month)}-${pad(day, 2)}`;
|
|
17
|
+
/** `YYYY-MM` to its parts, or null when malformed or out of range. */
|
|
18
|
+
export function parseMonth(iso) {
|
|
19
|
+
const m = /^(\d{4})-(\d{2})$/.exec(iso);
|
|
20
|
+
if (!m)
|
|
21
|
+
return null;
|
|
22
|
+
const year = Number(m[1]);
|
|
23
|
+
const month = Number(m[2]);
|
|
24
|
+
return year >= MIN_YEAR && month >= 1 && month <= 12 ? { year, month } : null;
|
|
25
|
+
}
|
|
26
|
+
/** `YYYY-MM-DD` to its parts, or null when malformed or not a real day. */
|
|
27
|
+
export function parseDay(iso) {
|
|
28
|
+
const m = /^(\d{4}-\d{2})-(\d{2})$/.exec(iso);
|
|
29
|
+
const ym = m && parseMonth(m[1]);
|
|
30
|
+
if (!ym)
|
|
31
|
+
return null;
|
|
32
|
+
const day = Number(m[2]);
|
|
33
|
+
return day >= 1 && day <= daysInMonth(ym.year, ym.month) ? { ...ym, day } : null;
|
|
34
|
+
}
|
|
35
|
+
/** Parses either form, by length; a month comes back as its first day. */
|
|
36
|
+
function parts(iso) {
|
|
37
|
+
const d = iso.length > 7 ? parseDay(iso) : null;
|
|
38
|
+
if (d)
|
|
39
|
+
return d;
|
|
40
|
+
const m = parseMonth(iso.slice(0, 7));
|
|
41
|
+
if (!m)
|
|
42
|
+
throw new RangeError(`[fs] not an ISO month or day: "${iso}"`);
|
|
43
|
+
return { ...m, day: 1 };
|
|
44
|
+
}
|
|
45
|
+
// Days since 1970-01-01 and back (H. Hinnant's civil-from-days), so adding days never builds a Date.
|
|
46
|
+
function toDayNumber({ year, month, day }) {
|
|
47
|
+
const y = month <= 2 ? year - 1 : year;
|
|
48
|
+
const era = Math.floor(y / 400);
|
|
49
|
+
const yoe = y - era * 400;
|
|
50
|
+
const doy = Math.floor((153 * (month + (month > 2 ? -3 : 9)) + 2) / 5) + day - 1;
|
|
51
|
+
const doe = yoe * 365 + Math.floor(yoe / 4) - Math.floor(yoe / 100) + doy;
|
|
52
|
+
return era * 146097 + doe - 719468;
|
|
53
|
+
}
|
|
54
|
+
function fromDayNumber(n) {
|
|
55
|
+
const z = n + 719468;
|
|
56
|
+
const era = Math.floor(z / 146097);
|
|
57
|
+
const doe = z - era * 146097;
|
|
58
|
+
const yoe = Math.floor((doe - Math.floor(doe / 1460) + Math.floor(doe / 36524) - Math.floor(doe / 146096)) / 365);
|
|
59
|
+
const doy = doe - (365 * yoe + Math.floor(yoe / 4) - Math.floor(yoe / 100));
|
|
60
|
+
const mp = Math.floor((5 * doy + 2) / 153);
|
|
61
|
+
const day = doy - Math.floor((153 * mp + 2) / 5) + 1;
|
|
62
|
+
const month = mp < 10 ? mp + 3 : mp - 9;
|
|
63
|
+
return { year: yoe + era * 400 + (month <= 2 ? 1 : 0), month, day };
|
|
64
|
+
}
|
|
65
|
+
/** The weekday of a day, 0 = Sunday. */
|
|
66
|
+
export function weekday(year, month, day) {
|
|
67
|
+
return mod(toDayNumber({ year, month, day }) + 4, 7); // 1970-01-01 was a Thursday
|
|
68
|
+
}
|
|
69
|
+
/** A day `n` days on (or back), held inside years 1–9999. */
|
|
70
|
+
export function addDays(iso, n) {
|
|
71
|
+
const first = toDayNumber({ year: MIN_YEAR, month: 1, day: 1 });
|
|
72
|
+
const last = toDayNumber({ year: MAX_YEAR, month: 12, day: 31 });
|
|
73
|
+
const { year, month, day } = fromDayNumber(Math.min(Math.max(toDayNumber(parts(iso)) + n, first), last));
|
|
74
|
+
return formatDay(year, month, day);
|
|
75
|
+
}
|
|
76
|
+
/** A month or day `n` months on; a day keeps its number, clamped to the month's length. */
|
|
77
|
+
export function addMonths(iso, n) {
|
|
78
|
+
const p = parts(iso);
|
|
79
|
+
const index = Math.min(Math.max(p.year * 12 + p.month - 1 + n, MIN_YEAR * 12), MAX_YEAR * 12 + 11);
|
|
80
|
+
const year = Math.floor(index / 12);
|
|
81
|
+
const month = (index % 12) + 1;
|
|
82
|
+
if (iso.length <= 7)
|
|
83
|
+
return formatMonth(year, month);
|
|
84
|
+
// Clamped at either end of time, the day goes to that end too.
|
|
85
|
+
const atEdge = index !== p.year * 12 + p.month - 1 + n;
|
|
86
|
+
const day = atEdge ? (n < 0 ? 1 : daysInMonth(year, month)) : Math.min(p.day, daysInMonth(year, month));
|
|
87
|
+
return formatDay(year, month, day);
|
|
88
|
+
}
|
|
89
|
+
export const addYears = (iso, n) => addMonths(iso, n * 12);
|
|
90
|
+
/**
|
|
91
|
+
* Holds `iso` between `min` and `max` (either may be missing). Same-form ISO strings sort as
|
|
92
|
+
* text, because years are always four digits.
|
|
93
|
+
*/
|
|
94
|
+
export function clampIso(iso, min, max) {
|
|
95
|
+
if (min !== undefined && iso < min)
|
|
96
|
+
return min;
|
|
97
|
+
if (max !== undefined && iso > max)
|
|
98
|
+
return max;
|
|
99
|
+
return iso;
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* A month in weeks of seven, starting on `weekStart` (0 = Sunday): each entry is the day number,
|
|
103
|
+
* or null for a day of a neighbouring month.
|
|
104
|
+
*/
|
|
105
|
+
export function monthGrid(year, month, weekStart) {
|
|
106
|
+
const lead = mod(weekday(year, month, 1) - weekStart, 7);
|
|
107
|
+
const cells = [...Array(lead).fill(null)];
|
|
108
|
+
for (let d = 1; d <= daysInMonth(year, month); d++)
|
|
109
|
+
cells.push(d);
|
|
110
|
+
while (cells.length % 7)
|
|
111
|
+
cells.push(null);
|
|
112
|
+
const weeks = [];
|
|
113
|
+
for (let i = 0; i < cells.length; i += 7)
|
|
114
|
+
weeks.push(cells.slice(i, i + 7));
|
|
115
|
+
return weeks;
|
|
116
|
+
}
|
|
117
|
+
/** The page of twelve years that holds `year`: pages start at multiples of 12 (2016–2027). */
|
|
118
|
+
export function yearPage(year) {
|
|
119
|
+
const from = year - mod(year, 12);
|
|
120
|
+
return { from, to: from + 11 };
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Today in the user's own time zone, as a month or a day. The one place the calendar reads the
|
|
124
|
+
* clock, and only when the product passes no `today`.
|
|
125
|
+
*/
|
|
126
|
+
export function localToday(granularity, now = new Date()) {
|
|
127
|
+
const month = formatMonth(now.getFullYear(), now.getMonth() + 1);
|
|
128
|
+
return granularity === 'month' ? month : `${month}-${pad(now.getDate(), 2)}`;
|
|
129
|
+
}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { type Granularity } from './calendar-dates.ts';
|
|
2
|
+
export type { Granularity };
|
|
3
|
+
export type CalendarView = 'year' | 'month' | 'day';
|
|
4
|
+
/** What a page of the calendar shows, for the heading. Months are 1–12. */
|
|
5
|
+
export type CalendarPage = {
|
|
6
|
+
view: 'day';
|
|
7
|
+
year: number;
|
|
8
|
+
month: number;
|
|
9
|
+
} | {
|
|
10
|
+
view: 'month';
|
|
11
|
+
year: number;
|
|
12
|
+
} | {
|
|
13
|
+
view: 'year';
|
|
14
|
+
from: number;
|
|
15
|
+
to: number;
|
|
16
|
+
};
|
|
17
|
+
/**
|
|
18
|
+
* A day cell, for its accessible name: `value` is `YYYY-MM-DD`, `weekday` 0 (Sunday) to 6. Only day
|
|
19
|
+
* cells take a name: a month or year cell is named by its visible text, and the grid by the heading,
|
|
20
|
+
* which carries the year (WCAG 2.5.3: a short "Jan" must not hide behind "January 2026").
|
|
21
|
+
*/
|
|
22
|
+
export interface CalendarDay {
|
|
23
|
+
value: string;
|
|
24
|
+
year: number;
|
|
25
|
+
month: number;
|
|
26
|
+
day: number;
|
|
27
|
+
weekday: number;
|
|
28
|
+
}
|
|
29
|
+
export interface CalendarLabels {
|
|
30
|
+
/** The previous-page button's name in each view ("Előző hónap", "Előző év", "Előző 12 év"). */
|
|
31
|
+
prev: Record<CalendarView, string>;
|
|
32
|
+
/** The next-page button's name in each view. */
|
|
33
|
+
next: Record<CalendarView, string>;
|
|
34
|
+
/** Optional: said after the heading in the view button's name ("2026. november, hónap választása"). */
|
|
35
|
+
up?: Partial<Record<'day' | 'month', string>>;
|
|
36
|
+
}
|
|
37
|
+
export interface CalendarOptions {
|
|
38
|
+
/** The picked day (`YYYY-MM-DD`) or month (`YYYY-MM`), or null. */
|
|
39
|
+
value?: string | null;
|
|
40
|
+
/** The first and last pickable day or month; either form is read at the granularity. */
|
|
41
|
+
min?: string;
|
|
42
|
+
max?: string;
|
|
43
|
+
/** Default: the local date, read from the clock once. */
|
|
44
|
+
today?: string;
|
|
45
|
+
/** The lowest view, and the form of the value. Default `day`. */
|
|
46
|
+
granularity?: Granularity;
|
|
47
|
+
/** The first column, 0 = Sunday. Default 1 (Monday). */
|
|
48
|
+
weekStart?: number;
|
|
49
|
+
/** Twelve names the month cells show, January first. Short forms fit ("jan.", "febr."). */
|
|
50
|
+
monthNames: readonly string[];
|
|
51
|
+
/** Seven column headers, Sunday first whatever the week start ("V", "H", "K", "Sze", …). */
|
|
52
|
+
weekdayNames: readonly string[];
|
|
53
|
+
/** The heading of a page: "2026. november", "2026", "2016–2027". */
|
|
54
|
+
heading(page: CalendarPage): string;
|
|
55
|
+
/** A day cell's full accessible name, which must contain the day number: "2026. november 9., hétfő". */
|
|
56
|
+
cellLabel(day: CalendarDay): string;
|
|
57
|
+
labels: CalendarLabels;
|
|
58
|
+
/** The host's live region (CE §7). Without one the calendar keeps its own, visually hidden. */
|
|
59
|
+
announcer?: HTMLElement;
|
|
60
|
+
}
|
|
61
|
+
export interface CalendarChangeDetail {
|
|
62
|
+
value: string;
|
|
63
|
+
}
|
|
64
|
+
export interface Calendar {
|
|
65
|
+
readonly value: string | null;
|
|
66
|
+
readonly view: CalendarView;
|
|
67
|
+
/** Sets the value and shows its page, without an `fs-change` event; null clears it. */
|
|
68
|
+
setValue(value: string | null): void;
|
|
69
|
+
/** Shows a view (not one below the granularity). */
|
|
70
|
+
setView(view: CalendarView): void;
|
|
71
|
+
/** Focuses the grid's one tab stop. */
|
|
72
|
+
focus(): void;
|
|
73
|
+
/** Takes new options; a `value` among them moves to its page as `setValue` does. */
|
|
74
|
+
update(options: Partial<CalendarOptions>): void;
|
|
75
|
+
/** Removes every listener and empties the root. */
|
|
76
|
+
destroy(): void;
|
|
77
|
+
}
|
|
78
|
+
/** `root` is an empty `.fs-calendar` element; the helper renders the header and the grid into it. */
|
|
79
|
+
export declare function createCalendar(root: HTMLElement, options: CalendarOptions): Calendar;
|
|
@@ -0,0 +1,396 @@
|
|
|
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.
|
|
4
|
+
import { createIcon } from "../icons/index.js";
|
|
5
|
+
import { addDays, addMonths, addYears, clampIso, daysInMonth, formatDay, formatMonth, localToday, monthGrid, parseDay, parseMonth, weekday, yearPage, } from "./calendar-dates.js";
|
|
6
|
+
const VIEWS = {
|
|
7
|
+
day: ['year', 'month', 'day'],
|
|
8
|
+
month: ['year', 'month'],
|
|
9
|
+
};
|
|
10
|
+
/** How many characters of an ISO value a view's cell reads: `YYYY-MM-DD`, `YYYY-MM`, `YYYY`. */
|
|
11
|
+
const WIDTH = { day: 10, month: 7, year: 4 };
|
|
12
|
+
const mod = (a, b) => ((a % b) + b) % b;
|
|
13
|
+
let seq = 0;
|
|
14
|
+
/** `root` is an empty `.fs-calendar` element; the helper renders the header and the grid into it. */
|
|
15
|
+
export function createCalendar(root, options) {
|
|
16
|
+
const doc = root.ownerDocument;
|
|
17
|
+
const headingId = root.id ? `${root.id}-heading` : `fs-calendar-${++seq}-heading`;
|
|
18
|
+
let opts = check({ granularity: 'day', weekStart: 1, ...options });
|
|
19
|
+
let value = null;
|
|
20
|
+
let min;
|
|
21
|
+
let max;
|
|
22
|
+
let today = '';
|
|
23
|
+
let view = opts.granularity ?? 'day';
|
|
24
|
+
/** The cell with the tab stop, always at the granularity, whatever the view. */
|
|
25
|
+
let focusIso = '';
|
|
26
|
+
let lastHeading = '';
|
|
27
|
+
let cellValues = new Map();
|
|
28
|
+
const lowest = () => opts.granularity;
|
|
29
|
+
const rootNode = () => root.getRootNode();
|
|
30
|
+
/** Reads an ISO day or month at the granularity; `edge` says where a month falls in a day calendar. */
|
|
31
|
+
function read(iso, edge = 'first') {
|
|
32
|
+
if (!iso)
|
|
33
|
+
return undefined;
|
|
34
|
+
if (lowest() === 'month') {
|
|
35
|
+
const m = parseMonth(iso.slice(0, 7));
|
|
36
|
+
return m && (iso.length === 7 || parseDay(iso)) ? formatMonth(m.year, m.month) : undefined;
|
|
37
|
+
}
|
|
38
|
+
if (parseDay(iso))
|
|
39
|
+
return iso;
|
|
40
|
+
const m = parseMonth(iso);
|
|
41
|
+
if (!m)
|
|
42
|
+
return undefined;
|
|
43
|
+
return formatDay(m.year, m.month, edge === 'first' ? 1 : daysInMonth(m.year, m.month));
|
|
44
|
+
}
|
|
45
|
+
function readAll(next, first) {
|
|
46
|
+
const granularityChanged = !first && next.granularity !== undefined && next.granularity !== lowest();
|
|
47
|
+
opts = check({ ...opts, ...next });
|
|
48
|
+
min = read(opts.min, 'first');
|
|
49
|
+
max = read(opts.max, 'last');
|
|
50
|
+
today = read(opts.today) ?? localToday(lowest());
|
|
51
|
+
if (first || granularityChanged || 'value' in next)
|
|
52
|
+
value = read(opts.value) ?? null;
|
|
53
|
+
if (first || granularityChanged)
|
|
54
|
+
view = lowest();
|
|
55
|
+
if (first || granularityChanged || 'value' in next || !focusIso)
|
|
56
|
+
focusIso = clampIso(value ?? today, min, max);
|
|
57
|
+
}
|
|
58
|
+
// The header, built once: its buttons keep focus across pages.
|
|
59
|
+
const header = doc.createElement('div');
|
|
60
|
+
header.className = 'fs-calendar__header';
|
|
61
|
+
const viewButton = button('fs-calendar__view');
|
|
62
|
+
const heading = doc.createElement('span');
|
|
63
|
+
heading.className = 'fs-calendar__heading';
|
|
64
|
+
heading.id = headingId;
|
|
65
|
+
const viewIcon = createIcon('chevron-down', { size: 16, className: 'fs-calendar__view-icon', document: doc });
|
|
66
|
+
viewButton.append(heading, viewIcon);
|
|
67
|
+
const prev = button('fs-icon-button fs-calendar__prev');
|
|
68
|
+
prev.append(createIcon('chevron-left', { size: 16, document: doc }));
|
|
69
|
+
const next = button('fs-icon-button fs-calendar__next');
|
|
70
|
+
next.append(createIcon('chevron-right', { size: 16, document: doc }));
|
|
71
|
+
header.append(viewButton, prev, next);
|
|
72
|
+
const grid = doc.createElement('div');
|
|
73
|
+
grid.setAttribute('role', 'grid');
|
|
74
|
+
/** The fallback live region, when the host passes no `announcer` (spec 06 §5). */
|
|
75
|
+
const live = doc.createElement('span');
|
|
76
|
+
live.className = 'fs-visually-hidden fs-calendar__live';
|
|
77
|
+
live.setAttribute('aria-live', 'polite');
|
|
78
|
+
grid.setAttribute('aria-labelledby', headingId);
|
|
79
|
+
function button(className) {
|
|
80
|
+
const b = doc.createElement('button');
|
|
81
|
+
b.type = 'button';
|
|
82
|
+
b.className = className;
|
|
83
|
+
return b;
|
|
84
|
+
}
|
|
85
|
+
function pageOf(v, iso) {
|
|
86
|
+
const year = Number(iso.slice(0, 4));
|
|
87
|
+
if (v === 'day')
|
|
88
|
+
return { view: 'day', year, month: Number(iso.slice(5, 7)) };
|
|
89
|
+
if (v === 'month')
|
|
90
|
+
return { view: 'month', year };
|
|
91
|
+
return { view: 'year', ...yearPage(year) };
|
|
92
|
+
}
|
|
93
|
+
/** The first and last value a page holds, at the granularity. */
|
|
94
|
+
function pageRange(page) {
|
|
95
|
+
const [fy, fm, ly, lm] = page.view === 'day'
|
|
96
|
+
? [page.year, page.month, page.year, page.month]
|
|
97
|
+
: page.view === 'month'
|
|
98
|
+
? [page.year, 1, page.year, 12]
|
|
99
|
+
: [Math.max(page.from, 1), 1, page.to, 12];
|
|
100
|
+
if (lowest() === 'month')
|
|
101
|
+
return [formatMonth(fy, fm), formatMonth(ly, lm)];
|
|
102
|
+
return [formatDay(fy, fm, 1), formatDay(ly, lm, daysInMonth(ly, lm))];
|
|
103
|
+
}
|
|
104
|
+
/** Whether any value on the page is pickable. */
|
|
105
|
+
function pageOpen(page) {
|
|
106
|
+
const [first, last] = pageRange(page);
|
|
107
|
+
return !(max !== undefined && first > max) && !(min !== undefined && last < min);
|
|
108
|
+
}
|
|
109
|
+
/** A cell is enabled when its span meets [min, max]: compared on the cell's own width. */
|
|
110
|
+
function enabled(v, iso) {
|
|
111
|
+
const n = WIDTH[v];
|
|
112
|
+
return !(min !== undefined && iso < min.slice(0, n)) && !(max !== undefined && iso > max.slice(0, n));
|
|
113
|
+
}
|
|
114
|
+
/** Paging: one month, one year or twelve years. */
|
|
115
|
+
function page(v, iso, dir) {
|
|
116
|
+
return v === 'day' ? addMonths(iso, dir) : addYears(iso, v === 'month' ? dir : 12 * dir);
|
|
117
|
+
}
|
|
118
|
+
/** A move that lands on a page with nothing pickable stops at min or max instead. */
|
|
119
|
+
function settle(iso) {
|
|
120
|
+
return pageOpen(pageOf(view, iso)) ? iso : clampIso(iso, min, max);
|
|
121
|
+
}
|
|
122
|
+
/** A cell button; only a day cell gets an aria-label (its full date), the others their text. */
|
|
123
|
+
function makeCell(v, iso, text, label) {
|
|
124
|
+
const el = button('fs-calendar__cell');
|
|
125
|
+
el.setAttribute('role', 'gridcell');
|
|
126
|
+
el.textContent = text;
|
|
127
|
+
if (label !== undefined)
|
|
128
|
+
el.setAttribute('aria-label', label);
|
|
129
|
+
const n = WIDTH[v];
|
|
130
|
+
el.tabIndex = focusIso.slice(0, n) === iso ? 0 : -1;
|
|
131
|
+
if (value !== null && value.slice(0, n) === iso)
|
|
132
|
+
el.setAttribute('aria-selected', 'true');
|
|
133
|
+
if (today.slice(0, n) === iso)
|
|
134
|
+
el.setAttribute('aria-current', 'date');
|
|
135
|
+
if (!enabled(v, iso))
|
|
136
|
+
el.setAttribute('aria-disabled', 'true');
|
|
137
|
+
cellValues.set(el, iso);
|
|
138
|
+
return el;
|
|
139
|
+
}
|
|
140
|
+
function blank() {
|
|
141
|
+
const el = doc.createElement('span');
|
|
142
|
+
el.className = 'fs-calendar__blank';
|
|
143
|
+
el.setAttribute('role', 'gridcell');
|
|
144
|
+
return el;
|
|
145
|
+
}
|
|
146
|
+
function row(children) {
|
|
147
|
+
const el = doc.createElement('div');
|
|
148
|
+
el.className = 'fs-calendar__row';
|
|
149
|
+
el.setAttribute('role', 'row');
|
|
150
|
+
el.append(...children);
|
|
151
|
+
return el;
|
|
152
|
+
}
|
|
153
|
+
function render(focusGrid = false) {
|
|
154
|
+
const hadFocus = focusGrid || grid.contains(rootNode().activeElement);
|
|
155
|
+
const current = pageOf(view, focusIso);
|
|
156
|
+
const rows = [];
|
|
157
|
+
cellValues = new Map();
|
|
158
|
+
if (current.view === 'day') {
|
|
159
|
+
const { year, month } = current;
|
|
160
|
+
const ws = opts.weekStart;
|
|
161
|
+
rows.push(row(Array.from({ length: 7 }, (_, i) => {
|
|
162
|
+
const th = doc.createElement('span');
|
|
163
|
+
th.className = 'fs-calendar__weekday';
|
|
164
|
+
th.setAttribute('role', 'columnheader');
|
|
165
|
+
th.textContent = opts.weekdayNames[mod(ws + i, 7)];
|
|
166
|
+
return th;
|
|
167
|
+
})));
|
|
168
|
+
for (const week of monthGrid(year, month, ws)) {
|
|
169
|
+
rows.push(row(week.map((day) => {
|
|
170
|
+
if (day === null)
|
|
171
|
+
return blank();
|
|
172
|
+
const iso = formatDay(year, month, day);
|
|
173
|
+
const label = opts.cellLabel({ value: iso, year, month, day, weekday: weekday(year, month, day) });
|
|
174
|
+
return makeCell('day', iso, String(day), label);
|
|
175
|
+
})));
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
else {
|
|
179
|
+
const units = current.view === 'month'
|
|
180
|
+
? Array.from({ length: 12 }, (_, i) => {
|
|
181
|
+
return makeCell('month', formatMonth(current.year, i + 1), opts.monthNames[i]);
|
|
182
|
+
})
|
|
183
|
+
: Array.from({ length: 12 }, (_, i) => {
|
|
184
|
+
const year = current.from + i;
|
|
185
|
+
if (year < 1)
|
|
186
|
+
return blank();
|
|
187
|
+
const iso = String(year).padStart(4, '0');
|
|
188
|
+
return makeCell('year', iso, String(year));
|
|
189
|
+
});
|
|
190
|
+
for (let i = 0; i < 12; i += 3)
|
|
191
|
+
rows.push(row(units.slice(i, i + 3)));
|
|
192
|
+
}
|
|
193
|
+
grid.className = `fs-calendar__grid fs-calendar__grid--${view}`;
|
|
194
|
+
grid.replaceChildren(...rows);
|
|
195
|
+
const text = opts.heading(current);
|
|
196
|
+
heading.textContent = text;
|
|
197
|
+
const views = VIEWS[lowest()];
|
|
198
|
+
const top = view === views[0];
|
|
199
|
+
const up = view === 'year' ? undefined : opts.labels.up?.[view];
|
|
200
|
+
if (up)
|
|
201
|
+
viewButton.setAttribute('aria-label', `${text}, ${up}`);
|
|
202
|
+
else
|
|
203
|
+
viewButton.removeAttribute('aria-label');
|
|
204
|
+
toggle(viewButton, 'aria-disabled', top);
|
|
205
|
+
if (top)
|
|
206
|
+
viewIcon.remove();
|
|
207
|
+
else
|
|
208
|
+
viewButton.append(viewIcon);
|
|
209
|
+
prev.setAttribute('aria-label', opts.labels.prev[view]);
|
|
210
|
+
next.setAttribute('aria-label', opts.labels.next[view]);
|
|
211
|
+
toggle(prev, 'aria-disabled', !pageOpen(pageOf(view, page(view, focusIso, -1))) || isEdge(-1));
|
|
212
|
+
toggle(next, 'aria-disabled', !pageOpen(pageOf(view, page(view, focusIso, 1))) || isEdge(1));
|
|
213
|
+
// Announce a new heading (paging, stepping), never the first one. Not from inside the view button:
|
|
214
|
+
// a live region in a focused button whose name changes is read twice.
|
|
215
|
+
const out = opts.announcer ?? live;
|
|
216
|
+
if (opts.announcer)
|
|
217
|
+
live.remove();
|
|
218
|
+
else if (live.parentElement !== root)
|
|
219
|
+
root.append(live);
|
|
220
|
+
if (lastHeading && text !== lastHeading)
|
|
221
|
+
out.textContent = text;
|
|
222
|
+
lastHeading = text;
|
|
223
|
+
if (hadFocus)
|
|
224
|
+
stopCell()?.focus();
|
|
225
|
+
}
|
|
226
|
+
/** At year 1 or 9999 paging goes nowhere. */
|
|
227
|
+
function isEdge(dir) {
|
|
228
|
+
return JSON.stringify(pageOf(view, page(view, focusIso, dir))) === JSON.stringify(pageOf(view, focusIso));
|
|
229
|
+
}
|
|
230
|
+
function toggle(el, attr, on) {
|
|
231
|
+
if (on)
|
|
232
|
+
el.setAttribute(attr, 'true');
|
|
233
|
+
else
|
|
234
|
+
el.removeAttribute(attr);
|
|
235
|
+
}
|
|
236
|
+
const stopCell = () => [...cellValues.keys()].find((el) => el.tabIndex === 0);
|
|
237
|
+
/** Puts the focus value on a cell of the current view, keeping the finer parts it doesn't show. */
|
|
238
|
+
function onto(v, iso) {
|
|
239
|
+
if (v === 'year')
|
|
240
|
+
return addYears(focusIso, Number(iso) - Number(focusIso.slice(0, 4)));
|
|
241
|
+
if (v === 'month') {
|
|
242
|
+
const [y, m] = [Number(iso.slice(0, 4)), Number(iso.slice(5, 7))];
|
|
243
|
+
if (lowest() === 'month')
|
|
244
|
+
return iso;
|
|
245
|
+
return formatDay(y, m, Math.min(Number(focusIso.slice(8, 10)), daysInMonth(y, m)));
|
|
246
|
+
}
|
|
247
|
+
return iso;
|
|
248
|
+
}
|
|
249
|
+
function moveTo(iso) {
|
|
250
|
+
focusIso = settle(iso);
|
|
251
|
+
render(true);
|
|
252
|
+
}
|
|
253
|
+
function onKeydown(e) {
|
|
254
|
+
const target = e.target;
|
|
255
|
+
if (!cellValues.has(target) || e.altKey || e.ctrlKey || e.metaKey)
|
|
256
|
+
return;
|
|
257
|
+
const rtl = root.ownerDocument.defaultView?.getComputedStyle(root).direction === 'rtl';
|
|
258
|
+
const across = view === 'day' ? 7 : 3;
|
|
259
|
+
const step = (n) => view === 'day' ? addDays(focusIso, n) : view === 'month' ? addMonths(focusIso, n) : addYears(focusIso, n);
|
|
260
|
+
/** The focus value's column in its row, and the row's length. */
|
|
261
|
+
const column = () => {
|
|
262
|
+
if (view === 'day') {
|
|
263
|
+
const [y, m, d] = [Number(focusIso.slice(0, 4)), Number(focusIso.slice(5, 7)), Number(focusIso.slice(8, 10))];
|
|
264
|
+
const col = mod(weekday(y, m, d) - opts.weekStart, 7);
|
|
265
|
+
return { back: Math.min(col, d - 1), ahead: Math.min(6 - col, daysInMonth(y, m) - d) };
|
|
266
|
+
}
|
|
267
|
+
const index = view === 'month'
|
|
268
|
+
? Number(focusIso.slice(5, 7)) - 1
|
|
269
|
+
: Number(focusIso.slice(0, 4)) - yearPage(Number(focusIso.slice(0, 4))).from;
|
|
270
|
+
return { back: index % 3, ahead: 2 - (index % 3) };
|
|
271
|
+
};
|
|
272
|
+
let to;
|
|
273
|
+
switch (e.key) {
|
|
274
|
+
case 'ArrowRight':
|
|
275
|
+
to = step(rtl ? -1 : 1);
|
|
276
|
+
break;
|
|
277
|
+
case 'ArrowLeft':
|
|
278
|
+
to = step(rtl ? 1 : -1);
|
|
279
|
+
break;
|
|
280
|
+
case 'ArrowDown':
|
|
281
|
+
to = step(across);
|
|
282
|
+
break;
|
|
283
|
+
case 'ArrowUp':
|
|
284
|
+
to = step(-across);
|
|
285
|
+
break;
|
|
286
|
+
case 'Home':
|
|
287
|
+
to = step(-column().back);
|
|
288
|
+
break;
|
|
289
|
+
case 'End':
|
|
290
|
+
to = step(column().ahead);
|
|
291
|
+
break;
|
|
292
|
+
case 'PageUp':
|
|
293
|
+
case 'PageDown': {
|
|
294
|
+
const dir = e.key === 'PageUp' ? -1 : 1;
|
|
295
|
+
to = e.shiftKey && view === 'day' ? addYears(focusIso, dir) : page(view, focusIso, dir);
|
|
296
|
+
break;
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
if (to === undefined)
|
|
300
|
+
return;
|
|
301
|
+
e.preventDefault();
|
|
302
|
+
moveTo(to);
|
|
303
|
+
}
|
|
304
|
+
function onClick(e) {
|
|
305
|
+
const target = e.target;
|
|
306
|
+
if (viewButton.contains(target)) {
|
|
307
|
+
const views = VIEWS[lowest()];
|
|
308
|
+
const i = views.indexOf(view);
|
|
309
|
+
if (i > 0) {
|
|
310
|
+
view = views[i - 1];
|
|
311
|
+
render();
|
|
312
|
+
}
|
|
313
|
+
return;
|
|
314
|
+
}
|
|
315
|
+
for (const [b, dir] of [
|
|
316
|
+
[prev, -1],
|
|
317
|
+
[next, 1],
|
|
318
|
+
]) {
|
|
319
|
+
if (b.contains(target)) {
|
|
320
|
+
if (b.getAttribute('aria-disabled') !== 'true') {
|
|
321
|
+
focusIso = settle(page(view, focusIso, dir));
|
|
322
|
+
render();
|
|
323
|
+
}
|
|
324
|
+
return;
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
const cell = target.closest('.fs-calendar__cell');
|
|
328
|
+
const iso = cell && cellValues.get(cell);
|
|
329
|
+
if (!cell || !iso || cell.getAttribute('aria-disabled') === 'true')
|
|
330
|
+
return;
|
|
331
|
+
if (view === lowest()) {
|
|
332
|
+
value = iso;
|
|
333
|
+
focusIso = iso;
|
|
334
|
+
render(true);
|
|
335
|
+
root.dispatchEvent(new CustomEvent('fs-change', { bubbles: true, detail: { value: iso } }));
|
|
336
|
+
return;
|
|
337
|
+
}
|
|
338
|
+
focusIso = clampIso(onto(view, iso), min, max);
|
|
339
|
+
const views = VIEWS[lowest()];
|
|
340
|
+
view = views[views.indexOf(view) + 1];
|
|
341
|
+
render(true);
|
|
342
|
+
}
|
|
343
|
+
function onFocusin(e) {
|
|
344
|
+
const iso = cellValues.get(e.target);
|
|
345
|
+
if (iso === undefined || iso === focusIso.slice(0, WIDTH[view]))
|
|
346
|
+
return;
|
|
347
|
+
focusIso = onto(view, iso);
|
|
348
|
+
for (const el of cellValues.keys())
|
|
349
|
+
el.tabIndex = el === e.target ? 0 : -1;
|
|
350
|
+
}
|
|
351
|
+
readAll(options, true);
|
|
352
|
+
root.replaceChildren(header, grid);
|
|
353
|
+
render();
|
|
354
|
+
root.addEventListener('keydown', onKeydown);
|
|
355
|
+
root.addEventListener('click', onClick);
|
|
356
|
+
root.addEventListener('focusin', onFocusin);
|
|
357
|
+
return {
|
|
358
|
+
get value() {
|
|
359
|
+
return value;
|
|
360
|
+
},
|
|
361
|
+
get view() {
|
|
362
|
+
return view;
|
|
363
|
+
},
|
|
364
|
+
setValue(next) {
|
|
365
|
+
readAll({ value: next }, false);
|
|
366
|
+
render();
|
|
367
|
+
},
|
|
368
|
+
setView(next) {
|
|
369
|
+
if (!VIEWS[lowest()].includes(next))
|
|
370
|
+
return;
|
|
371
|
+
view = next;
|
|
372
|
+
render();
|
|
373
|
+
},
|
|
374
|
+
focus() {
|
|
375
|
+
stopCell()?.focus();
|
|
376
|
+
},
|
|
377
|
+
update(next) {
|
|
378
|
+
readAll(next, false);
|
|
379
|
+
render();
|
|
380
|
+
},
|
|
381
|
+
destroy() {
|
|
382
|
+
root.removeEventListener('keydown', onKeydown);
|
|
383
|
+
root.removeEventListener('click', onClick);
|
|
384
|
+
root.removeEventListener('focusin', onFocusin);
|
|
385
|
+
root.replaceChildren();
|
|
386
|
+
cellValues = new Map();
|
|
387
|
+
},
|
|
388
|
+
};
|
|
389
|
+
}
|
|
390
|
+
function check(o) {
|
|
391
|
+
if (o.monthNames?.length !== 12)
|
|
392
|
+
throw new TypeError('[fs] createCalendar: monthNames needs 12 names');
|
|
393
|
+
if (o.weekdayNames?.length !== 7)
|
|
394
|
+
throw new TypeError('[fs] createCalendar: weekdayNames needs 7 names');
|
|
395
|
+
return o;
|
|
396
|
+
}
|
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 CalendarView, createCalendar, type Granularity, } 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";
|