Skip to content

Commit 0d094a6

Browse files
committed
refactor: simplfy components tree
1 parent 70c498e commit 0d094a6

5 files changed

Lines changed: 230 additions & 342 deletions

File tree

‎src/components/SegmentedButtons/SegmentedButtonContent.tsx‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,15 +64,15 @@ const SegmentedButtonContent = ({
6464

6565
return (
6666
<View style={styles.content}>
67-
{showCheckIcon ? (
67+
{showCheckIcon && (
6868
<AnimatedCheckIcon
6969
color={iconColor}
7070
opacity={iconOpacity}
7171
scale={checkmarkScale}
7272
testID={testID && `${testID}-check-icon`}
7373
/>
74-
) : null}
75-
{optionIcon ? (
74+
)}
75+
{optionIcon && (
7676
<AnimatedOptionIcon
7777
animated={Boolean(label && showSelectedCheck)}
7878
color={iconColor}
@@ -81,8 +81,8 @@ const SegmentedButtonContent = ({
8181
source={optionIcon}
8282
testID={testID && `${testID}-icon`}
8383
/>
84-
) : null}
85-
{label ? (
84+
)}
85+
{label && (
8686
<Text
8787
variant={SegmentedButtonTokens.labelTextType}
8888
style={[
@@ -98,7 +98,7 @@ const SegmentedButtonContent = ({
9898
>
9999
{label}
100100
</Text>
101-
) : null}
101+
)}
102102
</View>
103103
);
104104
};

‎src/components/SegmentedButtons/SegmentedButtonItem.tsx‎

Lines changed: 26 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -137,18 +137,17 @@ const SegmentedButtonItem = ({
137137
});
138138

139139
const borderRadius = getSegmentedButtonBorderRadius(segment);
140-
const { outline, divider } = getSegmentedButtonBorderStyles(segment);
140+
const borderStyles = getSegmentedButtonBorderStyles(segment, colors);
141141

142-
const containerHeight = SegmentedButtonTokens.containerHeight[density];
142+
const height = SegmentedButtonTokens.containerHeight[density];
143143

144144
return (
145145
<View
146146
testID={testID && `${testID}-wrapper`}
147147
style={[
148148
styles.wrapper,
149149
borderRadius,
150-
{ backgroundColor: colors.container },
151-
showFocusRing && styles.focusedWrapper,
150+
{ backgroundColor: colors.wrapper },
152151
style,
153152
]}
154153
>
@@ -165,60 +164,34 @@ const SegmentedButtonItem = ({
165164
background={background}
166165
hitSlop={hitSlop}
167166
theme={theme}
168-
style={[borderRadius, Platform.select({ web: { outline: 'none' } })]}
167+
style={[
168+
styles.touchable,
169+
borderRadius,
170+
borderStyles,
171+
Platform.select({ web: { outline: 'none' } }),
172+
{ height },
173+
]}
169174
onFocus={(event) => {
170175
if (!disabled && isKeyboardFocusEvent(event)) {
171176
setFocused(true);
172177
}
173178
}}
174179
onBlur={() => setFocused(false)}
175180
>
176-
<View
177-
testID={testID && `${testID}-container`}
178-
style={[styles.container, { height: containerHeight }]}
179-
>
180-
<SegmentedButtonContent
181-
checked={checked}
182-
iconColor={colors.content.iconColor}
183-
iconOpacity={colors.content.iconOpacity}
184-
icon={icon}
185-
label={label}
186-
labelColor={colors.content.labelColor}
187-
labelMaxFontSizeMultiplier={labelMaxFontSizeMultiplier}
188-
labelOpacity={colors.content.labelOpacity}
189-
labelStyle={labelStyle}
190-
showSelectedCheck={showSelectedCheck}
191-
testID={testID}
192-
theme={theme}
193-
/>
194-
<View
195-
pointerEvents="none"
196-
testID={testID && `${testID}-outline`}
197-
style={[
198-
styles.outline,
199-
borderRadius,
200-
{
201-
borderColor: colors.outline.color,
202-
opacity: colors.outline.opacity,
203-
},
204-
outline,
205-
]}
206-
/>
207-
{divider ? (
208-
<View
209-
pointerEvents="none"
210-
testID={testID && `${testID}-divider`}
211-
style={[
212-
styles.outline,
213-
{
214-
borderColor: colors.divider.color,
215-
opacity: colors.divider.opacity,
216-
},
217-
divider,
218-
]}
219-
/>
220-
) : null}
221-
</View>
181+
<SegmentedButtonContent
182+
checked={checked}
183+
iconColor={colors.content.iconColor}
184+
iconOpacity={colors.content.iconOpacity}
185+
icon={icon}
186+
label={label}
187+
labelColor={colors.content.labelColor}
188+
labelMaxFontSizeMultiplier={labelMaxFontSizeMultiplier}
189+
labelOpacity={colors.content.labelOpacity}
190+
labelStyle={labelStyle}
191+
showSelectedCheck={showSelectedCheck}
192+
testID={testID}
193+
theme={theme}
194+
/>
222195
</TouchableRipple>
223196
{showFocusRing ? (
224197
<View
@@ -227,9 +200,7 @@ const SegmentedButtonItem = ({
227200
style={[
228201
styles.focusRing,
229202
borderRadius,
230-
{
231-
borderColor: colors.focusIndicator,
232-
},
203+
{ borderColor: colors.focusIndicator },
233204
]}
234205
/>
235206
) : null}
@@ -241,21 +212,10 @@ const styles = StyleSheet.create({
241212
wrapper: {
242213
flex: 1,
243214
},
244-
focusedWrapper: {
245-
zIndex: 1,
246-
},
247-
container: {
215+
touchable: {
248216
width: '100%',
249217
justifyContent: 'center',
250218
},
251-
outline: {
252-
position: 'absolute',
253-
top: 0,
254-
right: 0,
255-
bottom: 0,
256-
left: 0,
257-
pointerEvents: 'none',
258-
},
259219
focusRing: {
260220
position: 'absolute',
261221
top: -FOCUS_RING_OUTSET,

‎src/components/SegmentedButtons/utils.ts‎

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
import type { ViewStyle } from 'react-native';
1+
import type { ColorValue, ViewStyle } from 'react-native';
2+
3+
import color from 'color';
24

35
import { SegmentedButtonTokens } from './tokens';
46
import type { InternalTheme } from '../../types';
@@ -41,28 +43,25 @@ export const getSegmentedButtonBorderRadius = (
4143
};
4244
};
4345

44-
type SegmentedButtonBorderStyles = {
45-
outline: ViewStyle;
46-
divider?: ViewStyle;
46+
type SegmentedButtonBorderColors = {
47+
outline: ColorValue;
48+
divider: ColorValue;
4749
};
4850

4951
export const getSegmentedButtonBorderStyles = (
50-
segment: SegmentedButtonPosition
51-
): SegmentedButtonBorderStyles => {
52+
segment: SegmentedButtonPosition,
53+
{ outline, divider }: SegmentedButtonBorderColors
54+
): ViewStyle => {
5255
const outlineWidth = SegmentedButtonTokens.outlineWidth;
53-
const outline = {
56+
57+
return {
58+
borderColor: outline,
59+
borderStartColor: segment === 'first' ? outline : divider,
5460
borderTopWidth: outlineWidth,
5561
borderBottomWidth: outlineWidth,
62+
borderStartWidth: outlineWidth,
5663
borderEndWidth: segment === 'last' ? outlineWidth : 0,
5764
};
58-
59-
if (segment === 'first') {
60-
return {
61-
outline: { ...outline, borderStartWidth: outlineWidth },
62-
};
63-
}
64-
65-
return { outline, divider: { borderStartWidth: outlineWidth } };
6665
};
6766

6867
const resolveContentColors = (
@@ -94,15 +93,24 @@ const resolveContentColors = (
9493
};
9594
};
9695

97-
const resolveOutlineColors = (theme: InternalTheme, disabled: boolean) => {
96+
const applyOpacity = (value: ColorValue, opacity: number): ColorValue => {
97+
if (opacity === 1 || typeof value !== 'string') {
98+
return value;
99+
}
100+
101+
return color(value)
102+
.fade(1 - opacity)
103+
.rgb()
104+
.string();
105+
};
106+
107+
const resolveOutlineColor = (theme: InternalTheme, disabled: boolean) => {
98108
const colorToken = disabled
99109
? SegmentedButtonTokens.disabledOutlineColor
100110
: SegmentedButtonTokens.outlineColor;
111+
const opacity = disabled ? SegmentedButtonTokens.disabledOutlineOpacity : 1;
101112

102-
return {
103-
color: theme.colors[colorToken],
104-
opacity: disabled ? SegmentedButtonTokens.disabledOutlineOpacity : 1,
105-
};
113+
return applyOpacity(theme.colors[colorToken], opacity);
106114
};
107115

108116
export const resolveColors = (
@@ -112,12 +120,12 @@ export const resolveColors = (
112120
const { checked, disabled, contentColor, dividerDisabled } = options;
113121

114122
return {
115-
container: checked
123+
wrapper: checked
116124
? theme.colors[SegmentedButtonTokens.selectedContainerColor]
117125
: 'transparent',
118126
content: resolveContentColors(theme, options, contentColor),
119-
outline: resolveOutlineColors(theme, disabled),
120-
divider: resolveOutlineColors(theme, dividerDisabled),
127+
outline: resolveOutlineColor(theme, disabled),
128+
divider: resolveOutlineColor(theme, dividerDisabled),
121129
focusIndicator: theme.colors[SegmentedButtonTokens.focusIndicatorColor],
122130
};
123131
};

0 commit comments

Comments
 (0)