Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
* @format
*/

import type {ViewStyle} from '../../StyleSheet/StyleSheetTypes';
import type {
ViewStyle,
WithConditionalStyleValues,
} from '../../StyleSheet/StyleSheetTypes';
import type {
AnimatedComponentType,
AnimatedProps,
Expand Down Expand Up @@ -96,7 +99,7 @@ const AnimatedScrollViewWithInvertedRefreshControl =

// Handle animated props on `refreshControl`.
const [refreshControlAnimatedProps, refreshControlRef] = useAnimatedProps<
{style: ?ViewStyle},
{style: ?WithConditionalStyleValues<ViewStyle>},
$FlowFixMe,
>(intermediatePropsForRefreshControl);
// NOTE: Assumes that refreshControl.ref` and `refreshControl.style` can be
Expand Down
48 changes: 46 additions & 2 deletions packages/react-native/Libraries/StyleSheet/StyleSheetTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,42 @@ export type DimensionValue = number | string | 'auto' | null;

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

/**
* A style value that depends on media queries. The value of the last matching
* query is used, or the `default` value if no query matches. Supported media
* query: `@media (prefers-color-scheme: light | dark)`.
*
* ```
* backgroundColor: {
* default: 'white',
* '@media (prefers-color-scheme: dark)': 'black',
* }
* ```
*/
export type ConditionalStyleValue<out T> = Readonly<{
default: T,
'@media (prefers-color-scheme: light)'?: T,
'@media (prefers-color-scheme: dark)'?: T,
}>;

// Components convert these style props in JavaScript, so they do not support
// conditional values yet.
type ____StylePropsWithoutConditionalValues_Internal =
'objectFit' | 'resizeMode' | 'tintColor' | 'userSelect' | 'verticalAlign';

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TouchableOpacity also reads opacity in JS (_getChildStyleOpacityWithDefault).

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch! But adding it to this list would block opacity on View too. This probably needs to be handled natively. For now I'll add it as a known issue, wyt?


/**
* A style object in which each value can be a `ConditionalStyleValue`.
*/
export type WithConditionalStyleValues<out T> = T extends {...}
? {
readonly [
K in keyof T
]: K extends ____StylePropsWithoutConditionalValues_Internal
? T[K]
: T[K] | ConditionalStyleValue<T[K]>,
}
: T;

/**
* React Native's layout system is based on Flexbox and is powered both
* on iOS and Android by an open source project called `Yoga`:
Expand Down Expand Up @@ -1141,7 +1177,13 @@ export type ____DangerouslyImpreciseStyle_Internal = Readonly<{
}>;

export type StyleProp<out T> =
null | void | T | false | '' | ReadonlyArray<StyleProp<T>>;
| null
| void
| T
| WithConditionalStyleValues<T>
| false
| ''
| ReadonlyArray<StyleProp<T>>;

export type ____DangerouslyImpreciseStyleProp_Internal = StyleProp<
Partial<____DangerouslyImpreciseStyle_Internal>,
Expand All @@ -1161,7 +1203,9 @@ export type ____ImageStyleProp_Internal = StyleProp<
>;

export type ____Styles_Internal = {
readonly [key: string]: Partial<____DangerouslyImpreciseStyle_Internal>,
readonly [key: string]: Partial<
WithConditionalStyleValues<____DangerouslyImpreciseStyle_Internal>,
>,
...
};

Expand Down
15 changes: 8 additions & 7 deletions packages/react-native/Libraries/StyleSheet/splitLayoutProps.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,17 @@
* @format
*/

import type {ViewStyle} from './StyleSheetTypes';
import type {ViewStyle, WithConditionalStyleValues} from './StyleSheetTypes';

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

export default function splitLayoutProps(props: ?ViewStyle): {
outer: ?ViewStyle,
inner: ?ViewStyle,
export default function splitLayoutProps(props: ?Style): {
outer: ?Style,
inner: ?Style,
} {
let outer: ?MutableViewStyle = null;
let inner: ?MutableViewStyle = null;
let outer: ?MutableStyle = null;
let inner: ?MutableStyle = null;

if (props != null) {
outer = {};
Expand Down
Loading
Loading