Skip to content

Commit 09c5e7a

Browse files
style(mobile): flatten segmented control surfaces (#20163)
1 parent 86534db commit 09c5e7a

1 file changed

Lines changed: 5 additions & 92 deletions

File tree

Lines changed: 5 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { useState } from "react";
22
import { I18nManager, Platform, Pressable, View } from "react-native";
3-
import Svg, { Defs, LinearGradient, Rect, Stop } from "react-native-svg";
43
import Animated, { useAnimatedStyle, useReducedMotion, withTiming } from "react-native-reanimated";
54

65
import { AppText } from "./AppText";
@@ -9,13 +8,13 @@ import type { SegmentedControlProps } from "./SegmentedControl.types";
98

109
export type { SegmentedControlProps } from "./SegmentedControl.types";
1110

12-
/** Shared capsule control with native press targets and a raised selection. */
11+
/** Shared capsule control with native press targets and a flat, themed selection. */
1312
export function SegmentedControl<Value extends string | number>(
1413
props: SegmentedControlProps<Value>,
1514
) {
1615
const { colorScheme, colors } = useControlTheme();
1716
const [layout, setLayout] = useState({ width: 0, height: 0 });
18-
const { width, height } = layout;
17+
const { width } = layout;
1918
const reduceMotion = useReducedMotion();
2019
const selectedIndex = props.options.findIndex((option) => option.value === props.selected);
2120
const segmentWidth = props.options.length ? Math.max(0, width - 6) / props.options.length : 0;
@@ -34,38 +33,20 @@ export function SegmentedControl<Value extends string | number>(
3433
className="flex-row rounded-full p-[3px]"
3534
style={{
3635
minHeight,
37-
boxShadow: dark ? "0px 1px 2px #00000062" : "0px 1px 2px #00000014",
36+
backgroundColor: colors[dark ? "--color-screen" : "--color-grouped-card"],
3837
}}
3938
>
40-
<CapsuleSurface
41-
width={width}
42-
height={height}
43-
dark={dark}
44-
fillTop={colors[dark ? "--color-screen" : "--color-grouped-card"]}
45-
fillBottom={colors["--color-card"]}
46-
edgeColor={colors["--color-foreground"]}
47-
/>
4839
{selectedIndex >= 0 && width > 0 ? (
4940
<Animated.View
5041
pointerEvents="none"
5142
className="absolute bottom-[3px] start-[3px] top-[3px] rounded-full"
5243
style={[
5344
{
54-
boxShadow: dark ? "0px 1px 2px #00000046" : "0px 2px 3px #00000026",
45+
backgroundColor: colors[dark ? "--color-secondary" : "--color-screen"],
5546
},
5647
selectionStyle,
5748
]}
58-
>
59-
<CapsuleSurface
60-
width={segmentWidth}
61-
height={height - 6}
62-
dark={dark}
63-
fillTop={colors[dark ? "--color-secondary" : "--color-screen"]}
64-
fillBottom={colors[dark ? "--color-secondary" : "--color-screen"]}
65-
edgeColor={colors["--color-secondary-foreground"]}
66-
selected
67-
/>
68-
</Animated.View>
49+
/>
6950
) : null}
7051
{props.options.map((option) => {
7152
const selected = option.value === props.selected;
@@ -96,71 +77,3 @@ export function SegmentedControl<Value extends string | number>(
9677
</View>
9778
);
9879
}
99-
100-
/** Pen's inset well and raised selection use opposite vertical edge lighting. */
101-
function CapsuleSurface({
102-
width,
103-
height,
104-
dark,
105-
fillTop,
106-
fillBottom,
107-
edgeColor,
108-
selected = false,
109-
}: {
110-
width: number;
111-
height: number;
112-
dark: boolean;
113-
fillTop: string;
114-
fillBottom: string;
115-
edgeColor: string;
116-
selected?: boolean;
117-
}) {
118-
if (width <= 0 || height <= 0) return null;
119-
const edgeOpacity = selected
120-
? dark
121-
? [0.22, 0.02]
122-
: [0.13, 0.01]
123-
: dark
124-
? [0.02, 0.16]
125-
: [0.27, 0.1];
126-
return (
127-
<Svg
128-
pointerEvents="none"
129-
width={width}
130-
height={height}
131-
style={{ position: "absolute", top: 0, left: 0 }}
132-
>
133-
<Defs>
134-
<LinearGradient id="fill" x1="0" y1="0" x2="0%" y2="100%">
135-
<Stop offset="0" stopColor={fillTop} />
136-
<Stop offset="1" stopColor={fillBottom} />
137-
</LinearGradient>
138-
<LinearGradient id="edge" x1="0" y1="0" x2="0%" y2="100%">
139-
<Stop offset="0" stopColor={edgeColor} stopOpacity={edgeOpacity[0]} />
140-
<Stop offset="1" stopColor={edgeColor} stopOpacity={edgeOpacity[1]} />
141-
</LinearGradient>
142-
</Defs>
143-
<Rect
144-
x={0.5}
145-
y={0.5}
146-
width={width - 1}
147-
height={height - 1}
148-
rx={(height - 1) / 2}
149-
fill="url(#fill)"
150-
/>
151-
{selected && !dark ? (
152-
<Rect width={width} height={height} rx={height / 2} fill="white" fillOpacity={0.85} />
153-
) : null}
154-
<Rect
155-
x={0.5}
156-
y={0.5}
157-
width={width - 1}
158-
height={height - 1}
159-
rx={(height - 1) / 2}
160-
fill="none"
161-
stroke="url(#edge)"
162-
strokeWidth={1}
163-
/>
164-
</Svg>
165-
);
166-
}

0 commit comments

Comments
 (0)