Skip to content

Commit 0556ba6

Browse files
fix: style types (callstack#3623)
1 parent 6ad1299 commit 0556ba6

Some content is hidden

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

56 files changed

+1369
-424
lines changed

Diff for: package.json

+6-2
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
"@types/react-dom": "^18.0.8",
6565
"@types/react-native": "^0.70.6",
6666
"@types/react-native-vector-icons": "^6.4.1",
67+
"@types/react-test-renderer": "^18.0.0",
6768
"@typescript-eslint/eslint-plugin": "^5.41.0",
6869
"@typescript-eslint/parser": "^5.41.0",
6970
"all-contributors-cli": "^6.24.0",
@@ -99,8 +100,8 @@
99100
"peerDependencies": {
100101
"react": "*",
101102
"react-native": "*",
102-
"react-native-vector-icons": "*",
103-
"react-native-safe-area-context": "*"
103+
"react-native-safe-area-context": "*",
104+
"react-native-vector-icons": "*"
104105
},
105106
"husky": {
106107
"hooks": {
@@ -117,6 +118,9 @@
117118
"@testing-library/jest-native/extend-expect"
118119
],
119120
"cacheDirectory": "./cache/jest",
121+
"testPathIgnorePatterns": [
122+
"\\.d\\.ts$"
123+
],
120124
"modulePathIgnorePatterns": [
121125
"<rootDir>/example/node_modules",
122126
"<rootDir>/lib/"

Diff for: src/components/Appbar/Appbar.tsx

+19-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
import * as React from 'react';
2-
import { Platform, StyleProp, StyleSheet, View, ViewStyle } from 'react-native';
2+
import {
3+
Animated,
4+
Platform,
5+
StyleProp,
6+
StyleSheet,
7+
View,
8+
ViewStyle,
9+
ColorValue,
10+
} from 'react-native';
311

412
import color from 'color';
513

@@ -18,7 +26,10 @@ import {
1826
renderAppbarContent,
1927
} from './utils';
2028

21-
export type Props = Partial<React.ComponentPropsWithRef<typeof View>> & {
29+
export type Props = Omit<
30+
Partial<React.ComponentPropsWithRef<typeof View>>,
31+
'style'
32+
> & {
2233
/**
2334
* Whether the background color is a dark color. A dark appbar will render light text and vice-versa.
2435
*/
@@ -55,7 +66,7 @@ export type Props = Partial<React.ComponentPropsWithRef<typeof View>> & {
5566
* @optional
5667
*/
5768
theme?: ThemeProp;
58-
style?: StyleProp<ViewStyle>;
69+
style?: Animated.WithAnimatedValue<StyleProp<ViewStyle>>;
5970
};
6071

6172
/**
@@ -160,11 +171,15 @@ const Appbar = ({
160171
}: Props) => {
161172
const theme = useInternalTheme(themeOverrides);
162173
const { isV3 } = theme;
174+
const flattenedStyle = StyleSheet.flatten(style);
163175
const {
164176
backgroundColor: customBackground,
165177
elevation = isV3 ? (elevated ? 2 : 0) : 4,
166178
...restStyle
167-
}: ViewStyle = StyleSheet.flatten(style) || {};
179+
} = (flattenedStyle || {}) as Exclude<typeof flattenedStyle, number> & {
180+
elevation?: number;
181+
backgroundColor?: ColorValue;
182+
};
168183

169184
let isDark: boolean;
170185

Diff for: src/components/Appbar/AppbarAction.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as React from 'react';
2-
import type { StyleProp, ViewStyle, View } from 'react-native';
2+
import type { StyleProp, ViewStyle, View, Animated } from 'react-native';
33

44
import color from 'color';
55
import type { ThemeProp } from 'src/types';
@@ -41,7 +41,7 @@ export type Props = React.ComponentPropsWithoutRef<typeof IconButton> & {
4141
* Whether it's the leading button.
4242
*/
4343
isLeading?: boolean;
44-
style?: StyleProp<ViewStyle>;
44+
style?: Animated.WithAnimatedValue<StyleProp<ViewStyle>>;
4545
ref?: React.RefObject<View>;
4646
/**
4747
* @optional

Diff for: src/components/Appbar/AppbarBackAction.tsx

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import type {
44
StyleProp,
55
ViewStyle,
66
View,
7+
Animated,
78
} from 'react-native';
89

910
import { forwardRef } from '../../utils/forwardRef';
@@ -35,7 +36,7 @@ export type Props = $Omit<
3536
* Function to execute on press.
3637
*/
3738
onPress?: (e: GestureResponderEvent) => void;
38-
style?: StyleProp<ViewStyle>;
39+
style?: Animated.WithAnimatedValue<StyleProp<ViewStyle>>;
3940
ref?: React.RefObject<View>;
4041
};
4142

Diff for: src/components/Appbar/AppbarHeader.tsx

+27-15
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
import * as React from 'react';
2-
import { Platform, StyleProp, StyleSheet, View, ViewStyle } from 'react-native';
2+
import {
3+
Animated,
4+
ColorValue,
5+
Platform,
6+
StyleProp,
7+
StyleSheet,
8+
View,
9+
ViewStyle,
10+
} from 'react-native';
311

412
import { useSafeAreaInsets } from 'react-native-safe-area-context';
513

@@ -48,7 +56,7 @@ export type Props = React.ComponentProps<typeof Appbar> & {
4856
* @optional
4957
*/
5058
theme?: ThemeProp;
51-
style?: StyleProp<ViewStyle>;
59+
style?: Animated.WithAnimatedValue<StyleProp<ViewStyle>>;
5260
};
5361

5462
/**
@@ -112,13 +120,19 @@ const AppbarHeader = ({
112120
const theme = useInternalTheme(themeOverrides);
113121
const { isV3 } = theme;
114122

123+
const flattenedStyle = StyleSheet.flatten(style);
115124
const {
116125
height = isV3 ? modeAppbarHeight[mode] : DEFAULT_APPBAR_HEIGHT,
117126
elevation = isV3 ? (elevated ? 2 : 0) : 4,
118127
backgroundColor: customBackground,
119128
zIndex = isV3 && elevated ? 1 : 0,
120129
...restStyle
121-
}: ViewStyle = StyleSheet.flatten(style) || {};
130+
} = (flattenedStyle || {}) as Exclude<typeof flattenedStyle, number> & {
131+
height?: number;
132+
elevation?: number;
133+
backgroundColor?: ColorValue;
134+
zIndex?: number;
135+
};
122136

123137
const backgroundColor = getAppbarColor(
124138
theme,
@@ -131,18 +145,16 @@ const AppbarHeader = ({
131145

132146
return (
133147
<View
134-
style={
135-
[
136-
{
137-
backgroundColor,
138-
zIndex,
139-
elevation,
140-
paddingTop: statusBarHeight ?? top,
141-
paddingHorizontal: Math.max(left, right),
142-
},
143-
shadow(elevation),
144-
] as StyleProp<ViewStyle>
145-
}
148+
style={[
149+
{
150+
backgroundColor,
151+
zIndex,
152+
elevation,
153+
paddingTop: statusBarHeight ?? top,
154+
paddingHorizontal: Math.max(left, right),
155+
},
156+
shadow(elevation) as ViewStyle,
157+
]}
146158
>
147159
<Appbar
148160
style={[{ height, backgroundColor }, styles.appbar, restStyle]}

Diff for: src/components/Appbar/utils.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ type RenderAppbarContentProps = {
4444
isDark: boolean;
4545
shouldCenterContent?: boolean;
4646
isV3: boolean;
47-
renderOnly?: React.ComponentType<any>[];
47+
renderOnly?: (React.ComponentType<any> | false)[];
4848
renderExcept?: React.ComponentType<any>[];
4949
mode?: AppbarModes;
5050
theme?: ThemeProp;
@@ -65,7 +65,7 @@ export const modeTextVariant = {
6565
medium: 'headlineSmall',
6666
large: 'headlineMedium',
6767
'center-aligned': 'titleLarge',
68-
};
68+
} as const;
6969

7070
export const renderAppbarContent = ({
7171
children,

Diff for: src/components/Banner.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export type Props = $RemoveChildren<typeof Surface> & {
3737
actions?: Array<
3838
{
3939
label: string;
40-
} & Omit<React.ComponentProps<typeof Button>, 'children'>
40+
} & $RemoveChildren<typeof Button>
4141
>;
4242
/**
4343
* Style of banner's inner content.
@@ -49,7 +49,7 @@ export type Props = $RemoveChildren<typeof Surface> & {
4949
* Changes Banner shadow and background on iOS and Android.
5050
*/
5151
elevation?: 0 | 1 | 2 | 3 | 4 | 5 | Animated.Value;
52-
style?: StyleProp<ViewStyle>;
52+
style?: Animated.WithAnimatedValue<StyleProp<ViewStyle>>;
5353
ref?: React.RefObject<View>;
5454
/**
5555
* @optional

Diff for: src/components/BottomNavigation/BottomNavigation.tsx

+32-26
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as React from 'react';
22
import {
33
Animated,
4+
ColorValue,
45
EasingFunction,
56
Platform,
67
StyleProp,
@@ -255,7 +256,7 @@ export type Props = {
255256
* barStyle={{ backgroundColor: '#694fad' }}
256257
* ```
257258
*/
258-
barStyle?: StyleProp<ViewStyle>;
259+
barStyle?: Animated.WithAnimatedValue<StyleProp<ViewStyle>>;
259260
/**
260261
* Specifies the largest possible scale a label font can reach.
261262
*/
@@ -620,8 +621,11 @@ const BottomNavigation = ({
620621
const { routes } = navigationState;
621622
const { colors, dark: isDarkTheme, mode, isV3 } = theme;
622623

623-
const { backgroundColor: customBackground, elevation = 4 }: ViewStyle =
624-
StyleSheet.flatten(barStyle) || {};
624+
const { backgroundColor: customBackground, elevation = 4 } =
625+
(StyleSheet.flatten(barStyle) || {}) as {
626+
elevation?: number;
627+
backgroundColor?: ColorValue;
628+
};
625629

626630
const approxBackgroundColor = customBackground
627631
? customBackground
@@ -758,29 +762,28 @@ const BottomNavigation = ({
758762
</View>
759763
<Surface
760764
{...(theme.isV3 && { elevation: 0 })}
761-
style={
762-
[
763-
!theme.isV3 && { elevation: 4 },
764-
styles.bar,
765-
keyboardHidesNavigationBar
766-
? {
767-
// When the keyboard is shown, slide down the navigation bar
768-
transform: [
769-
{
770-
translateY: visibleAnim.interpolate({
771-
inputRange: [0, 1],
772-
outputRange: [layout.height, 0],
773-
}),
774-
},
775-
],
776-
// Absolutely position the navigation bar so that the content is below it
777-
// This is needed to avoid gap at bottom when the navigation bar is hidden
778-
position: keyboardVisible ? 'absolute' : null,
779-
}
780-
: null,
781-
barStyle,
782-
] as StyleProp<ViewStyle>
783-
}
765+
testID={`${testID}-surface`}
766+
style={[
767+
!theme.isV3 && styles.elevation,
768+
styles.bar,
769+
keyboardHidesNavigationBar // eslint-disable-next-line react-native/no-inline-styles
770+
? {
771+
// When the keyboard is shown, slide down the navigation bar
772+
transform: [
773+
{
774+
translateY: visibleAnim.interpolate({
775+
inputRange: [0, 1],
776+
outputRange: [layout.height, 0],
777+
}),
778+
},
779+
],
780+
// Absolutely position the navigation bar so that the content is below it
781+
// This is needed to avoid gap at bottom when the navigation bar is hidden
782+
position: keyboardVisible ? 'absolute' : undefined,
783+
}
784+
: null,
785+
barStyle,
786+
]}
784787
pointerEvents={
785788
layout.measured
786789
? keyboardHidesNavigationBar && keyboardVisible
@@ -1233,4 +1236,7 @@ const styles = StyleSheet.create({
12331236
borderRadius: OUTLINE_WIDTH / 4,
12341237
alignSelf: 'center',
12351238
},
1239+
elevation: {
1240+
elevation: 4,
1241+
},
12361242
});

Diff for: src/components/Button/Button.tsx

+2-1
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ export type Props = React.ComponentProps<typeof Surface> & {
106106
* Use this prop to apply custom height and width and to set the icon on the right with `flexDirection: 'row-reverse'`.
107107
*/
108108
contentStyle?: StyleProp<ViewStyle>;
109-
style?: StyleProp<ViewStyle>;
109+
style?: Animated.WithAnimatedValue<StyleProp<ViewStyle>>;
110110
/**
111111
* Style for the button text.
112112
*/
@@ -292,6 +292,7 @@ const Button = ({
292292
return (
293293
<Surface
294294
{...rest}
295+
testID={`${testID}-container`}
295296
style={
296297
[
297298
styles.button,

Diff for: src/components/Card/Card.tsx

+2-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ export type Props = React.ComponentProps<typeof Surface> & {
6767
* Changes Card shadow and background on iOS and Android.
6868
*/
6969
elevation?: 0 | 1 | 2 | 3 | 4 | 5 | Animated.Value;
70-
style?: StyleProp<ViewStyle>;
70+
style?: Animated.WithAnimatedValue<StyleProp<ViewStyle>>;
7171
/**
7272
* @optional
7373
*/
@@ -248,6 +248,7 @@ const Card = ({
248248
{...(isV3 && {
249249
elevation: isMode('elevated') ? computedElevation : 0,
250250
})}
251+
testID={`${testID}-container`}
251252
{...rest}
252253
>
253254
{isMode('outlined') && (

0 commit comments

Comments
 (0)