Skip to content

Commit df201fd

Browse files
Add types and an RNTester example for conditional style values
1 parent 7941f82 commit df201fd

9 files changed

Lines changed: 386 additions & 82 deletions

File tree

‎packages/react-native/Libraries/Animated/components/AnimatedScrollView.js‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@
88
* @format
99
*/
1010

11-
import type {ViewStyle} from '../../StyleSheet/StyleSheetTypes';
11+
import type {
12+
ViewStyle,
13+
WithConditionalStyleValues,
14+
} from '../../StyleSheet/StyleSheetTypes';
1215
import type {
1316
AnimatedComponentType,
1417
AnimatedProps,
@@ -99,7 +102,7 @@ const AnimatedScrollViewWithInvertedRefreshControl =
99102

100103
// Handle animated props on `refreshControl`.
101104
const [refreshControlAnimatedProps, refreshControlRef] = useAnimatedProps<
102-
{style: ?ViewStyle},
105+
{style: ?WithConditionalStyleValues<ViewStyle>},
103106
$FlowFixMe,
104107
>(intermediatePropsForRefreshControl);
105108
// NOTE: Assumes that refreshControl.ref` and `refreshControl.style` can be

‎packages/react-native/Libraries/StyleSheet/StyleSheetTypes.js‎

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,41 @@ export type DimensionValue = number | string | 'auto' | null;
4242

4343
export type CursorValue = 'auto' | 'pointer';
4444

45+
/**
46+
* A style value that depends on media queries. The value of the last matching
47+
* query is used, or the `default` value if no query matches. Supported media
48+
* query: `@media (prefers-color-scheme: light | dark)`.
49+
*
50+
* ```
51+
* backgroundColor: {
52+
* default: 'white',
53+
* '@media (prefers-color-scheme: dark)': 'black',
54+
* }
55+
* ```
56+
*/
57+
export type ConditionalStyleValue<out T> = Readonly<{
58+
default: T,
59+
[mediaQuery: string]: T,
60+
}>;
61+
62+
// Components convert these style props in JavaScript, so they do not support
63+
// conditional values yet.
64+
type ____StylePropsWithoutConditionalValues_Internal =
65+
'objectFit' | 'resizeMode' | 'tintColor' | 'userSelect' | 'verticalAlign';
66+
67+
/**
68+
* A style object in which each value can be a `ConditionalStyleValue`.
69+
*/
70+
export type WithConditionalStyleValues<out T> = T extends {...}
71+
? {
72+
readonly [
73+
K in keyof T
74+
]: K extends ____StylePropsWithoutConditionalValues_Internal
75+
? T[K]
76+
: T[K] | ConditionalStyleValue<T[K]>,
77+
}
78+
: T;
79+
4580
/**
4681
* React Native's layout system is based on Flexbox and is powered both
4782
* on iOS and Android by an open source project called `Yoga`:
@@ -1096,7 +1131,13 @@ export type ____DangerouslyImpreciseStyle_Internal = Readonly<{
10961131
}>;
10971132

10981133
export type StyleProp<out T> =
1099-
null | void | T | false | '' | ReadonlyArray<StyleProp<T>>;
1134+
| null
1135+
| void
1136+
| T
1137+
| WithConditionalStyleValues<T>
1138+
| false
1139+
| ''
1140+
| ReadonlyArray<StyleProp<T>>;
11001141

11011142
export type ____DangerouslyImpreciseStyleProp_Internal = StyleProp<
11021143
Partial<____DangerouslyImpreciseStyle_Internal>,
@@ -1118,7 +1159,9 @@ export type ____ImageStyleProp_Internal = StyleProp<
11181159
export type ____Styles_Internal = {
11191160
// $FlowFixMe[incompatible-exact]
11201161
// $FlowFixMe[incompatible-type]
1121-
readonly [key: string]: Partial<____DangerouslyImpreciseStyle_Internal>,
1162+
readonly [key: string]: Partial<
1163+
WithConditionalStyleValues<____DangerouslyImpreciseStyle_Internal>,
1164+
>,
11221165
...
11231166
};
11241167

‎packages/react-native/Libraries/StyleSheet/splitLayoutProps.js‎

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,16 @@
88
* @format
99
*/
1010

11-
import type {ViewStyle} from './StyleSheetTypes';
11+
import type {ViewStyle, WithConditionalStyleValues} from './StyleSheetTypes';
1212

13-
export default function splitLayoutProps(props: ?ViewStyle): {
14-
outer: ?ViewStyle,
15-
inner: ?ViewStyle,
13+
type Style = WithConditionalStyleValues<ViewStyle>;
14+
15+
export default function splitLayoutProps(props: ?Style): {
16+
outer: ?Style,
17+
inner: ?Style,
1618
} {
17-
let outer: ?ViewStyle = null;
18-
let inner: ?ViewStyle = null;
19+
let outer: ?Style = null;
20+
let inner: ?Style = null;
1921

2022
if (props != null) {
2123
// $FlowFixMe[incompatible-exact] Will contain a subset of keys from `props`.

0 commit comments

Comments
 (0)