magic-pill 9.0.0 → 9.1.1
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 +50 -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 +1454 -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,139 @@
|
|
|
1
|
+
// src/PillForm/dates.ts
|
|
2
|
+
const DAY = 864e5;
|
|
3
|
+
const pad = (value, length = 2) => String(value).padStart(length, "0");
|
|
4
|
+
/** A day as a date, at midnight UTC; undefined if it isn't one. */
|
|
5
|
+
function parseDay(day) {
|
|
6
|
+
const match = /^(\d{4})-(\d{2})-(\d{2})$/.exec(day);
|
|
7
|
+
if (!match) return void 0;
|
|
8
|
+
const [year, month, date] = match.slice(1).map(Number);
|
|
9
|
+
const parsed = new Date(Date.UTC(year, month - 1, date));
|
|
10
|
+
parsed.setUTCFullYear(year);
|
|
11
|
+
return formatDay(parsed) === day ? parsed : void 0;
|
|
12
|
+
}
|
|
13
|
+
const formatDay = (date) => `${pad(date.getUTCFullYear(), 4)}-${pad(date.getUTCMonth() + 1)}-${pad(date.getUTCDate())}`;
|
|
14
|
+
const addDays = (day, count) => {
|
|
15
|
+
const date = parseDay(day);
|
|
16
|
+
return date ? formatDay(new Date(date.getTime() + count * DAY)) : day;
|
|
17
|
+
};
|
|
18
|
+
/** The same day in another month, or that month's last one. */
|
|
19
|
+
function addMonths(day, count) {
|
|
20
|
+
const date = parseDay(day);
|
|
21
|
+
if (!date) return day;
|
|
22
|
+
const target = new Date(Date.UTC(date.getUTCFullYear(), date.getUTCMonth() + count, 1));
|
|
23
|
+
target.setUTCDate(Math.min(date.getUTCDate(), daysInMonth(target)));
|
|
24
|
+
return formatDay(target);
|
|
25
|
+
}
|
|
26
|
+
const daysInMonth = (date) => new Date(Date.UTC(date.getUTCFullYear(), date.getUTCMonth() + 1, 0)).getUTCDate();
|
|
27
|
+
const monthOf = (day) => day.slice(0, 7);
|
|
28
|
+
/** The ISO week a day is in: the one its Thursday is in. */
|
|
29
|
+
function weekOf(day) {
|
|
30
|
+
const date = parseDay(day);
|
|
31
|
+
if (!date) return "";
|
|
32
|
+
const thursday = new Date(date.getTime() + (4 - (date.getUTCDay() || 7)) * DAY);
|
|
33
|
+
const year = thursday.getUTCFullYear();
|
|
34
|
+
const firstDay = Date.UTC(year, 0, 1);
|
|
35
|
+
const week = Math.ceil(((thursday.getTime() - firstDay) / DAY + 1) / 7);
|
|
36
|
+
return `${pad(year, 4)}-W${pad(week)}`;
|
|
37
|
+
}
|
|
38
|
+
/** The Monday of an ISO week. */
|
|
39
|
+
function firstDayOfWeek(week) {
|
|
40
|
+
const match = /^(\d{4})-W(\d{2})$/.exec(week);
|
|
41
|
+
if (!match) return "";
|
|
42
|
+
const [year, number] = match.slice(1).map(Number);
|
|
43
|
+
const fourth = new Date(Date.UTC(year, 0, 4));
|
|
44
|
+
const monday = fourth.getTime() - ((fourth.getUTCDay() || 7) - 1) * DAY;
|
|
45
|
+
return formatDay(new Date(monday + (number - 1) * 7 * DAY));
|
|
46
|
+
}
|
|
47
|
+
/** Six weeks of days around a month, so its calendar never changes height. `firstDay`: 0 for Sunday. */
|
|
48
|
+
function monthGrid(month, firstDay) {
|
|
49
|
+
const first = `${month}-01`;
|
|
50
|
+
const date = parseDay(first);
|
|
51
|
+
if (!date) return [];
|
|
52
|
+
const start = addDays(first, -((date.getUTCDay() - firstDay + 7) % 7));
|
|
53
|
+
return Array.from({ length: 6 }, (_, week) => Array.from({ length: 7 }, (_, day) => addDays(start, week * 7 + day)));
|
|
54
|
+
}
|
|
55
|
+
/** The day a value is about: what its calendar opens on. */
|
|
56
|
+
function dayOf(value, type) {
|
|
57
|
+
switch (type) {
|
|
58
|
+
case "month": return parseDay(`${value}-01`) ? `${value}-01` : "";
|
|
59
|
+
case "week": return firstDayOfWeek(value);
|
|
60
|
+
case "time": return "";
|
|
61
|
+
default: return parseDay(value.slice(0, 10)) ? value.slice(0, 10) : "";
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
const timeOf = (value, type) => (/(\d{2}:\d{2})/.exec(type === "time" ? value : type === "datetime-local" ? value.slice(11) : "") ?? [""])[0];
|
|
65
|
+
/** A day (and a time) as a value of this type. */
|
|
66
|
+
function toValue(type, day, time = "") {
|
|
67
|
+
switch (type) {
|
|
68
|
+
case "month": return monthOf(day);
|
|
69
|
+
case "week": return weekOf(day);
|
|
70
|
+
case "time": return time;
|
|
71
|
+
case "datetime-local": return day ? `${day}T${time || "00:00"}` : "";
|
|
72
|
+
default: return day;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
/** Whether a whole day (or the month, or the week it's in) is out of bounds. */
|
|
76
|
+
function isDayOutside(day, type, min = "", max = "") {
|
|
77
|
+
const value = type === "month" || type === "week" ? toValue(type, day) : day;
|
|
78
|
+
const [from, to] = type === "datetime-local" ? [min.slice(0, 10), max.slice(0, 10)] : [min, max];
|
|
79
|
+
return from !== "" && value < from || to !== "" && value > to;
|
|
80
|
+
}
|
|
81
|
+
const isOutside = (value, min = "", max = "") => min !== "" && value < min || max !== "" && value > max;
|
|
82
|
+
/** Today and now, where the person is. */
|
|
83
|
+
function today(now = /* @__PURE__ */ new Date()) {
|
|
84
|
+
return {
|
|
85
|
+
day: `${pad(now.getFullYear(), 4)}-${pad(now.getMonth() + 1)}-${pad(now.getDate())}`,
|
|
86
|
+
time: `${pad(now.getHours())}:${pad(now.getMinutes())}`
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
/** The first day of the week there, 0 for Sunday; Monday where the browser doesn't know. */
|
|
90
|
+
function localFirstDay(locale) {
|
|
91
|
+
try {
|
|
92
|
+
const language = locale ?? new Intl.DateTimeFormat().resolvedOptions().locale;
|
|
93
|
+
const info = new Intl.Locale(language);
|
|
94
|
+
return ((info.getWeekInfo?.() ?? info.weekInfo)?.firstDay ?? 1) % 7;
|
|
95
|
+
} catch {
|
|
96
|
+
return 1;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
/** Whether the clock there goes to 12. */
|
|
100
|
+
function usesTwelveHours(locale) {
|
|
101
|
+
return new Intl.DateTimeFormat(locale, { hour: "numeric" }).resolvedOptions().hour12 === true;
|
|
102
|
+
}
|
|
103
|
+
const FORMATS = {
|
|
104
|
+
date: { dateStyle: "long" },
|
|
105
|
+
month: {
|
|
106
|
+
month: "long",
|
|
107
|
+
year: "numeric"
|
|
108
|
+
},
|
|
109
|
+
time: { timeStyle: "short" },
|
|
110
|
+
"datetime-local": {
|
|
111
|
+
dateStyle: "medium",
|
|
112
|
+
timeStyle: "short"
|
|
113
|
+
}
|
|
114
|
+
};
|
|
115
|
+
/** A value, as it's written there. */
|
|
116
|
+
function formatValue(value, type, locale, options) {
|
|
117
|
+
if (!value) return "";
|
|
118
|
+
const formatter = new Intl.DateTimeFormat(locale, {
|
|
119
|
+
...options ?? (type === "week" ? {
|
|
120
|
+
day: "numeric",
|
|
121
|
+
month: "long",
|
|
122
|
+
year: "numeric"
|
|
123
|
+
} : FORMATS[type]),
|
|
124
|
+
timeZone: "UTC"
|
|
125
|
+
});
|
|
126
|
+
const day = dayOf(value, type);
|
|
127
|
+
if (type === "week") {
|
|
128
|
+
const monday = parseDay(day);
|
|
129
|
+
const sunday = parseDay(addDays(day, 6));
|
|
130
|
+
return monday && sunday ? formatter.formatRange(monday, sunday) : value;
|
|
131
|
+
}
|
|
132
|
+
const date = parseDay(day || "2000-01-01");
|
|
133
|
+
if (!date || !day && type !== "time") return value;
|
|
134
|
+
const [hours, minutes] = timeOf(value, type).split(":").map(Number);
|
|
135
|
+
date.setUTCHours(hours || 0, minutes || 0);
|
|
136
|
+
return formatter.format(date);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
export { addDays, addMonths, dayOf, firstDayOfWeek, formatDay, formatValue, isDayOutside, isOutside, localFirstDay, monthGrid, monthOf, parseDay, timeOf, toValue, today, usesTwelveHours, weekOf };
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
let react_jsx_runtime = require("react/jsx-runtime");
|
|
2
|
+
|
|
3
|
+
// src/PillForm/glyphs.tsx
|
|
4
|
+
const Glyph = ({ children, width = 2.4 }) => /* @__PURE__ */ (0, react_jsx_runtime.jsx)("svg", {
|
|
5
|
+
width: "1em",
|
|
6
|
+
height: "1em",
|
|
7
|
+
viewBox: "0 0 24 24",
|
|
8
|
+
fill: "none",
|
|
9
|
+
stroke: "currentColor",
|
|
10
|
+
strokeWidth: width,
|
|
11
|
+
strokeLinecap: "round",
|
|
12
|
+
strokeLinejoin: "round",
|
|
13
|
+
"aria-hidden": "true",
|
|
14
|
+
focusable: "false",
|
|
15
|
+
children
|
|
16
|
+
});
|
|
17
|
+
const AlertGlyph = () => /* @__PURE__ */ (0, react_jsx_runtime.jsx)(Glyph, {
|
|
18
|
+
width: 3,
|
|
19
|
+
children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)("path", { d: "M12 6v7M12 17.6v.01" })
|
|
20
|
+
});
|
|
21
|
+
const CalendarGlyph = () => /* @__PURE__ */ (0, react_jsx_runtime.jsxs)(Glyph, { children: [/* @__PURE__ */ (0, react_jsx_runtime.jsx)("rect", {
|
|
22
|
+
x: "3.5",
|
|
23
|
+
y: "5.5",
|
|
24
|
+
width: "17",
|
|
25
|
+
height: "15",
|
|
26
|
+
rx: "3"
|
|
27
|
+
}), /* @__PURE__ */ (0, react_jsx_runtime.jsx)("path", { d: "M3.5 10.5h17M8.5 3v4M15.5 3v4" })] });
|
|
28
|
+
const ChevronGlyph = () => /* @__PURE__ */ (0, react_jsx_runtime.jsx)(Glyph, { children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)("path", { d: "m6 9 6 6 6-6" }) });
|
|
29
|
+
const ClockGlyph = () => /* @__PURE__ */ (0, react_jsx_runtime.jsxs)(Glyph, { children: [/* @__PURE__ */ (0, react_jsx_runtime.jsx)("circle", {
|
|
30
|
+
cx: "12",
|
|
31
|
+
cy: "12",
|
|
32
|
+
r: "8.5"
|
|
33
|
+
}), /* @__PURE__ */ (0, react_jsx_runtime.jsx)("path", { d: "M12 7.5V12l3 2" })] });
|
|
34
|
+
const EyeGlyph = ({ crossed }) => /* @__PURE__ */ (0, react_jsx_runtime.jsxs)(Glyph, { children: [
|
|
35
|
+
/* @__PURE__ */ (0, react_jsx_runtime.jsx)("path", { d: "M2 12s3.6-6.5 10-6.5S22 12 22 12s-3.6 6.5-10 6.5S2 12 2 12Z" }),
|
|
36
|
+
/* @__PURE__ */ (0, react_jsx_runtime.jsx)("circle", {
|
|
37
|
+
cx: "12",
|
|
38
|
+
cy: "12",
|
|
39
|
+
r: "2.6"
|
|
40
|
+
}),
|
|
41
|
+
crossed && /* @__PURE__ */ (0, react_jsx_runtime.jsx)("path", { d: "M4 20 20 4" })
|
|
42
|
+
] });
|
|
43
|
+
const SearchGlyph = () => /* @__PURE__ */ (0, react_jsx_runtime.jsxs)(Glyph, { children: [/* @__PURE__ */ (0, react_jsx_runtime.jsx)("circle", {
|
|
44
|
+
cx: "11",
|
|
45
|
+
cy: "11",
|
|
46
|
+
r: "6.5"
|
|
47
|
+
}), /* @__PURE__ */ (0, react_jsx_runtime.jsx)("path", { d: "m16 16 4.5 4.5" })] });
|
|
48
|
+
const TickGlyph = () => /* @__PURE__ */ (0, react_jsx_runtime.jsx)(Glyph, {
|
|
49
|
+
width: 3.4,
|
|
50
|
+
children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)("path", { d: "m5.5 12.5 4.2 4.2 8.8-9.2" })
|
|
51
|
+
});
|
|
52
|
+
const AddGlyph = () => /* @__PURE__ */ (0, react_jsx_runtime.jsx)(Glyph, { children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)("path", { d: "M5 12h14M12 5v14" }) });
|
|
53
|
+
const MinusGlyph = () => /* @__PURE__ */ (0, react_jsx_runtime.jsx)(Glyph, { children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)("path", { d: "M5 12h14" }) });
|
|
54
|
+
const UploadGlyph = () => /* @__PURE__ */ (0, react_jsx_runtime.jsx)(Glyph, { children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)("path", { d: "M12 16V4.5m0 0L7.5 9M12 4.5 16.5 9M4.5 15.500v2a3 3 0 0 0 3 3h9a3 3 0 0 0 3-3v-2" }) });
|
|
55
|
+
|
|
56
|
+
exports.AddGlyph = AddGlyph;
|
|
57
|
+
exports.AlertGlyph = AlertGlyph;
|
|
58
|
+
exports.CalendarGlyph = CalendarGlyph;
|
|
59
|
+
exports.ChevronGlyph = ChevronGlyph;
|
|
60
|
+
exports.ClockGlyph = ClockGlyph;
|
|
61
|
+
exports.EyeGlyph = EyeGlyph;
|
|
62
|
+
exports.MinusGlyph = MinusGlyph;
|
|
63
|
+
exports.SearchGlyph = SearchGlyph;
|
|
64
|
+
exports.TickGlyph = TickGlyph;
|
|
65
|
+
exports.UploadGlyph = UploadGlyph;
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
2
|
+
|
|
3
|
+
// src/PillForm/glyphs.tsx
|
|
4
|
+
const Glyph = ({ children, width = 2.4 }) => /* @__PURE__ */ jsx("svg", {
|
|
5
|
+
width: "1em",
|
|
6
|
+
height: "1em",
|
|
7
|
+
viewBox: "0 0 24 24",
|
|
8
|
+
fill: "none",
|
|
9
|
+
stroke: "currentColor",
|
|
10
|
+
strokeWidth: width,
|
|
11
|
+
strokeLinecap: "round",
|
|
12
|
+
strokeLinejoin: "round",
|
|
13
|
+
"aria-hidden": "true",
|
|
14
|
+
focusable: "false",
|
|
15
|
+
children
|
|
16
|
+
});
|
|
17
|
+
const AlertGlyph = () => /* @__PURE__ */ jsx(Glyph, {
|
|
18
|
+
width: 3,
|
|
19
|
+
children: /* @__PURE__ */ jsx("path", { d: "M12 6v7M12 17.6v.01" })
|
|
20
|
+
});
|
|
21
|
+
const CalendarGlyph = () => /* @__PURE__ */ jsxs(Glyph, { children: [/* @__PURE__ */ jsx("rect", {
|
|
22
|
+
x: "3.5",
|
|
23
|
+
y: "5.5",
|
|
24
|
+
width: "17",
|
|
25
|
+
height: "15",
|
|
26
|
+
rx: "3"
|
|
27
|
+
}), /* @__PURE__ */ jsx("path", { d: "M3.5 10.5h17M8.5 3v4M15.5 3v4" })] });
|
|
28
|
+
const ChevronGlyph = () => /* @__PURE__ */ jsx(Glyph, { children: /* @__PURE__ */ jsx("path", { d: "m6 9 6 6 6-6" }) });
|
|
29
|
+
const ClockGlyph = () => /* @__PURE__ */ jsxs(Glyph, { children: [/* @__PURE__ */ jsx("circle", {
|
|
30
|
+
cx: "12",
|
|
31
|
+
cy: "12",
|
|
32
|
+
r: "8.5"
|
|
33
|
+
}), /* @__PURE__ */ jsx("path", { d: "M12 7.5V12l3 2" })] });
|
|
34
|
+
const EyeGlyph = ({ crossed }) => /* @__PURE__ */ jsxs(Glyph, { children: [
|
|
35
|
+
/* @__PURE__ */ jsx("path", { d: "M2 12s3.6-6.5 10-6.5S22 12 22 12s-3.6 6.5-10 6.5S2 12 2 12Z" }),
|
|
36
|
+
/* @__PURE__ */ jsx("circle", {
|
|
37
|
+
cx: "12",
|
|
38
|
+
cy: "12",
|
|
39
|
+
r: "2.6"
|
|
40
|
+
}),
|
|
41
|
+
crossed && /* @__PURE__ */ jsx("path", { d: "M4 20 20 4" })
|
|
42
|
+
] });
|
|
43
|
+
const SearchGlyph = () => /* @__PURE__ */ jsxs(Glyph, { children: [/* @__PURE__ */ jsx("circle", {
|
|
44
|
+
cx: "11",
|
|
45
|
+
cy: "11",
|
|
46
|
+
r: "6.5"
|
|
47
|
+
}), /* @__PURE__ */ jsx("path", { d: "m16 16 4.5 4.5" })] });
|
|
48
|
+
const TickGlyph = () => /* @__PURE__ */ jsx(Glyph, {
|
|
49
|
+
width: 3.4,
|
|
50
|
+
children: /* @__PURE__ */ jsx("path", { d: "m5.5 12.5 4.2 4.2 8.8-9.2" })
|
|
51
|
+
});
|
|
52
|
+
const AddGlyph = () => /* @__PURE__ */ jsx(Glyph, { children: /* @__PURE__ */ jsx("path", { d: "M5 12h14M12 5v14" }) });
|
|
53
|
+
const MinusGlyph = () => /* @__PURE__ */ jsx(Glyph, { children: /* @__PURE__ */ jsx("path", { d: "M5 12h14" }) });
|
|
54
|
+
const UploadGlyph = () => /* @__PURE__ */ jsx(Glyph, { children: /* @__PURE__ */ jsx("path", { d: "M12 16V4.5m0 0L7.5 9M12 4.5 16.5 9M4.5 15.500v2a3 3 0 0 0 3 3h9a3 3 0 0 0 3-3v-2" }) });
|
|
55
|
+
|
|
56
|
+
export { AddGlyph, AlertGlyph, CalendarGlyph, ChevronGlyph, ClockGlyph, EyeGlyph, MinusGlyph, SearchGlyph, TickGlyph, UploadGlyph };
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
let react = require("react");
|
|
4
|
+
|
|
5
|
+
// src/PillForm/keyboard.ts
|
|
6
|
+
/** Smaller than any keyboard: a toolbar that came back, a zoomed page… */
|
|
7
|
+
const MIN_HEIGHT = 120;
|
|
8
|
+
const opensKeyboard = (element) => !!element?.matches("input:not([type=checkbox], [type=radio], [type=range], [type=color], [type=file]), textarea");
|
|
9
|
+
function useKeyboard(pillRef) {
|
|
10
|
+
(0, react.useEffect)(() => {
|
|
11
|
+
const pill = pillRef.current;
|
|
12
|
+
const viewport = window.visualViewport;
|
|
13
|
+
if (!pill || !viewport) return;
|
|
14
|
+
const update = () => {
|
|
15
|
+
const isTyping = pill.contains(document.activeElement) && opensKeyboard(document.activeElement);
|
|
16
|
+
const covered = window.innerHeight - viewport.height - viewport.offsetTop;
|
|
17
|
+
if (isTyping && viewport.scale === 1 && covered > MIN_HEIGHT) pill.style.setProperty("--_mp-keyboard", `${Math.round(covered)}px`);
|
|
18
|
+
else pill.style.removeProperty("--_mp-keyboard");
|
|
19
|
+
};
|
|
20
|
+
viewport.addEventListener("resize", update);
|
|
21
|
+
viewport.addEventListener("scroll", update);
|
|
22
|
+
pill.addEventListener("focusin", update);
|
|
23
|
+
pill.addEventListener("focusout", update);
|
|
24
|
+
return () => {
|
|
25
|
+
viewport.removeEventListener("resize", update);
|
|
26
|
+
viewport.removeEventListener("scroll", update);
|
|
27
|
+
pill.removeEventListener("focusin", update);
|
|
28
|
+
pill.removeEventListener("focusout", update);
|
|
29
|
+
pill.style.removeProperty("--_mp-keyboard");
|
|
30
|
+
};
|
|
31
|
+
}, [pillRef]);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
exports.useKeyboard = useKeyboard;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import { useEffect } from "react";
|
|
4
|
+
|
|
5
|
+
// src/PillForm/keyboard.ts
|
|
6
|
+
/** Smaller than any keyboard: a toolbar that came back, a zoomed page… */
|
|
7
|
+
const MIN_HEIGHT = 120;
|
|
8
|
+
const opensKeyboard = (element) => !!element?.matches("input:not([type=checkbox], [type=radio], [type=range], [type=color], [type=file]), textarea");
|
|
9
|
+
function useKeyboard(pillRef) {
|
|
10
|
+
useEffect(() => {
|
|
11
|
+
const pill = pillRef.current;
|
|
12
|
+
const viewport = window.visualViewport;
|
|
13
|
+
if (!pill || !viewport) return;
|
|
14
|
+
const update = () => {
|
|
15
|
+
const isTyping = pill.contains(document.activeElement) && opensKeyboard(document.activeElement);
|
|
16
|
+
const covered = window.innerHeight - viewport.height - viewport.offsetTop;
|
|
17
|
+
if (isTyping && viewport.scale === 1 && covered > MIN_HEIGHT) pill.style.setProperty("--_mp-keyboard", `${Math.round(covered)}px`);
|
|
18
|
+
else pill.style.removeProperty("--_mp-keyboard");
|
|
19
|
+
};
|
|
20
|
+
viewport.addEventListener("resize", update);
|
|
21
|
+
viewport.addEventListener("scroll", update);
|
|
22
|
+
pill.addEventListener("focusin", update);
|
|
23
|
+
pill.addEventListener("focusout", update);
|
|
24
|
+
return () => {
|
|
25
|
+
viewport.removeEventListener("resize", update);
|
|
26
|
+
viewport.removeEventListener("scroll", update);
|
|
27
|
+
pill.removeEventListener("focusin", update);
|
|
28
|
+
pill.removeEventListener("focusout", update);
|
|
29
|
+
pill.style.removeProperty("--_mp-keyboard");
|
|
30
|
+
};
|
|
31
|
+
}, [pillRef]);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export { useKeyboard };
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
let react = require("react");
|
|
4
|
+
|
|
5
|
+
// src/PillForm/popover.ts
|
|
6
|
+
/** From the field, and from the screen's edges */
|
|
7
|
+
const GAP = 6;
|
|
8
|
+
const MARGIN = 8;
|
|
9
|
+
const supportsPopover = () => typeof HTMLElement !== "undefined" && "showPopover" in HTMLElement.prototype;
|
|
10
|
+
/** Where it goes: under the field, or above it when there's more room there. Pure, so it can be tested. */
|
|
11
|
+
function placePopover(field, popover, viewport, isRtl) {
|
|
12
|
+
const below = viewport.height - field.bottom - GAP - MARGIN;
|
|
13
|
+
const over = field.top - GAP - MARGIN;
|
|
14
|
+
const above = popover.height > below && over > below;
|
|
15
|
+
const maxHeight = Math.max(0, above ? over : below);
|
|
16
|
+
const height = Math.min(popover.height, maxHeight);
|
|
17
|
+
const wanted = isRtl ? field.right - popover.width : field.left;
|
|
18
|
+
return {
|
|
19
|
+
left: Math.max(MARGIN, Math.min(wanted, viewport.width - popover.width - MARGIN)),
|
|
20
|
+
top: above ? field.top - GAP - height : field.bottom + GAP,
|
|
21
|
+
maxHeight,
|
|
22
|
+
above
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Shows the popover while it's open, next to its field, and follows it. A press
|
|
27
|
+
* elsewhere closes it. Without popovers, it's shown in the page, under the field.
|
|
28
|
+
*/
|
|
29
|
+
function usePopover(popoverRef, fieldRef, isOpen, onClose) {
|
|
30
|
+
(0, react.useLayoutEffect)(() => {
|
|
31
|
+
const popover = popoverRef.current;
|
|
32
|
+
const field = fieldRef.current;
|
|
33
|
+
if (!isOpen || !popover || !field || !supportsPopover()) return;
|
|
34
|
+
popover.showPopover();
|
|
35
|
+
const place = () => {
|
|
36
|
+
popover.style.maxHeight = "";
|
|
37
|
+
const { left, top, maxHeight, above } = placePopover(field.getBoundingClientRect(), {
|
|
38
|
+
width: popover.offsetWidth,
|
|
39
|
+
height: popover.scrollHeight
|
|
40
|
+
}, {
|
|
41
|
+
width: document.documentElement.clientWidth,
|
|
42
|
+
height: window.innerHeight
|
|
43
|
+
}, getComputedStyle(field).direction === "rtl");
|
|
44
|
+
Object.assign(popover.style, {
|
|
45
|
+
left: `${left}px`,
|
|
46
|
+
top: `${top}px`,
|
|
47
|
+
maxHeight: `${maxHeight}px`
|
|
48
|
+
});
|
|
49
|
+
popover.toggleAttribute("data-above", above);
|
|
50
|
+
};
|
|
51
|
+
place();
|
|
52
|
+
window.addEventListener("resize", place);
|
|
53
|
+
window.addEventListener("scroll", place, {
|
|
54
|
+
capture: true,
|
|
55
|
+
passive: true
|
|
56
|
+
});
|
|
57
|
+
const observer = typeof ResizeObserver === "undefined" ? void 0 : new ResizeObserver(place);
|
|
58
|
+
observer?.observe(field);
|
|
59
|
+
return () => {
|
|
60
|
+
window.removeEventListener("resize", place);
|
|
61
|
+
window.removeEventListener("scroll", place, { capture: true });
|
|
62
|
+
observer?.disconnect();
|
|
63
|
+
try {
|
|
64
|
+
popover.hidePopover();
|
|
65
|
+
} catch {}
|
|
66
|
+
};
|
|
67
|
+
}, [
|
|
68
|
+
popoverRef,
|
|
69
|
+
fieldRef,
|
|
70
|
+
isOpen
|
|
71
|
+
]);
|
|
72
|
+
(0, react.useEffect)(() => {
|
|
73
|
+
if (!isOpen) return;
|
|
74
|
+
const onPress = (event) => {
|
|
75
|
+
const target = event.target;
|
|
76
|
+
if (!popoverRef.current?.contains(target) && !fieldRef.current?.contains(target)) onClose();
|
|
77
|
+
};
|
|
78
|
+
document.addEventListener("pointerdown", onPress, true);
|
|
79
|
+
return () => document.removeEventListener("pointerdown", onPress, true);
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
exports.placePopover = placePopover;
|
|
84
|
+
exports.usePopover = usePopover;
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import { useEffect, useLayoutEffect } from "react";
|
|
4
|
+
|
|
5
|
+
// src/PillForm/popover.ts
|
|
6
|
+
/** From the field, and from the screen's edges */
|
|
7
|
+
const GAP = 6;
|
|
8
|
+
const MARGIN = 8;
|
|
9
|
+
const supportsPopover = () => typeof HTMLElement !== "undefined" && "showPopover" in HTMLElement.prototype;
|
|
10
|
+
/** Where it goes: under the field, or above it when there's more room there. Pure, so it can be tested. */
|
|
11
|
+
function placePopover(field, popover, viewport, isRtl) {
|
|
12
|
+
const below = viewport.height - field.bottom - GAP - MARGIN;
|
|
13
|
+
const over = field.top - GAP - MARGIN;
|
|
14
|
+
const above = popover.height > below && over > below;
|
|
15
|
+
const maxHeight = Math.max(0, above ? over : below);
|
|
16
|
+
const height = Math.min(popover.height, maxHeight);
|
|
17
|
+
const wanted = isRtl ? field.right - popover.width : field.left;
|
|
18
|
+
return {
|
|
19
|
+
left: Math.max(MARGIN, Math.min(wanted, viewport.width - popover.width - MARGIN)),
|
|
20
|
+
top: above ? field.top - GAP - height : field.bottom + GAP,
|
|
21
|
+
maxHeight,
|
|
22
|
+
above
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Shows the popover while it's open, next to its field, and follows it. A press
|
|
27
|
+
* elsewhere closes it. Without popovers, it's shown in the page, under the field.
|
|
28
|
+
*/
|
|
29
|
+
function usePopover(popoverRef, fieldRef, isOpen, onClose) {
|
|
30
|
+
useLayoutEffect(() => {
|
|
31
|
+
const popover = popoverRef.current;
|
|
32
|
+
const field = fieldRef.current;
|
|
33
|
+
if (!isOpen || !popover || !field || !supportsPopover()) return;
|
|
34
|
+
popover.showPopover();
|
|
35
|
+
const place = () => {
|
|
36
|
+
popover.style.maxHeight = "";
|
|
37
|
+
const { left, top, maxHeight, above } = placePopover(field.getBoundingClientRect(), {
|
|
38
|
+
width: popover.offsetWidth,
|
|
39
|
+
height: popover.scrollHeight
|
|
40
|
+
}, {
|
|
41
|
+
width: document.documentElement.clientWidth,
|
|
42
|
+
height: window.innerHeight
|
|
43
|
+
}, getComputedStyle(field).direction === "rtl");
|
|
44
|
+
Object.assign(popover.style, {
|
|
45
|
+
left: `${left}px`,
|
|
46
|
+
top: `${top}px`,
|
|
47
|
+
maxHeight: `${maxHeight}px`
|
|
48
|
+
});
|
|
49
|
+
popover.toggleAttribute("data-above", above);
|
|
50
|
+
};
|
|
51
|
+
place();
|
|
52
|
+
window.addEventListener("resize", place);
|
|
53
|
+
window.addEventListener("scroll", place, {
|
|
54
|
+
capture: true,
|
|
55
|
+
passive: true
|
|
56
|
+
});
|
|
57
|
+
const observer = typeof ResizeObserver === "undefined" ? void 0 : new ResizeObserver(place);
|
|
58
|
+
observer?.observe(field);
|
|
59
|
+
return () => {
|
|
60
|
+
window.removeEventListener("resize", place);
|
|
61
|
+
window.removeEventListener("scroll", place, { capture: true });
|
|
62
|
+
observer?.disconnect();
|
|
63
|
+
try {
|
|
64
|
+
popover.hidePopover();
|
|
65
|
+
} catch {}
|
|
66
|
+
};
|
|
67
|
+
}, [
|
|
68
|
+
popoverRef,
|
|
69
|
+
fieldRef,
|
|
70
|
+
isOpen
|
|
71
|
+
]);
|
|
72
|
+
useEffect(() => {
|
|
73
|
+
if (!isOpen) return;
|
|
74
|
+
const onPress = (event) => {
|
|
75
|
+
const target = event.target;
|
|
76
|
+
if (!popoverRef.current?.contains(target) && !fieldRef.current?.contains(target)) onClose();
|
|
77
|
+
};
|
|
78
|
+
document.addEventListener("pointerdown", onPress, true);
|
|
79
|
+
return () => document.removeEventListener("pointerdown", onPress, true);
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export { placePopover, usePopover };
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { FormEvent } from "react";
|
|
2
|
+
/**
|
|
3
|
+
* Called once every field is valid. While the promise it returns is pending,
|
|
4
|
+
* the fields wait. If it's rejected, the error's message is shown.
|
|
5
|
+
*/
|
|
6
|
+
export type SendHandler = (data: FormData, event: FormEvent<HTMLFormElement>) => unknown;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { FormEvent } from "react";
|
|
2
|
+
/**
|
|
3
|
+
* Called once every field is valid. While the promise it returns is pending,
|
|
4
|
+
* the fields wait. If it's rejected, the error's message is shown.
|
|
5
|
+
*/
|
|
6
|
+
export type SendHandler = (data: FormData, event: FormEvent<HTMLFormElement>) => unknown;
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
let react = require("react");
|
|
4
|
+
|
|
5
|
+
// src/PillForm/send.ts
|
|
6
|
+
const messageOf = (error) => error instanceof Error ? error.message : typeof error === "string" ? error : "";
|
|
7
|
+
/**
|
|
8
|
+
* What a form does once it's valid: it sends its data (once at a time), waits,
|
|
9
|
+
* then tells whether it went through. `onSent` isn't called if the handler
|
|
10
|
+
* returned `false`.
|
|
11
|
+
*/
|
|
12
|
+
function useSend(onSend, errorLabel, onSent) {
|
|
13
|
+
const isSending = (0, react.useRef)(false);
|
|
14
|
+
const [isBusy, setIsBusy] = (0, react.useState)(false);
|
|
15
|
+
const [failure, setFailure] = (0, react.useState)("");
|
|
16
|
+
const send = async (event) => {
|
|
17
|
+
event.preventDefault();
|
|
18
|
+
if (isSending.current) return;
|
|
19
|
+
isSending.current = true;
|
|
20
|
+
setFailure("");
|
|
21
|
+
const focused = document.activeElement;
|
|
22
|
+
try {
|
|
23
|
+
const result = onSend?.(new FormData(event.currentTarget), event);
|
|
24
|
+
if (result instanceof Promise) setIsBusy(true);
|
|
25
|
+
if (await result !== false) onSent?.();
|
|
26
|
+
} catch (error) {
|
|
27
|
+
setFailure(messageOf(error) || errorLabel);
|
|
28
|
+
setTimeout(() => {
|
|
29
|
+
if (focused instanceof HTMLElement && document.activeElement === document.body) focused.focus();
|
|
30
|
+
});
|
|
31
|
+
} finally {
|
|
32
|
+
isSending.current = false;
|
|
33
|
+
setIsBusy(false);
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
return {
|
|
37
|
+
send,
|
|
38
|
+
isBusy,
|
|
39
|
+
failure,
|
|
40
|
+
forget: () => setFailure("")
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
exports.useSend = useSend;
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import { useRef, useState } from "react";
|
|
4
|
+
|
|
5
|
+
// src/PillForm/send.ts
|
|
6
|
+
const messageOf = (error) => error instanceof Error ? error.message : typeof error === "string" ? error : "";
|
|
7
|
+
/**
|
|
8
|
+
* What a form does once it's valid: it sends its data (once at a time), waits,
|
|
9
|
+
* then tells whether it went through. `onSent` isn't called if the handler
|
|
10
|
+
* returned `false`.
|
|
11
|
+
*/
|
|
12
|
+
function useSend(onSend, errorLabel, onSent) {
|
|
13
|
+
const isSending = useRef(false);
|
|
14
|
+
const [isBusy, setIsBusy] = useState(false);
|
|
15
|
+
const [failure, setFailure] = useState("");
|
|
16
|
+
const send = async (event) => {
|
|
17
|
+
event.preventDefault();
|
|
18
|
+
if (isSending.current) return;
|
|
19
|
+
isSending.current = true;
|
|
20
|
+
setFailure("");
|
|
21
|
+
const focused = document.activeElement;
|
|
22
|
+
try {
|
|
23
|
+
const result = onSend?.(new FormData(event.currentTarget), event);
|
|
24
|
+
if (result instanceof Promise) setIsBusy(true);
|
|
25
|
+
if (await result !== false) onSent?.();
|
|
26
|
+
} catch (error) {
|
|
27
|
+
setFailure(messageOf(error) || errorLabel);
|
|
28
|
+
setTimeout(() => {
|
|
29
|
+
if (focused instanceof HTMLElement && document.activeElement === document.body) focused.focus();
|
|
30
|
+
});
|
|
31
|
+
} finally {
|
|
32
|
+
isSending.current = false;
|
|
33
|
+
setIsBusy(false);
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
return {
|
|
37
|
+
send,
|
|
38
|
+
isBusy,
|
|
39
|
+
failure,
|
|
40
|
+
forget: () => setFailure("")
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export { useSend };
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
let react = require("react");
|
|
4
|
+
const require_FormContext = require("./FormContext.js");
|
|
5
|
+
|
|
6
|
+
// src/PillForm/validity.ts
|
|
7
|
+
const isControl = (element) => element instanceof HTMLInputElement || element instanceof HTMLTextAreaElement || element instanceof HTMLSelectElement;
|
|
8
|
+
/** Whether the person has changed this control (or tried to submit it) and it's wrong. */
|
|
9
|
+
function isUserInvalid(control) {
|
|
10
|
+
try {
|
|
11
|
+
return control.matches(":user-invalid");
|
|
12
|
+
} catch {
|
|
13
|
+
return false;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
/** Runs after the form this control belongs to has been reset. */
|
|
17
|
+
function useFormReset(controlRef, onReset) {
|
|
18
|
+
(0, react.useEffect)(() => {
|
|
19
|
+
const form = controlRef.current?.form;
|
|
20
|
+
if (!form) return;
|
|
21
|
+
let timer;
|
|
22
|
+
const listener = () => {
|
|
23
|
+
timer = setTimeout(onReset);
|
|
24
|
+
};
|
|
25
|
+
form.addEventListener("reset", listener);
|
|
26
|
+
return () => {
|
|
27
|
+
clearTimeout(timer);
|
|
28
|
+
form.removeEventListener("reset", listener);
|
|
29
|
+
};
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* The browser's own validation, to show in the page rather than in its bubble:
|
|
34
|
+
* on an attempt to submit, or on leaving a field that was changed and is wrong.
|
|
35
|
+
* The message goes as soon as the value is right. The app's own error wins.
|
|
36
|
+
*/
|
|
37
|
+
function useValidity(error, controlRef) {
|
|
38
|
+
const { errors } = (0, react.useContext)(require_FormContext.FormContext);
|
|
39
|
+
const [message, setMessage] = (0, react.useState)("");
|
|
40
|
+
useFormReset(controlRef, () => setMessage(""));
|
|
41
|
+
return {
|
|
42
|
+
error: error || message || void 0,
|
|
43
|
+
handlers: {
|
|
44
|
+
onInvalid: (event) => {
|
|
45
|
+
const control = event.target;
|
|
46
|
+
if (errors === "native" || !isControl(control)) return;
|
|
47
|
+
event.preventDefault();
|
|
48
|
+
setMessage(control.validationMessage);
|
|
49
|
+
if ([...control.form?.elements ?? []].find((element) => isControl(element) && element.willValidate && !element.validity.valid) === control) control.focus();
|
|
50
|
+
},
|
|
51
|
+
onInput: (event) => {
|
|
52
|
+
const control = event.target;
|
|
53
|
+
if (!isControl(control)) return;
|
|
54
|
+
setMessage((shown) => shown && !control.validity.valid ? control.validationMessage : "");
|
|
55
|
+
},
|
|
56
|
+
onBlur: (event) => {
|
|
57
|
+
const control = event.target;
|
|
58
|
+
if (errors === "native" || !isControl(control)) return;
|
|
59
|
+
if (isUserInvalid(control)) setMessage(control.validationMessage);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
/** Both handlers, the app's after ours. */
|
|
65
|
+
function both(ours, theirs) {
|
|
66
|
+
return (event) => {
|
|
67
|
+
ours?.(event);
|
|
68
|
+
theirs?.(event);
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
exports.both = both;
|
|
73
|
+
exports.useFormReset = useFormReset;
|
|
74
|
+
exports.useValidity = useValidity;
|