Skip to content

Commit 6e03b8b

Browse files
feat(mobile): use compact themed custom snooze popups
1 parent 51ca00e commit 6e03b8b

4 files changed

Lines changed: 364 additions & 102 deletions

File tree

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import { Platform, Pressable, View } from "react-native";
2+
import Animated, { Easing, LinearTransition, ReduceMotion } from "react-native-reanimated";
3+
import { AppText as Text } from "./AppText";
4+
import { cn } from "../lib/cn";
5+
6+
export function SegmentedControl<Value extends number | string>(props: {
7+
readonly options: readonly {
8+
readonly value: Value;
9+
readonly label: string;
10+
readonly accessibilityLabel?: string;
11+
}[];
12+
readonly selected: Value;
13+
readonly onSelect: (value: Value) => void;
14+
/** The tab bar is full height; filters under it are shorter so it stays primary. */
15+
readonly size?: "default" | "compact";
16+
/** "tab" for the view switcher; filters stay plain buttons. */
17+
readonly role?: "tab" | "button";
18+
readonly className?: string;
19+
}) {
20+
const compact = props.size === "compact";
21+
return (
22+
<View
23+
accessible={false}
24+
className={cn(
25+
"flex-row overflow-hidden rounded-full border-continuous bg-card",
26+
props.className,
27+
)}
28+
>
29+
<Animated.View
30+
pointerEvents="none"
31+
layout={LinearTransition.duration(200)
32+
.easing(Easing.out(Easing.cubic))
33+
.reduceMotion(ReduceMotion.System)}
34+
className="absolute bottom-0 top-0 rounded-full bg-subtle-strong"
35+
style={{
36+
width: `${100 / props.options.length}%`,
37+
start: `${
38+
(Math.max(
39+
0,
40+
props.options.findIndex((option) => option.value === props.selected),
41+
) *
42+
100) /
43+
props.options.length
44+
}%`,
45+
}}
46+
/>
47+
{props.options.map((option) => {
48+
const active = option.value === props.selected;
49+
return (
50+
<Pressable
51+
key={String(option.value)}
52+
accessibilityRole={Platform.OS === "ios" ? "button" : (props.role ?? "button")}
53+
accessibilityLabel={option.accessibilityLabel ?? option.label}
54+
accessibilityState={{ selected: active }}
55+
onPress={() => props.onSelect(option.value)}
56+
className={cn(
57+
"flex-1 items-center justify-center rounded-full",
58+
compact ? "h-9" : "h-11",
59+
)}
60+
>
61+
<Text
62+
className={cn(
63+
compact ? "text-xs" : "text-sm",
64+
active ? "font-t3-medium text-foreground" : "text-foreground-muted",
65+
)}
66+
>
67+
{option.label}
68+
</Text>
69+
</Pressable>
70+
);
71+
})}
72+
</View>
73+
);
74+
}
Lines changed: 268 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,268 @@
1+
import {
2+
Button,
3+
DatePicker,
4+
Host,
5+
HStack,
6+
Menu,
7+
Picker,
8+
Popover,
9+
Spacer,
10+
Text,
11+
VStack,
12+
} from "@expo/ui/swift-ui";
13+
import {
14+
background,
15+
buttonStyle,
16+
datePickerStyle,
17+
font,
18+
foregroundStyle,
19+
frame,
20+
padding,
21+
pickerStyle,
22+
presentationBackground,
23+
shapes,
24+
tag,
25+
} from "@expo/ui/swift-ui/modifiers";
26+
import {
27+
localSnoozeDate,
28+
localSnoozeTime,
29+
resolveCustomSnooze,
30+
type CustomSnoozeInput,
31+
} from "@t3tools/client-runtime/state/thread-settled";
32+
import { useState } from "react";
33+
import { Modal, Pressable, ScrollView, View } from "react-native";
34+
import { AppText } from "../../components/AppText";
35+
import { SegmentedControl } from "../../components/SegmentedControl";
36+
import { useAppearancePreferences } from "../settings/appearance/AppearancePreferencesProvider";
37+
38+
const durationAmounts = Array.from({ length: 99 }, (_, index) => index + 1);
39+
const modes = [
40+
{ value: "date", label: "Date and time" },
41+
{ value: "duration", label: "Duration" },
42+
] as const;
43+
const units = [
44+
{ value: "minutes", label: "Minutes" },
45+
{ value: "hours", label: "Hours" },
46+
{ value: "days", label: "Days" },
47+
] as const;
48+
49+
export function CustomSnoozeSheet(props: {
50+
readonly onClose: () => void;
51+
readonly onSnooze: (snoozedUntil: string) => void;
52+
}) {
53+
const [mode, setMode] = useState<CustomSnoozeInput["mode"]>("date");
54+
const [date, setDate] = useState(() => new Date(Date.now() + 3_600_000));
55+
const [amount, setAmount] = useState(2);
56+
const [amountOpen, setAmountOpen] = useState(false);
57+
const [unit, setUnit] = useState<"minutes" | "hours" | "days">("hours");
58+
const [error, setError] = useState<string | null>(null);
59+
const { themeVariables: colors, themeAppearance, appearance } = useAppearancePreferences();
60+
const updateDate = (value: Date) => {
61+
setDate(value);
62+
setError(null);
63+
};
64+
65+
const submit = () => {
66+
const input: CustomSnoozeInput =
67+
mode === "date"
68+
? { mode, date: localSnoozeDate(date), time: localSnoozeTime(date) }
69+
: { mode, amount: String(amount), unit };
70+
const snoozedUntil = resolveCustomSnooze(input, new Date());
71+
if (!snoozedUntil) {
72+
setError(
73+
mode === "date" ? "Choose a date and time in the future." : "Enter a positive duration.",
74+
);
75+
return;
76+
}
77+
props.onSnooze(snoozedUntil);
78+
props.onClose();
79+
};
80+
81+
return (
82+
<Modal visible transparent animationType="fade" onRequestClose={props.onClose}>
83+
<View className="flex-1 items-center justify-center bg-black/40 px-6">
84+
<ScrollView
85+
className="max-h-[80%] w-full max-w-sm grow-0 rounded-3xl border border-border bg-sheet-solid"
86+
contentContainerStyle={{ padding: 20, gap: 16 }}
87+
>
88+
<View className="flex-row items-center justify-between gap-2">
89+
<Pressable accessibilityRole="button" onPress={props.onClose} hitSlop={10}>
90+
<AppText className="text-base">Cancel</AppText>
91+
</Pressable>
92+
<AppText accessibilityRole="header" className="text-base font-t3-semibold">
93+
Custom snooze
94+
</AppText>
95+
<Pressable accessibilityRole="button" onPress={submit} hitSlop={10}>
96+
<AppText className="text-base text-primary">Snooze</AppText>
97+
</Pressable>
98+
</View>
99+
<SegmentedControl
100+
options={modes}
101+
selected={mode}
102+
onSelect={(value) => {
103+
setMode(value);
104+
setError(null);
105+
}}
106+
role="tab"
107+
/>
108+
<Host
109+
matchContents={{ vertical: true }}
110+
style={{ width: "100%" }}
111+
colorScheme={themeAppearance}
112+
seedColor={colors["--color-primary"]}
113+
>
114+
<HStack
115+
spacing={6}
116+
modifiers={[
117+
frame({ minHeight: 44 }),
118+
foregroundStyle(colors["--color-foreground"]),
119+
font({ size: appearance.baseFontSize }),
120+
]}
121+
>
122+
<Text>{mode === "date" ? "Until" : "Snooze for"}</Text>
123+
<Spacer />
124+
{mode === "date" ? (
125+
<>
126+
<SnoozeDateField date={date} component="date" onChange={updateDate} />
127+
<SnoozeDateField date={date} component="hourAndMinute" onChange={updateDate} />
128+
</>
129+
) : (
130+
<>
131+
<Popover isPresented={amountOpen} onIsPresentedChange={setAmountOpen}>
132+
<Popover.Trigger>
133+
<Button
134+
onPress={() => setAmountOpen(true)}
135+
modifiers={[buttonStyle("plain")]}
136+
>
137+
<FieldLabel label={String(amount)} />
138+
</Button>
139+
</Popover.Trigger>
140+
<Popover.Content>
141+
<VStack
142+
modifiers={[
143+
padding({ all: 12 }),
144+
frame({ width: 180 }),
145+
presentationBackground(colors["--color-sheet-solid"]),
146+
]}
147+
>
148+
<Picker
149+
label="Duration"
150+
selection={amount}
151+
onSelectionChange={(value: number) => {
152+
setAmount(value);
153+
setError(null);
154+
}}
155+
modifiers={[pickerStyle("wheel"), frame({ height: 160 })]}
156+
>
157+
{durationAmounts.map((value) => (
158+
<Text
159+
key={value}
160+
modifiers={[
161+
tag(value),
162+
foregroundStyle(colors["--color-foreground"]),
163+
]}
164+
>
165+
{String(value)}
166+
</Text>
167+
))}
168+
</Picker>
169+
<Button
170+
label="Done"
171+
onPress={() => setAmountOpen(false)}
172+
modifiers={[foregroundStyle(colors["--color-primary"])]}
173+
/>
174+
</VStack>
175+
</Popover.Content>
176+
</Popover>
177+
<Menu
178+
label={
179+
<FieldLabel
180+
label={unit === "minutes" ? "Minutes" : unit === "hours" ? "Hours" : "Days"}
181+
/>
182+
}
183+
>
184+
{units.map((option) => (
185+
<Button
186+
key={option.value}
187+
label={option.label}
188+
onPress={() => {
189+
setUnit(option.value);
190+
setError(null);
191+
}}
192+
/>
193+
))}
194+
</Menu>
195+
</>
196+
)}
197+
</HStack>
198+
</Host>
199+
{error && <AppText className="text-sm text-danger-foreground">{error}</AppText>}
200+
</ScrollView>
201+
</View>
202+
</Modal>
203+
);
204+
}
205+
206+
function FieldLabel(props: { readonly label: string }) {
207+
const { themeVariables: colors } = useAppearancePreferences();
208+
return (
209+
<Text
210+
modifiers={[
211+
padding({ horizontal: 12, vertical: 8 }),
212+
foregroundStyle(colors["--color-foreground"]),
213+
background(colors["--color-card"], shapes.capsule()),
214+
]}
215+
>
216+
{props.label}
217+
</Text>
218+
);
219+
}
220+
221+
function SnoozeDateField(props: {
222+
readonly date: Date;
223+
readonly component: "date" | "hourAndMinute";
224+
readonly onChange: (date: Date) => void;
225+
}) {
226+
const [open, setOpen] = useState(false);
227+
const { themeVariables: colors } = useAppearancePreferences();
228+
return (
229+
<Popover isPresented={open} onIsPresentedChange={setOpen}>
230+
<Popover.Trigger>
231+
<Button onPress={() => setOpen(true)} modifiers={[buttonStyle("plain")]}>
232+
<FieldLabel
233+
label={
234+
props.component === "date"
235+
? props.date.toLocaleDateString(undefined, {
236+
month: "short",
237+
day: "numeric",
238+
year: "numeric",
239+
})
240+
: props.date.toLocaleTimeString(undefined, { hour: "numeric", minute: "2-digit" })
241+
}
242+
/>
243+
</Button>
244+
</Popover.Trigger>
245+
<Popover.Content>
246+
<VStack
247+
modifiers={[
248+
padding({ all: 12 }),
249+
frame({ width: props.component === "date" ? 340 : 300 }),
250+
presentationBackground(colors["--color-sheet-solid"]),
251+
]}
252+
>
253+
<DatePicker
254+
selection={props.date}
255+
displayedComponents={[props.component]}
256+
onDateChange={props.onChange}
257+
modifiers={[datePickerStyle(props.component === "date" ? "graphical" : "wheel")]}
258+
/>
259+
<Button
260+
label="Done"
261+
onPress={() => setOpen(false)}
262+
modifiers={[foregroundStyle(colors["--color-primary"])]}
263+
/>
264+
</VStack>
265+
</Popover.Content>
266+
</Popover>
267+
);
268+
}

0 commit comments

Comments
 (0)