@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,123 @@
1
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
+ import { useRef, useState } from "react";
3
+ import { Platform, Pressable, StyleSheet, View } from "react-native";
4
+ import Svg, { Circle, Line } from "react-native-svg";
5
+ import { useTheme } from "../../theme/ThemeProvider";
6
+ import { Text } from "../Text/Text";
7
+ import { pad2, periodLabels } from "./dates";
8
+ const DIAL = 256;
9
+ const OUTER = 101;
10
+ const INNER = 69;
11
+ const KNOB = 48;
12
+ /**
13
+ * Android's time picker dial (Material 3 TimePicker): "Select time" in Medium 12, the hour and the minute in Regular
14
+ * 57 boxes 96 by 80 (the one being set in the primary container, the other in the highest surface container), AM and
15
+ * PM stacked beside them on a 12-hour clock, and the 256dp dial: a 48dp knob in the primary color on a line from the
16
+ * middle, the numbers 101dp out (a 24-hour clock adds 00 and 13–23 69dp out). A tap or a drag on the dial sets the
17
+ * hour, then it turns to the minutes.
18
+ */
19
+ export function TimeDial({ hours, minutes, onChange, hour12, locale, title, hoursLabel = "Hours", minutesLabel = "Minutes", testID }) {
20
+ const theme = useTheme();
21
+ const m = theme.material;
22
+ const [part, setPart] = useState("hours");
23
+ const pm = hours >= 12;
24
+ const [am, pmLabel] = periodLabels(locale);
25
+ const shownHour = hour12 ? (hours % 12 === 0 ? 12 : hours % 12) : hours;
26
+ const center = DIAL / 2;
27
+ // The value under the knob: its angle from 12 o'clock and its ring
28
+ const inner = part === "hours" && !hour12 && (hours === 0 || hours > 12);
29
+ const angle = part === "hours" ? ((hours % 12) / 12) * 360 : (minutes / 60) * 360;
30
+ const radius = inner ? INNER : OUTER;
31
+ const knob = { x: center + radius * Math.sin((angle * Math.PI) / 180), y: center - radius * Math.cos((angle * Math.PI) / 180) };
32
+ const dragging = useRef(false);
33
+ const pick = (event, done) => {
34
+ const x = event.nativeEvent.locationX - center;
35
+ const y = event.nativeEvent.locationY - center;
36
+ let degrees = (Math.atan2(x, -y) * 180) / Math.PI;
37
+ if (degrees < 0)
38
+ degrees += 360;
39
+ if (part === "hours") {
40
+ const slot = Math.round(degrees / 30) % 12;
41
+ const ringInner = !hour12 && Math.hypot(x, y) < (OUTER + INNER) / 2;
42
+ // Outer ring 12, 1…11; inner 00, 13…23 (Compose's 24-hour dial); a 12-hour clock keeps its half of the day
43
+ const next = hour12 ? (slot % 12) + (pm ? 12 : 0) : ringInner ? (slot === 0 ? 0 : slot + 12) : slot === 0 ? 12 : slot;
44
+ if (next !== hours)
45
+ onChange(next, minutes);
46
+ if (done)
47
+ setPart("minutes");
48
+ }
49
+ else {
50
+ const next = Math.round(degrees / 6) % 60;
51
+ if (next !== minutes)
52
+ onChange(hours, next);
53
+ }
54
+ };
55
+ const numbers = part === "minutes"
56
+ ? Array.from({ length: 12 }, (_, slot) => ({ label: pad2(slot * 5), slot, ring: OUTER, value: slot * 5 }))
57
+ : [
58
+ ...Array.from({ length: 12 }, (_, slot) => ({ label: String(slot === 0 ? 12 : slot), slot, ring: OUTER, value: slot === 0 ? 12 : slot })),
59
+ ...(hour12 ? [] : Array.from({ length: 12 }, (_, slot) => ({ label: slot === 0 ? "00" : String(slot + 12), slot, ring: INNER, value: slot === 0 ? 0 : slot + 12 }))),
60
+ ];
61
+ const chosenValue = part === "hours" ? (hour12 ? shownHour : hours) : minutes;
62
+ const box = (which) => {
63
+ const active = part === which;
64
+ return (_jsx(Pressable, { accessibilityRole: "button", accessibilityLabel: `${which === "hours" ? hoursLabel : minutesLabel}, ${which === "hours" ? shownHour : pad2(minutes)}`, accessibilityState: { selected: active }, onPress: () => setPart(which), testID: testID ? `${testID}-${which}` : undefined, style: [styles.box, { backgroundColor: active ? m.primaryContainer : m.surfaceContainerHighest }], children: _jsx(Text, { color: active ? m.onPrimaryContainer : m.onSurface, style: styles.boxText, tabularNums: true, children: which === "hours" ? (hour12 ? String(shownHour) : pad2(hours)) : pad2(minutes) }) }));
65
+ };
66
+ const period = (afternoon) => {
67
+ const active = pm === afternoon;
68
+ return (_jsx(Pressable, { accessibilityRole: "button", accessibilityState: { selected: active }, onPress: () => {
69
+ if (active)
70
+ return;
71
+ onChange(afternoon ? hours + 12 : hours - 12, minutes);
72
+ }, style: [styles.period, active ? { backgroundColor: m.tertiaryContainer } : null], children: _jsx(Text, { color: active ? m.onTertiaryContainer : m.onSurfaceVariant, style: [styles.periodText, theme.font("medium")], children: afternoon ? pmLabel : am }) }));
73
+ };
74
+ return (_jsxs(View, { style: styles.root, testID: testID, children: [_jsx(Text, { color: m.onSurfaceVariant, style: [styles.title, theme.font("medium")], children: title }), _jsxs(View, { style: styles.display, children: [box("hours"), _jsx(Text, { color: m.onSurface, style: styles.colon, children: ":" }), box("minutes"), hour12 ? (_jsxs(View, { style: [styles.periods, { borderColor: m.outline }], children: [period(false), _jsx(View, { style: { height: 1, backgroundColor: m.outline } }), period(true)] })) : null] }), _jsxs(View, { accessible: true, accessibilityRole: "adjustable", accessibilityLabel: part === "hours" ? hoursLabel : minutesLabel, accessibilityValue: { text: part === "hours" ? String(shownHour) : pad2(minutes) }, accessibilityActions: [{ name: "increment" }, { name: "decrement" }], onAccessibilityAction: (event) => {
75
+ const up = event.nativeEvent.actionName === "increment" ? 1 : -1;
76
+ if (part === "hours")
77
+ onChange((hours + up + 24) % 24, minutes);
78
+ else
79
+ onChange(hours, (minutes + up + 60) % 60);
80
+ }, ...(Platform.OS === "web"
81
+ ? {
82
+ tabIndex: 0,
83
+ onKeyDown: (event) => {
84
+ const up = event.key === "ArrowUp" || event.key === "ArrowRight" ? 1 : event.key === "ArrowDown" || event.key === "ArrowLeft" ? -1 : 0;
85
+ if (event.key === "Enter" && part === "hours")
86
+ setPart("minutes");
87
+ else if (!up)
88
+ return;
89
+ else if (part === "hours")
90
+ onChange((hours + up + 24) % 24, minutes);
91
+ else
92
+ onChange(hours, (minutes + up + 60) % 60);
93
+ event.preventDefault();
94
+ },
95
+ }
96
+ : null), onStartShouldSetResponder: () => true, onMoveShouldSetResponder: () => true, onResponderGrant: (event) => {
97
+ dragging.current = true;
98
+ pick(event, false);
99
+ }, onResponderMove: (event) => pick(event, false), onResponderRelease: (event) => {
100
+ dragging.current = false;
101
+ pick(event, true);
102
+ }, testID: testID ? `${testID}-dial` : undefined, style: [styles.dial, { backgroundColor: m.surfaceContainerHighest }], children: [_jsxs(Svg, { width: DIAL, height: DIAL, style: StyleSheet.absoluteFill, pointerEvents: "none", children: [_jsx(Line, { x1: center, y1: center, x2: knob.x, y2: knob.y, stroke: m.primary, strokeWidth: 2 }), _jsx(Circle, { cx: center, cy: center, r: 4, fill: m.primary }), _jsx(Circle, { cx: knob.x, cy: knob.y, r: KNOB / 2, fill: m.primary })] }), numbers.map(({ label, slot, ring, value }) => {
103
+ const x = center + ring * Math.sin((slot * 30 * Math.PI) / 180);
104
+ const y = center - ring * Math.cos((slot * 30 * Math.PI) / 180);
105
+ const chosen = value === chosenValue && ring === radius;
106
+ return (_jsx(View, { pointerEvents: "none", style: [styles.number, { left: x - KNOB / 2, top: y - KNOB / 2 }], children: _jsx(Text, { color: chosen ? m.onPrimary : m.onSurface, style: ring === INNER ? styles.innerText : styles.numberText, children: label }) }, `${ring}-${slot}`));
107
+ })] })] }));
108
+ }
109
+ const styles = StyleSheet.create({
110
+ root: { paddingHorizontal: 24, paddingTop: 24, alignItems: "center" },
111
+ title: { alignSelf: "flex-start", fontSize: 12, lineHeight: 16, letterSpacing: 0.5, marginBottom: 20 },
112
+ display: { flexDirection: "row", alignItems: "center", height: 80 },
113
+ box: { width: 96, height: 80, borderRadius: 8, alignItems: "center", justifyContent: "center" },
114
+ boxText: { fontSize: 57, lineHeight: 64 },
115
+ colon: { width: 24, textAlign: "center", fontSize: 57, lineHeight: 64 },
116
+ periods: { width: 52, height: 80, marginStart: 12, borderWidth: 1, borderRadius: 8, overflow: "hidden" },
117
+ period: { flex: 1, alignItems: "center", justifyContent: "center" },
118
+ periodText: { fontSize: 16, lineHeight: 24, letterSpacing: 0.15 },
119
+ dial: { width: DIAL, height: DIAL, borderRadius: DIAL / 2, marginTop: 36 },
120
+ number: { position: "absolute", width: KNOB, height: KNOB, alignItems: "center", justifyContent: "center" },
121
+ numberText: { fontSize: 16, lineHeight: 24, letterSpacing: 0.5 },
122
+ innerText: { fontSize: 14, lineHeight: 20, letterSpacing: 0.25 },
123
+ });
@@ -0,0 +1,164 @@
1
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
+ import { useEffect, useRef, useState } from "react";
3
+ import { Animated, Platform, Pressable, ScrollView, StyleSheet, View } from "react-native";
4
+ import { withAlpha } from "../../theme/material";
5
+ import { useTheme } from "../../theme/ThemeProvider";
6
+ /** iOS: a row of UIDatePicker's wheels, 32 apart (the band of iOS 27.2, 32 tall). Android: a NumberPicker's 48dp rows. */
7
+ export const IOS_WHEEL_ROW = 32;
8
+ export const ANDROID_WHEEL_ROW = 48;
9
+ /**
10
+ * The wheels of a time (or date) picker. On iOS UIDatePicker's `.wheels` as iOS 27.2 draws them in a compact time
11
+ * picker's popover: rows 32 apart in Regular 22, a 32 band of the ink at 7% across the columns 20 in from the sides,
12
+ * the rows on the band in the ink and the others in its 35% gray, turning away like a cylinder's. On Android the
13
+ * spinner mode of its DatePicker and TimePicker: three 48dp rows, the middle one between two lines in the ink, the
14
+ * others faded. A wheel stops on a row; a tap on a row turns it there; VoiceOver and TalkBack turn it one row at a time.
15
+ */
16
+ export function Wheels({ columns, height: heightProp, bandInset = 20, disabled = false, testID }) {
17
+ const theme = useTheme();
18
+ const android = theme.isAndroid;
19
+ const row = android ? ANDROID_WHEEL_ROW : IOS_WHEEL_ROW;
20
+ const height = heightProp ?? (android ? row * 3 : 202);
21
+ const ink = android ? theme.material.onSurface : theme.colors.text;
22
+ const band = (height - row) / 2;
23
+ // Columns wider than the room narrow in step, so the wheels keep inside the band
24
+ const [room, setRoom] = useState(0);
25
+ const total = columns.reduce((sum, column) => sum + column.width, 0);
26
+ const fit = room > 0 ? Math.min(1, (room - 2 * (android ? 0 : bandInset)) / total) : 1;
27
+ return (_jsxs(View, { style: [styles.wheels, { height }], testID: testID, onLayout: (event) => setRoom(event.nativeEvent.layout.width), children: [_jsx(View, { pointerEvents: "none", style: [
28
+ styles.band,
29
+ { top: band, height: row },
30
+ android
31
+ ? { borderTopWidth: 2, borderBottomWidth: 2, borderColor: theme.material.outline, left: 0, right: 0 }
32
+ : { left: bandInset, right: bandInset, borderRadius: 8, backgroundColor: withAlpha(ink, theme.scheme === "dark" ? 0.12 : 0.07) },
33
+ ] }), columns.map((column) => (_jsx(Wheel, { column: fit < 1 ? { ...column, width: Math.floor(column.width * fit) } : column, disabled: disabled, row: row, height: height, ink: ink, testID: testID ? `${testID}-${column.key}` : undefined }, column.key)))] }));
34
+ }
35
+ function Wheel({ column, row, height, ink, disabled, testID }) {
36
+ const theme = useTheme();
37
+ const android = theme.isAndroid;
38
+ const { items, index, onChange, width, label, align = "center", inset = 4 } = column;
39
+ const scroll = useRef(null);
40
+ const [offset] = useState(() => new Animated.Value(index * row));
41
+ const last = useRef(index * row);
42
+ const timer = useRef(null);
43
+ const touching = useRef(false);
44
+ const pad = (height - row) / 2;
45
+ const web = Platform.OS === "web";
46
+ const nearestEnabled = (target) => {
47
+ const clamped = Math.max(0, Math.min(items.length - 1, target));
48
+ if (!items[clamped]?.disabled)
49
+ return clamped;
50
+ for (let step = 1; step < items.length; step += 1) {
51
+ if (!items[clamped - step]?.disabled && clamped - step >= 0)
52
+ return clamped - step;
53
+ if (!items[clamped + step]?.disabled && clamped + step < items.length)
54
+ return clamped + step;
55
+ }
56
+ return clamped;
57
+ };
58
+ const turnTo = (next, animated) => {
59
+ scroll.current?.scrollTo({ y: next * row, animated: animated && !theme.reducedMotion });
60
+ };
61
+ // The wheel comes to rest on the row nearest the band, or the nearest one that can be chosen
62
+ const settle = () => {
63
+ timer.current = null;
64
+ if (touching.current)
65
+ return;
66
+ const next = nearestEnabled(Math.round(last.current / row));
67
+ if (Math.abs(last.current - next * row) > 0.5)
68
+ turnTo(next, true);
69
+ if (next !== index)
70
+ onChange(next);
71
+ };
72
+ const onScroll = (event) => {
73
+ const y = event.nativeEvent.contentOffset.y;
74
+ last.current = y;
75
+ offset.setValue(y);
76
+ if (timer.current)
77
+ clearTimeout(timer.current);
78
+ timer.current = setTimeout(settle, web ? 120 : 90);
79
+ };
80
+ useEffect(() => () => {
81
+ if (timer.current)
82
+ clearTimeout(timer.current);
83
+ }, []);
84
+ // A value set from outside turns the wheel to it (a settle still waiting gives way to it)
85
+ useEffect(() => {
86
+ if (touching.current)
87
+ return;
88
+ if (timer.current) {
89
+ clearTimeout(timer.current);
90
+ timer.current = null;
91
+ }
92
+ if (Math.abs(last.current - index * row) > 0.5)
93
+ turnTo(index, false);
94
+ // eslint-disable-next-line react-hooks/exhaustive-deps
95
+ }, [index, row]);
96
+ const textAlign = align === "start" ? "left" : align === "end" ? "right" : "center";
97
+ const fontSize = android ? 20 : 22;
98
+ const lineHeight = android ? 28 : 28;
99
+ // A row's look by its distance from the band: it turns away and fades as on a cylinder (iOS); Android fades it
100
+ const rowStyle = (position) => {
101
+ const distance = Animated.divide(Animated.subtract(offset, position * row), row);
102
+ return {
103
+ opacity: distance.interpolate({ inputRange: [-3, -1, 0, 1, 3], outputRange: [0.25, android ? 0.6 : 0.85, 1, android ? 0.6 : 0.85, 0.25], extrapolate: "clamp" }),
104
+ transform: android
105
+ ? []
106
+ : [
107
+ { perspective: 600 },
108
+ { rotateX: distance.interpolate({ inputRange: [-4, 4], outputRange: ["72deg", "-72deg"], extrapolate: "clamp" }) },
109
+ { scale: distance.interpolate({ inputRange: [-3, 0, 3], outputRange: [0.9, 1, 0.9], extrapolate: "clamp" }) },
110
+ ],
111
+ };
112
+ };
113
+ const step = (direction) => {
114
+ if (disabled)
115
+ return;
116
+ const next = nearestEnabled(index + direction);
117
+ if (next === index)
118
+ return;
119
+ turnTo(next, true);
120
+ onChange(next);
121
+ };
122
+ const gray = withAlpha(ink, android ? 0.38 : 0.35);
123
+ const labelFor = (item, position, color, pressable) => {
124
+ const text = (_jsx(Animated.Text, { numberOfLines: 1, style: [
125
+ { width, height: row, fontSize, lineHeight: row, textAlign, color: item.disabled ? withAlpha(ink, 0.2) : color, paddingStart: align === "end" ? 4 : inset, paddingEnd: align === "start" ? 4 : align === "end" ? inset : 4 },
126
+ theme.font("regular"),
127
+ web ? { userSelect: "none" } : null,
128
+ rowStyle(position),
129
+ ], children: item.label }));
130
+ if (!pressable)
131
+ return _jsx(View, { children: text }, position);
132
+ return (_jsx(Pressable, { disabled: item.disabled || disabled, importantForAccessibility: "no", accessibilityElementsHidden: true, onPress: () => {
133
+ turnTo(position, true);
134
+ if (position !== index)
135
+ onChange(position);
136
+ }, children: text }, position));
137
+ };
138
+ return (_jsxs(View, { accessible: true, accessibilityRole: "adjustable", accessibilityLabel: label, accessibilityValue: { text: items[index]?.label }, accessibilityActions: [{ name: "increment" }, { name: "decrement" }], onAccessibilityAction: (event) => step(event.nativeEvent.actionName === "increment" ? 1 : -1), ...(web
139
+ ? {
140
+ tabIndex: disabled ? -1 : 0,
141
+ onKeyDown: (event) => {
142
+ if (event.key === "ArrowDown")
143
+ step(1);
144
+ else if (event.key === "ArrowUp")
145
+ step(-1);
146
+ else
147
+ return;
148
+ event.preventDefault();
149
+ },
150
+ }
151
+ : null), style: { width, height }, testID: testID, children: [_jsx(ScrollView, { ref: scroll, scrollEnabled: !disabled, showsVerticalScrollIndicator: false, snapToInterval: row, decelerationRate: "fast", scrollEventThrottle: 16, onScroll: onScroll, onScrollBeginDrag: () => {
152
+ touching.current = true;
153
+ }, onScrollEndDrag: () => {
154
+ touching.current = false;
155
+ if (timer.current)
156
+ clearTimeout(timer.current);
157
+ timer.current = setTimeout(settle, 90);
158
+ }, onLayout: () => turnTo(index, false), contentContainerStyle: { paddingVertical: pad }, style: web ? { scrollbarWidth: "none" } : null, children: items.map((item, position) => labelFor(item, position, gray, true)) }), _jsx(View, { pointerEvents: "none", style: [styles.clip, { top: pad, height: row, width }], children: _jsx(Animated.View, { style: { transform: [{ translateY: Animated.multiply(offset, -1) }] }, children: items.map((item, position) => labelFor(item, position, ink, false)) }) })] }));
159
+ }
160
+ const styles = StyleSheet.create({
161
+ wheels: { flexDirection: "row", justifyContent: "center", alignSelf: "stretch" },
162
+ band: { position: "absolute" },
163
+ clip: { position: "absolute", left: 0, overflow: "hidden" },
164
+ });
@@ -0,0 +1,147 @@
1
+ /** Date helpers of DatePicker: plain `Date`s in the device's time zone, days compared by their calendar date. */
2
+ export function sameDay(a, b) {
3
+ return a != null && b != null && a.getFullYear() === b.getFullYear() && a.getMonth() === b.getMonth() && a.getDate() === b.getDate();
4
+ }
5
+ export function startOfDay(date) {
6
+ return new Date(date.getFullYear(), date.getMonth(), date.getDate());
7
+ }
8
+ /** The first day of the month `offset` months from `date`'s. */
9
+ export function monthOf(date, offset = 0) {
10
+ return new Date(date.getFullYear(), date.getMonth() + offset, 1);
11
+ }
12
+ /** A day lies outside `min`…`max` (compared by calendar day). */
13
+ export function dayOutside(day, min, max) {
14
+ const time = startOfDay(day).getTime();
15
+ return (min != null && time < startOfDay(min).getTime()) || (max != null && time > startOfDay(max).getTime());
16
+ }
17
+ /** A moment lies outside `min`…`max` (compared to the minute). */
18
+ export function timeOutside(date, min, max) {
19
+ return (min != null && date.getTime() < min.getTime()) || (max != null && date.getTime() > max.getTime());
20
+ }
21
+ /** The date kept inside `min`…`max`. */
22
+ export function clampDate(date, min, max) {
23
+ if (min != null && date.getTime() < min.getTime())
24
+ return new Date(min.getTime());
25
+ if (max != null && date.getTime() > max.getTime())
26
+ return new Date(max.getTime());
27
+ return date;
28
+ }
29
+ /** The weeks of a month, each of seven days; days of the months around it are `null`. */
30
+ export function monthWeeks(month, weekStartsOn) {
31
+ const year = month.getFullYear();
32
+ const index = month.getMonth();
33
+ const lead = (new Date(year, index, 1).getDay() - weekStartsOn + 7) % 7;
34
+ const days = new Date(year, index + 1, 0).getDate();
35
+ const cells = [...Array(lead).fill(null)];
36
+ for (let day = 1; day <= days; day += 1)
37
+ cells.push(new Date(year, index, day));
38
+ while (cells.length % 7)
39
+ cells.push(null);
40
+ const weeks = [];
41
+ for (let start = 0; start < cells.length; start += 7)
42
+ weeks.push(cells.slice(start, start + 7));
43
+ return weeks;
44
+ }
45
+ /** `value` on `day`: its time kept, the start of the day when there is no value yet. */
46
+ export function withDay(value, day) {
47
+ return new Date(day.getFullYear(), day.getMonth(), day.getDate(), value?.getHours() ?? 0, value?.getMinutes() ?? 0);
48
+ }
49
+ /** `value` at `hours`:`minutes`, today when there is no value yet. */
50
+ export function withTime(value, hours, minutes) {
51
+ const base = value ?? new Date();
52
+ return new Date(base.getFullYear(), base.getMonth(), base.getDate(), hours, minutes);
53
+ }
54
+ /** The day a week starts on in the locale (0 Sunday … 6 Saturday), from `Intl.Locale`'s week info; Monday without it. */
55
+ export function localeWeekStart(locale) {
56
+ try {
57
+ const info = new Intl.Locale(locale ?? Intl.DateTimeFormat().resolvedOptions().locale);
58
+ const first = info.getWeekInfo?.().firstDay ?? info.weekInfo?.firstDay;
59
+ if (first != null)
60
+ return first % 7;
61
+ }
62
+ catch {
63
+ // An engine without Intl.Locale
64
+ }
65
+ return 1;
66
+ }
67
+ /** The locale reads time on a 12-hour clock. */
68
+ export function localeHour12(locale) {
69
+ try {
70
+ const options = new Intl.DateTimeFormat(locale, { hour: "numeric" }).resolvedOptions();
71
+ if (options.hourCycle)
72
+ return options.hourCycle === "h11" || options.hourCycle === "h12";
73
+ return options.hour12 === true;
74
+ }
75
+ catch {
76
+ return false;
77
+ }
78
+ }
79
+ function format(locale, options, date) {
80
+ try {
81
+ // The grid and the wheels are Gregorian: the names follow it in every locale (fa-IR, th-TH)
82
+ return new Intl.DateTimeFormat(locale, { calendar: "gregory", ...options }).format(date);
83
+ }
84
+ catch {
85
+ return date.toDateString();
86
+ }
87
+ }
88
+ /** The date as a compact picker shows it: "13 Nov 2026" (iOS) or "Nov 13, 2026", by the locale. */
89
+ export function formatDate(date, locale) {
90
+ return format(locale, { day: "numeric", month: "short", year: "numeric" }, date);
91
+ }
92
+ /** The time: "09:00", or "9:00 AM" on a 12-hour clock. */
93
+ export function formatTime(date, locale, hour12) {
94
+ return format(locale, { hour: hour12 ? "numeric" : "2-digit", minute: "2-digit", hour12 }, date);
95
+ }
96
+ /** The month's title: "November 2026". */
97
+ export function formatMonth(month, locale) {
98
+ return format(locale, { month: "long", year: "numeric" }, month);
99
+ }
100
+ /** Android's dialog headline: "Tue, Nov 24". */
101
+ export function formatHeadline(date, locale) {
102
+ return format(locale, { weekday: "short", month: "short", day: "numeric" }, date);
103
+ }
104
+ /** A day on a date-and-time wheel: "Wed Nov 25". */
105
+ export function formatWheelDay(date, locale) {
106
+ return format(locale, { weekday: "short", month: "short", day: "numeric" }, date).replace(/,/g, "");
107
+ }
108
+ /** A day read out in full: "Tuesday, 24 November 2026". */
109
+ export function formatFullDay(date, locale) {
110
+ return format(locale, { weekday: "long", day: "numeric", month: "long", year: "numeric" }, date);
111
+ }
112
+ /** The weekdays' labels from `weekStartsOn`: "MON" (`short`, iOS) or "M" (`narrow`, Android). */
113
+ export function weekdayLabels(locale, weekStartsOn, width) {
114
+ // 2023-01-01 was a Sunday
115
+ return Array.from({ length: 7 }, (_, index) => {
116
+ const day = new Date(2023, 0, 1 + ((weekStartsOn + index) % 7));
117
+ return { label: format(locale, { weekday: width }, day), name: format(locale, { weekday: "long" }, day) };
118
+ });
119
+ }
120
+ /** The month names of the locale, January first. */
121
+ export function monthNames(locale) {
122
+ return Array.from({ length: 12 }, (_, index) => format(locale, { month: "long" }, new Date(2023, index, 1)));
123
+ }
124
+ /** The order of day, month and year in the locale's dates, for the date wheels. */
125
+ export function dateOrder(locale) {
126
+ try {
127
+ const parts = new Intl.DateTimeFormat(locale, { calendar: "gregory", day: "numeric", month: "long", year: "numeric" }).formatToParts(new Date(2023, 10, 24));
128
+ const order = parts.map((part) => part.type).filter((type) => type === "day" || type === "month" || type === "year");
129
+ if (order.length === 3)
130
+ return order;
131
+ }
132
+ catch {
133
+ // An engine without formatToParts
134
+ }
135
+ return ["month", "day", "year"];
136
+ }
137
+ /** The locale's day period labels: ["AM", "PM"]. */
138
+ export function periodLabels(locale) {
139
+ try {
140
+ const read = (hour) => new Intl.DateTimeFormat(locale, { hour: "numeric", hour12: true }).formatToParts(new Date(2023, 0, 1, hour)).find((part) => part.type === "dayPeriod")?.value;
141
+ return [read(9) ?? "AM", read(21) ?? "PM"];
142
+ }
143
+ catch {
144
+ return ["AM", "PM"];
145
+ }
146
+ }
147
+ export const pad2 = (value) => String(value).padStart(2, "0");
@@ -107,6 +107,27 @@ export function ChoiceDialog({ open, onClose, title, cancelLabel, okLabel, onOk,
107
107
  ]
108
108
  : [{ label: cancelLabel, testID: testID ? `${testID}-cancel` : undefined }], run: run, ids: ids, list: _jsx(View, { ...listProps, children: children }) }) }));
109
109
  }
110
+ /**
111
+ * Android's picker dialog: the dialog's panel holding a picker of its own (Material 3's date picker or time picker
112
+ * dial), Cancel and OK at the end. DatePicker's dialog on Android; not part of the public API.
113
+ */
114
+ export function PickerDialog({ open, onClose, onOk, cancelLabel, okLabel, label, children, testID }) {
115
+ const theme = useTheme();
116
+ const ids = { title: useId(), description: useId() };
117
+ const a11y = Platform.OS === "web"
118
+ ? { role: "dialog", "aria-modal": true, "aria-label": label }
119
+ : { accessibilityViewIsModal: true, accessibilityLabel: label };
120
+ const run = (action) => {
121
+ if (!open)
122
+ return;
123
+ action.onPress?.();
124
+ onClose();
125
+ };
126
+ return (_jsx(AndroidFrame, { open: open, onClose: onClose, dismissible: true, a11y: a11y, testID: testID, children: _jsx(AndroidDialog, { theme: theme, actions: [
127
+ { label: cancelLabel, variant: "cancel", testID: testID ? `${testID}-cancel` : undefined },
128
+ { label: okLabel, preferred: true, onPress: onOk, testID: testID ? `${testID}-ok` : undefined },
129
+ ], run: run, ids: ids, list: children }) }));
130
+ }
110
131
  /** An iOS alert action's label inset from its capsule's ends. */
111
132
  const IOS_ACTION_INSET = 16;
112
133
  function IOSAlert({ theme, title, description, actions, run, ids, layout, children }) {
@@ -32,3 +32,19 @@ export const MaterialCheckSymbol = createIcon("MaterialCheckSymbol", "0 -960 960
32
32
  * `abc_ic_ab_back_material` that Settings' toolbar shows); it is mirrored right to left.
33
33
  */
34
34
  export const MaterialArrowBack = createIcon("MaterialArrowBack", "0 0 24 24", [["path", { d: "M20 11H7.83l5.59-5.59L12 4l-8 8 8 8 1.41-1.41L7.83 13H20v-2z", fill: 0 }]]);
35
+ /** Material Symbols' chevron_left and chevron_right (24, the outlined weight 400): a month's arrows in the date picker. */
36
+ export const MaterialChevronLeft = createIcon("MaterialChevronLeft", "0 -960 960 960", [["path", { d: "M560-240 320-480l240-240 56 56-184 184 184 184-56 56Z", fill: 0 }]]);
37
+ export const MaterialChevronRight = createIcon("MaterialChevronRight", "0 -960 960 960", [["path", { d: "M504-480 320-664l56-56 240 240-240 240-56-56 184-184Z", fill: 0 }]]);
38
+ /** Material Symbols' calendar_today and schedule (24, outlined weight 400): the trailing icons of a date and a time field. */
39
+ export const MaterialCalendarToday = createIcon("MaterialCalendarToday", "0 -960 960 960", [
40
+ ["path", { d: "M200-80q-33 0-56.5-23.5T120-160v-560q0-33 23.5-56.5T200-800h40v-80h80v80h320v-80h80v80h40q33 0 56.5 23.5T840-720v560q0 33-23.5 56.5T760-80H200Zm0-80h560v-400H200v400Zm0-480h560v-80H200v80Zm0 0v-80 80Z", fill: 0 }],
41
+ ]);
42
+ export const MaterialSchedule = createIcon("MaterialSchedule", "0 -960 960 960", [
43
+ [
44
+ "path",
45
+ {
46
+ d: "m612-292 56-56-148-148v-184h-80v216l172 172ZM480-80q-83 0-156-31.5T197-197q-54-54-85.5-127T80-480q0-83 31.5-156T197-763q54-54 127-85.5T480-880q83 0 156 31.5T763-763q54 54 85.5 127T880-480q0 83-31.5 156T763-197q-54 54-127 85.5T480-80Zm0-400Zm0 320q133 0 226.5-93.5T800-480q0-133-93.5-226.5T480-800q-133 0-226.5 93.5T160-480q0 133 93.5 226.5T480-160Z",
47
+ fill: 0,
48
+ },
49
+ ],
50
+ ]);
@@ -115,7 +115,7 @@ export function List({ children, header, description, footer, error, surface = "
115
115
  * in (a multi-select list); a 1px divider from the text's start to the card's end, none under a row with a tile (a
116
116
  * tiled row draws none).
117
117
  */
118
- export function ListItem({ label, description, leading, leadingColor, value, valuePlacement = "end", trailing, error, checked: checkedProp, defaultChecked, onCheckedChange, disclosure, onPress, disabled = false, destructive = false, accessibilityLabel, testID, }) {
118
+ export function ListItem({ label, description, leading, leadingColor, value, valuePlacement = "end", trailing, error, checked: checkedProp, defaultChecked, onCheckedChange, disclosure, onPress, onLongPress, disabled = false, destructive = false, accessibilityLabel, testID, }) {
119
119
  const theme = useTheme();
120
120
  const { last, top, bottom } = useContext(PlaceContext);
121
121
  // A menu opened from the row finds the row (Android lights it over the dim of the screen)
@@ -185,13 +185,13 @@ export function ListItem({ label, description, leading, leadingColor, value, val
185
185
  const startColor = atStart || checkLeads ? undefined : leadingColor;
186
186
  return (_jsx(RowFrameContext.Provider, { value: frame, children: theme.isAndroid ? (_jsx(AndroidContent, { handPress: handPress, theme: theme, label: label, description: description, descriptionId: described.id, leading: start, leadingColor: startColor, leadingMark: atStart != null || checkLeads, value: value, valuePlacement: valuePlacement, trailing: end, edgeControl: found != null || switchRow, error: error, disabled: disabled, destructive: destructive, last: last })) : (_jsx(IOSContent, { theme: theme, label: label, description: description, descriptionId: described.id, leading: start, leadingColor: startColor, value: value, valuePlacement: valuePlacement, trailing: end, edgeControl: found != null || switchRow, error: error, chevron: chevron, disabled: disabled, destructive: destructive, last: last })) }));
187
187
  };
188
- if (press) {
188
+ if (press || onLongPress) {
189
189
  // Android's rows press in the list selector: the native ripple on a device, the drawn one on the web
190
190
  const highlight = controlHighlight(theme.scheme);
191
191
  const drawn = theme.isAndroid && !nativeRipple;
192
192
  return (_jsx(Pressable, { ref: frameNode, collapsable: false, ...described.props, ...(switchRow ? spaceToggles(toggle) : null),
193
193
  // A row that toggles its control is not a second element for a screen reader or a Tab stop: the control is
194
- accessible: endOwnsFocus ? false : undefined, focusable: endOwnsFocus ? false : undefined, tabIndex: endOwnsFocus ? -1 : undefined, accessibilityRole: endOwnsFocus ? undefined : switchRow ? "switch" : "button", accessibilityLabel: endOwnsFocus ? undefined : name, accessibilityState: endOwnsFocus ? undefined : switchRow ? { checked, disabled } : { disabled }, "aria-checked": switchRow ? checked : undefined, disabled: disabled, onPress: press, onPressIn: drawn ? ripple.start : undefined, onPressOut: drawn ? ripple.end : undefined, android_ripple: nativeRipple ? { color: highlight, foreground: true } : undefined, testID: testID, style: ({ pressed }) => row(pressed || held), children: ({ pressed }) => (_jsxs(RowHoldContext.Provider, { value: disabled ? null : theme.isAndroid ? hold : setHeld, children: [ripple.layer, content(pressed || held)] })) }));
194
+ accessible: endOwnsFocus ? false : undefined, focusable: endOwnsFocus ? false : undefined, tabIndex: endOwnsFocus ? -1 : undefined, accessibilityRole: endOwnsFocus ? undefined : switchRow ? "switch" : "button", accessibilityLabel: endOwnsFocus ? undefined : name, accessibilityState: endOwnsFocus ? undefined : switchRow ? { checked, disabled } : { disabled }, "aria-checked": switchRow ? checked : undefined, disabled: disabled, onPress: press, onLongPress: onLongPress, delayLongPress: onLongPress ? 500 : undefined, accessibilityActions: onLongPress ? [{ name: "longpress" }] : undefined, onAccessibilityAction: onLongPress ? (event) => event.nativeEvent.actionName === "longpress" && onLongPress() : undefined, onPressIn: drawn ? ripple.start : undefined, onPressOut: drawn ? ripple.end : undefined, android_ripple: nativeRipple ? { color: highlight, foreground: true } : undefined, testID: testID, style: ({ pressed }) => row(pressed || held), children: ({ pressed }) => (_jsxs(RowHoldContext.Provider, { value: disabled ? null : theme.isAndroid ? hold : setHeld, children: [ripple.layer, content(pressed || held)] })) }));
195
195
  }
196
196
  // A row that only shows (a detail, a value) is one element named by `accessibilityLabel` when it has one; with a
197
197
  // control at its end it stays a group, so the control is still reached
@@ -1,4 +1,4 @@
1
- import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
1
+ import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
2
2
  import { Children, cloneElement, createContext, Fragment, isValidElement, useCallback, useContext, useEffect, useLayoutEffect, useMemo, useRef, useState } from "react";
3
3
  import { Animated, Easing, I18nManager, PanResponder, Platform, Pressable, ScrollView, StyleSheet, View, useWindowDimensions } from "react-native";
4
4
  import { useSafeAreaInsets } from "react-native-safe-area-context";
@@ -156,7 +156,7 @@ function MenuContent(props) {
156
156
  // A menu whose items say `checked` (true or false) is a set of options: it keeps the checkmark's column, as Select's
157
157
  return _jsx(MenuSurface, { ...props, check: hasCheckedItems(props.children), open: menu.open, anchor: menu.anchor, row: menu.row, header: menu.header, onClose: () => menu.setOpen(false) });
158
158
  }
159
- function hasCheckedItems(children) {
159
+ export function hasCheckedItems(children) {
160
160
  return Children.toArray(children).some((child) => isValidElement(child) ? child.props.checked !== undefined || (child.type === Fragment && hasCheckedItems(child.props.children)) : false);
161
161
  }
162
162
  MenuContent.displayName = "Menu.Content";
@@ -166,7 +166,7 @@ export function MenuSurface(props) {
166
166
  return theme.isAndroid ? _jsx(AndroidMenuSurface, { ...props }) : _jsx(IOSMenuSurface, { ...props });
167
167
  }
168
168
  /** iOS 26's pop-up menu: glass, a submenu over the menu at its row while the menu steps back. */
169
- function IOSMenuSurface({ open, anchor, onClose, align = "start", width, check = false, multiple = false, cover, children, "aria-label": ariaLabel, testID }) {
169
+ function IOSMenuSurface({ open, anchor, onClose, align = "start", width, check = false, multiple = false, cover, context, children, "aria-label": ariaLabel, testID }) {
170
170
  const theme = useTheme();
171
171
  const insets = useSafeAreaInsets();
172
172
  const window = useWindowDimensions();
@@ -207,9 +207,34 @@ function IOSMenuSurface({ open, anchor, onClose, align = "start", width, check =
207
207
  const below = over ? box.y : box.y + box.height + gap;
208
208
  const fitsBelow = below + height <= window.height - bottomEdge;
209
209
  const above = over ? box.y + box.height - height : box.y - gap - height;
210
- const top = size == null ? -window.height - 1000 : fitsBelow ? below : Math.max(topEdge, above);
210
+ let top = size == null ? -window.height - 1000 : fitsBelow ? below : Math.max(topEdge, above);
211
+ let previewTop = box.y;
212
+ if (context) {
213
+ // iOS 27.2 places a context menu by where the element stands (ref-app menus-context): in the left third at its
214
+ // left edge, in the right third at its right edge, else at its centre; 16 under it, or over it when the element
215
+ // is in the lower half and the menu fits there (the web's ContextMenu, contextMenuPreview)
216
+ const centre = box.x + box.width / 2;
217
+ left = centre < window.width / 3 ? box.x : centre > (window.width * 2) / 3 ? box.x + box.width - measured : centre - measured / 2;
218
+ left = Math.max(margin, Math.min(left, window.width - measured - margin));
219
+ const lift = 16;
220
+ const overIt = box.y - lift - height;
221
+ const underIt = box.y + box.height + lift;
222
+ const lower = box.y + box.height / 2 > window.height / 2;
223
+ if (size != null) {
224
+ if (lower && overIt >= topEdge)
225
+ top = overIt;
226
+ else {
227
+ // Where the menu does not fit under the element it lifts the element up with it, as iOS moves the preview, so
228
+ // the menu never covers it
229
+ const overflow = underIt + height - (window.height - bottomEdge);
230
+ previewTop = overflow > 0 ? Math.max(topEdge, box.y - overflow) : box.y;
231
+ top = Math.max(topEdge, Math.min(previewTop + box.height + lift, window.height - bottomEdge - height));
232
+ }
233
+ }
234
+ }
211
235
  // The measures are from the screen's left; a phone in a right-to-left layout reads `left` as the start edge
212
- const horizontal = Platform.OS !== "web" && I18nManager.isRTL && I18nManager.doLeftAndRightSwapInRTL ? { right: left } : { left };
236
+ const swap = Platform.OS !== "web" && I18nManager.isRTL && I18nManager.doLeftAndRightSwapInRTL;
237
+ const horizontal = swap ? { right: left } : { left };
213
238
  const maxHeight = window.height - topEdge - bottomEdge;
214
239
  const timing = overlayTiming(theme, "menu");
215
240
  const moveTo = (value, to, enter, done) => {
@@ -282,7 +307,22 @@ function IOSMenuSurface({ open, anchor, onClose, align = "start", width, check =
282
307
  const subTop = sub == null || subHeight == null
283
308
  ? -window.height - 1000
284
309
  : Math.max(topEdge, Math.min(sub.y - gutter, window.height - bottomEdge - subHeight));
285
- return (_jsx(Overlay, { open: open, onDismiss: onClose, motion: "menu", testID: testID, children: (progress, opening) => (_jsxs(_Fragment, { children: [_jsx(Animated.View, { ref: panel, ...(web ? { tabIndex: -1 } : null), accessibilityRole: "menu", "aria-label": ariaLabel, ...keysOf(panel), onLayout: (event) => setSize({ width: event.nativeEvent.layout.width, height: event.nativeEvent.layout.height }), style: [
310
+ return (_jsx(Overlay, { open: open, onDismiss: onClose, motion: "menu", testID: testID, children: (progress, opening) => (_jsxs(_Fragment, { children: [context ? (_jsxs(_Fragment, { children: [_jsx(Animated.View, { pointerEvents: "none", style: [
311
+ StyleSheet.absoluteFill,
312
+ { backgroundColor: theme.color("--context-menu-dimming-background", theme.scheme === "dark" ? "rgba(0, 0, 0, 0.5)" : "rgba(0, 0, 0, 0.2)"), opacity: progress },
313
+ web ? { backdropFilter: "blur(30px)", WebkitBackdropFilter: "blur(30px)" } : null,
314
+ ] }), _jsx(View, { pointerEvents: "none", style: [
315
+ styles.preview,
316
+ swap ? { right: box.x } : { left: box.x },
317
+ {
318
+ top: previewTop,
319
+ width: box.width,
320
+ height: box.height,
321
+ borderRadius: context.radius,
322
+ backgroundColor: theme.colors.groupedSurface,
323
+ boxShadow: String(theme.token("--context-menu-preview-shadow") ?? "0 0 32px rgba(0, 0, 0, 0.2)"),
324
+ },
325
+ ], children: context.preview })] })) : null, _jsx(Animated.View, { ref: panel, ...(web ? { tabIndex: -1 } : null), accessibilityRole: "menu", "aria-label": ariaLabel, ...keysOf(panel), onLayout: (event) => setSize({ width: event.nativeEvent.layout.width, height: event.nativeEvent.layout.height }), style: [
286
326
  styles.surface,
287
327
  horizontal,
288
328
  {
@@ -730,7 +770,7 @@ const focusVisible = (node) => {
730
770
  function PopupCheck({ color }) {
731
771
  return (_jsx(Svg, { width: 26, height: 26, viewBox: "0 0 26 26", children: _jsx(Path, { d: "M7.54 13.04 L11.5 17 L20.41 8.09", stroke: color, strokeWidth: 1.9, strokeLinecap: "round", strokeLinejoin: "round", fill: "none" }) }));
732
772
  }
733
- function MenuItem(props) {
773
+ export function MenuItem(props) {
734
774
  return _jsx(MenuRow, { ...props });
735
775
  }
736
776
  MenuItem.displayName = "Menu.Item";
@@ -850,7 +890,7 @@ MenuSubTrigger.displayName = "Menu.SubTrigger";
850
890
  /** The items of a submenu; they show in the menu's panel once its trigger is pressed. */
851
891
  const MenuSubContent = () => null;
852
892
  MenuSubContent.displayName = "Menu.SubContent";
853
- function MenuSeparator() {
893
+ export function MenuSeparator() {
854
894
  const theme = useTheme();
855
895
  const surface = useContext(SurfaceContext);
856
896
  // Android parts a popup's groups with an 8dp band: a light gray (#F5F5F5, #262626 at night) in a row's menu, the text
@@ -870,7 +910,7 @@ function MenuSeparator() {
870
910
  }
871
911
  MenuSeparator.displayName = "Menu.Separator";
872
912
  /** A heading over a group of items. */
873
- function MenuLabel({ children }) {
913
+ export function MenuLabel({ children }) {
874
914
  const theme = useTheme();
875
915
  const surface = useContext(SurfaceContext);
876
916
  if (theme.isAndroid) {
@@ -905,6 +945,7 @@ export const Menu = Object.assign(MenuRoot, {
905
945
  SubContent: MenuSubContent,
906
946
  });
907
947
  const styles = StyleSheet.create({
948
+ preview: { position: "absolute", overflow: "hidden" },
908
949
  trigger: { alignSelf: "flex-start" },
909
950
  surface: { position: "absolute", outlineWidth: 0, outlineStyle: "none" },
910
951
  shrink: { flexShrink: 1 },
package/dist/es/index.js CHANGED
@@ -25,6 +25,8 @@ export { TagInput } from "./components/TagInput/TagInput";
25
25
  export { OTPInput } from "./components/OTPInput/OTPInput";
26
26
  export { Select } from "./components/Select/Select";
27
27
  export { Menu } from "./components/Menu/Menu";
28
+ export { ContextMenu } from "./components/ContextMenu/ContextMenu";
29
+ export { DatePicker } from "./components/DatePicker/DatePicker";
28
30
  export { ActionSheet } from "./components/ActionSheet/ActionSheet";
29
31
  export { BottomSheet } from "./components/BottomSheet/BottomSheet";
30
32
  export { Dialog } from "./components/Dialog/Dialog";