@plexui/react-native 0.2.3 → 0.2.5

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.
Files changed (34) hide show
  1. package/dist/es/components/ContextMenu/ContextMenu.js +128 -0
  2. package/dist/es/components/DatePicker/Calendar.js +112 -0
  3. package/dist/es/components/DatePicker/DatePicker.js +335 -0
  4. package/dist/es/components/DatePicker/TimeDial.js +123 -0
  5. package/dist/es/components/DatePicker/Wheel.js +164 -0
  6. package/dist/es/components/DatePicker/dates.js +147 -0
  7. package/dist/es/components/Dialog/Dialog.js +21 -0
  8. package/dist/es/components/Icon/material.js +16 -0
  9. package/dist/es/components/List/List.js +3 -3
  10. package/dist/es/components/Menu/Menu.js +50 -9
  11. package/dist/es/index.js +2 -0
  12. package/dist/types/components/ContextMenu/ContextMenu.d.ts +57 -0
  13. package/dist/types/components/DatePicker/Calendar.d.ts +29 -0
  14. package/dist/types/components/DatePicker/DatePicker.d.ts +57 -0
  15. package/dist/types/components/DatePicker/TimeDial.d.ts +20 -0
  16. package/dist/types/components/DatePicker/Wheel.d.ts +40 -0
  17. package/dist/types/components/DatePicker/dates.d.ts +45 -0
  18. package/dist/types/components/Dialog/Dialog.d.ts +18 -0
  19. package/dist/types/components/Icon/material.d.ts +6 -0
  20. package/dist/types/components/List/List.d.ts +3 -1
  21. package/dist/types/components/Menu/Menu.d.ts +15 -6
  22. package/dist/types/index.d.ts +2 -0
  23. package/package.json +1 -1
  24. package/src/components/ContextMenu/ContextMenu.tsx +164 -0
  25. package/src/components/DatePicker/Calendar.tsx +227 -0
  26. package/src/components/DatePicker/DatePicker.tsx +608 -0
  27. package/src/components/DatePicker/TimeDial.tsx +201 -0
  28. package/src/components/DatePicker/Wheel.tsx +247 -0
  29. package/src/components/DatePicker/dates.ts +161 -0
  30. package/src/components/Dialog/Dialog.tsx +46 -0
  31. package/src/components/Icon/material.ts +18 -0
  32. package/src/components/List/List.tsx +8 -1
  33. package/src/components/Menu/Menu.tsx +67 -7
  34. package/src/index.ts +2 -0
@@ -0,0 +1,128 @@
1
+ import { jsx as _jsx } from "react/jsx-runtime";
2
+ import { cloneElement, createContext, isValidElement, useCallback, useContext, useEffect, useRef, useState } from "react";
3
+ import { Dimensions, Platform, View } from "react-native";
4
+ import { useTheme } from "../../theme/ThemeProvider";
5
+ import { Menu, MenuSurface, hasCheckedItems, measure } from "../Menu/Menu";
6
+ /** A touch and hold opens the menu after half a second, as the web's ContextMenu (CONTEXT_MENU_LONG_PRESS). */
7
+ export const CONTEXT_MENU_LONG_PRESS = 500;
8
+ const ContextMenuContext = createContext(null);
9
+ function useContextMenu(part) {
10
+ const context = useContext(ContextMenuContext);
11
+ if (!context)
12
+ throw new Error(`${part} must be inside a ContextMenu`);
13
+ return context;
14
+ }
15
+ /**
16
+ * A menu of actions for an element, opened by a touch and hold (and a right click on the web): on iOS 27 the screen
17
+ * dims and blurs, the element stays clear over it as a lifted preview, and the glass menu stands 16 from it by the
18
+ * third of the screen it is in, over it when it stands in the lower half; on Android Material's popup under the
19
+ * element, the screen dimmed around it. Parts as @plexui/ui: `ContextMenu.Trigger`, `ContextMenu.Content`, and the
20
+ * items of Menu (`ContextMenu.Item`, `ContextMenu.Separator`, `ContextMenu.Label`).
21
+ */
22
+ function ContextMenuRoot({ children, open: openProp, onOpenChange }) {
23
+ const [own, setOwn] = useState(false);
24
+ const open = openProp ?? own;
25
+ const [anchor, setAnchor] = useState(null);
26
+ const target = useRef({ node: null, preview: null });
27
+ const [preview, setPreview] = useState(null);
28
+ const apply = useCallback((next) => {
29
+ if (openProp === undefined)
30
+ setOwn(next);
31
+ onOpenChange?.(next);
32
+ }, [openProp, onOpenChange]);
33
+ const setOpen = apply;
34
+ const applyRef = useRef(apply);
35
+ applyRef.current = apply;
36
+ // The element is measured as the menu opens, by a touch and hold or by `open`: the menu and its preview stand where
37
+ // it is. Until then nothing is drawn
38
+ useEffect(() => {
39
+ if (!open) {
40
+ setAnchor(null);
41
+ return;
42
+ }
43
+ let live = true;
44
+ measure(target.current.node, (box) => {
45
+ if (!live)
46
+ return;
47
+ setAnchor(box);
48
+ setPreview(target.current.preview);
49
+ });
50
+ // A turn of the screen moves the element: the menu closes, as iOS dismisses a context menu on rotation
51
+ const sub = Dimensions.addEventListener("change", () => applyRef.current(false));
52
+ return () => {
53
+ live = false;
54
+ sub.remove();
55
+ };
56
+ }, [open]);
57
+ const setTarget = useCallback((node, next) => {
58
+ target.current = { node, preview: next };
59
+ }, []);
60
+ return _jsx(ContextMenuContext.Provider, { value: { open, setOpen, anchor, preview, setTarget }, children: children });
61
+ }
62
+ ContextMenuRoot.displayName = "ContextMenu";
63
+ /**
64
+ * The element the menu is for: a ListItem, a Pressable, a Button, anything with `onLongPress`. Its own press still
65
+ * runs; a touch and hold opens the menu, and so does a right click on the web. The lifted preview on iOS is the
66
+ * element drawn again from its props, so state the element keeps inside itself is not in it.
67
+ */
68
+ function ContextMenuTrigger({ children, style }) {
69
+ const theme = useTheme();
70
+ const { setOpen, setTarget, open, anchor } = useContextMenu("ContextMenu.Trigger");
71
+ const child = isValidElement(children) ? children : null;
72
+ const own = child?.props;
73
+ // The element's own long press gets its event, then the menu opens
74
+ const hold = (...args) => {
75
+ own?.onLongPress?.(...args);
76
+ setOpen(true);
77
+ };
78
+ // VoiceOver and TalkBack open it through the element's long-press action (ListItem names it itself)
79
+ const actions = own?.accessibilityActions ?? [];
80
+ const a11y = actions.some((action) => action.name === "longpress")
81
+ ? null
82
+ : {
83
+ accessibilityActions: [...actions, { name: "longpress" }],
84
+ onAccessibilityAction: (event) => {
85
+ if (event.nativeEvent.actionName === "longpress")
86
+ hold();
87
+ else
88
+ own?.onAccessibilityAction?.(event);
89
+ },
90
+ };
91
+ // The preview is the element drawn again over the dimming, taking no presses
92
+ const preview = child ? cloneElement(child, { onLongPress: undefined, onPress: undefined }) : null;
93
+ const web = Platform.OS === "web";
94
+ return (_jsx(View, { ref: (next) => setTarget(next, preview), collapsable: false,
95
+ // On iOS the element hides under its lifted preview while the menu is open, so it is not drawn twice; Android
96
+ // keeps it lit in its place
97
+ style: [{ opacity: open && anchor && !theme.isAndroid ? 0 : 1 }, style], ...(web
98
+ ? {
99
+ onContextMenu: (event) => {
100
+ event.preventDefault();
101
+ setOpen(true);
102
+ },
103
+ }
104
+ : null), children: child ? cloneElement(child, { onLongPress: hold, ...a11y }) : children }));
105
+ }
106
+ ContextMenuTrigger.displayName = "ContextMenu.Trigger";
107
+ /** The menu: Menu's items, 250 wide on iOS. */
108
+ function ContextMenuContent(props) {
109
+ const theme = useTheme();
110
+ const { open, setOpen, anchor, preview } = useContextMenu("ContextMenu.Content");
111
+ // The preview keeps a row's corners to half its height, else the kit's 30 (--context-menu-preview-radius)
112
+ // Items that say `checked` make a set of options, with the checkmark's column, as in Menu
113
+ const check = hasCheckedItems(props.children);
114
+ const radius = Math.min(theme.length("--context-menu-preview-radius", 30), (anchor?.height ?? 60) / 2);
115
+ if (theme.isAndroid) {
116
+ // Android: Material's popup under the element at its end, the screen dimmed around it
117
+ return _jsx(MenuSurface, { ...props, check: check, open: open && anchor != null, anchor: anchor, row: anchor ? { box: anchor, radius: { top: 0, bottom: 0 } } : null, cover: false, onClose: () => setOpen(false) });
118
+ }
119
+ return _jsx(MenuSurface, { ...props, check: check, open: open && anchor != null, anchor: anchor, context: { preview, radius }, onClose: () => setOpen(false) });
120
+ }
121
+ ContextMenuContent.displayName = "ContextMenu.Content";
122
+ export const ContextMenu = Object.assign(ContextMenuRoot, {
123
+ Trigger: ContextMenuTrigger,
124
+ Content: ContextMenuContent,
125
+ Item: Menu.Item,
126
+ Separator: Menu.Separator,
127
+ Label: Menu.Label,
128
+ });
@@ -0,0 +1,112 @@
1
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
+ import { useMemo, useState } from "react";
3
+ import { Platform, Pressable, StyleSheet, View } from "react-native";
4
+ import ChevronLeftLg from "../../icons/plex/ChevronLeftLg";
5
+ import ChevronRightLg from "../../icons/plex/ChevronRightLg";
6
+ import { isRTL } from "../../internal/direction";
7
+ import { withAlpha } from "../../theme/material";
8
+ import { useTheme } from "../../theme/ThemeProvider";
9
+ import { MaterialChevronLeft, MaterialChevronRight } from "../Icon/material";
10
+ import { Text } from "../Text/Text";
11
+ import { dayOutside, formatFullDay, formatMonth, monthOf, monthWeeks, sameDay, weekdayLabels } from "./dates";
12
+ /** iOS: the corner's inset around the grid of a compact picker's calendar (the web's `--calendar-inner-padding`). */
13
+ export const IOS_CALENDAR_INSET = 10;
14
+ /**
15
+ * The month grid of DatePicker. On iOS the phone calendar of @plexui/ui (Calendar.module.css under 768px): the month's
16
+ * title Semibold 17 at the row's text inset, the two arrows as 44 targets at the end, the weekdays Semibold 13 in
17
+ * capitals in the tertiary label, the days Regular 17 in 44 cells side by side and row on row, the chosen day white on
18
+ * a 44 circle of the text's color, today a 4 dot under its digits, days out of range at 40%. On Android Material 3's
19
+ * date picker: the month at the start in Medium 14 with the arrows at the end, the weekdays' letters, the days in 40dp
20
+ * circles, the chosen one filled with the primary color and today ringed in it.
21
+ */
22
+ export function Calendar({ value, onSelect, min, max, locale, weekStartsOn, width, disabled: off = false, previousLabel = "Previous month", nextLabel = "Next month", testID }) {
23
+ const theme = useTheme();
24
+ const android = theme.isAndroid;
25
+ const [month, setMonth] = useState(() => monthOf(value ?? new Date()));
26
+ // The value moved to another month from outside (a wheel, a reset): the calendar turns to it
27
+ const [shownFor, setShownFor] = useState(value?.getTime() ?? null);
28
+ if ((value?.getTime() ?? null) !== shownFor) {
29
+ setShownFor(value?.getTime() ?? null);
30
+ if (value != null && monthOf(value).getTime() !== month.getTime())
31
+ setMonth(monthOf(value));
32
+ }
33
+ const weeks = useMemo(() => monthWeeks(month, weekStartsOn), [month, weekStartsOn]);
34
+ const labels = useMemo(() => weekdayLabels(locale, weekStartsOn, android ? "narrow" : "short"), [locale, weekStartsOn, android]);
35
+ const today = new Date();
36
+ // The arrows stop at the months that hold no day in range
37
+ const canPrevious = min == null || monthOf(month, -1).getTime() >= monthOf(min).getTime();
38
+ const canNext = max == null || monthOf(month, 1).getTime() <= monthOf(max).getTime();
39
+ const rtl = isRTL();
40
+ const cell = android ? Math.min(48, width / 7) : Math.max(44, width / 7);
41
+ const m = theme.material;
42
+ const text = android ? m.onSurface : theme.colors.text;
43
+ const arrowColor = android ? m.onSurfaceVariant : text;
44
+ const arrow = (direction) => {
45
+ const enabled = !off && (direction < 0 ? canPrevious : canNext);
46
+ // In a right-to-left layout the earlier month is on the right, its arrow pointing right
47
+ const pointsLeft = (direction < 0) !== rtl;
48
+ const Icon = android ? (pointsLeft ? MaterialChevronLeft : MaterialChevronRight) : pointsLeft ? ChevronLeftLg : ChevronRightLg;
49
+ return (_jsx(Pressable, { accessibilityRole: "button", accessibilityLabel: direction < 0 ? previousLabel : nextLabel, accessibilityState: { disabled: !enabled }, disabled: !enabled, onPress: () => setMonth((current) => monthOf(current, direction)), testID: testID ? `${testID}-${direction < 0 ? "previous" : "next"}` : undefined, style: ({ pressed }) => [
50
+ android ? styles.androidArrow : styles.arrow,
51
+ pressed ? { backgroundColor: android ? withAlpha(m.onSurfaceVariant, 0.1) : theme.color("--button-background-color-active", withAlpha(text, 0.1)) } : null,
52
+ { opacity: enabled ? 1 : android ? 0.38 : 0.4 },
53
+ ], children: _jsx(Icon, { size: 24, color: arrowColor }) }));
54
+ };
55
+ return (_jsxs(View, { style: { width }, testID: testID, children: [_jsxs(View, { style: [styles.header, android ? styles.androidHeader : { height: 44 }], children: [_jsx(Text, { accessibilityRole: "header", accessibilityLiveRegion: "polite", numberOfLines: 1, color: android ? m.onSurfaceVariant : text, style: [
56
+ styles.title,
57
+ android
58
+ ? [styles.androidTitle, theme.font("medium")]
59
+ : [{ fontSize: 17, lineHeight: 22, paddingStart: Math.max(0, theme.length("--field-row-padding-inline", 16) - IOS_CALENDAR_INSET) }, theme.font("semibold")],
60
+ ], children: formatMonth(month, locale) }), arrow(-1), arrow(1)] }), _jsx(View, { style: [styles.week, android ? { height: 48 } : styles.weekdays, android ? null : { marginBottom: 3 }], importantForAccessibility: "no-hide-descendants", accessibilityElementsHidden: true, children: labels.map(({ label, name }) => (_jsx(Text, { color: android ? m.onSurface : theme.colors.labelTertiary, style: [
61
+ { width: cell, textAlign: "center" },
62
+ android ? styles.androidWeekday : [styles.weekday, theme.font("semibold")],
63
+ ], children: android ? label : label.toUpperCase() }, name))) }), weeks.map((week, row) => (_jsx(View, { style: styles.week, children: week.map((day, column) => day == null ? (_jsx(View, { style: { width: cell, height: android ? 48 : 44 } }, column)) : (_jsx(Day, { day: day, cell: cell, chosen: sameDay(day, value), isToday: sameDay(day, today), disabled: dayOutside(day, min, max), locked: off, locale: locale, onSelect: onSelect, testID: testID ? `${testID}-day-${day.getDate()}` : undefined }, column))) }, row)))] }));
64
+ }
65
+ function Day({ day, cell, chosen, isToday, disabled, locked, locale, onSelect, testID }) {
66
+ const theme = useTheme();
67
+ const android = theme.isAndroid;
68
+ const m = theme.material;
69
+ const ink = theme.colors.text;
70
+ const size = android ? 40 : 44;
71
+ const digitColor = android
72
+ ? chosen
73
+ ? m.onPrimary
74
+ : disabled
75
+ ? withAlpha(m.onSurface, 0.38)
76
+ : isToday
77
+ ? m.primary
78
+ : m.onSurface
79
+ : chosen
80
+ ? theme.colors.textInverse
81
+ : ink;
82
+ return (_jsx(Pressable, { accessibilityRole: "button", accessibilityLabel: formatFullDay(day, locale), accessibilityState: { selected: chosen, disabled: disabled || locked }, disabled: disabled || locked, onPress: () => onSelect(day), testID: testID, style: [styles.cell, { width: cell, height: android ? 48 : 44 }], children: ({ pressed }) => (_jsxs(View, { style: [
83
+ styles.circle,
84
+ { width: size, height: size, borderRadius: size / 2 },
85
+ chosen
86
+ ? { backgroundColor: android ? m.primary : ink }
87
+ : android
88
+ ? [isToday ? { borderWidth: 1, borderColor: m.primary } : null, pressed ? { backgroundColor: withAlpha(m.onSurface, 0.1) } : null]
89
+ : // iOS: the web's hover ring, shown while the finger is on the day
90
+ pressed
91
+ ? { borderWidth: 1, borderColor: ink }
92
+ : null,
93
+ !android && disabled ? { opacity: 0.4 } : null,
94
+ ], children: [_jsx(Text, { color: digitColor, style: android ? styles.androidDigit : styles.digit, children: day.getDate() }), isToday && !android ? _jsx(View, { style: [styles.dot, { backgroundColor: chosen ? theme.colors.textInverse : ink }] }) : null] })) }));
95
+ }
96
+ const styles = StyleSheet.create({
97
+ header: { flexDirection: "row", alignItems: "center" },
98
+ androidHeader: { height: 56, paddingStart: 12, paddingEnd: 0 },
99
+ title: { flex: 1 },
100
+ androidTitle: { fontSize: 14, lineHeight: 20, letterSpacing: 0.1 },
101
+ arrow: { width: 44, height: 44, borderRadius: 22, alignItems: "center", justifyContent: "center" },
102
+ androidArrow: { width: 48, height: 48, borderRadius: 24, alignItems: "center", justifyContent: "center" },
103
+ week: { flexDirection: "row", alignItems: "center", justifyContent: "center" },
104
+ weekdays: { height: 20 },
105
+ weekday: { fontSize: 13, lineHeight: 18 },
106
+ androidWeekday: { fontSize: 16, lineHeight: 24, letterSpacing: 0.5 },
107
+ cell: { alignItems: "center", justifyContent: "center" },
108
+ circle: { alignItems: "center", justifyContent: "center" },
109
+ digit: { fontSize: 17, lineHeight: 22, ...(Platform.OS === "web" ? { userSelect: "none" } : null) },
110
+ androidDigit: { fontSize: 16, lineHeight: 24, letterSpacing: 0.5, ...(Platform.OS === "web" ? { userSelect: "none" } : null) },
111
+ dot: { position: "absolute", bottom: 8, width: 4, height: 4, borderRadius: 2 },
112
+ });
@@ -0,0 +1,335 @@
1
+ import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
2
+ import { useContext, useEffect, useRef, useState } from "react";
3
+ import { Pressable, StyleSheet, View, useWindowDimensions } from "react-native";
4
+ import { RowEndContext, RowHoldContext, rowHold } from "../../internal/row";
5
+ import { withAlpha } from "../../theme/material";
6
+ import { useTheme } from "../../theme/ThemeProvider";
7
+ import { PickerDialog } from "../Dialog/Dialog";
8
+ import { MaterialCalendarToday, MaterialSchedule } from "../Icon/material";
9
+ import { FieldOutline, fieldLook } from "../Input/Input";
10
+ import { Popover } from "../Popover/Popover";
11
+ import { Text } from "../Text/Text";
12
+ import { Calendar, IOS_CALENDAR_INSET } from "./Calendar";
13
+ import { clampDate, dateOrder, formatDate, formatHeadline, formatTime, formatWheelDay, localeHour12, localeWeekStart, monthNames, pad2, sameDay, periodLabels, startOfDay, timeOutside, withDay, withTime, } from "./dates";
14
+ import { TimeDial } from "./TimeDial";
15
+ import { Wheels } from "./Wheel";
16
+ /**
17
+ * A date and time picker. On iOS UIDatePicker of iOS 27.2: `compact`, the date and the time in two capsules 8 apart
18
+ * (34 tall, 12 in, the secondary soft fill, the text in the accent while open) that open the month's calendar and the
19
+ * time's wheels in the glass popover, at most 32 from the screen's edge; `inline`, the calendar in place (a Time row
20
+ * under it for both); `wheels`, the wheels. On Android Material 3's pickers: the value as a row's value in the accent
21
+ * (or an outlined field with its icon) that opens the date picker dialog (the headline date over the month) or the
22
+ * time picker dial, both applied with OK; `inline` the calendar in place, `wheels` Android's spinners. The value is a
23
+ * plain `Date`; `min` and `max` bound it.
24
+ */
25
+ export function DatePicker({ value, onChange, mode = "date", display = "compact", min, max, locale, weekStartsOn, hour12, placeholder, disabled = false, "aria-label": ariaLabel, dateTitle = "Select date", timeTitle = "Select time", cancelLabel = "Cancel", okLabel = "OK", timeLabel = "Time", testID, style, }) {
26
+ const theme = useTheme();
27
+ const busy = useRef(false);
28
+ const shared = {
29
+ busy,
30
+ value,
31
+ set: (next) => onChange(clampDate(next, min, max)),
32
+ min,
33
+ max,
34
+ locale,
35
+ weekStartsOn: weekStartsOn ?? localeWeekStart(locale),
36
+ hour12: hour12 ?? localeHour12(locale),
37
+ disabled,
38
+ ariaLabel,
39
+ placeholder: { date: placeholder ?? "Select date", time: placeholder ?? "Select time" },
40
+ dateTitle,
41
+ timeTitle,
42
+ cancelLabel,
43
+ okLabel,
44
+ testID,
45
+ };
46
+ if (display === "wheels" || (display === "inline" && mode === "time")) {
47
+ return (_jsx(View, { style: style, pointerEvents: disabled ? "none" : "auto", children: _jsx(View, { style: { opacity: disabled ? 0.4 : 1 }, children: _jsx(Wheels, { columns: wheelColumns(shared, mode), height: theme.isAndroid ? undefined : 216, bandInset: 8, disabled: disabled, testID: testID }) }) }));
48
+ }
49
+ if (display === "inline")
50
+ return _jsx(InlinePicker, { shared: shared, mode: mode, timeLabel: timeLabel, style: style });
51
+ return _jsx(Compact, { shared: shared, mode: mode, style: style });
52
+ }
53
+ DatePicker.displayName = "DatePicker";
54
+ /** The compact parts, at a row's end: they take the room after the label (two capsules pass its 60%), set at its end. */
55
+ function Compact({ shared, mode, style }) {
56
+ const row = useContext(RowEndContext);
57
+ const fill = row?.fill;
58
+ useEffect(() => {
59
+ if (!fill)
60
+ return;
61
+ fill(true);
62
+ return () => fill(false);
63
+ }, [fill]);
64
+ return (_jsxs(View, { style: [styles.parts, row ? styles.atEnd : null, style], testID: shared.testID, children: [mode !== "time" ? _jsx(Part, { shared: shared, kind: "date", first: true }) : null, mode !== "date" ? _jsx(Part, { shared: shared, kind: "time", first: mode === "time" }) : null] }));
65
+ }
66
+ /**
67
+ * The wheels, as UIDatePicker's `.wheels` (iOS 27.2, en_US, 402pt): a date's month, day and year in the locale's order,
68
+ * their centres 104 and 77.5 apart (the month 157 wide, the day 50, the year 105); a time's hour and minute 63.5 apart,
69
+ * AM / PM from 49 after the minute's centre; with both, one column of days ("Wed Nov 25", "Today") before the time.
70
+ */
71
+ function wheelColumns(shared, mode) {
72
+ const { value, set, min, max, locale } = shared;
73
+ const base = value ?? clampDate(new Date(), min, max);
74
+ const columns = [];
75
+ if (mode === "datetime")
76
+ columns.push(dayColumn(shared, base));
77
+ else if (mode === "date") {
78
+ const names = monthNames(locale);
79
+ // The years stand still around today (not the value), so turning the wheel never moves its rows
80
+ const thisYear = new Date().getFullYear();
81
+ const yearFrom = Math.min(min?.getFullYear() ?? thisYear - 100, base.getFullYear());
82
+ const yearTo = Math.max(max?.getFullYear() ?? thisYear + 50, base.getFullYear());
83
+ const days = new Date(base.getFullYear(), base.getMonth() + 1, 0).getDate();
84
+ // A day that would fall out of the month (31 into February) is the month's last
85
+ const on = (year, month, day) => {
86
+ const last = new Date(year, month + 1, 0).getDate();
87
+ return new Date(year, month, Math.min(day, last), base.getHours(), base.getMinutes());
88
+ };
89
+ const dayOut = (date) => (min != null && startOfDay(date) < startOfDay(min)) || (max != null && startOfDay(date) > startOfDay(max));
90
+ const byPart = {
91
+ month: {
92
+ key: "month",
93
+ label: "Month",
94
+ width: 157,
95
+ items: names.map((label, month) => ({
96
+ label,
97
+ disabled: (min != null && new Date(base.getFullYear(), month + 1, 0) < startOfDay(min)) || (max != null && new Date(base.getFullYear(), month, 1) > startOfDay(max)),
98
+ })),
99
+ index: base.getMonth(),
100
+ onChange: (month) => set(on(base.getFullYear(), month, base.getDate())),
101
+ },
102
+ day: {
103
+ key: "day",
104
+ label: "Day",
105
+ width: 50,
106
+ items: Array.from({ length: days }, (_, index) => ({ label: String(index + 1), disabled: dayOut(on(base.getFullYear(), base.getMonth(), index + 1)) })),
107
+ index: base.getDate() - 1,
108
+ onChange: (index) => set(on(base.getFullYear(), base.getMonth(), index + 1)),
109
+ },
110
+ year: {
111
+ key: "year",
112
+ label: "Year",
113
+ width: 105,
114
+ items: Array.from({ length: yearTo - yearFrom + 1 }, (_, index) => ({ label: String(yearFrom + index) })),
115
+ index: base.getFullYear() - yearFrom,
116
+ onChange: (index) => set(on(yearFrom + index, base.getMonth(), base.getDate())),
117
+ },
118
+ };
119
+ for (const part of dateOrder(locale))
120
+ columns.push(byPart[part]);
121
+ }
122
+ if (mode !== "date")
123
+ columns.push(...timeColumns(shared, base, set));
124
+ return columns;
125
+ }
126
+ /** The days of a date-and-time wheel: a year each side of today (or `min` to `max`), "Today" for today. */
127
+ function dayColumn(shared, base) {
128
+ const { min, max, locale, set } = shared;
129
+ const today = startOfDay(new Date());
130
+ const from = min ? startOfDay(min) : new Date(today.getFullYear() - 1, today.getMonth(), today.getDate());
131
+ const to = max ? startOfDay(max) : new Date(today.getFullYear() + 1, today.getMonth(), today.getDate());
132
+ const first = startOfDay(base) < from ? startOfDay(base) : from;
133
+ const last = startOfDay(base) > to ? startOfDay(base) : to;
134
+ const days = [];
135
+ for (let day = first; day <= last; day = new Date(day.getFullYear(), day.getMonth(), day.getDate() + 1))
136
+ days.push(day);
137
+ return {
138
+ key: "day",
139
+ label: "Day",
140
+ width: 130,
141
+ align: "end",
142
+ items: days.map((day) => ({ label: sameDay(day, today) ? "Today" : formatWheelDay(day, locale) })),
143
+ index: Math.max(0, days.findIndex((day) => sameDay(day, base))),
144
+ onChange: (index) => set(withDay(base, days[index])),
145
+ };
146
+ }
147
+ function timeColumns(shared, base, set) {
148
+ const { min, max, locale, hour12 } = shared;
149
+ const hours = base.getHours();
150
+ const minutes = base.getMinutes();
151
+ // An hour can be chosen when any of its minutes lies in range
152
+ const hourOut = (hour) => (min != null && withTime(base, hour, 59) < min) || (max != null && withTime(base, hour, 0) > max);
153
+ const columns = [
154
+ hour12
155
+ ? {
156
+ key: "hours",
157
+ label: "Hours",
158
+ width: 64,
159
+ items: Array.from({ length: 12 }, (_, index) => ({ label: String(index === 0 ? 12 : index), disabled: hourOut(index + (hours >= 12 ? 12 : 0)) })),
160
+ index: hours % 12,
161
+ onChange: (index) => set(withTime(base, index + (hours >= 12 ? 12 : 0), minutes)),
162
+ }
163
+ : {
164
+ key: "hours",
165
+ label: "Hours",
166
+ width: 64,
167
+ items: Array.from({ length: 24 }, (_, hour) => ({ label: pad2(hour), disabled: hourOut(hour) })),
168
+ index: hours,
169
+ onChange: (hour) => set(withTime(base, hour, minutes)),
170
+ },
171
+ {
172
+ key: "minutes",
173
+ label: "Minutes",
174
+ width: 64,
175
+ items: Array.from({ length: 60 }, (_, minute) => ({ label: pad2(minute), disabled: timeOutside(withTime(base, hours, minute), min, max) })),
176
+ index: minutes,
177
+ onChange: (minute) => set(withTime(base, hours, minute)),
178
+ },
179
+ ];
180
+ if (hour12) {
181
+ const [am, pm] = periodLabels(locale);
182
+ // AM / PM from 49 after the minutes' centre: 17 into its column
183
+ columns.push({
184
+ key: "period",
185
+ label: "AM/PM",
186
+ width: 64,
187
+ align: "start",
188
+ inset: 17,
189
+ items: [
190
+ { label: am, disabled: hourOut(hours % 12) && [...Array(12).keys()].every((hour) => hourOut(hour)) },
191
+ { label: pm, disabled: [...Array(12).keys()].every((hour) => hourOut(hour + 12)) },
192
+ ],
193
+ index: hours >= 12 ? 1 : 0,
194
+ onChange: (index) => set(withTime(base, (hours % 12) + (index ? 12 : 0), minutes)),
195
+ });
196
+ }
197
+ return columns;
198
+ }
199
+ /** One part of a compact picker: the date or the time. */
200
+ function Part({ shared, kind, first }) {
201
+ const theme = useTheme();
202
+ const row = useContext(RowEndContext);
203
+ const hold = useContext(RowHoldContext);
204
+ const [open, setOwnOpen] = useState(false);
205
+ // Presses inside a picker's dialog or popover reach the row in React's tree: while one is open, and for the press
206
+ // that closes it, the row opens nothing
207
+ const setOpen = (next) => {
208
+ if (next) {
209
+ if (shared.busy.current)
210
+ return;
211
+ shared.busy.current = true;
212
+ }
213
+ else
214
+ setTimeout(() => (shared.busy.current = false), 0);
215
+ setOwnOpen(next);
216
+ };
217
+ const screen = useWindowDimensions().width;
218
+ const { value, disabled, locale, hour12, ariaLabel, testID } = shared;
219
+ if (disabled && open) {
220
+ shared.busy.current = false;
221
+ setOwnOpen(false);
222
+ }
223
+ const text = value == null ? null : kind === "date" ? formatDate(value, locale) : formatTime(value, locale, hour12);
224
+ const shown = text ?? shared.placeholder[kind];
225
+ const partID = testID ? `${testID}-${kind}` : undefined;
226
+ const a11y = {
227
+ accessibilityRole: "button",
228
+ accessibilityLabel: [ariaLabel, shown].filter(Boolean).join(", "),
229
+ accessibilityState: { disabled, expanded: open },
230
+ };
231
+ if (theme.isAndroid) {
232
+ return _jsx(AndroidPart, { shared: shared, kind: kind, first: first, open: open, setOpen: setOpen, shown: shown, filled: text != null, a11y: a11y, partID: partID });
233
+ }
234
+ // iOS: the capsule, and the popover it opens
235
+ const calendarWidth = 7 * 44 + 2 * IOS_CALENDAR_INSET;
236
+ // At most 32 from the screen's edge, as the compact picker's popover stands (less on a narrow screen)
237
+ const edge = Math.max(8, Math.min(32, (screen - calendarWidth) / 2));
238
+ // The popover's wheels: a 24-hour time 75 apart (iOS 27.2's compact time popover), a 12-hour one as the wheels
239
+ const columns = kind === "time" ? timeColumns(shared, value ?? clampDate(new Date(), shared.min, shared.max), shared.set).map((column) => (hour12 ? column : { ...column, width: 75 })) : [];
240
+ const wheelsWidth = Math.max(233, 40 + columns.reduce((sum, column) => sum + column.width, 0));
241
+ return (_jsxs(Popover, { open: open, onOpenChange: (next) => setOpen(next && !disabled), children: [_jsx(Popover.Trigger, { children: _jsx(Pressable, { ...a11y, disabled: disabled, ...rowHold(hold), testID: partID, style: ({ pressed }) => [
242
+ styles.capsule,
243
+ disabled
244
+ ? null
245
+ : { backgroundColor: theme.color("--color-background-secondary-soft-alpha", withAlpha(theme.colors.text, 0.07)), opacity: pressed ? 0.6 : 1 },
246
+ ], children: _jsx(Text, { numberOfLines: 1, color: disabled || text == null ? theme.colors.labelTertiary : open ? theme.colors.textAccent : theme.colors.text, style: styles.capsuleText, tabularNums: kind === "time", children: shown }) }) }), _jsx(Popover.Content, { width: kind === "date" ? calendarWidth : wheelsWidth, minWidth: "auto", align: row ? "end" : "center", alignOffset: 0, collisionPadding: { left: edge, right: edge, top: 20, bottom: 20 }, "aria-label": kind === "date" ? shared.dateTitle : shared.timeTitle, testID: partID ? `${partID}-popover` : undefined, style: kind === "date" ? { padding: IOS_CALENDAR_INSET } : { padding: 0 }, children: kind === "date" ? (_jsx(Calendar, { value: value, onSelect: (day) => {
247
+ shared.set(withDay(value, day));
248
+ setOpen(false);
249
+ }, min: shared.min, max: shared.max, locale: locale, weekStartsOn: shared.weekStartsOn, width: 7 * 44, testID: partID ? `${partID}-calendar` : undefined })) : (_jsx(Wheels, { columns: columns, testID: partID ? `${partID}-wheels` : undefined })) })] }));
250
+ }
251
+ /** Android: a row's value in the accent, or an outlined field with its icon; either opens Material's dialog. */
252
+ function AndroidPart({ shared, kind, first, open, setOpen, shown, filled, a11y, partID }) {
253
+ const theme = useTheme();
254
+ const m = theme.material;
255
+ const row = useContext(RowEndContext);
256
+ const hold = useContext(RowHoldContext);
257
+ const { disabled } = shared;
258
+ // In a row the first part takes the row's press, as an Android value row opens its picker from anywhere on it
259
+ const handRow = row != null && first && !disabled ? row.press : undefined;
260
+ useEffect(() => {
261
+ if (!handRow)
262
+ return;
263
+ handRow(() => setOpen(true));
264
+ return () => handRow(null);
265
+ }, [handRow, setOpen]);
266
+ const look = fieldLook(theme, { variant: "outline", size: "md", pill: false }, { focused: open, filled, disabled, invalid: false });
267
+ const Icon = kind === "date" ? MaterialCalendarToday : MaterialSchedule;
268
+ return (_jsxs(_Fragment, { children: [row != null ? (_jsx(Pressable, { ...a11y, disabled: disabled, ...rowHold(hold), onPress: () => setOpen(true), hitSlop: ROW_SLOP, testID: partID, children: _jsx(Text, { numberOfLines: 1, color: withAlpha(filled ? m.primary : withAlpha(m.onSurface, 0.35), disabled ? 0.5 : 1), style: styles.androidValue, tabularNums: kind === "time", children: shown }) })) : (_jsxs(Pressable, { ...a11y, disabled: disabled, onPress: () => setOpen(true), testID: partID, style: [styles.field, look.box, { gap: look.gap, paddingEnd: 12 }], children: [_jsx(FieldOutline, { frame: look.frame }), _jsx(Text, { numberOfLines: 1, color: filled ? look.text.color : look.placeholder, style: [styles.grow, { fontSize: look.text.fontSize, lineHeight: look.text.lineHeight }], children: shown }), _jsx(Icon, { size: 24, color: look.trailingIcon })] })), kind === "date" ? _jsx(AndroidDateDialog, { shared: shared, open: open, onClose: () => setOpen(false), partID: partID }) : _jsx(AndroidTimeDialog, { shared: shared, open: open, onClose: () => setOpen(false), partID: partID })] }));
269
+ }
270
+ /** Material 3's date picker dialog: "Select date", the headline date, a line, then the month; OK applies the day. */
271
+ function AndroidDateDialog({ shared, open, onClose, partID }) {
272
+ const theme = useTheme();
273
+ const m = theme.material;
274
+ const { value, locale } = shared;
275
+ const [draft, setDraft] = useState(value);
276
+ const [wasOpen, setWasOpen] = useState(open);
277
+ if (wasOpen !== open) {
278
+ setWasOpen(open);
279
+ if (open)
280
+ setDraft(value);
281
+ }
282
+ const width = Math.min(useWindowDimensions().width - 48, 356);
283
+ return (_jsxs(PickerDialog, { open: open, onClose: onClose, onOk: () => {
284
+ if (draft)
285
+ shared.set(withDay(value, draft));
286
+ }, cancelLabel: shared.cancelLabel, okLabel: shared.okLabel, label: shared.dateTitle, testID: partID ? `${partID}-dialog` : undefined, children: [_jsxs(View, { style: styles.androidHeader, children: [_jsx(Text, { color: m.onSurfaceVariant, style: [styles.androidSupporting, theme.font("medium")], children: shared.dateTitle }), _jsx(Text, { color: m.onSurfaceVariant, style: styles.androidHeadline, accessibilityLiveRegion: "polite", children: draft ? formatHeadline(draft, locale) : shared.placeholder.date })] }), _jsx(View, { style: { height: 1, backgroundColor: m.outlineVariant } }), _jsx(View, { style: styles.androidCalendar, children: _jsx(Calendar, { value: draft, onSelect: setDraft, min: shared.min, max: shared.max, locale: locale, weekStartsOn: shared.weekStartsOn, width: width - 24, testID: partID ? `${partID}-calendar` : undefined }) })] }));
287
+ }
288
+ /** Material 3's time picker dialog: the dial; OK applies the time. */
289
+ function AndroidTimeDialog({ shared, open, onClose, partID }) {
290
+ const { value } = shared;
291
+ const now = value ?? new Date();
292
+ const [draft, setDraft] = useState({ hours: now.getHours(), minutes: now.getMinutes() });
293
+ const [wasOpen, setWasOpen] = useState(open);
294
+ if (wasOpen !== open) {
295
+ setWasOpen(open);
296
+ if (open)
297
+ setDraft({ hours: now.getHours(), minutes: now.getMinutes() });
298
+ }
299
+ return (_jsx(PickerDialog, { open: open, onClose: onClose, onOk: () => shared.set(withTime(value, draft.hours, draft.minutes)), cancelLabel: shared.cancelLabel, okLabel: shared.okLabel, label: shared.timeTitle, testID: partID ? `${partID}-dialog` : undefined, children: _jsx(TimeDial, { hours: draft.hours, minutes: draft.minutes, onChange: (hours, minutes) => setDraft({ hours, minutes }), hour12: shared.hour12, locale: shared.locale, title: shared.timeTitle, testID: partID ? `${partID}-dial` : undefined }) }));
300
+ }
301
+ /** The calendar in place; with the time, a Time row under it whose value opens the time's picker. */
302
+ function InlinePicker({ shared, mode, timeLabel, style }) {
303
+ const theme = useTheme();
304
+ const [width, setWidth] = useState(0);
305
+ const { value, testID } = shared;
306
+ const android = theme.isAndroid;
307
+ const timeRow = mode === "datetime" ? (
308
+ // iOS 27.2's inline picker with the time (dates-uikit-inline-time): no line over the row, its centre 51 under the
309
+ // last week's, the capsule 11 above the card's edge, the label Semibold; Android a row under a line
310
+ _jsxs(View, { style: [styles.timeRow, android ? [styles.androidTimeRow, { borderTopColor: theme.material.outlineVariant }] : styles.iosTimeRow], children: [_jsx(Text, { style: android ? styles.androidRowLabel : [styles.rowLabel, theme.font("semibold")], color: android ? theme.material.onSurface : theme.colors.text, children: timeLabel }), _jsx(RowEndContext.Provider, { value: { fill: () => { } }, children: _jsx(Part, { shared: { ...shared, ariaLabel: timeLabel }, kind: "time", first: false }) })] })) : null;
311
+ return (_jsxs(View, { style: [styles.inline, android ? null : styles.iosInline, mode === "datetime" && !android ? { paddingBottom: 8 } : null, style, { opacity: shared.disabled ? 0.4 : 1 }], pointerEvents: shared.disabled ? "none" : "auto", onLayout: (event) => setWidth(event.nativeEvent.layout.width), testID: testID, children: [width > 0 ? (_jsx(View, { style: android ? null : { paddingHorizontal: IOS_CALENDAR_INSET }, children: _jsx(Calendar, { value: value, onSelect: (day) => shared.set(withDay(value, day)), disabled: shared.disabled, min: shared.min, max: shared.max, locale: shared.locale, weekStartsOn: shared.weekStartsOn, width: android ? width : width - 2 * IOS_CALENDAR_INSET, testID: testID ? `${testID}-calendar` : undefined }) })) : null, timeRow] }));
312
+ }
313
+ const ROW_SLOP = { top: 15, bottom: 15, left: 8, right: 8 };
314
+ const styles = StyleSheet.create({
315
+ parts: { flexDirection: "row", alignItems: "center", gap: 8, flexShrink: 1 },
316
+ atEnd: { flexGrow: 1, justifyContent: "flex-end" },
317
+ capsule: { height: 34, borderRadius: 17, paddingHorizontal: 12, justifyContent: "center" },
318
+ capsuleText: { fontSize: 17, lineHeight: 22 },
319
+ androidValue: { fontSize: 16, lineHeight: 20 },
320
+ field: { flexDirection: "row", alignItems: "center", flexGrow: 1, minWidth: 140 },
321
+ grow: { flex: 1, minWidth: 0 },
322
+ androidHeader: { paddingStart: 24, paddingEnd: 12, paddingTop: 16, paddingBottom: 12 },
323
+ androidSupporting: { fontSize: 14, lineHeight: 20, letterSpacing: 0.1 },
324
+ androidHeadline: { fontSize: 32, lineHeight: 40, marginTop: 36 },
325
+ androidCalendar: { paddingHorizontal: 12, paddingBottom: 8 },
326
+ inline: { alignSelf: "stretch" },
327
+ timeRow: { flexDirection: "row", alignItems: "center", justifyContent: "space-between", paddingHorizontal: 16 },
328
+ iosTimeRow: { minHeight: 40, marginTop: 9 },
329
+ androidTimeRow: { minHeight: 52, marginTop: 8, borderTopWidth: StyleSheet.hairlineWidth },
330
+ // As iOS 27.2's inline picker in a cell: the month's title centred 34 under the top, the last week (or the Time row's
331
+ // capsule) 11 above the bottom
332
+ iosInline: { paddingTop: 12, paddingBottom: 11 },
333
+ rowLabel: { fontSize: 17, lineHeight: 22 },
334
+ androidRowLabel: { fontSize: 16, lineHeight: 24 },
335
+ });