magic-pill 9.0.0 → 9.1.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 +44 -0
- package/README.md +259 -15
- package/dist/MagicPill/MagicPill.d.mts +7 -1
- package/dist/MagicPill/MagicPill.d.ts +7 -1
- package/dist/MagicPill/MagicPill.js +40 -17
- package/dist/MagicPill/MagicPill.mjs +42 -19
- package/dist/MagicPill/choreography.js +1 -1
- package/dist/MagicPill/choreography.mjs +1 -1
- package/dist/Pill/Pill.js +53 -17
- package/dist/Pill/Pill.mjs +54 -18
- package/dist/Pill/PillContext.js +3 -1
- package/dist/Pill/PillContext.mjs +3 -1
- package/dist/Pill/PillPanel.d.mts +5 -5
- package/dist/Pill/PillPanel.d.ts +5 -5
- package/dist/Pill/PillPanel.js +28 -20
- package/dist/Pill/PillPanel.mjs +28 -20
- package/dist/Pill/morph.js +25 -7
- package/dist/Pill/morph.mjs +22 -8
- package/dist/Pill/scene.js +90 -0
- package/dist/Pill/scene.mjs +89 -0
- package/dist/Pill/themed.js +36 -0
- package/dist/Pill/themed.mjs +34 -0
- package/dist/Pill/types.d.mts +50 -0
- package/dist/Pill/types.d.ts +50 -0
- package/dist/Pill/zone.js +160 -0
- package/dist/Pill/zone.mjs +159 -0
- package/dist/PillForm/Calendar.d.mts +15 -0
- package/dist/PillForm/Calendar.d.ts +16 -0
- package/dist/PillForm/Calendar.js +371 -0
- package/dist/PillForm/Calendar.mjs +370 -0
- package/dist/PillForm/FormContext.d.mts +2 -0
- package/dist/PillForm/FormContext.d.ts +2 -0
- package/dist/PillForm/FormContext.js +8 -0
- package/dist/PillForm/FormContext.mjs +8 -0
- package/dist/PillForm/PillCheckbox.d.mts +8 -0
- package/dist/PillForm/PillCheckbox.d.ts +8 -0
- package/dist/PillForm/PillCheckbox.js +45 -0
- package/dist/PillForm/PillCheckbox.mjs +45 -0
- package/dist/PillForm/PillChoices.d.mts +39 -0
- package/dist/PillForm/PillChoices.d.ts +39 -0
- package/dist/PillForm/PillChoices.js +103 -0
- package/dist/PillForm/PillChoices.mjs +103 -0
- package/dist/PillForm/PillCode.d.mts +12 -0
- package/dist/PillForm/PillCode.d.ts +12 -0
- package/dist/PillForm/PillCode.js +60 -0
- package/dist/PillForm/PillCode.mjs +60 -0
- package/dist/PillForm/PillColor.d.mts +10 -0
- package/dist/PillForm/PillColor.d.ts +10 -0
- package/dist/PillForm/PillColor.js +56 -0
- package/dist/PillForm/PillColor.mjs +56 -0
- package/dist/PillForm/PillDate.d.mts +44 -0
- package/dist/PillForm/PillDate.d.ts +44 -0
- package/dist/PillForm/PillDate.js +193 -0
- package/dist/PillForm/PillDate.mjs +193 -0
- package/dist/PillForm/PillField.d.mts +38 -0
- package/dist/PillForm/PillField.d.ts +38 -0
- package/dist/PillForm/PillField.js +81 -0
- package/dist/PillForm/PillField.mjs +80 -0
- package/dist/PillForm/PillFields.d.mts +11 -0
- package/dist/PillForm/PillFields.d.ts +11 -0
- package/dist/PillForm/PillFields.js +21 -0
- package/dist/PillForm/PillFields.mjs +21 -0
- package/dist/PillForm/PillFile.d.mts +8 -0
- package/dist/PillForm/PillFile.d.ts +8 -0
- package/dist/PillForm/PillFile.js +55 -0
- package/dist/PillForm/PillFile.mjs +55 -0
- package/dist/PillForm/PillForm.d.mts +29 -0
- package/dist/PillForm/PillForm.d.ts +29 -0
- package/dist/PillForm/PillForm.js +66 -0
- package/dist/PillForm/PillForm.mjs +65 -0
- package/dist/PillForm/PillFormPanel.d.mts +24 -0
- package/dist/PillForm/PillFormPanel.d.ts +24 -0
- package/dist/PillForm/PillFormPanel.js +53 -0
- package/dist/PillForm/PillFormPanel.mjs +53 -0
- package/dist/PillForm/PillInput.d.mts +20 -0
- package/dist/PillForm/PillInput.d.ts +20 -0
- package/dist/PillForm/PillInput.js +84 -0
- package/dist/PillForm/PillInput.mjs +84 -0
- package/dist/PillForm/PillSelect.d.mts +23 -0
- package/dist/PillForm/PillSelect.d.ts +23 -0
- package/dist/PillForm/PillSelect.js +87 -0
- package/dist/PillForm/PillSelect.mjs +87 -0
- package/dist/PillForm/PillSlider.d.mts +17 -0
- package/dist/PillForm/PillSlider.d.ts +17 -0
- package/dist/PillForm/PillSlider.js +61 -0
- package/dist/PillForm/PillSlider.mjs +61 -0
- package/dist/PillForm/PillStepper.d.mts +13 -0
- package/dist/PillForm/PillStepper.d.ts +13 -0
- package/dist/PillForm/PillStepper.js +84 -0
- package/dist/PillForm/PillStepper.mjs +84 -0
- package/dist/PillForm/PillSubmit.d.mts +14 -0
- package/dist/PillForm/PillSubmit.d.ts +14 -0
- package/dist/PillForm/PillSubmit.js +90 -0
- package/dist/PillForm/PillSubmit.mjs +89 -0
- package/dist/PillForm/PillSwitch.d.mts +5 -0
- package/dist/PillForm/PillSwitch.d.ts +5 -0
- package/dist/PillForm/PillSwitch.js +38 -0
- package/dist/PillForm/PillSwitch.mjs +38 -0
- package/dist/PillForm/PillTextarea.d.mts +5 -0
- package/dist/PillForm/PillTextarea.d.ts +5 -0
- package/dist/PillForm/PillTextarea.js +49 -0
- package/dist/PillForm/PillTextarea.mjs +49 -0
- package/dist/PillForm/dates.d.mts +1 -0
- package/dist/PillForm/dates.d.ts +1 -0
- package/dist/PillForm/dates.js +155 -0
- package/dist/PillForm/dates.mjs +139 -0
- package/dist/PillForm/glyphs.js +65 -0
- package/dist/PillForm/glyphs.mjs +56 -0
- package/dist/PillForm/keyboard.js +34 -0
- package/dist/PillForm/keyboard.mjs +34 -0
- package/dist/PillForm/popover.js +84 -0
- package/dist/PillForm/popover.mjs +83 -0
- package/dist/PillForm/send.d.mts +6 -0
- package/dist/PillForm/send.d.ts +6 -0
- package/dist/PillForm/send.js +44 -0
- package/dist/PillForm/send.mjs +44 -0
- package/dist/PillForm/validity.js +74 -0
- package/dist/PillForm/validity.mjs +72 -0
- package/dist/PillNavbar/PillNavbar.d.mts +29 -6
- package/dist/PillNavbar/PillNavbar.d.ts +29 -6
- package/dist/PillNavbar/PillNavbar.js +35 -11
- package/dist/PillNavbar/PillNavbar.mjs +36 -12
- package/dist/PillNavbar/room.js +84 -0
- package/dist/PillNavbar/room.mjs +83 -0
- package/dist/PillNotification/PillNotification.d.mts +6 -0
- package/dist/PillNotification/PillNotification.d.ts +6 -0
- package/dist/PillNotification/PillNotification.js +3 -0
- package/dist/PillNotification/PillNotification.mjs +3 -0
- package/dist/PillOptics/PillOptics.d.mts +8 -0
- package/dist/PillOptics/PillOptics.d.ts +8 -0
- package/dist/PillOptics/PillOptics.js +195 -0
- package/dist/PillOptics/PillOptics.mjs +195 -0
- package/dist/PillOptics/adaptive.js +212 -0
- package/dist/PillOptics/adaptive.mjs +208 -0
- package/dist/PillOptics/lens.js +77 -0
- package/dist/PillOptics/lens.mjs +73 -0
- package/dist/PillOptics/light.js +37 -0
- package/dist/PillOptics/light.mjs +36 -0
- package/dist/calendar.css +499 -0
- package/dist/forms.css +1452 -0
- package/dist/index.d.mts +24 -3
- package/dist/index.d.ts +24 -3
- package/dist/index.js +36 -0
- package/dist/index.mjs +19 -1
- package/dist/styles.css +169 -19
- package/dist/themes.css +449 -0
- package/dist/utils/visuallyHidden.js +18 -0
- package/dist/utils/visuallyHidden.mjs +18 -0
- package/package.json +4 -1
|
@@ -0,0 +1,370 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import { useEffect, useId, useMemo, useRef, useState } from "react";
|
|
4
|
+
import { addDays, addMonths, formatValue, isDayOutside, isOutside, localFirstDay, monthGrid, monthOf, parseDay, today, usesTwelveHours, weekOf } from "./dates.mjs";
|
|
5
|
+
import { ChevronGlyph } from "./glyphs.mjs";
|
|
6
|
+
import { Fragment as Fragment$1, jsx, jsxs } from "react/jsx-runtime";
|
|
7
|
+
|
|
8
|
+
// src/PillForm/Calendar.tsx
|
|
9
|
+
const CALENDAR_LABELS = {
|
|
10
|
+
previous: "Previous",
|
|
11
|
+
next: "Next",
|
|
12
|
+
zoomOut: "Choose a month",
|
|
13
|
+
today: "Today",
|
|
14
|
+
now: "Now",
|
|
15
|
+
clear: "Clear",
|
|
16
|
+
done: "Done",
|
|
17
|
+
hours: "Hours",
|
|
18
|
+
minutes: "Minutes",
|
|
19
|
+
period: "AM or PM",
|
|
20
|
+
week: "Week"
|
|
21
|
+
};
|
|
22
|
+
const YEARS = 12;
|
|
23
|
+
const range = (length) => Array.from({ length }, (_, index) => index);
|
|
24
|
+
const startOfYears = (year) => year - (year % YEARS + YEARS) % YEARS;
|
|
25
|
+
/** Where a grid's focus goes with a key, in items; `undefined` for any other key. */
|
|
26
|
+
function gridStep(key, columns, isRtl) {
|
|
27
|
+
switch (key) {
|
|
28
|
+
case "ArrowLeft": return isRtl ? 1 : -1;
|
|
29
|
+
case "ArrowRight": return isRtl ? -1 : 1;
|
|
30
|
+
case "ArrowUp": return -columns;
|
|
31
|
+
case "ArrowDown": return columns;
|
|
32
|
+
default: return;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
const isRtl = (element) => getComputedStyle(element).direction === "rtl";
|
|
36
|
+
/** A month of days, a year of months, a page of years, and the time: the part that's drawn. */
|
|
37
|
+
function Calendar({ type, day, time, min = "", max = "", step, locale, firstDay, disabledDate, labels, canClear, autoFocus, onChoose, onDone }) {
|
|
38
|
+
const now = useMemo(() => today(), []);
|
|
39
|
+
const hasDays = type !== "time";
|
|
40
|
+
const hasTime = type === "time" || type === "datetime-local";
|
|
41
|
+
const [view, setView] = useState(type === "month" ? "months" : "days");
|
|
42
|
+
const [cursor, setCursor] = useState(day || (isOutside(now.day, min.slice(0, 10), max.slice(0, 10)) && parseDay(min.slice(0, 10)) ? min.slice(0, 10) : now.day));
|
|
43
|
+
const [direction, setDirection] = useState(0);
|
|
44
|
+
const gridRef = useRef(null);
|
|
45
|
+
const moveFocus = useRef(autoFocus);
|
|
46
|
+
useEffect(() => {
|
|
47
|
+
if (day) setCursor(day);
|
|
48
|
+
}, [day]);
|
|
49
|
+
useEffect(() => {
|
|
50
|
+
if (!moveFocus.current) return;
|
|
51
|
+
moveFocus.current = false;
|
|
52
|
+
gridRef.current?.querySelector("[tabindex=\"0\"]")?.focus({ preventScroll: true });
|
|
53
|
+
});
|
|
54
|
+
const goTo = (next, focus = true) => {
|
|
55
|
+
setDirection(Math.sign(next.slice(0, 7).localeCompare(cursor.slice(0, 7))));
|
|
56
|
+
setCursor(next);
|
|
57
|
+
moveFocus.current = focus;
|
|
58
|
+
};
|
|
59
|
+
const choose = (chosen) => {
|
|
60
|
+
setCursor(chosen);
|
|
61
|
+
onChoose(chosen, time || (type === "datetime-local" ? roundTime(now.time, step) : ""), !hasTime);
|
|
62
|
+
};
|
|
63
|
+
const isDisabled = (candidate) => isDayOutside(candidate, type, min, max) || disabledDate?.(candidate) === true;
|
|
64
|
+
const year = Number(cursor.slice(0, 4));
|
|
65
|
+
const month = monthOf(cursor);
|
|
66
|
+
const first = type === "week" ? 1 : firstDay ?? localFirstDay(locale);
|
|
67
|
+
const name = (candidate, options) => formatValue(candidate, "date", locale, options);
|
|
68
|
+
const pageBy = (count) => goTo(addMonths(cursor, count * (view === "days" ? 1 : view === "months" ? 12 : 144)), false);
|
|
69
|
+
const firstYear = startOfYears(year);
|
|
70
|
+
const heading = view === "days" ? name(`${month}-01`, {
|
|
71
|
+
month: "long",
|
|
72
|
+
year: "numeric"
|
|
73
|
+
}) : view === "months" ? String(year) : `${firstYear} – ${firstYear + YEARS - 1}`;
|
|
74
|
+
const handleKeys = (event) => {
|
|
75
|
+
const rtl = isRtl(event.currentTarget);
|
|
76
|
+
const columns = view === "days" ? 7 : 3;
|
|
77
|
+
const stepBy = gridStep(event.key, columns, rtl);
|
|
78
|
+
const pages = view === "days" ? event.shiftKey ? 12 : 1 : view === "months" ? 12 : 144;
|
|
79
|
+
let next;
|
|
80
|
+
if (stepBy !== void 0) next = view === "days" ? addDays(cursor, stepBy) : addMonths(cursor, stepBy * (view === "months" ? 1 : 12));
|
|
81
|
+
else if (event.key === "PageUp") next = addMonths(cursor, -pages);
|
|
82
|
+
else if (event.key === "PageDown") next = addMonths(cursor, pages);
|
|
83
|
+
else if (view === "days" && (event.key === "Home" || event.key === "End")) {
|
|
84
|
+
const weekday = ((parseDay(cursor)?.getUTCDay() ?? 0) - first + 7) % 7;
|
|
85
|
+
next = addDays(cursor, event.key === "Home" ? -weekday : 6 - weekday);
|
|
86
|
+
}
|
|
87
|
+
if (next === void 0) return;
|
|
88
|
+
event.preventDefault();
|
|
89
|
+
goTo(next);
|
|
90
|
+
};
|
|
91
|
+
const cell = (key, content, { isChosen, isNow, isCursor, disabled, isOther, label, onClick }) => /* @__PURE__ */ jsx("td", {
|
|
92
|
+
role: "gridcell",
|
|
93
|
+
"aria-selected": isChosen,
|
|
94
|
+
children: /* @__PURE__ */ jsx("button", {
|
|
95
|
+
type: "button",
|
|
96
|
+
"data-mp": "calendar-cell",
|
|
97
|
+
"data-chosen": isChosen ? "" : void 0,
|
|
98
|
+
"data-other": isOther ? "" : void 0,
|
|
99
|
+
"aria-current": isNow ? "date" : void 0,
|
|
100
|
+
"aria-label": label,
|
|
101
|
+
"aria-disabled": disabled || void 0,
|
|
102
|
+
tabIndex: isCursor ? 0 : -1,
|
|
103
|
+
onClick: disabled ? void 0 : onClick,
|
|
104
|
+
children: content
|
|
105
|
+
})
|
|
106
|
+
}, key);
|
|
107
|
+
const renderDays = () => {
|
|
108
|
+
const weeks = monthGrid(month, first);
|
|
109
|
+
return /* @__PURE__ */ jsxs(Fragment$1, { children: [/* @__PURE__ */ jsx("thead", { children: /* @__PURE__ */ jsxs("tr", { children: [type === "week" && /* @__PURE__ */ jsx("th", {
|
|
110
|
+
scope: "col",
|
|
111
|
+
"aria-label": labels.week
|
|
112
|
+
}), weeks[0].map((weekday) => /* @__PURE__ */ jsx("th", {
|
|
113
|
+
scope: "col",
|
|
114
|
+
abbr: name(weekday, { weekday: "long" }),
|
|
115
|
+
children: name(weekday, { weekday: "narrow" })
|
|
116
|
+
}, weekday))] }) }), /* @__PURE__ */ jsx("tbody", { children: weeks.map((week) => {
|
|
117
|
+
const isChosenWeek = type === "week" && day !== "" && weekOf(week[0]) === weekOf(day);
|
|
118
|
+
return /* @__PURE__ */ jsxs("tr", {
|
|
119
|
+
"data-chosen": isChosenWeek ? "" : void 0,
|
|
120
|
+
children: [type === "week" && /* @__PURE__ */ jsx("th", {
|
|
121
|
+
scope: "row",
|
|
122
|
+
"data-mp": "calendar-week",
|
|
123
|
+
children: Number(weekOf(week[0]).slice(6))
|
|
124
|
+
}), week.map((candidate) => cell(candidate, Number(candidate.slice(8)), {
|
|
125
|
+
isChosen: type === "week" ? isChosenWeek : candidate === day,
|
|
126
|
+
isNow: candidate === now.day,
|
|
127
|
+
isCursor: candidate === cursor,
|
|
128
|
+
disabled: isDisabled(candidate),
|
|
129
|
+
isOther: monthOf(candidate) !== month,
|
|
130
|
+
label: name(candidate, { dateStyle: "full" }),
|
|
131
|
+
onClick: () => choose(candidate)
|
|
132
|
+
}))]
|
|
133
|
+
}, week[0]);
|
|
134
|
+
}) })] });
|
|
135
|
+
};
|
|
136
|
+
const renderTiles = (tiles) => /* @__PURE__ */ jsx("tbody", { children: range(tiles.length / 3).map((row) => /* @__PURE__ */ jsx("tr", { children: tiles.slice(row * 3, row * 3 + 3).map(({ key, content, target, isChosen, isNow, disabled }) => cell(key, content, {
|
|
137
|
+
isChosen,
|
|
138
|
+
isNow,
|
|
139
|
+
isCursor: key === (view === "months" ? month : String(year)),
|
|
140
|
+
disabled,
|
|
141
|
+
onClick: () => {
|
|
142
|
+
if (view === "months" && type === "month") choose(target);
|
|
143
|
+
else {
|
|
144
|
+
setView(view === "years" ? "months" : "days");
|
|
145
|
+
goTo(target);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
})) }, row)) });
|
|
149
|
+
const renderMonths = () => renderTiles(range(12).map((index) => {
|
|
150
|
+
const key = `${cursor.slice(0, 4)}-${String(index + 1).padStart(2, "0")}`;
|
|
151
|
+
return {
|
|
152
|
+
key,
|
|
153
|
+
content: name(`${key}-01`, { month: "short" }),
|
|
154
|
+
target: addMonths(cursor, index + 1 - Number(cursor.slice(5, 7))),
|
|
155
|
+
isChosen: type === "month" && day !== "" && monthOf(day) === key,
|
|
156
|
+
isNow: monthOf(now.day) === key,
|
|
157
|
+
disabled: isOutside(key, min.slice(0, 7), max.slice(0, 7)) && type !== "week"
|
|
158
|
+
};
|
|
159
|
+
}));
|
|
160
|
+
const renderYears = () => renderTiles(range(YEARS).map((index) => {
|
|
161
|
+
const key = String(firstYear + index);
|
|
162
|
+
return {
|
|
163
|
+
key,
|
|
164
|
+
content: key,
|
|
165
|
+
target: addMonths(cursor, (firstYear + index - year) * 12),
|
|
166
|
+
isChosen: day !== "" && day.slice(0, 4) === key.padStart(4, "0"),
|
|
167
|
+
isNow: now.day.slice(0, 4) === key,
|
|
168
|
+
disabled: isOutside(key.padStart(4, "0"), min.slice(0, 4), max.slice(0, 4))
|
|
169
|
+
};
|
|
170
|
+
}));
|
|
171
|
+
const isTodayDisabled = hasDays ? isDisabled(now.day) : isOutside(now.time, min, max);
|
|
172
|
+
return /* @__PURE__ */ jsxs("div", {
|
|
173
|
+
"data-mp": "calendar-body",
|
|
174
|
+
"data-type": type,
|
|
175
|
+
children: [
|
|
176
|
+
hasDays && /* @__PURE__ */ jsxs("div", {
|
|
177
|
+
"data-mp": "calendar-days",
|
|
178
|
+
children: [/* @__PURE__ */ jsxs("div", {
|
|
179
|
+
"data-mp": "calendar-head",
|
|
180
|
+
children: [
|
|
181
|
+
/* @__PURE__ */ jsx("button", {
|
|
182
|
+
type: "button",
|
|
183
|
+
"data-mp": "calendar-button",
|
|
184
|
+
"aria-label": labels.previous,
|
|
185
|
+
onClick: () => pageBy(-1),
|
|
186
|
+
children: /* @__PURE__ */ jsx(ChevronGlyph, {})
|
|
187
|
+
}),
|
|
188
|
+
/* @__PURE__ */ jsx("button", {
|
|
189
|
+
type: "button",
|
|
190
|
+
"data-mp": "calendar-heading",
|
|
191
|
+
"aria-live": "polite",
|
|
192
|
+
disabled: view === "years",
|
|
193
|
+
title: view === "years" ? void 0 : labels.zoomOut,
|
|
194
|
+
onClick: () => {
|
|
195
|
+
setDirection(0);
|
|
196
|
+
setView(view === "days" ? "months" : "years");
|
|
197
|
+
moveFocus.current = true;
|
|
198
|
+
},
|
|
199
|
+
children: heading
|
|
200
|
+
}),
|
|
201
|
+
/* @__PURE__ */ jsx("button", {
|
|
202
|
+
type: "button",
|
|
203
|
+
"data-mp": "calendar-button",
|
|
204
|
+
"data-next": "",
|
|
205
|
+
"aria-label": labels.next,
|
|
206
|
+
onClick: () => pageBy(1),
|
|
207
|
+
children: /* @__PURE__ */ jsx(ChevronGlyph, {})
|
|
208
|
+
})
|
|
209
|
+
]
|
|
210
|
+
}), /* @__PURE__ */ jsx("table", {
|
|
211
|
+
ref: gridRef,
|
|
212
|
+
role: "grid",
|
|
213
|
+
"aria-label": heading,
|
|
214
|
+
"data-mp": "calendar-grid",
|
|
215
|
+
"data-view": view,
|
|
216
|
+
"data-direction": direction || void 0,
|
|
217
|
+
onKeyDown: handleKeys,
|
|
218
|
+
children: view === "days" ? renderDays() : view === "months" ? renderMonths() : renderYears()
|
|
219
|
+
}, `${view} ${view === "days" ? month : view === "months" ? year : firstYear}`)]
|
|
220
|
+
}),
|
|
221
|
+
hasTime && /* @__PURE__ */ jsx(TimeColumns, {
|
|
222
|
+
time,
|
|
223
|
+
step,
|
|
224
|
+
locale,
|
|
225
|
+
labels,
|
|
226
|
+
autoFocus: autoFocus && !hasDays,
|
|
227
|
+
isDisabled: (candidate) => type === "time" ? isOutside(candidate, min, max) : day !== "" && isOutside(`${day}T${candidate}`, min, max),
|
|
228
|
+
onChoose: (chosen) => onChoose(day || (hasDays ? cursor : ""), chosen, false)
|
|
229
|
+
}),
|
|
230
|
+
/* @__PURE__ */ jsxs("div", {
|
|
231
|
+
"data-mp": "calendar-foot",
|
|
232
|
+
children: [
|
|
233
|
+
/* @__PURE__ */ jsx("button", {
|
|
234
|
+
type: "button",
|
|
235
|
+
"data-mp": "calendar-button",
|
|
236
|
+
disabled: isTodayDisabled,
|
|
237
|
+
onClick: () => {
|
|
238
|
+
if (hasDays) goTo(now.day, false);
|
|
239
|
+
onChoose(hasDays ? now.day : "", hasTime ? now.time : "", !hasTime);
|
|
240
|
+
},
|
|
241
|
+
children: hasDays ? labels.today : labels.now
|
|
242
|
+
}),
|
|
243
|
+
canClear && /* @__PURE__ */ jsx("button", {
|
|
244
|
+
type: "button",
|
|
245
|
+
"data-mp": "calendar-button",
|
|
246
|
+
onClick: () => onChoose("", "", true),
|
|
247
|
+
children: labels.clear
|
|
248
|
+
}),
|
|
249
|
+
hasTime && onDone && /* @__PURE__ */ jsx("button", {
|
|
250
|
+
type: "button",
|
|
251
|
+
"data-mp": "calendar-button",
|
|
252
|
+
"data-main": "",
|
|
253
|
+
onClick: onDone,
|
|
254
|
+
children: labels.done
|
|
255
|
+
})
|
|
256
|
+
]
|
|
257
|
+
})
|
|
258
|
+
]
|
|
259
|
+
});
|
|
260
|
+
}
|
|
261
|
+
/** Minutes between two choices: every five, unless the step asks for another. */
|
|
262
|
+
const minuteStep = (step) => step && step >= 60 ? Math.max(1, Math.round(step / 60)) : 5;
|
|
263
|
+
/** A time one can choose, at or before this one. */
|
|
264
|
+
function roundTime(time, step) {
|
|
265
|
+
const [hours, minutes] = time.split(":").map(Number);
|
|
266
|
+
const by = minuteStep(step);
|
|
267
|
+
return `${String(hours).padStart(2, "0")}:${String(minutes - minutes % by).padStart(2, "0")}`;
|
|
268
|
+
}
|
|
269
|
+
function TimeColumns({ time, step, locale, labels, autoFocus, isDisabled, onChoose }) {
|
|
270
|
+
const isTwelve = useMemo(() => usesTwelveHours(locale), [locale]);
|
|
271
|
+
const [hours, minutes] = time ? time.split(":").map(Number) : [void 0, void 0];
|
|
272
|
+
const by = minuteStep(step);
|
|
273
|
+
const at = (hour, minute) => `${String(hour).padStart(2, "0")}:${String(minute).padStart(2, "0")}`;
|
|
274
|
+
const isAfternoon = (hours ?? 0) >= 12;
|
|
275
|
+
const twoDigits = (value) => String(value).padStart(2, "0");
|
|
276
|
+
const periods = useMemo(() => [9, 15].map((hour) => new Intl.DateTimeFormat(locale, {
|
|
277
|
+
hour: "numeric",
|
|
278
|
+
hour12: true,
|
|
279
|
+
timeZone: "UTC"
|
|
280
|
+
}).formatToParts(new Date(Date.UTC(2e3, 0, 1, hour))).find((part) => part.type === "dayPeriod")?.value ?? (hour < 12 ? "AM" : "PM")), [locale]);
|
|
281
|
+
const minuteChoices = [.../* @__PURE__ */ new Set([...range(Math.ceil(60 / by)).map((index) => index * by), ...minutes === void 0 ? [] : [minutes]])].sort((a, b) => a - b);
|
|
282
|
+
const hourChoices = isTwelve ? range(12).map((index) => index + (isAfternoon ? 12 : 0)) : range(24);
|
|
283
|
+
return /* @__PURE__ */ jsxs("div", {
|
|
284
|
+
"data-mp": "calendar-time",
|
|
285
|
+
children: [
|
|
286
|
+
/* @__PURE__ */ jsx(Column, {
|
|
287
|
+
label: labels.hours,
|
|
288
|
+
autoFocus,
|
|
289
|
+
chosen: hours,
|
|
290
|
+
choices: hourChoices.map((hour) => ({
|
|
291
|
+
value: hour,
|
|
292
|
+
text: isTwelve ? String(hour % 12 || 12) : twoDigits(hour),
|
|
293
|
+
disabled: minuteChoices.every((minute) => isDisabled(at(hour, minute)))
|
|
294
|
+
})),
|
|
295
|
+
onChoose: (hour) => onChoose(at(hour, minutes ?? 0))
|
|
296
|
+
}),
|
|
297
|
+
/* @__PURE__ */ jsx(Column, {
|
|
298
|
+
label: labels.minutes,
|
|
299
|
+
chosen: minutes,
|
|
300
|
+
choices: minuteChoices.map((minute) => ({
|
|
301
|
+
value: minute,
|
|
302
|
+
text: twoDigits(minute),
|
|
303
|
+
disabled: hours !== void 0 && isDisabled(at(hours, minute))
|
|
304
|
+
})),
|
|
305
|
+
onChoose: (minute) => onChoose(at(hours ?? 0, minute))
|
|
306
|
+
}),
|
|
307
|
+
isTwelve && /* @__PURE__ */ jsx(Column, {
|
|
308
|
+
label: labels.period,
|
|
309
|
+
chosen: hours === void 0 ? void 0 : isAfternoon ? 1 : 0,
|
|
310
|
+
choices: periods.map((text, value) => ({
|
|
311
|
+
value,
|
|
312
|
+
text,
|
|
313
|
+
disabled: hours !== void 0 && isDisabled(at(hours % 12 + value * 12, minutes ?? 0))
|
|
314
|
+
})),
|
|
315
|
+
onChoose: (period) => onChoose(at((hours ?? 0) % 12 + period * 12, minutes ?? 0))
|
|
316
|
+
})
|
|
317
|
+
]
|
|
318
|
+
});
|
|
319
|
+
}
|
|
320
|
+
/** A list to scroll, or to go through with the arrows. */
|
|
321
|
+
function Column({ label, chosen, choices, autoFocus, onChoose }) {
|
|
322
|
+
const id = useId();
|
|
323
|
+
const listRef = useRef(null);
|
|
324
|
+
useEffect(() => {
|
|
325
|
+
const list = listRef.current;
|
|
326
|
+
const option = list?.querySelector("[aria-selected=\"true\"]");
|
|
327
|
+
if (list && option) list.scrollTop = option.offsetTop - (list.clientHeight - option.offsetHeight) / 2;
|
|
328
|
+
}, [chosen, choices.length]);
|
|
329
|
+
useEffect(() => {
|
|
330
|
+
if (autoFocus) listRef.current?.focus({ preventScroll: true });
|
|
331
|
+
}, [autoFocus]);
|
|
332
|
+
const handleKeys = (event) => {
|
|
333
|
+
const enabled = choices.filter((choice) => !choice.disabled);
|
|
334
|
+
const index = enabled.findIndex((choice) => choice.value === chosen);
|
|
335
|
+
const target = {
|
|
336
|
+
ArrowDown: enabled[index + 1],
|
|
337
|
+
ArrowUp: enabled[index < 0 ? enabled.length - 1 : index - 1],
|
|
338
|
+
Home: enabled[0],
|
|
339
|
+
End: enabled[enabled.length - 1]
|
|
340
|
+
}[event.key];
|
|
341
|
+
if (![
|
|
342
|
+
"ArrowDown",
|
|
343
|
+
"ArrowUp",
|
|
344
|
+
"Home",
|
|
345
|
+
"End"
|
|
346
|
+
].includes(event.key)) return;
|
|
347
|
+
event.preventDefault();
|
|
348
|
+
if (target) onChoose(target.value);
|
|
349
|
+
};
|
|
350
|
+
return /* @__PURE__ */ jsx("div", {
|
|
351
|
+
ref: listRef,
|
|
352
|
+
role: "listbox",
|
|
353
|
+
"aria-label": label,
|
|
354
|
+
"aria-activedescendant": chosen === void 0 ? void 0 : id + chosen,
|
|
355
|
+
tabIndex: 0,
|
|
356
|
+
"data-mp": "calendar-column",
|
|
357
|
+
onKeyDown: handleKeys,
|
|
358
|
+
children: choices.map(({ value, text, disabled }) => /* @__PURE__ */ jsx("div", {
|
|
359
|
+
id: id + value,
|
|
360
|
+
role: "option",
|
|
361
|
+
"aria-selected": value === chosen,
|
|
362
|
+
"aria-disabled": disabled || void 0,
|
|
363
|
+
"data-mp": "calendar-option",
|
|
364
|
+
onClick: disabled ? void 0 : () => onChoose(value),
|
|
365
|
+
children: text
|
|
366
|
+
}, value))
|
|
367
|
+
});
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
export { CALENDAR_LABELS, Calendar };
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { ComponentPropsWithoutRef } from "react";
|
|
2
|
+
import { FieldProps } from "./PillField.mjs";
|
|
3
|
+
export interface PillCheckboxProps extends Omit<FieldProps, "hideLabel" | "optional">, Omit<ComponentPropsWithoutRef<"input">, "type" | "children"> {
|
|
4
|
+
/** Neither checked nor not: some of what it stands for is. */
|
|
5
|
+
indeterminate?: boolean;
|
|
6
|
+
}
|
|
7
|
+
/** Something to agree to, sent with the form. */
|
|
8
|
+
export declare const PillCheckbox: import("react").ForwardRefExoticComponent<PillCheckboxProps & import("react").RefAttributes<HTMLInputElement>>;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { ComponentPropsWithoutRef } from "react";
|
|
2
|
+
import { FieldProps } from "./PillField.js";
|
|
3
|
+
export interface PillCheckboxProps extends Omit<FieldProps, "hideLabel" | "optional">, Omit<ComponentPropsWithoutRef<"input">, "type" | "children"> {
|
|
4
|
+
/** Neither checked nor not: some of what it stands for is. */
|
|
5
|
+
indeterminate?: boolean;
|
|
6
|
+
}
|
|
7
|
+
/** Something to agree to, sent with the form. */
|
|
8
|
+
export declare const PillCheckbox: import("react").ForwardRefExoticComponent<PillCheckboxProps & import("react").RefAttributes<HTMLInputElement>>;
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
let react = require("react");
|
|
4
|
+
const require_PillField = require("./PillField.js");
|
|
5
|
+
const require_glyphs = require("./glyphs.js");
|
|
6
|
+
const require_validity = require("./validity.js");
|
|
7
|
+
const require_react = require("../utils/react.js");
|
|
8
|
+
let react_jsx_runtime = require("react/jsx-runtime");
|
|
9
|
+
|
|
10
|
+
// src/PillForm/PillCheckbox.tsx
|
|
11
|
+
/** Something to agree to, sent with the form. */
|
|
12
|
+
const PillCheckbox = (0, react.forwardRef)(function PillCheckbox({ label, hint, error, className, indeterminate = false, onInput, onInvalid, ...rest }, forwardedRef) {
|
|
13
|
+
const inputRef = (0, react.useRef)(null);
|
|
14
|
+
const ref = (0, react.useMemo)(() => require_react.mergeRefs(inputRef, forwardedRef), [forwardedRef]);
|
|
15
|
+
const validity = require_validity.useValidity(error, inputRef);
|
|
16
|
+
(0, react.useEffect)(() => {
|
|
17
|
+
if (inputRef.current) inputRef.current.indeterminate = indeterminate;
|
|
18
|
+
}, [indeterminate]);
|
|
19
|
+
return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(require_PillField.FieldShell, {
|
|
20
|
+
label,
|
|
21
|
+
hint,
|
|
22
|
+
className,
|
|
23
|
+
error: validity.error,
|
|
24
|
+
layout: "leading",
|
|
25
|
+
"data-kind": "checkbox",
|
|
26
|
+
children: (control) => /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("span", {
|
|
27
|
+
"data-mp": "field-control",
|
|
28
|
+
"data-type": "checkbox",
|
|
29
|
+
children: [/* @__PURE__ */ (0, react_jsx_runtime.jsx)("input", {
|
|
30
|
+
...rest,
|
|
31
|
+
...control,
|
|
32
|
+
ref,
|
|
33
|
+
type: "checkbox",
|
|
34
|
+
onInput: require_validity.both(validity.handlers.onInput, onInput),
|
|
35
|
+
onInvalid: require_validity.both(validity.handlers.onInvalid, onInvalid)
|
|
36
|
+
}), /* @__PURE__ */ (0, react_jsx_runtime.jsx)("span", {
|
|
37
|
+
"data-mp": "field-tick",
|
|
38
|
+
"aria-hidden": "true",
|
|
39
|
+
children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(require_glyphs.TickGlyph, {})
|
|
40
|
+
})]
|
|
41
|
+
})
|
|
42
|
+
});
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
exports.PillCheckbox = PillCheckbox;
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import { forwardRef, useEffect, useMemo, useRef } from "react";
|
|
4
|
+
import { FieldShell } from "./PillField.mjs";
|
|
5
|
+
import { TickGlyph } from "./glyphs.mjs";
|
|
6
|
+
import { both, useValidity } from "./validity.mjs";
|
|
7
|
+
import { mergeRefs } from "../utils/react.mjs";
|
|
8
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
9
|
+
|
|
10
|
+
// src/PillForm/PillCheckbox.tsx
|
|
11
|
+
/** Something to agree to, sent with the form. */
|
|
12
|
+
const PillCheckbox = forwardRef(function PillCheckbox({ label, hint, error, className, indeterminate = false, onInput, onInvalid, ...rest }, forwardedRef) {
|
|
13
|
+
const inputRef = useRef(null);
|
|
14
|
+
const ref = useMemo(() => mergeRefs(inputRef, forwardedRef), [forwardedRef]);
|
|
15
|
+
const validity = useValidity(error, inputRef);
|
|
16
|
+
useEffect(() => {
|
|
17
|
+
if (inputRef.current) inputRef.current.indeterminate = indeterminate;
|
|
18
|
+
}, [indeterminate]);
|
|
19
|
+
return /* @__PURE__ */ jsx(FieldShell, {
|
|
20
|
+
label,
|
|
21
|
+
hint,
|
|
22
|
+
className,
|
|
23
|
+
error: validity.error,
|
|
24
|
+
layout: "leading",
|
|
25
|
+
"data-kind": "checkbox",
|
|
26
|
+
children: (control) => /* @__PURE__ */ jsxs("span", {
|
|
27
|
+
"data-mp": "field-control",
|
|
28
|
+
"data-type": "checkbox",
|
|
29
|
+
children: [/* @__PURE__ */ jsx("input", {
|
|
30
|
+
...rest,
|
|
31
|
+
...control,
|
|
32
|
+
ref,
|
|
33
|
+
type: "checkbox",
|
|
34
|
+
onInput: both(validity.handlers.onInput, onInput),
|
|
35
|
+
onInvalid: both(validity.handlers.onInvalid, onInvalid)
|
|
36
|
+
}), /* @__PURE__ */ jsx("span", {
|
|
37
|
+
"data-mp": "field-tick",
|
|
38
|
+
"aria-hidden": "true",
|
|
39
|
+
children: /* @__PURE__ */ jsx(TickGlyph, {})
|
|
40
|
+
})]
|
|
41
|
+
})
|
|
42
|
+
});
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
export { PillCheckbox };
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { ChangeEvent, ReactNode } from "react";
|
|
2
|
+
import { FieldProps } from "./PillField.mjs";
|
|
3
|
+
export interface PillChoice {
|
|
4
|
+
value: string;
|
|
5
|
+
label: ReactNode;
|
|
6
|
+
icon?: ReactNode;
|
|
7
|
+
/** Under its label, in a list. */
|
|
8
|
+
hint?: ReactNode;
|
|
9
|
+
disabled?: boolean;
|
|
10
|
+
}
|
|
11
|
+
interface PillChoicesBase extends FieldProps {
|
|
12
|
+
/** A string is both the value and the label. */
|
|
13
|
+
options: readonly (string | PillChoice)[];
|
|
14
|
+
/** The name they're sent with. */
|
|
15
|
+
name?: string;
|
|
16
|
+
/** One under the other (`list`, the default), in one bar (`segmented`), or as `chips`. */
|
|
17
|
+
look?: "list" | "segmented" | "chips";
|
|
18
|
+
disabled?: boolean;
|
|
19
|
+
className?: string;
|
|
20
|
+
}
|
|
21
|
+
export interface PillSingleChoiceProps extends PillChoicesBase {
|
|
22
|
+
multiple?: false;
|
|
23
|
+
value?: string;
|
|
24
|
+
defaultValue?: string;
|
|
25
|
+
onChange?: (value: string, event: ChangeEvent<HTMLInputElement>) => void;
|
|
26
|
+
required?: boolean;
|
|
27
|
+
}
|
|
28
|
+
export interface PillMultipleChoiceProps extends PillChoicesBase {
|
|
29
|
+
/** Several of them, on checkboxes. */
|
|
30
|
+
multiple: true;
|
|
31
|
+
value?: readonly string[];
|
|
32
|
+
defaultValue?: readonly string[];
|
|
33
|
+
onChange?: (value: string[], event: ChangeEvent<HTMLInputElement>) => void;
|
|
34
|
+
required?: undefined;
|
|
35
|
+
}
|
|
36
|
+
export type PillChoicesProps = PillSingleChoiceProps | PillMultipleChoiceProps;
|
|
37
|
+
/** One of several choices, or several of them. */
|
|
38
|
+
export declare function PillChoices(props: PillChoicesProps): import("react").JSX.Element;
|
|
39
|
+
export {};
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { ChangeEvent, ReactNode } from "react";
|
|
2
|
+
import { FieldProps } from "./PillField.js";
|
|
3
|
+
export interface PillChoice {
|
|
4
|
+
value: string;
|
|
5
|
+
label: ReactNode;
|
|
6
|
+
icon?: ReactNode;
|
|
7
|
+
/** Under its label, in a list. */
|
|
8
|
+
hint?: ReactNode;
|
|
9
|
+
disabled?: boolean;
|
|
10
|
+
}
|
|
11
|
+
interface PillChoicesBase extends FieldProps {
|
|
12
|
+
/** A string is both the value and the label. */
|
|
13
|
+
options: readonly (string | PillChoice)[];
|
|
14
|
+
/** The name they're sent with. */
|
|
15
|
+
name?: string;
|
|
16
|
+
/** One under the other (`list`, the default), in one bar (`segmented`), or as `chips`. */
|
|
17
|
+
look?: "list" | "segmented" | "chips";
|
|
18
|
+
disabled?: boolean;
|
|
19
|
+
className?: string;
|
|
20
|
+
}
|
|
21
|
+
export interface PillSingleChoiceProps extends PillChoicesBase {
|
|
22
|
+
multiple?: false;
|
|
23
|
+
value?: string;
|
|
24
|
+
defaultValue?: string;
|
|
25
|
+
onChange?: (value: string, event: ChangeEvent<HTMLInputElement>) => void;
|
|
26
|
+
required?: boolean;
|
|
27
|
+
}
|
|
28
|
+
export interface PillMultipleChoiceProps extends PillChoicesBase {
|
|
29
|
+
/** Several of them, on checkboxes. */
|
|
30
|
+
multiple: true;
|
|
31
|
+
value?: readonly string[];
|
|
32
|
+
defaultValue?: readonly string[];
|
|
33
|
+
onChange?: (value: string[], event: ChangeEvent<HTMLInputElement>) => void;
|
|
34
|
+
required?: undefined;
|
|
35
|
+
}
|
|
36
|
+
export type PillChoicesProps = PillSingleChoiceProps | PillMultipleChoiceProps;
|
|
37
|
+
/** One of several choices, or several of them. */
|
|
38
|
+
export declare function PillChoices(props: PillChoicesProps): import("react").JSX.Element;
|
|
39
|
+
export {};
|