Skip to content

Commit 6bc12b3

Browse files
Add types and an RNTester example for conditional style values
1 parent a507adf commit 6bc12b3

10 files changed

Lines changed: 394 additions & 85 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,
@@ -96,7 +99,7 @@ const AnimatedScrollViewWithInvertedRefreshControl =
9699

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

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

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,42 @@ 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+
'@media (prefers-color-scheme: light)'?: T,
60+
'@media (prefers-color-scheme: dark)'?: T,
61+
}>;
62+
63+
// Components convert these style props in JavaScript, so they do not support
64+
// conditional values yet.
65+
type ____StylePropsWithoutConditionalValues_Internal =
66+
'objectFit' | 'resizeMode' | 'tintColor' | 'userSelect' | 'verticalAlign';
67+
68+
/**
69+
* A style object in which each value can be a `ConditionalStyleValue`.
70+
*/
71+
export type WithConditionalStyleValues<out T> = T extends {...}
72+
? {
73+
readonly [
74+
K in keyof T
75+
]: K extends ____StylePropsWithoutConditionalValues_Internal
76+
? T[K]
77+
: T[K] | ConditionalStyleValue<T[K]>,
78+
}
79+
: T;
80+
4581
/**
4682
* React Native's layout system is based on Flexbox and is powered both
4783
* on iOS and Android by an open source project called `Yoga`:
@@ -1141,7 +1177,13 @@ export type ____DangerouslyImpreciseStyle_Internal = Readonly<{
11411177
}>;
11421178

11431179
export type StyleProp<out T> =
1144-
null | void | T | false | '' | ReadonlyArray<StyleProp<T>>;
1180+
| null
1181+
| void
1182+
| T
1183+
| WithConditionalStyleValues<T>
1184+
| false
1185+
| ''
1186+
| ReadonlyArray<StyleProp<T>>;
11451187

11461188
export type ____DangerouslyImpreciseStyleProp_Internal = StyleProp<
11471189
Partial<____DangerouslyImpreciseStyle_Internal>,
@@ -1161,7 +1203,9 @@ export type ____ImageStyleProp_Internal = StyleProp<
11611203
>;
11621204

11631205
export type ____Styles_Internal = {
1164-
readonly [key: string]: Partial<____DangerouslyImpreciseStyle_Internal>,
1206+
readonly [key: string]: Partial<
1207+
WithConditionalStyleValues<____DangerouslyImpreciseStyle_Internal>,
1208+
>,
11651209
...
11661210
};
11671211

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

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

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

13-
type MutableViewStyle = {-readonly [K in keyof ViewStyle]: ViewStyle[K]};
13+
type Style = WithConditionalStyleValues<ViewStyle>;
14+
type MutableStyle = {-readonly [K in keyof Style]: Style[K]};
1415

15-
export default function splitLayoutProps(props: ?ViewStyle): {
16-
outer: ?ViewStyle,
17-
inner: ?ViewStyle,
16+
export default function splitLayoutProps(props: ?Style): {
17+
outer: ?Style,
18+
inner: ?Style,
1819
} {
19-
let outer: ?MutableViewStyle = null;
20-
let inner: ?MutableViewStyle = null;
20+
let outer: ?MutableStyle = null;
21+
let inner: ?MutableStyle = null;
2122

2223
if (props != null) {
2324
outer = {};

0 commit comments

Comments
 (0)