TypeScript
feat(mobile): share actionable toasts and native progress (#19952)
Julius Marminge487423a320fa
diff --git a/apps/mobile/src/components/LoadingStrip.tsx b/apps/mobile/src/components/LoadingStrip.tsxindex 9c16e1c68e7..0d62897e930 100644--- a/apps/mobile/src/components/LoadingStrip.tsx+++ b/apps/mobile/src/components/LoadingStrip.tsx@@ -1,93 +1,16 @@-import { useEffect, useState } from "react";import { View } from "react-native";-import Animated, {- cancelAnimation,- Easing,- useAnimatedStyle,- useSharedValue,- withRepeat,- withTiming,-} from "react-native-reanimated";+import { Progress } from "./Progress";-const INDICATOR_WIDTH_FRACTION = 0.3;-const MIN_INDICATOR_WIDTH = 48;--function LoadingStripFrame(props: {- readonly children: React.ReactNode;- readonly onLayout?: (width: number) => void;-}) {+/** Thin, noninteractive progress at the top of its owning screen. */+export function LoadingStrip(props: { readonly progress?: number }) {return (<ViewpointerEvents="none"accessibilityElementsHiddenimportantForAccessibility="no-hide-descendants"- className="absolute inset-x-0 top-0 z-10 h-0.5 overflow-hidden"- onLayout={- props.onLayout- ? (event) => {- props.onLayout?.(event.nativeEvent.layout.width);- }- : undefined- }+ className="absolute inset-x-0 top-0 z-10 h-1 overflow-hidden">- {props.children}+ <Progress value={props.progress} /></View>);}--function IndeterminateLoadingStrip() {- const [containerWidth, setContainerWidth] = useState(0);- const travelProgress = useSharedValue(0);- const indicatorWidth = Math.max(MIN_INDICATOR_WIDTH, containerWidth * INDICATOR_WIDTH_FRACTION);-- useEffect(() => {- travelProgress.value = 0;- travelProgress.value = withRepeat(- withTiming(1, {- duration: 1100,- easing: Easing.inOut(Easing.quad),- }),- -1,- false,- );-- return () => {- cancelAnimation(travelProgress);- };- }, [travelProgress]);-- const indicatorStyle = useAnimatedStyle(- () => ({- transform: [- {- translateX: (containerWidth + indicatorWidth) * travelProgress.value - indicatorWidth,- },- ],- width: indicatorWidth,- }),- [containerWidth, indicatorWidth],- );-- return (- <LoadingStripFrame onLayout={setContainerWidth}>- <Animated.View className="h-full rounded-full bg-primary" style={indicatorStyle} />- </LoadingStripFrame>- );-}--export function LoadingStrip(props: { readonly progress?: number }) {- if (props.progress === undefined) {- return <IndeterminateLoadingStrip />;- }-- const clampedProgress = Math.min(1, Math.max(0, props.progress));-- return (- <LoadingStripFrame>- <View- className="h-full rounded-r-full bg-primary"- style={{ width: `${clampedProgress * 100}%` }}- />- </LoadingStripFrame>- );-}diff --git a/apps/mobile/src/components/Progress.android.tsx b/apps/mobile/src/components/Progress.android.tsxnew file mode 100644index 00000000000..652ccef2fa2--- /dev/null+++ b/apps/mobile/src/components/Progress.android.tsx@@ -0,0 +1,34 @@+import { LinearProgressIndicator } from "@expo/ui/jetpack-compose";+import { fillMaxWidth } from "@expo/ui/jetpack-compose/modifiers";+import { View } from "react-native";+import { NativeThemeHost } from "./NativeThemeHost.android";+import { useControlTheme } from "./useControlTheme";+import { clampProgress, type ProgressProps } from "./Progress.types";++export function Progress(props: ProgressProps) {+ const value = clampProgress(props.value);+ const { colors } = useControlTheme();+ return (+ <View+ className={props.className}+ accessible+ accessibilityRole="progressbar"+ accessibilityLabel={props.label}+ accessibilityValue={+ value === undefined ? undefined : { min: 0, max: 100, now: Math.round(value * 100) }+ }+ accessibilityState={{ busy: value === undefined }}+ >+ <View importantForAccessibility="no-hide-descendants">+ <NativeThemeHost matchContents={{ vertical: true }} style={{ width: "100%" }}>+ <LinearProgressIndicator+ progress={value}+ color={colors["--color-primary"]}+ trackColor={colors["--color-subtle"]}+ modifiers={[fillMaxWidth()]}+ />+ </NativeThemeHost>+ </View>+ </View>+ );+}diff --git a/apps/mobile/src/components/Progress.ios.tsx b/apps/mobile/src/components/Progress.ios.tsxnew file mode 100644index 00000000000..f342d1cfb03--- /dev/null+++ b/apps/mobile/src/components/Progress.ios.tsx@@ -0,0 +1,30 @@+import { ProgressView } from "@expo/ui/swift-ui";+import { progressViewStyle, tint } from "@expo/ui/swift-ui/modifiers";+import { View } from "react-native";+import { NativeThemeHost } from "./NativeThemeHost";+import { useControlTheme } from "./useControlTheme";+import { clampProgress, type ProgressProps } from "./Progress.types";++export function Progress(props: ProgressProps) {+ const value = clampProgress(props.value);+ const { colors } = useControlTheme();+ return (+ <View+ className={props.className}+ accessible+ accessibilityRole="progressbar"+ accessibilityLabel={props.label}+ accessibilityValue={+ value === undefined ? undefined : { min: 0, max: 100, now: Math.round(value * 100) }+ }+ accessibilityState={{ busy: value === undefined }}+ >+ <NativeThemeHost matchContents={{ vertical: true }} style={{ width: "100%" }}>+ <ProgressView+ value={value}+ modifiers={[progressViewStyle("linear"), tint(colors["--color-primary"])]}+ />+ </NativeThemeHost>+ </View>+ );+}diff --git a/apps/mobile/src/components/Progress.tsx b/apps/mobile/src/components/Progress.tsxnew file mode 100644index 00000000000..5f1accf71a5--- /dev/null+++ b/apps/mobile/src/components/Progress.tsx@@ -0,0 +1,2 @@+export { Progress } from "./Progress.ios";+export type { ProgressProps } from "./Progress.types";diff --git a/apps/mobile/src/components/Progress.types.ts b/apps/mobile/src/components/Progress.types.tsnew file mode 100644index 00000000000..98f70bf55f1--- /dev/null+++ b/apps/mobile/src/components/Progress.types.ts@@ -0,0 +1,12 @@+export interface ProgressProps {+ /** Omit for indeterminate progress. Finite values are clamped to 0..1. */+ readonly value?: number;+ readonly label?: string;+ readonly className?: string;+}++export function clampProgress(value: number | undefined): number | undefined {+ return value === undefined || !Number.isFinite(value)+ ? undefined+ : Math.max(0, Math.min(1, value));+}diff --git a/apps/mobile/src/components/Toast.tsx b/apps/mobile/src/components/Toast.tsxnew file mode 100644index 00000000000..08ced0ff051--- /dev/null+++ b/apps/mobile/src/components/Toast.tsx@@ -0,0 +1,56 @@+import { useEffect } from "react";+import { AccessibilityInfo, Platform, View } from "react-native";++import { AppText } from "./AppText";+import { Button } from "./Button";+import { GlassSurface } from "./GlassSurface";+import { IconButton } from "./IconButton";+import { AppActivityIndicator } from "./AppActivityIndicator";+import { SymbolView } from "./AppSymbol";++export interface ToastProps {+ readonly message: string;+ readonly description?: string;+ readonly tone?: "default" | "success" | "danger";+ readonly busy?: boolean;+ readonly action?: { readonly label: string; readonly onPress: () => void };+ readonly onDismiss?: () => void;+}++/** The owning screen places and dismisses feedback; actions are never implicit. */+export function Toast(props: ToastProps) {+ useEffect(() => {+ if (Platform.OS === "ios") AccessibilityInfo.announceForAccessibility(props.message);+ }, [props.message]);+ return (+ <GlassSurface className="p-3">+ <View className="flex-row items-center gap-3">+ {props.busy ? (+ <AppActivityIndicator />+ ) : (+ <SymbolView+ name={props.tone === "danger" ? "exclamationmark.triangle" : "checkmark.circle"}+ size={20}+ tintColorClassName={+ props.tone === "danger" ? "accent-danger-foreground" : "accent-icon"+ }+ />+ )}+ <View className="min-w-0 flex-1" accessibilityLiveRegion="polite">+ <AppText className="text-sm font-t3-semibold">{props.message}</AppText>+ {props.description ? (+ <AppText className="text-xs text-foreground-muted">{props.description}</AppText>+ ) : null}+ </View>+ {props.onDismiss ? (+ <IconButton accessibilityLabel="Dismiss message" icon="xmark" onPress={props.onDismiss} />+ ) : null}+ </View>+ {props.action ? (+ <View className="mt-2 self-end">+ <Button label={props.action.label} onPress={props.action.onPress} />+ </View>+ ) : null}+ </GlassSurface>+ );+}diff --git a/apps/mobile/src/features/development/ComponentCatalogScreen.tsx b/apps/mobile/src/features/development/ComponentCatalogScreen.tsxindex d428bfba325..90e86ea16a9 100644--- a/apps/mobile/src/features/development/ComponentCatalogScreen.tsx+++ b/apps/mobile/src/features/development/ComponentCatalogScreen.tsx@@ -1,3 +1,5 @@+import { Toast } from "../../components/Toast";+import { Progress } from "../../components/Progress";import { useNavigation } from "@react-navigation/native";import { useRef, useState } from "react";import { View, type TextInputInstance } from "react-native";@@ -15,7 +17,6 @@ import { Button } from "../../components/Button";import { IconButton } from "../../components/IconButton";import { FloatingActionButton } from "../../components/FloatingActionButton";import { SearchField } from "../../components/SearchField";-import { LoadingStrip } from "../../components/LoadingStrip";import { Notice } from "../../components/Notice";import { NumberField } from "../../components/NumberField";import { AppBar } from "../../components/AppBar";@@ -203,10 +204,16 @@ export function ComponentCatalogScreen() {<AppText className="text-lg font-t3-semibold">Feedback and presentation</AppText><Notice message="This action could not finish. Try again." tone="danger" />{notice ? (- <Notice+ <Toastmessage="Changes are ready to review."- icon="checkmark.circle"- floating+ tone="success"+ action={{+ label: "Undo",+ onPress: () => {+ setNotice(false);+ setText("Restored example");+ },+ }}onDismiss={() => setNotice(false)}/>) : (@@ -239,9 +246,8 @@ export function ComponentCatalogScreen() {<Chip label="Menu" trailingIcon="chevron.down" /></Menu></View>- <View className="relative h-3 overflow-hidden rounded-full bg-subtle">- <LoadingStrip progress={0.6} />- </View>+ <Progress value={0.6} label="Example upload" />+ <Progress label="Connecting" /><View className="items-start"><FloatingActionButtonlabel="New thread"diff --git a/apps/mobile/src/features/threads/GitActionProgressOverlay.tsx b/apps/mobile/src/features/threads/GitActionProgressOverlay.tsxindex 29491ff4d76..7faf434bd2a 100644--- a/apps/mobile/src/features/threads/GitActionProgressOverlay.tsx+++ b/apps/mobile/src/features/threads/GitActionProgressOverlay.tsx@@ -1,24 +1,12 @@import { tryOpenExternalUrl } from "../../lib/openExternalUrl";import * as Haptics from "expo-haptics";-import { GlassView } from "expo-glass-effect";-import { AppActivityIndicator } from "../../components/AppActivityIndicator";-import { SymbolView } from "../../components/AppSymbol";-import { useCallback, useEffect, useRef } from "react";-import { Pressable, StyleSheet, useColorScheme, View } from "react-native";-import Animated, { FadeIn, FadeOut, LinearTransition } from "react-native-reanimated";+import { useEffect, useRef } from "react";+import Animated, { FadeIn, FadeOut, ReduceMotion } from "react-native-reanimated";import { useSafeAreaInsets } from "react-native-safe-area-context";--import { AppText as Text } from "../../components/AppText";+import { Toast } from "../../components/Toast";import { APP_BAR_HEIGHT } from "../../lib/layoutMetrics";-import { themeColorWithAlpha } from "../../lib/mobileTheme";-import { useUniwindTheme } from "../../lib/useUniwindTheme";-import { NATIVE_LIQUID_GLASS_SUPPORTED } from "../../native/native-glass";import type { GitActionProgress } from "../../state/use-vcs-action-state";-const OVERLAY_LAYOUT_TRANSITION = LinearTransition.duration(220);-const OVERLAY_TOP_GAP = 8;-const AnimatedGlassView = Animated.createAnimatedComponent(GlassView);-export function GitActionProgressOverlay(props: {readonly progress: GitActionProgress;readonly onDismiss: () => void;@@ -40,7 +28,7 @@ export function GitActionProgressOverlay(props: {}}, [progress.phase]);- const handlePress = useCallback(() => {+ const handlePress = () => {if (progress.prUrl) {void (openPullRequest? openPullRequest(progress.prUrl)@@ -50,7 +38,7 @@ export function GitActionProgressOverlay(props: {if (progress.phase === "success" || progress.phase === "error") {onDismiss();}- }, [onDismiss, progress.phase, progress.prUrl, openPullRequest]);+ };if (progress.phase === "idle") {return null;@@ -58,130 +46,26 @@ export function GitActionProgressOverlay(props: {return (<Animated.View- entering={NATIVE_LIQUID_GLASS_SUPPORTED ? undefined : FadeIn.duration(200)}- exiting={FadeOut.duration(150)}+ entering={FadeIn.duration(200).reduceMotion(ReduceMotion.System)}+ exiting={FadeOut.duration(150).reduceMotion(ReduceMotion.System)}className="absolute inset-x-3 z-[100]"- style={{ top: insets.top + APP_BAR_HEIGHT + OVERLAY_TOP_GAP }}+ style={{ top: insets.top + APP_BAR_HEIGHT + 8 }}pointerEvents="box-none">- <Pressable onPress={handlePress}>- <OverlayContent progress={progress} />- </Pressable>+ <Toast+ message={progress.label ?? "Source control"}+ description={progress.description ?? undefined}+ tone={+ progress.phase === "error"+ ? "danger"+ : progress.phase === "success"+ ? "success"+ : "default"+ }+ busy={progress.phase === "running"}+ action={progress.prUrl ? { label: "Open PR", onPress: handlePress } : undefined}+ onDismiss={progress.phase === "running" ? undefined : onDismiss}+ /></Animated.View>);}--function OverlayContent(props: { readonly progress: GitActionProgress }) {- const { progress } = props;- const colors = useUniwindTheme();- const glassSurface = colors["--color-glass-surface"];- const foreground = colors["--color-foreground"];- const shadowColor = colors["--color-primary-shadow"];- const isDarkMode = useColorScheme() === "dark";- const glassTint = themeColorWithAlpha(String(glassSurface), isDarkMode ? 0.48 : 0.38);- const glassBorder = themeColorWithAlpha(String(foreground), isDarkMode ? 0.18 : 0.12);- const content = (- <>- <OverlayIcon phase={progress.phase} />-- <View className="flex-1 gap-0.5">- {progress.label ? (- <Text className="text-sm font-t3-bold text-foreground" numberOfLines={1}>- {progress.label}- </Text>- ) : null}- {progress.description ? (- <Text className="text-2xs text-foreground-muted" numberOfLines={1}>- {progress.description}- </Text>- ) : null}- </View>-- {progress.prUrl ? (- <SymbolView- name="arrow.up.right"- size={13}- tintColorClassName={"accent-icon"}- type="monochrome"- />- ) : null}- </>- );-- if (NATIVE_LIQUID_GLASS_SUPPORTED) {- return (- <Animated.View- layout={OVERLAY_LAYOUT_TRANSITION}- style={{- borderCurve: "continuous",- borderRadius: 26,- elevation: 12,- shadowColor,- shadowOffset: { width: 0, height: 8 },- shadowOpacity: isDarkMode ? 0.42 : 0.2,- shadowRadius: 18,- }}- >- <AnimatedGlassView- colorScheme={isDarkMode ? "dark" : "light"}- glassEffectStyle="regular"- isInteractive- layout={OVERLAY_LAYOUT_TRANSITION}- tintColor={glassTint}- style={{- borderColor: glassBorder,- borderCurve: "continuous",- borderRadius: 26,- borderWidth: StyleSheet.hairlineWidth,- overflow: "hidden",- }}- >- <Animated.View- entering={FadeIn.delay(60).duration(140)}- className="flex-row items-center gap-2.5 px-3.5 py-3"- >- {content}- </Animated.View>- </AnimatedGlassView>- </Animated.View>- );- }-- const bgClass =- progress.phase === "error" ? "border-danger-border bg-danger" : "bg-card border-border";-- return (- <Animated.View- layout={OVERLAY_LAYOUT_TRANSITION}- className={`flex-row items-center gap-2.5 rounded-[26px] border border-continuous px-3.5 py-3 shadow-lg shadow-black/10 ${bgClass}`}- >- {content}- </Animated.View>- );-}--function OverlayIcon(props: { readonly phase: GitActionProgress["phase"] }) {- switch (props.phase) {- case "running":- return <AppActivityIndicator size="small" colorClassName={"accent-icon"} />;- case "success":- return (- <View className="h-6 w-6 items-center justify-center rounded-full bg-green-500">- <SymbolView name="checkmark" size={12} tintColor="white" type="monochrome" />- </View>- );- case "error":- return (- <View className="h-6 w-6 items-center justify-center rounded-full bg-danger">- <SymbolView- name="exclamationmark.triangle"- size={12}- tintColorClassName="accent-danger-foreground"- type="monochrome"- />- </View>- );- default:- return null;- }-}