Skip to content

Commit 384019f

Browse files
committed
refactor: destructure theme prop
1 parent 9f93aa8 commit 384019f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+609
-349
lines changed

src/components/Badge.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ const Badge = ({
6767

6868
const {
6969
animation: { scale },
70-
colors,
70+
colors: { error, onError },
7171
} = theme;
7272

7373
React.useEffect(() => {
@@ -84,7 +84,7 @@ const Badge = ({
8484
}).start();
8585
}, [visible, opacity, scale]);
8686

87-
const { backgroundColor = colors.error, ...restStyle } = (StyleSheet.flatten(
87+
const { backgroundColor = error, ...restStyle } = (StyleSheet.flatten(
8888
style
8989
) || {}) as TextStyle;
9090

@@ -95,7 +95,7 @@ const Badge = ({
9595
{
9696
opacity,
9797
backgroundColor,
98-
color: colors.onError,
98+
color: onError,
9999
fontSize: size * 0.5,
100100
lineHeight: size / fontScale,
101101
height: size,

src/components/Banner.tsx

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,11 @@ const Banner = ({
131131
...rest
132132
}: Props) => {
133133
const theme = useInternalTheme(themeOverrides);
134+
const {
135+
animation: { scale },
136+
colors: { onSurface, primary },
137+
} = theme;
138+
134139
const { current: position } = React.useRef<Animated.Value>(
135140
new Animated.Value(visible ? 1 : 0)
136141
);
@@ -145,11 +150,6 @@ const Banner = ({
145150
const showCallback = useLatestCallback(onShowAnimationFinished);
146151
const hideCallback = useLatestCallback(onHideAnimationFinished);
147152

148-
const {
149-
animation: { scale },
150-
colors,
151-
} = theme;
152-
153153
const opacity = position.interpolate({
154154
inputRange: [0, 0.1, 1],
155155
outputRange: [0, 1, 1],
@@ -227,7 +227,7 @@ const Banner = ({
227227
style={[
228228
styles.message,
229229
{
230-
color: colors.onSurface,
230+
color: onSurface,
231231
},
232232
]}
233233
accessibilityLiveRegion={visible ? 'polite' : 'none'}
@@ -244,7 +244,7 @@ const Banner = ({
244244
compact
245245
mode="text"
246246
style={styles.button}
247-
textColor={colors.primary}
247+
textColor={primary}
248248
theme={theme}
249249
{...others}
250250
>

src/components/BottomNavigation/BottomNavigation.tsx

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -343,18 +343,21 @@ const BottomNavigation = <Route extends BaseRoute>({
343343
onTabPress,
344344
onTabLongPress,
345345
onIndexChange,
346-
shifting: shiftingProp,
346+
shifting: shiftingProp = false,
347347
safeAreaInsets,
348348
labelMaxFontSizeMultiplier = 1,
349-
compact: compactProp,
349+
compact = false,
350350
testID = 'bottom-navigation',
351351
theme: themeOverrides,
352352
getLazy = ({ route }: { route: Route }) => route.lazy,
353353
}: Props<Route>) => {
354354
const theme = useInternalTheme(themeOverrides);
355-
const { scale } = theme.animation;
356-
const compact = compactProp ?? false;
357-
let shifting = shiftingProp ?? false;
355+
const {
356+
animation: { scale },
357+
colors,
358+
} = theme;
359+
360+
let shifting = shiftingProp;
358361

359362
if (shifting && navigationState.routes.length < 2) {
360363
shifting = false;
@@ -486,7 +489,6 @@ const BottomNavigation = <Route extends BaseRoute>({
486489
});
487490

488491
const { routes } = navigationState;
489-
const { colors } = theme;
490492

491493
return (
492494
<View style={[styles.container, style]} testID={testID}>

src/components/BottomNavigation/BottomNavigationBar.tsx

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,11 @@ const BottomNavigationBar = <Route extends BaseRoute>({
339339
}: Props<Route>) => {
340340
const theme = useInternalTheme(themeOverrides);
341341
const { bottom, left, right } = useSafeAreaInsets();
342-
const { scale } = theme.animation;
342+
const {
343+
animation: { scale },
344+
colors: { elevation: elevationColors, secondaryContainer },
345+
fonts: { labelMedium },
346+
} = theme;
343347
let shifting = shiftingProp;
344348

345349
if (shifting && navigationState.routes.length < 2) {
@@ -479,7 +483,7 @@ const BottomNavigationBar = <Route extends BaseRoute>({
479483
backgroundColor?: ColorValue;
480484
};
481485

482-
const backgroundColor = customBackground || theme.colors.elevation.level2;
486+
const backgroundColor = customBackground || elevationColors.level2;
483487

484488
const activeTintColor = getActiveTintColor({
485489
activeColor,
@@ -615,8 +619,6 @@ const BottomNavigationBar = <Route extends BaseRoute>({
615619

616620
const isLegacyOrV3Shifting = shifting && labeled;
617621

618-
const font = theme.fonts.labelMedium;
619-
620622
return renderTouchable({
621623
key: route.key,
622624
route,
@@ -657,7 +659,7 @@ const BottomNavigationBar = <Route extends BaseRoute>({
657659
scaleX: outlineScale,
658660
},
659661
],
660-
backgroundColor: theme.colors.secondaryContainer,
662+
backgroundColor: secondaryContainer,
661663
},
662664
activeIndicatorStyle,
663665
]}
@@ -751,7 +753,7 @@ const BottomNavigationBar = <Route extends BaseRoute>({
751753
styles.label,
752754
{
753755
color: activeLabelColor,
754-
...font,
756+
...labelMedium,
755757
},
756758
]}
757759
>
@@ -785,7 +787,7 @@ const BottomNavigationBar = <Route extends BaseRoute>({
785787
styles.label,
786788
{
787789
color: inactiveLabelColor,
788-
...font,
790+
...labelMedium,
789791
},
790792
]}
791793
>

src/components/Button/Button.tsx

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -212,13 +212,18 @@ const Button = (
212212
ref: React.ForwardedRef<View>
213213
) => {
214214
const theme = useInternalTheme(themeOverrides);
215+
const {
216+
roundness,
217+
animation,
218+
fonts: { labelLarge },
219+
} = theme;
220+
215221
const isMode = React.useCallback(
216222
(modeToCompare: ButtonMode) => {
217223
return mode === modeToCompare;
218224
},
219225
[mode]
220226
);
221-
const { roundness, animation, fonts } = theme;
222227
const isWeb = Platform.OS === 'web';
223228

224229
const hasPassedTouchHandler = hasTouchHandler({
@@ -309,11 +314,9 @@ const Button = (
309314
const { color: customLabelColor, fontSize: customLabelSize } =
310315
StyleSheet.flatten(labelStyle) || {};
311316

312-
const font = fonts.labelLarge;
313-
314317
const textStyle = {
315318
color: textColor,
316-
...font,
319+
...labelLarge,
317320
};
318321

319322
const iconStyle =

src/components/Button/utils.tsx

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ const getButtonBackgroundColor = ({
4949
}: BaseProps & {
5050
customButtonColor?: string;
5151
}) => {
52+
const {
53+
colors: { surfaceDisabled, elevation, primary, secondaryContainer },
54+
} = theme;
55+
5256
if (customButtonColor && !disabled) {
5357
return customButtonColor;
5458
}
@@ -58,19 +62,19 @@ const getButtonBackgroundColor = ({
5862
return 'transparent';
5963
}
6064

61-
return theme.colors.surfaceDisabled;
65+
return surfaceDisabled;
6266
}
6367

6468
if (isMode('elevated')) {
65-
return theme.colors.elevation.level1;
69+
return elevation.level1;
6670
}
6771

6872
if (isMode('contained')) {
69-
return theme.colors.primary;
73+
return primary;
7074
}
7175

7276
if (isMode('contained-tonal')) {
73-
return theme.colors.secondaryContainer;
77+
return secondaryContainer;
7478
}
7579

7680
return 'transparent';
@@ -88,12 +92,15 @@ const getButtonTextColor = ({
8892
backgroundColor: string;
8993
dark?: boolean;
9094
}) => {
95+
const {
96+
colors: { onSurfaceDisabled, primary, onPrimary, onSecondaryContainer },
97+
} = theme;
9198
if (customTextColor && !disabled) {
9299
return customTextColor;
93100
}
94101

95102
if (disabled) {
96-
return theme.colors.onSurfaceDisabled;
103+
return onSurfaceDisabled;
97104
}
98105

99106
if (typeof dark === 'boolean') {
@@ -107,27 +114,31 @@ const getButtonTextColor = ({
107114
}
108115

109116
if (isMode('outlined') || isMode('text') || isMode('elevated')) {
110-
return theme.colors.primary;
117+
return primary;
111118
}
112119

113120
if (isMode('contained')) {
114-
return theme.colors.onPrimary;
121+
return onPrimary;
115122
}
116123

117124
if (isMode('contained-tonal')) {
118-
return theme.colors.onSecondaryContainer;
125+
return onSecondaryContainer;
119126
}
120127

121-
return theme.colors.primary;
128+
return primary;
122129
};
123130

124131
const getButtonBorderColor = ({ isMode, disabled, theme }: BaseProps) => {
132+
const {
133+
colors: { surfaceDisabled, outline },
134+
} = theme;
135+
125136
if (disabled && isMode('outlined')) {
126-
return theme.colors.surfaceDisabled;
137+
return surfaceDisabled;
127138
}
128139

129140
if (isMode('outlined')) {
130-
return theme.colors.outline;
141+
return outline;
131142
}
132143

133144
return 'transparent';

src/components/Card/utils.tsx

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,11 @@ export const getCardCoverStyle = ({
3939
};
4040

4141
const getBorderColor = ({ theme }: { theme: InternalTheme }) => {
42-
return theme.colors.outline;
42+
const {
43+
colors: { outline },
44+
} = theme;
45+
46+
return outline;
4347
};
4448

4549
const getBackgroundColor = ({
@@ -49,11 +53,15 @@ const getBackgroundColor = ({
4953
theme: InternalTheme;
5054
isMode: (mode: CardMode) => boolean;
5155
}) => {
56+
const {
57+
colors: { surfaceVariant, surface },
58+
} = theme;
59+
5260
if (isMode('contained')) {
53-
return theme.colors.surfaceVariant;
61+
return surfaceVariant;
5462
}
5563
if (isMode('outlined')) {
56-
return theme.colors.surface;
64+
return surface;
5765
}
5866
return undefined;
5967
};

src/components/Checkbox/CheckboxItem.tsx

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,9 @@ const CheckboxItem = ({
154154
...props
155155
}: Props) => {
156156
const theme = useInternalTheme(themeOverrides);
157-
const { colors } = theme;
157+
const {
158+
colors: { onSurface, onSurfaceDisabled },
159+
} = theme;
158160

159161
const checkboxProps = { ...props, status, theme, disabled };
160162
const isLeading = position === 'leading';
@@ -168,14 +170,9 @@ const CheckboxItem = ({
168170
checkbox = <Checkbox {...checkboxProps} />;
169171
}
170172

171-
const textColor = colors.onSurface;
172-
const disabledTextColor = colors.onSurfaceDisabled;
173-
174-
const textAlign = isLeading ? 'right' : 'left';
175-
176173
const computedStyle = {
177-
color: disabled ? disabledTextColor : textColor,
178-
textAlign,
174+
color: disabled ? onSurfaceDisabled : onSurface,
175+
textAlign: isLeading ? 'right' : 'left',
179176
} as TextStyle;
180177

181178
return (

0 commit comments

Comments
 (0)