Skip to content

Commit 673d3fe

Browse files
fix(mobile): use native Material controls and adaptive settings
1 parent 8af949f commit 673d3fe

26 files changed

Lines changed: 628 additions & 307 deletions

‎apps/mobile/src/components/AndroidScreenHeader.tsx‎

Lines changed: 85 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1-
import type { ReactNode } from "react";
1+
import { useState, type ReactNode } from "react";
22
import { Pressable, View } from "react-native";
33
import { useSafeAreaInsets } from "react-native-safe-area-context";
44

55
import { SymbolView, type AppSymbolName } from "./AppSymbol";
66
import { AppText as Text } from "./AppText";
77
import { cn } from "../lib/cn";
8+
import { useAppearancePreferences } from "../features/settings/appearance/AppearancePreferencesProvider";
9+
import { MaterialIconButton } from "./MaterialIconButton";
10+
import { AndroidAnchoredMenu } from "./AndroidAnchoredMenu";
11+
import { useScaledTextRole } from "../features/settings/appearance/useScaledTextRole";
812

913
export interface AndroidHeaderAction {
1014
readonly accessibilityLabel: string;
@@ -19,6 +23,8 @@ export function AndroidHeaderIconButton(props: {
1923
readonly onPress?: () => void;
2024
readonly disabled?: boolean;
2125
}) {
26+
const { materialYouStyleLayoutActive } = useAppearancePreferences();
27+
if (materialYouStyleLayoutActive) return <MaterialIconButton {...props} />;
2228
return (
2329
<Pressable
2430
accessibilityLabel={props.accessibilityLabel}
@@ -51,48 +57,89 @@ export function AndroidScreenHeader(props: {
5157
readonly hideBottomBorder?: boolean;
5258
}) {
5359
const insets = useSafeAreaInsets();
60+
const titleTypography = useScaledTextRole("title");
61+
const subtitleTypography = useScaledTextRole("label");
62+
const { materialYouStyleLayoutActive } = useAppearancePreferences();
63+
const [headerWidth, setHeaderWidth] = useState(0);
64+
const actions = props.actions ?? [];
65+
const directCount =
66+
materialYouStyleLayoutActive && actions.length > 2
67+
? headerWidth >= 600
68+
? 3
69+
: 1
70+
: actions.length;
71+
const visibleActions = actions.slice(0, directCount);
72+
const overflowActions = actions.slice(directCount);
5473

5574
return (
5675
<View
57-
className="border-b border-header-border bg-header px-3 pb-2.5"
76+
onLayout={(event) => setHeaderWidth(event.nativeEvent.layout.width)}
77+
className={
78+
materialYouStyleLayoutActive
79+
? "border-b border-header-border bg-header px-2 pb-2"
80+
: "border-b border-header-border bg-header px-3 pb-2.5"
81+
}
5882
style={{
5983
paddingTop: props.embedded ? 8 : Math.max(insets.top, 12),
6084
borderBottomWidth: props.hideBottomBorder ? 0 : undefined,
6185
}}
6286
>
63-
<View className="min-h-12 flex-row items-center gap-2">
87+
<View
88+
className={
89+
materialYouStyleLayoutActive
90+
? "min-h-14 flex-row items-center gap-1"
91+
: "min-h-12 flex-row items-center gap-2"
92+
}
93+
>
6494
{props.onBack ? (
65-
<Pressable
66-
accessibilityLabel="Navigate up"
67-
accessibilityRole="button"
68-
hitSlop={8}
69-
onPress={props.onBack}
70-
className="-mr-2 size-11 items-center justify-center"
71-
>
72-
<SymbolView
73-
name="chevron.left"
74-
size={24}
75-
tintColorClassName={"accent-foreground"}
76-
type="monochrome"
95+
materialYouStyleLayoutActive ? (
96+
<MaterialIconButton
97+
accessibilityLabel="Navigate up"
98+
icon="arrow.left"
99+
onPress={props.onBack}
77100
/>
78-
</Pressable>
101+
) : (
102+
<Pressable
103+
accessibilityLabel="Navigate up"
104+
accessibilityRole="button"
105+
hitSlop={8}
106+
onPress={props.onBack}
107+
className="-mr-2 size-11 items-center justify-center"
108+
>
109+
<SymbolView
110+
name="chevron.left"
111+
size={24}
112+
tintColorClassName={"accent-foreground"}
113+
type="monochrome"
114+
/>
115+
</Pressable>
116+
)
79117
) : null}
80118

81119
<View className={cn("min-w-0 flex-1", !props.onBack && "pl-1")}>
82-
<Text numberOfLines={1} className="text-lg font-t3-bold text-foreground">
120+
<Text
121+
numberOfLines={1}
122+
style={materialYouStyleLayoutActive ? titleTypography : undefined}
123+
className={
124+
materialYouStyleLayoutActive
125+
? "text-foreground"
126+
: "text-lg font-t3-bold text-foreground"
127+
}
128+
>
83129
{props.title}
84130
</Text>
85131
{props.subtitle ? (
86132
<Text
87133
numberOfLines={1}
134+
style={materialYouStyleLayoutActive ? subtitleTypography : undefined}
88135
className="mt-px text-[13px] font-t3-medium text-foreground-muted"
89136
>
90137
{props.subtitle}
91138
</Text>
92139
) : null}
93140
</View>
94141

95-
{props.actions?.map((action) => (
142+
{visibleActions.map((action) => (
96143
<AndroidHeaderIconButton
97144
key={action.accessibilityLabel}
98145
accessibilityLabel={action.accessibilityLabel}
@@ -101,6 +148,26 @@ export function AndroidScreenHeader(props: {
101148
onPress={action.onPress}
102149
/>
103150
))}
151+
{overflowActions.length > 0 ? (
152+
<AndroidAnchoredMenu
153+
actions={overflowActions.map((action, index) => ({
154+
id: String(index),
155+
title: action.accessibilityLabel,
156+
attributes: { disabled: Boolean(action.disabled) },
157+
}))}
158+
onPressAction={({ nativeEvent }) =>
159+
overflowActions[Number(nativeEvent.event)]?.onPress()
160+
}
161+
>
162+
{(open) => (
163+
<MaterialIconButton
164+
accessibilityLabel="More actions"
165+
icon="ellipsis"
166+
onPress={open}
167+
/>
168+
)}
169+
</AndroidAnchoredMenu>
170+
) : null}
104171
{props.trailing}
105172
</View>
106173
</View>

‎apps/mobile/src/components/AppSymbol.tsx‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import IconAlertTriangle from "@tabler/icons-react-native/IconAlertTriangle";
99
import IconApps from "@tabler/icons-react-native/IconApps";
1010
import IconArchive from "@tabler/icons-react-native/IconArchive";
1111
import IconArrowBackUp from "@tabler/icons-react-native/IconArrowBackUp";
12+
import IconArrowLeft from "@tabler/icons-react-native/IconArrowLeft";
1213
import IconArrowDownCircle from "@tabler/icons-react-native/IconArrowDownCircle";
1314
import IconArrowRightCircle from "@tabler/icons-react-native/IconArrowRightCircle";
1415
import IconArrowUp from "@tabler/icons-react-native/IconArrowUp";
@@ -97,6 +98,7 @@ import { withUniwind } from "uniwind";
9798

9899
const ANDROID_ICON_BY_SF_SYMBOL: Partial<Record<SFSymbol, Icon>> = {
99100
"arrow.branch": IconGitBranch,
101+
"arrow.left": IconArrowLeft,
100102
"arrow.clockwise": IconRefresh,
101103
"arrow.down.circle": IconArrowDownCircle,
102104
"arrow.right.circle": IconArrowRightCircle,
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { Host, IconButton } from "@expo/ui/jetpack-compose";
2+
import { size } from "@expo/ui/jetpack-compose/modifiers";
3+
import { View } from "react-native";
4+
5+
import { useAppearancePreferences } from "../features/settings/appearance/AppearancePreferencesProvider";
6+
import { SymbolView, type AppSymbolName } from "./AppSymbol";
7+
8+
export function MaterialIconButton(props: {
9+
readonly accessibilityLabel: string;
10+
readonly icon: AppSymbolName;
11+
readonly onPress?: () => void;
12+
readonly disabled?: boolean;
13+
}) {
14+
const { themeAppearance } = useAppearancePreferences();
15+
return (
16+
<View
17+
accessible
18+
accessibilityLabel={props.accessibilityLabel}
19+
accessibilityRole="button"
20+
accessibilityState={{ disabled: Boolean(props.disabled) }}
21+
accessibilityActions={[{ name: "activate" }]}
22+
onAccessibilityAction={() => {
23+
if (!props.disabled) props.onPress?.();
24+
}}
25+
style={{ width: 48, height: 48 }}
26+
>
27+
<View importantForAccessibility="no-hide-descendants">
28+
<Host colorScheme={themeAppearance} style={{ width: 48, height: 48 }}>
29+
<IconButton onClick={props.onPress} enabled={!props.disabled} modifiers={[size(48, 48)]}>
30+
{null}
31+
</IconButton>
32+
</Host>
33+
</View>
34+
<View pointerEvents="none" className="absolute inset-0 items-center justify-center">
35+
<SymbolView
36+
name={props.icon}
37+
size={24}
38+
tintColorClassName={props.disabled ? "accent-icon-subtle" : "accent-foreground"}
39+
type="monochrome"
40+
/>
41+
</View>
42+
</View>
43+
);
44+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { Pressable } from "react-native";
2+
3+
import { SymbolView, type AppSymbolName } from "./AppSymbol";
4+
5+
export function MaterialIconButton(props: {
6+
readonly accessibilityLabel: string;
7+
readonly icon: AppSymbolName;
8+
readonly onPress?: () => void;
9+
readonly disabled?: boolean;
10+
}) {
11+
return (
12+
<Pressable
13+
{...props}
14+
accessibilityRole="button"
15+
className="size-12 items-center justify-center"
16+
>
17+
<SymbolView name={props.icon} size={24} tintColorClassName="accent-foreground" />
18+
</Pressable>
19+
);
20+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { Host, Switch as ComposeSwitch } from "@expo/ui/jetpack-compose";
2+
import { View } from "react-native";
3+
import type { ThemedSwitchProps } from "./ThemedSwitch";
4+
5+
import { useAppearancePreferences } from "../features/settings/appearance/AppearancePreferencesProvider";
6+
7+
/** Material's native switch, with the same palette and accessibility contract as our RN controls. */
8+
export function MaterialSwitch(props: ThemedSwitchProps) {
9+
const { themeAppearance, themeVariables: colors } = useAppearancePreferences();
10+
const toggle = () => {
11+
if (!props.disabled) props.onValueChange?.(!props.value);
12+
};
13+
14+
return (
15+
<View
16+
accessible
17+
accessibilityLabel={props.accessibilityLabel}
18+
accessibilityHint={props.accessibilityHint}
19+
accessibilityRole="switch"
20+
accessibilityState={{ checked: Boolean(props.value), disabled: Boolean(props.disabled) }}
21+
accessibilityActions={[{ name: "activate", label: "Toggle" }]}
22+
onAccessibilityAction={toggle}
23+
style={props.style}
24+
testID={props.testID}
25+
>
26+
<View importantForAccessibility="no-hide-descendants">
27+
<Host colorScheme={themeAppearance} style={{ width: 52, height: 48 }}>
28+
<ComposeSwitch
29+
value={Boolean(props.value)}
30+
enabled={!props.disabled}
31+
onCheckedChange={props.onValueChange ?? undefined}
32+
colors={{
33+
checkedTrackColor: colors["--color-switch-active-track"],
34+
checkedThumbColor: colors["--color-switch-active-thumb"],
35+
checkedBorderColor: colors["--color-switch-active-track"],
36+
uncheckedTrackColor: colors["--color-switch-inactive-track"],
37+
uncheckedThumbColor: colors["--color-switch-inactive-thumb"],
38+
uncheckedBorderColor: colors["--color-switch-inactive-thumb"],
39+
}}
40+
/>
41+
</Host>
42+
</View>
43+
</View>
44+
);
45+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { Switch as MaterialSwitch } from "react-native";
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import type { ComponentProps } from "react";
2+
import { ScrollView } from "react-native";
3+
4+
import { useAppearancePreferences } from "../features/settings/appearance/AppearancePreferencesProvider";
5+
6+
/** Keeps forms and settings readable inside a wide pane while its surface fills the screen. */
7+
export function ScreenScrollView(props: ComponentProps<typeof ScrollView>) {
8+
const { materialYouStyleLayoutActive } = useAppearancePreferences();
9+
return (
10+
<ScrollView
11+
{...props}
12+
contentContainerStyle={[
13+
props.contentContainerStyle,
14+
materialYouStyleLayoutActive && {
15+
width: "100%",
16+
maxWidth: 720,
17+
alignSelf: "center",
18+
},
19+
]}
20+
/>
21+
);
22+
}

‎apps/mobile/src/components/ThemedSwitch.tsx‎

Lines changed: 15 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,23 @@
1-
import { Platform, Pressable, Switch, View, type SwitchProps } from "react-native";
1+
import { Platform, Switch, type SwitchProps } from "react-native";
22

33
import { useAppearancePreferences } from "../features/settings/appearance/AppearancePreferencesProvider";
4-
import { SymbolView } from "./AppSymbol";
4+
import { MaterialSwitch } from "./MaterialSwitch";
55

6-
export function ThemedSwitch(props: SwitchProps) {
6+
export type ThemedSwitchProps = Pick<
7+
SwitchProps,
8+
| "accessibilityHint"
9+
| "accessibilityLabel"
10+
| "disabled"
11+
| "onValueChange"
12+
| "style"
13+
| "testID"
14+
| "value"
15+
>;
16+
17+
export function ThemedSwitch(props: ThemedSwitchProps) {
718
const { materialYouStyleLayoutActive } = useAppearancePreferences();
819
if (materialYouStyleLayoutActive) {
9-
return (
10-
<Pressable
11-
accessibilityHint={props.accessibilityHint}
12-
accessibilityLabel={props.accessibilityLabel}
13-
accessibilityRole="switch"
14-
accessibilityState={{ checked: Boolean(props.value), disabled: props.disabled }}
15-
disabled={props.disabled}
16-
hitSlop={8}
17-
onPress={() => props.onValueChange?.(!props.value)}
18-
style={props.style}
19-
testID={props.testID}
20-
className={props.disabled ? "opacity-40" : "active:opacity-70"}
21-
>
22-
<View
23-
className={
24-
props.value
25-
? "h-8 w-[52px] items-end justify-center rounded-full border-2 border-switch-active-track bg-switch-active-track px-0.5"
26-
: "h-8 w-[52px] items-start justify-center rounded-full border-2 border-switch-inactive-thumb bg-switch-inactive-track px-0.5"
27-
}
28-
>
29-
<View
30-
className={
31-
props.value
32-
? "size-6 items-center justify-center rounded-full bg-switch-active-thumb"
33-
: "size-6 items-center justify-center rounded-full bg-switch-inactive-thumb"
34-
}
35-
>
36-
<SymbolView
37-
name={props.value ? "checkmark" : "xmark"}
38-
size={13}
39-
tintColorClassName={
40-
props.value ? "accent-switch-active-track" : "accent-switch-inactive-track"
41-
}
42-
type="monochrome"
43-
/>
44-
</View>
45-
</View>
46-
</Pressable>
47-
);
20+
return <MaterialSwitch {...props} />;
4821
}
4922

5023
return (

‎apps/mobile/src/features/cloud/ConnectOnboardingRouteScreen.tsx‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1+
import { ScreenScrollView as ScrollView } from "../../components/ScreenScrollView";
12
import { NativeHeaderToolbar } from "../../native/StackHeader";
23
import { useAuth } from "@clerk/expo";
34
import { StackActions, useNavigation } from "@react-navigation/native";
45
import { useCallback, useEffect, useState } from "react";
5-
import { Platform, Pressable, RefreshControl, ScrollView, View } from "react-native";
6+
import { Platform, Pressable, RefreshControl, View } from "react-native";
67
import { useSafeAreaInsets } from "react-native-safe-area-context";
78

89
import { reportAtomCommandResult, settlePromise } from "@t3tools/client-runtime/state/runtime";

0 commit comments

Comments
 (0)