Open History to see the last commit
1import {
2 isLiquidGlassSupported,
3 LiquidGlassContainerView,
4 LiquidGlassView,
5} from "@callstack/liquid-glass";
6import { useEffect, useState } from "react";
7import { Text as SystemText, View } from "react-native";
8import Animated, {
9 Easing,
10 FadeIn,
11 FadeOut,
12 ReduceMotion,
13 useAnimatedStyle,
14 useSharedValue,
15 withTiming,
16} from "react-native-reanimated";
17import { withUniwind } from "uniwind";
18
19import { AppText as Text } from "../../components/AppText";
20import { ControlPill } from "../../components/ControlPill";
21
22const CONTROL_HEIGHT = 44;
23const CONTROL_COMPOSER_GAP = 8;
24const GLASS_MERGE_SPACING = 12;
25const CONTROL_ENTERING = FadeIn.duration(180).reduceMotion(ReduceMotion.System);
26const CONTROL_EXITING = FadeOut.duration(120).reduceMotion(ReduceMotion.System);
27const CONTROL_TIMING = {
28 duration: 240,
29 easing: Easing.out(Easing.cubic),
30 reduceMotion: ReduceMotion.System,
31} as const;
32const CONTROL_SEPARATION = (16 + CONTROL_HEIGHT) / 2;
33
34const UniwindLiquidGlassView = withUniwind(LiquidGlassView);
35const UniwindLiquidGlassContainerView = withUniwind(LiquidGlassContainerView);
36const AnimatedLiquidGlassView = Animated.createAnimatedComponent(UniwindLiquidGlassView);
37
38export const FLOATING_WORKING_CONTROL_COVERAGE = CONTROL_HEIGHT + CONTROL_COMPOSER_GAP;
39
40export function FloatingWorkingControl(props: {
41 readonly colorScheme: "light" | "dark";
42 readonly startedAt: string | null;
43 readonly showScrollToEnd: boolean;
44 readonly onScrollToEnd: () => void;
45}) {
46 const separationProgress = useSharedValue(props.showScrollToEnd ? 1 : 0);
47
48 useEffect(() => {
49 separationProgress.value = withTiming(props.showScrollToEnd ? 1 : 0, CONTROL_TIMING);
50 }, [props.showScrollToEnd, separationProgress]);
51
52 const timerStyle = useAnimatedStyle(() => ({
53 transform: [{ translateX: CONTROL_SEPARATION * (1 - separationProgress.value) }],
54 }));
55 const arrowTransformStyle = useAnimatedStyle(() => ({
56 transform: [{ translateX: -CONTROL_SEPARATION * (1 - separationProgress.value) }],
57 }));
58 const arrowContentStyle = useAnimatedStyle(() => ({
59 opacity: separationProgress.value,
60 }));
61
62 if (props.startedAt === null && !props.showScrollToEnd) {
63 return null;
64 }
65
66 return (
67 <Animated.View
68 pointerEvents="box-none"
69 className="absolute left-0 right-0 z-20 items-center"
70 style={{ top: -FLOATING_WORKING_CONTROL_COVERAGE }}
71 entering={isLiquidGlassSupported ? undefined : CONTROL_ENTERING}
72 exiting={isLiquidGlassSupported ? undefined : CONTROL_EXITING}
73 >
74 {props.startedAt !== null && isLiquidGlassSupported ? (
75 <UniwindLiquidGlassContainerView
76 spacing={GLASS_MERGE_SPACING}
77 pointerEvents="box-none"
78 className="flex-row items-center gap-4"
79 >
80 <AnimatedLiquidGlassView
81 colorScheme={props.colorScheme}
82 effect="regular"
83 pointerEvents="none"
84 className="h-11 justify-center overflow-hidden rounded-full"
85 style={timerStyle}
86 >
87 <WorkingDuration startedAt={props.startedAt} />
88 </AnimatedLiquidGlassView>
89
90 <AnimatedLiquidGlassView
91 colorScheme={props.colorScheme}
92 effect="regular"
93 interactive
94 pointerEvents={props.showScrollToEnd ? "auto" : "none"}
95 accessibilityElementsHidden={!props.showScrollToEnd}
96 importantForAccessibility={props.showScrollToEnd ? "auto" : "no-hide-descendants"}
97 className="h-11 w-11 items-center justify-center overflow-hidden rounded-full"
98 style={arrowTransformStyle}
99 >
100 <Animated.View style={arrowContentStyle}>
101 <ScrollToEndButton disabled={!props.showScrollToEnd} onPress={props.onScrollToEnd} />
102 </Animated.View>
103 </AnimatedLiquidGlassView>
104 </UniwindLiquidGlassContainerView>
105 ) : props.startedAt !== null ? (
106 <View pointerEvents="box-none" className="flex-row items-center gap-4">
107 <Animated.View
108 pointerEvents="none"
109 className="h-11 justify-center rounded-full border border-border bg-card shadow-md shadow-black/10"
110 style={timerStyle}
111 >
112 <WorkingDuration startedAt={props.startedAt} />
113 </Animated.View>
114
115 <Animated.View
116 pointerEvents={props.showScrollToEnd ? "auto" : "none"}
117 accessibilityElementsHidden={!props.showScrollToEnd}
118 importantForAccessibility={props.showScrollToEnd ? "auto" : "no-hide-descendants"}
119 style={[arrowTransformStyle, arrowContentStyle]}
120 >
121 <ControlPill
122 accessibilityLabel="Scroll to end"
123 activateOnPressIn
124 className="h-11 w-11 border border-border bg-card shadow-md shadow-black/10"
125 disabled={!props.showScrollToEnd}
126 icon={{ ios: "chevron.down", android: "keyboard_arrow_down" }}
127 onPress={props.onScrollToEnd}
128 />
129 </Animated.View>
130 </View>
131 ) : isLiquidGlassSupported ? (
132 <UniwindLiquidGlassView
133 colorScheme={props.colorScheme}
134 effect="regular"
135 interactive
136 className="h-11 w-11 items-center justify-center overflow-hidden rounded-full"
137 >
138 <ScrollToEndButton onPress={props.onScrollToEnd} />
139 </UniwindLiquidGlassView>
140 ) : (
141 <ControlPill
142 accessibilityLabel="Scroll to end"
143 activateOnPressIn
144 className="h-11 w-11 border border-border bg-card shadow-md shadow-black/10"
145 icon={{ ios: "chevron.down", android: "keyboard_arrow_down" }}
146 onPress={props.onScrollToEnd}
147 />
148 )}
149 </Animated.View>
150 );
151}
152
153function WorkingDuration(props: { readonly startedAt: string }) {
154 const [nowMs, setNowMs] = useState(() => Date.now());
155
156 useEffect(() => {
157 setNowMs(Date.now());
158 const intervalId = setInterval(() => setNowMs(Date.now()), 1_000);
159 return () => clearInterval(intervalId);
160 }, [props.startedAt]);
161
162 const duration = formatWorkingDuration(props.startedAt, nowMs);
163 const label = `Working for ${duration}`;
164
165 return (
166 <View accessible accessibilityLabel={label} className="h-11 flex-row items-center px-4">
167 <Text className="font-t3-medium text-xs text-foreground">Working for </Text>
168 <SystemText
169 className="text-xs text-foreground"
170 style={{ fontVariant: ["tabular-nums"], fontWeight: "500" }}
171 >
172 {duration}
173 </SystemText>
174 </View>
175 );
176}
177
178function formatWorkingDuration(startedAt: string, nowMs: number): string {
179 const startedAtMs = Date.parse(startedAt);
180 if (!Number.isFinite(startedAtMs) || nowMs <= startedAtMs) {
181 return "0s";
182 }
183
184 const totalSeconds = Math.floor((nowMs - startedAtMs) / 1_000);
185 if (totalSeconds < 60) {
186 return `${totalSeconds}s`;
187 }
188
189 const minutes = Math.floor(totalSeconds / 60);
190 const seconds = String(totalSeconds % 60).padStart(2, "0");
191 return `${minutes}m ${seconds}s`;
192}
193
194function ScrollToEndButton(props: { readonly disabled?: boolean; readonly onPress: () => void }) {
195 return (
196 <ControlPill
197 accessibilityLabel="Scroll to end"
198 activateOnPressIn
199 className="h-11 w-11 bg-transparent"
200 disabled={props.disabled}
201 icon={{ ios: "chevron.down", android: "keyboard_arrow_down" }}
202 onPress={props.onPress}
203 />
204 );
205}