Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

floating-working-control.tsx

208 lines7.5 KB

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