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,14 @@
|
|
|
1
|
+
import { PillButtonActionProps } from "../Pill/types.mjs";
|
|
2
|
+
export interface PillSubmitProps extends Omit<PillButtonActionProps, "type" | "href"> {
|
|
3
|
+
/**
|
|
4
|
+
* The label while the form is sending, or several, one after the other: the
|
|
5
|
+
* last one stays. Default "Just a moment…".
|
|
6
|
+
*/
|
|
7
|
+
busyLabel?: string | readonly string[];
|
|
8
|
+
/** How long each label stays, in ms. Default 2500. */
|
|
9
|
+
busyInterval?: number;
|
|
10
|
+
/** Default: whether the <PillForm> it's in is sending. */
|
|
11
|
+
busy?: boolean;
|
|
12
|
+
}
|
|
13
|
+
/** The action that sends a form, and says it's doing so. */
|
|
14
|
+
export declare const PillSubmit: import("react").ForwardRefExoticComponent<PillSubmitProps & import("react").RefAttributes<HTMLButtonElement>>;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { PillButtonActionProps } from "../Pill/types.js";
|
|
2
|
+
export interface PillSubmitProps extends Omit<PillButtonActionProps, "type" | "href"> {
|
|
3
|
+
/**
|
|
4
|
+
* The label while the form is sending, or several, one after the other: the
|
|
5
|
+
* last one stays. Default "Just a moment…".
|
|
6
|
+
*/
|
|
7
|
+
busyLabel?: string | readonly string[];
|
|
8
|
+
/** How long each label stays, in ms. Default 2500. */
|
|
9
|
+
busyInterval?: number;
|
|
10
|
+
/** Default: whether the <PillForm> it's in is sending. */
|
|
11
|
+
busy?: boolean;
|
|
12
|
+
}
|
|
13
|
+
/** The action that sends a form, and says it's doing so. */
|
|
14
|
+
export declare const PillSubmit: import("react").ForwardRefExoticComponent<PillSubmitProps & import("react").RefAttributes<HTMLButtonElement>>;
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
let react = require("react");
|
|
4
|
+
const require_FormContext = require("./FormContext.js");
|
|
5
|
+
const require_PillAction = require("../Pill/PillAction.js");
|
|
6
|
+
const require_CheckIcon = require("../icons/CheckIcon.js");
|
|
7
|
+
const require_react = require("../utils/react.js");
|
|
8
|
+
const require_timing = require("../utils/timing.js");
|
|
9
|
+
const require_visuallyHidden = require("../utils/visuallyHidden.js");
|
|
10
|
+
let react_jsx_runtime = require("react/jsx-runtime");
|
|
11
|
+
|
|
12
|
+
// src/PillForm/PillSubmit.tsx
|
|
13
|
+
const BUSY_LABEL = "Just a moment…";
|
|
14
|
+
/** The label to show, moving to the next one every `interval` ms while it's busy. */
|
|
15
|
+
function useBusyLabel(busyLabel, interval, isBusy) {
|
|
16
|
+
const labels = typeof busyLabel === "string" ? [busyLabel] : busyLabel;
|
|
17
|
+
const last = labels.length - 1;
|
|
18
|
+
const [index, setIndex] = (0, react.useState)(0);
|
|
19
|
+
(0, react.useEffect)(() => {
|
|
20
|
+
if (!isBusy) setIndex(0);
|
|
21
|
+
else if (index < last) {
|
|
22
|
+
const timer = setTimeout(() => setIndex(index + 1), interval);
|
|
23
|
+
return () => clearTimeout(timer);
|
|
24
|
+
}
|
|
25
|
+
}, [
|
|
26
|
+
isBusy,
|
|
27
|
+
index,
|
|
28
|
+
last,
|
|
29
|
+
interval
|
|
30
|
+
]);
|
|
31
|
+
return isBusy ? labels[Math.min(index, last)] : void 0;
|
|
32
|
+
}
|
|
33
|
+
/** One label replacing another: the action widens or narrows with it. */
|
|
34
|
+
function useLabelMorph(buttonRef, label) {
|
|
35
|
+
const previous = (0, react.useRef)(null);
|
|
36
|
+
(0, react.useLayoutEffect)(() => {
|
|
37
|
+
const text = buttonRef.current?.querySelector("[data-mp=\"label\"] > span");
|
|
38
|
+
if (!text) return;
|
|
39
|
+
text.getAnimations?.().forEach((animation) => animation.cancel());
|
|
40
|
+
const width = text.offsetWidth;
|
|
41
|
+
const from = previous.current;
|
|
42
|
+
previous.current = {
|
|
43
|
+
label,
|
|
44
|
+
width
|
|
45
|
+
};
|
|
46
|
+
if (!from || from.label === label || !from.width || !text.animate || require_timing.prefersReducedMotion()) return;
|
|
47
|
+
text.animate([{
|
|
48
|
+
width: `${from.width}px`,
|
|
49
|
+
opacity: 0,
|
|
50
|
+
transform: "translateY(40%)"
|
|
51
|
+
}, {
|
|
52
|
+
width: `${width}px`,
|
|
53
|
+
opacity: 1,
|
|
54
|
+
transform: "none"
|
|
55
|
+
}], {
|
|
56
|
+
duration: 250,
|
|
57
|
+
easing: "ease-out"
|
|
58
|
+
});
|
|
59
|
+
}, [buttonRef, label]);
|
|
60
|
+
}
|
|
61
|
+
/** The action that sends a form, and says it's doing so. */
|
|
62
|
+
const PillSubmit = (0, react.forwardRef)(function PillSubmit({ label, icon = /* @__PURE__ */ (0, react_jsx_runtime.jsx)(require_CheckIcon, {}), busyLabel = BUSY_LABEL, busyInterval = 2500, busy, onClick, ...rest }, forwardedRef) {
|
|
63
|
+
const form = (0, react.useContext)(require_FormContext.FormContext);
|
|
64
|
+
const isBusy = busy ?? form.isBusy ?? false;
|
|
65
|
+
const buttonRef = (0, react.useRef)(null);
|
|
66
|
+
const ref = (0, react.useMemo)(() => require_react.mergeRefs(buttonRef, forwardedRef), [forwardedRef]);
|
|
67
|
+
const current = useBusyLabel(busyLabel, busyInterval, isBusy);
|
|
68
|
+
const shown = current ?? label;
|
|
69
|
+
useLabelMorph(buttonRef, shown);
|
|
70
|
+
return /* @__PURE__ */ (0, react_jsx_runtime.jsxs)(react_jsx_runtime.Fragment, { children: [/* @__PURE__ */ (0, react_jsx_runtime.jsx)(require_PillAction, {
|
|
71
|
+
...rest,
|
|
72
|
+
ref,
|
|
73
|
+
type: "submit",
|
|
74
|
+
icon,
|
|
75
|
+
label: shown,
|
|
76
|
+
expanded: rest.expanded || isBusy,
|
|
77
|
+
"aria-disabled": isBusy || rest["aria-disabled"] || void 0,
|
|
78
|
+
onClick: (event) => {
|
|
79
|
+
if (isBusy) event.preventDefault();
|
|
80
|
+
else onClick?.(event);
|
|
81
|
+
}
|
|
82
|
+
}), /* @__PURE__ */ (0, react_jsx_runtime.jsx)("span", {
|
|
83
|
+
role: "status",
|
|
84
|
+
style: require_visuallyHidden.VISUALLY_HIDDEN,
|
|
85
|
+
children: current
|
|
86
|
+
})] });
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
exports.BUSY_LABEL = BUSY_LABEL;
|
|
90
|
+
exports.PillSubmit = PillSubmit;
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import { forwardRef, useContext, useEffect, useLayoutEffect, useMemo, useRef, useState } from "react";
|
|
4
|
+
import { FormContext } from "./FormContext.mjs";
|
|
5
|
+
import PillAction from "../Pill/PillAction.mjs";
|
|
6
|
+
import CheckIcon from "../icons/CheckIcon.mjs";
|
|
7
|
+
import { mergeRefs } from "../utils/react.mjs";
|
|
8
|
+
import { prefersReducedMotion } from "../utils/timing.mjs";
|
|
9
|
+
import { VISUALLY_HIDDEN } from "../utils/visuallyHidden.mjs";
|
|
10
|
+
import { Fragment as Fragment$1, jsx, jsxs } from "react/jsx-runtime";
|
|
11
|
+
|
|
12
|
+
// src/PillForm/PillSubmit.tsx
|
|
13
|
+
const BUSY_LABEL = "Just a moment…";
|
|
14
|
+
/** The label to show, moving to the next one every `interval` ms while it's busy. */
|
|
15
|
+
function useBusyLabel(busyLabel, interval, isBusy) {
|
|
16
|
+
const labels = typeof busyLabel === "string" ? [busyLabel] : busyLabel;
|
|
17
|
+
const last = labels.length - 1;
|
|
18
|
+
const [index, setIndex] = useState(0);
|
|
19
|
+
useEffect(() => {
|
|
20
|
+
if (!isBusy) setIndex(0);
|
|
21
|
+
else if (index < last) {
|
|
22
|
+
const timer = setTimeout(() => setIndex(index + 1), interval);
|
|
23
|
+
return () => clearTimeout(timer);
|
|
24
|
+
}
|
|
25
|
+
}, [
|
|
26
|
+
isBusy,
|
|
27
|
+
index,
|
|
28
|
+
last,
|
|
29
|
+
interval
|
|
30
|
+
]);
|
|
31
|
+
return isBusy ? labels[Math.min(index, last)] : void 0;
|
|
32
|
+
}
|
|
33
|
+
/** One label replacing another: the action widens or narrows with it. */
|
|
34
|
+
function useLabelMorph(buttonRef, label) {
|
|
35
|
+
const previous = useRef(null);
|
|
36
|
+
useLayoutEffect(() => {
|
|
37
|
+
const text = buttonRef.current?.querySelector("[data-mp=\"label\"] > span");
|
|
38
|
+
if (!text) return;
|
|
39
|
+
text.getAnimations?.().forEach((animation) => animation.cancel());
|
|
40
|
+
const width = text.offsetWidth;
|
|
41
|
+
const from = previous.current;
|
|
42
|
+
previous.current = {
|
|
43
|
+
label,
|
|
44
|
+
width
|
|
45
|
+
};
|
|
46
|
+
if (!from || from.label === label || !from.width || !text.animate || prefersReducedMotion()) return;
|
|
47
|
+
text.animate([{
|
|
48
|
+
width: `${from.width}px`,
|
|
49
|
+
opacity: 0,
|
|
50
|
+
transform: "translateY(40%)"
|
|
51
|
+
}, {
|
|
52
|
+
width: `${width}px`,
|
|
53
|
+
opacity: 1,
|
|
54
|
+
transform: "none"
|
|
55
|
+
}], {
|
|
56
|
+
duration: 250,
|
|
57
|
+
easing: "ease-out"
|
|
58
|
+
});
|
|
59
|
+
}, [buttonRef, label]);
|
|
60
|
+
}
|
|
61
|
+
/** The action that sends a form, and says it's doing so. */
|
|
62
|
+
const PillSubmit = forwardRef(function PillSubmit({ label, icon = /* @__PURE__ */ jsx(CheckIcon, {}), busyLabel = BUSY_LABEL, busyInterval = 2500, busy, onClick, ...rest }, forwardedRef) {
|
|
63
|
+
const form = useContext(FormContext);
|
|
64
|
+
const isBusy = busy ?? form.isBusy ?? false;
|
|
65
|
+
const buttonRef = useRef(null);
|
|
66
|
+
const ref = useMemo(() => mergeRefs(buttonRef, forwardedRef), [forwardedRef]);
|
|
67
|
+
const current = useBusyLabel(busyLabel, busyInterval, isBusy);
|
|
68
|
+
const shown = current ?? label;
|
|
69
|
+
useLabelMorph(buttonRef, shown);
|
|
70
|
+
return /* @__PURE__ */ jsxs(Fragment$1, { children: [/* @__PURE__ */ jsx(PillAction, {
|
|
71
|
+
...rest,
|
|
72
|
+
ref,
|
|
73
|
+
type: "submit",
|
|
74
|
+
icon,
|
|
75
|
+
label: shown,
|
|
76
|
+
expanded: rest.expanded || isBusy,
|
|
77
|
+
"aria-disabled": isBusy || rest["aria-disabled"] || void 0,
|
|
78
|
+
onClick: (event) => {
|
|
79
|
+
if (isBusy) event.preventDefault();
|
|
80
|
+
else onClick?.(event);
|
|
81
|
+
}
|
|
82
|
+
}), /* @__PURE__ */ jsx("span", {
|
|
83
|
+
role: "status",
|
|
84
|
+
style: VISUALLY_HIDDEN,
|
|
85
|
+
children: current
|
|
86
|
+
})] });
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
export { BUSY_LABEL, PillSubmit };
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { ComponentPropsWithoutRef } from "react";
|
|
2
|
+
import { FieldProps } from "./PillField.mjs";
|
|
3
|
+
export interface PillSwitchProps extends Omit<FieldProps, "hideLabel" | "optional">, Omit<ComponentPropsWithoutRef<"input">, "type" | "children"> {}
|
|
4
|
+
/** On or off, at once: a native checkbox with the switch role. */
|
|
5
|
+
export declare const PillSwitch: import("react").ForwardRefExoticComponent<PillSwitchProps & import("react").RefAttributes<HTMLInputElement>>;
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { ComponentPropsWithoutRef } from "react";
|
|
2
|
+
import { FieldProps } from "./PillField.js";
|
|
3
|
+
export interface PillSwitchProps extends Omit<FieldProps, "hideLabel" | "optional">, Omit<ComponentPropsWithoutRef<"input">, "type" | "children"> {}
|
|
4
|
+
/** On or off, at once: a native checkbox with the switch role. */
|
|
5
|
+
export declare const PillSwitch: import("react").ForwardRefExoticComponent<PillSwitchProps & import("react").RefAttributes<HTMLInputElement>>;
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
let react = require("react");
|
|
4
|
+
const require_PillField = require("./PillField.js");
|
|
5
|
+
const require_validity = require("./validity.js");
|
|
6
|
+
const require_react = require("../utils/react.js");
|
|
7
|
+
let react_jsx_runtime = require("react/jsx-runtime");
|
|
8
|
+
|
|
9
|
+
// src/PillForm/PillSwitch.tsx
|
|
10
|
+
/** On or off, at once: a native checkbox with the switch role. */
|
|
11
|
+
const PillSwitch = (0, react.forwardRef)(function PillSwitch({ label, hint, error, className, onInput, onInvalid, ...rest }, forwardedRef) {
|
|
12
|
+
const inputRef = (0, react.useRef)(null);
|
|
13
|
+
const ref = (0, react.useMemo)(() => require_react.mergeRefs(inputRef, forwardedRef), [forwardedRef]);
|
|
14
|
+
const validity = require_validity.useValidity(error, inputRef);
|
|
15
|
+
return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(require_PillField.FieldShell, {
|
|
16
|
+
label,
|
|
17
|
+
hint,
|
|
18
|
+
className,
|
|
19
|
+
error: validity.error,
|
|
20
|
+
layout: "inline",
|
|
21
|
+
"data-kind": "switch",
|
|
22
|
+
children: (control) => /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("span", {
|
|
23
|
+
"data-mp": "field-control",
|
|
24
|
+
"data-type": "switch",
|
|
25
|
+
children: [/* @__PURE__ */ (0, react_jsx_runtime.jsx)("input", {
|
|
26
|
+
...rest,
|
|
27
|
+
...control,
|
|
28
|
+
ref,
|
|
29
|
+
type: "checkbox",
|
|
30
|
+
role: "switch",
|
|
31
|
+
onInput: require_validity.both(validity.handlers.onInput, onInput),
|
|
32
|
+
onInvalid: require_validity.both(validity.handlers.onInvalid, onInvalid)
|
|
33
|
+
}), /* @__PURE__ */ (0, react_jsx_runtime.jsx)("span", { "aria-hidden": "true" })]
|
|
34
|
+
})
|
|
35
|
+
});
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
exports.PillSwitch = PillSwitch;
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import { forwardRef, useMemo, useRef } from "react";
|
|
4
|
+
import { FieldShell } from "./PillField.mjs";
|
|
5
|
+
import { both, useValidity } from "./validity.mjs";
|
|
6
|
+
import { mergeRefs } from "../utils/react.mjs";
|
|
7
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
8
|
+
|
|
9
|
+
// src/PillForm/PillSwitch.tsx
|
|
10
|
+
/** On or off, at once: a native checkbox with the switch role. */
|
|
11
|
+
const PillSwitch = forwardRef(function PillSwitch({ label, hint, error, className, onInput, onInvalid, ...rest }, forwardedRef) {
|
|
12
|
+
const inputRef = useRef(null);
|
|
13
|
+
const ref = useMemo(() => mergeRefs(inputRef, forwardedRef), [forwardedRef]);
|
|
14
|
+
const validity = useValidity(error, inputRef);
|
|
15
|
+
return /* @__PURE__ */ jsx(FieldShell, {
|
|
16
|
+
label,
|
|
17
|
+
hint,
|
|
18
|
+
className,
|
|
19
|
+
error: validity.error,
|
|
20
|
+
layout: "inline",
|
|
21
|
+
"data-kind": "switch",
|
|
22
|
+
children: (control) => /* @__PURE__ */ jsxs("span", {
|
|
23
|
+
"data-mp": "field-control",
|
|
24
|
+
"data-type": "switch",
|
|
25
|
+
children: [/* @__PURE__ */ jsx("input", {
|
|
26
|
+
...rest,
|
|
27
|
+
...control,
|
|
28
|
+
ref,
|
|
29
|
+
type: "checkbox",
|
|
30
|
+
role: "switch",
|
|
31
|
+
onInput: both(validity.handlers.onInput, onInput),
|
|
32
|
+
onInvalid: both(validity.handlers.onInvalid, onInvalid)
|
|
33
|
+
}), /* @__PURE__ */ jsx("span", { "aria-hidden": "true" })]
|
|
34
|
+
})
|
|
35
|
+
});
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
export { PillSwitch };
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { ComponentPropsWithoutRef } from "react";
|
|
2
|
+
import { FieldProps } from "./PillField.mjs";
|
|
3
|
+
export interface PillTextareaProps extends FieldProps, Omit<ComponentPropsWithoutRef<"textarea">, "children"> {}
|
|
4
|
+
/** Several lines of text: it grows with them, and counts them with `maxLength`. */
|
|
5
|
+
export declare const PillTextarea: import("react").ForwardRefExoticComponent<PillTextareaProps & import("react").RefAttributes<HTMLTextAreaElement>>;
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { ComponentPropsWithoutRef } from "react";
|
|
2
|
+
import { FieldProps } from "./PillField.js";
|
|
3
|
+
export interface PillTextareaProps extends FieldProps, Omit<ComponentPropsWithoutRef<"textarea">, "children"> {}
|
|
4
|
+
/** Several lines of text: it grows with them, and counts them with `maxLength`. */
|
|
5
|
+
export declare const PillTextarea: import("react").ForwardRefExoticComponent<PillTextareaProps & import("react").RefAttributes<HTMLTextAreaElement>>;
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
let react = require("react");
|
|
4
|
+
const require_PillField = require("./PillField.js");
|
|
5
|
+
const require_validity = require("./validity.js");
|
|
6
|
+
const require_react = require("../utils/react.js");
|
|
7
|
+
let react_jsx_runtime = require("react/jsx-runtime");
|
|
8
|
+
|
|
9
|
+
// src/PillForm/PillTextarea.tsx
|
|
10
|
+
const lengthOf = (value) => String(value ?? "").length;
|
|
11
|
+
/** Several lines of text: it grows with them, and counts them with `maxLength`. */
|
|
12
|
+
const PillTextarea = (0, react.forwardRef)(function PillTextarea({ label, hideLabel, hint, error, optional, className, rows = 3, onInput, onBlur, onInvalid, ...rest }, forwardedRef) {
|
|
13
|
+
const textareaRef = (0, react.useRef)(null);
|
|
14
|
+
const ref = (0, react.useMemo)(() => require_react.mergeRefs(textareaRef, forwardedRef), [forwardedRef]);
|
|
15
|
+
const validity = require_validity.useValidity(error, textareaRef);
|
|
16
|
+
const [ownLength, setOwnLength] = (0, react.useState)(lengthOf(rest.defaultValue));
|
|
17
|
+
require_validity.useFormReset(textareaRef, () => setOwnLength(lengthOf(textareaRef.current?.value)));
|
|
18
|
+
const length = rest.value === void 0 ? ownLength : lengthOf(rest.value);
|
|
19
|
+
const handleInput = (event) => {
|
|
20
|
+
setOwnLength(event.currentTarget.value.length);
|
|
21
|
+
validity.handlers.onInput(event);
|
|
22
|
+
onInput?.(event);
|
|
23
|
+
};
|
|
24
|
+
return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(require_PillField.FieldShell, {
|
|
25
|
+
label,
|
|
26
|
+
hideLabel,
|
|
27
|
+
hint,
|
|
28
|
+
optional,
|
|
29
|
+
className,
|
|
30
|
+
error: validity.error,
|
|
31
|
+
aside: rest.maxLength ? `${length} / ${rest.maxLength}` : void 0,
|
|
32
|
+
"data-kind": "area",
|
|
33
|
+
children: (control) => /* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", {
|
|
34
|
+
"data-mp": "field-box",
|
|
35
|
+
"data-type": "textarea",
|
|
36
|
+
children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)("textarea", {
|
|
37
|
+
...rest,
|
|
38
|
+
...control,
|
|
39
|
+
ref,
|
|
40
|
+
rows,
|
|
41
|
+
onInput: handleInput,
|
|
42
|
+
onBlur: require_validity.both(validity.handlers.onBlur, onBlur),
|
|
43
|
+
onInvalid: require_validity.both(validity.handlers.onInvalid, onInvalid)
|
|
44
|
+
})
|
|
45
|
+
})
|
|
46
|
+
});
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
exports.PillTextarea = PillTextarea;
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import { forwardRef, useMemo, useRef, useState } from "react";
|
|
4
|
+
import { FieldShell } from "./PillField.mjs";
|
|
5
|
+
import { both, useFormReset, useValidity } from "./validity.mjs";
|
|
6
|
+
import { mergeRefs } from "../utils/react.mjs";
|
|
7
|
+
import { jsx } from "react/jsx-runtime";
|
|
8
|
+
|
|
9
|
+
// src/PillForm/PillTextarea.tsx
|
|
10
|
+
const lengthOf = (value) => String(value ?? "").length;
|
|
11
|
+
/** Several lines of text: it grows with them, and counts them with `maxLength`. */
|
|
12
|
+
const PillTextarea = forwardRef(function PillTextarea({ label, hideLabel, hint, error, optional, className, rows = 3, onInput, onBlur, onInvalid, ...rest }, forwardedRef) {
|
|
13
|
+
const textareaRef = useRef(null);
|
|
14
|
+
const ref = useMemo(() => mergeRefs(textareaRef, forwardedRef), [forwardedRef]);
|
|
15
|
+
const validity = useValidity(error, textareaRef);
|
|
16
|
+
const [ownLength, setOwnLength] = useState(lengthOf(rest.defaultValue));
|
|
17
|
+
useFormReset(textareaRef, () => setOwnLength(lengthOf(textareaRef.current?.value)));
|
|
18
|
+
const length = rest.value === void 0 ? ownLength : lengthOf(rest.value);
|
|
19
|
+
const handleInput = (event) => {
|
|
20
|
+
setOwnLength(event.currentTarget.value.length);
|
|
21
|
+
validity.handlers.onInput(event);
|
|
22
|
+
onInput?.(event);
|
|
23
|
+
};
|
|
24
|
+
return /* @__PURE__ */ jsx(FieldShell, {
|
|
25
|
+
label,
|
|
26
|
+
hideLabel,
|
|
27
|
+
hint,
|
|
28
|
+
optional,
|
|
29
|
+
className,
|
|
30
|
+
error: validity.error,
|
|
31
|
+
aside: rest.maxLength ? `${length} / ${rest.maxLength}` : void 0,
|
|
32
|
+
"data-kind": "area",
|
|
33
|
+
children: (control) => /* @__PURE__ */ jsx("div", {
|
|
34
|
+
"data-mp": "field-box",
|
|
35
|
+
"data-type": "textarea",
|
|
36
|
+
children: /* @__PURE__ */ jsx("textarea", {
|
|
37
|
+
...rest,
|
|
38
|
+
...control,
|
|
39
|
+
ref,
|
|
40
|
+
rows,
|
|
41
|
+
onInput: handleInput,
|
|
42
|
+
onBlur: both(validity.handlers.onBlur, onBlur),
|
|
43
|
+
onInvalid: both(validity.handlers.onInvalid, onInvalid)
|
|
44
|
+
})
|
|
45
|
+
})
|
|
46
|
+
});
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
export { PillTextarea };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export type DateType = "date" | "month" | "week" | "time" | "datetime-local";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export type DateType = "date" | "month" | "week" | "time" | "datetime-local";
|
|
@@ -0,0 +1,155 @@
|
|
|
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
|
+
exports.addDays = addDays;
|
|
140
|
+
exports.addMonths = addMonths;
|
|
141
|
+
exports.dayOf = dayOf;
|
|
142
|
+
exports.firstDayOfWeek = firstDayOfWeek;
|
|
143
|
+
exports.formatDay = formatDay;
|
|
144
|
+
exports.formatValue = formatValue;
|
|
145
|
+
exports.isDayOutside = isDayOutside;
|
|
146
|
+
exports.isOutside = isOutside;
|
|
147
|
+
exports.localFirstDay = localFirstDay;
|
|
148
|
+
exports.monthGrid = monthGrid;
|
|
149
|
+
exports.monthOf = monthOf;
|
|
150
|
+
exports.parseDay = parseDay;
|
|
151
|
+
exports.timeOf = timeOf;
|
|
152
|
+
exports.toValue = toValue;
|
|
153
|
+
exports.today = today;
|
|
154
|
+
exports.usesTwelveHours = usesTwelveHours;
|
|
155
|
+
exports.weekOf = weekOf;
|