Skip to content
Draft
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 @@ -99,7 +102,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
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,8 @@ function InternalTextInput(props: TextInputProps): React.Node {
flattenedStyle.fontWeight.toString() as TextStyle['fontWeight'];
}

if (flattenedStyle.verticalAlign != null) {
// A conditional value (with media queries) is not converted.
if (typeof flattenedStyle.verticalAlign === 'string') {
overrides = overrides || ({} as {...TextStyle});
overrides.textAlignVertical =
verticalAlignToTextAlignVerticalMap[flattenedStyle.verticalAlign];
Expand Down
8 changes: 7 additions & 1 deletion packages/react-native/Libraries/Image/Image.android.js
Original file line number Diff line number Diff line change
Expand Up @@ -300,9 +300,15 @@ let BaseImage: AbstractImageAndroid = ({
}

const flattenedStyle_ = flattenStyle<ImageStyleProp>(style);
const objectFit_ = convertObjectFitToResizeMode(flattenedStyle_?.objectFit);
const styleObjectFit_ = flattenedStyle_?.objectFit;
// A conditional value (with media queries) is not converted.
const objectFit_ =
typeof styleObjectFit_ === 'string'
? convertObjectFitToResizeMode(styleObjectFit_)
: undefined;
const resizeMode_ =
objectFit_ || resizeMode || flattenedStyle_?.resizeMode || 'cover';
// $FlowFixMe[incompatible-type] A conditional value is resolved natively.
nativeProps.resizeMode = resizeMode_;

const actualRef = useWrapRefWithImageAttachedCallbacks(forwardedRef);
Expand Down
12 changes: 10 additions & 2 deletions packages/react-native/Libraries/Image/Image.ios.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,9 +190,17 @@ let BaseImage: AbstractImageIOS = ({
nativeProps.source = sources;

const flattenedStyle = flattenStyle<ImageStyleProp>(resolvedStyle);
const objectFit = convertObjectFitToResizeMode(flattenedStyle?.objectFit);
nativeProps.resizeMode =
const styleObjectFit = flattenedStyle?.objectFit;
// A conditional value (with media queries) is not converted.
const objectFit =
typeof styleObjectFit === 'string'
? convertObjectFitToResizeMode(styleObjectFit)
: undefined;
const resolvedResizeMode =
objectFit || resizeMode || flattenedStyle?.resizeMode || 'cover';
// $FlowFixMe[incompatible-type] A conditional value is resolved natively.
nativeProps.resizeMode = resolvedResizeMode;
// $FlowFixMe[incompatible-type] A conditional value is resolved natively.
nativeProps.tintColor = tintColor ?? flattenedStyle?.tintColor;

// Maintain pre-existing order, accessibilityLabel takes priority over alt
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,130 @@ import deepDiffer from '../../Utilities/differ/deepDiffer';

const emptyObject = {};

// Conditional values (for example `{default: 'white', '@media
// (prefers-color-scheme: dark)': 'black'}`) are sent to native under this prop,
// and native resolves them. See `ConditionalProps.h` for the format.
const CONDITIONAL_PROPS_KEY = '__conditionalProps';

type ConditionalPropCondition = {
queries: Array<string>,
value: unknown,
};

class ConditionalProp {
definition: {
default: unknown,
conditions: Array<ConditionalPropCondition>,
};

constructor(
defaultValue: unknown,
conditions: Array<ConditionalPropCondition>,
) {
this.definition = {default: defaultValue, conditions};
}
}

// Set when a `ConditionalProp` is added to the payload, so that `create` and
// `diff` only move them to `CONDITIONAL_PROPS_KEY` when needed.
let hasConditionalProps = false;

function isConditionalValue(value: unknown): boolean {
return (
typeof value === 'object' &&
value !== null &&
!Array.isArray(value) &&
'default' in value
);
}

function addConditions(
value: unknown,
queries: Array<string>,
conditions: Array<ConditionalPropCondition>,
process: ?(value: unknown) => unknown,
): void {
if (!isConditionalValue(value)) {
conditions.push({
queries,
value: value == null ? null : process != null ? process(value) : value,
});
return;
}
// $FlowFixMe[incompatible-type] refined by `isConditionalValue`
const conditionalValue: {readonly [key: string]: unknown} = value;
// The default value applies first, whatever its position in the object.
addConditions(conditionalValue.default, queries, conditions, process);
for (const key in conditionalValue) {
if (key !== 'default') {
addConditions(
conditionalValue[key],
[...queries, key],
conditions,
process,
);
}
}
}

function createConditionalProp(
value: unknown,
process: ?(value: unknown) => unknown,
): ConditionalProp {
const conditions: Array<ConditionalPropCondition> = [];
// Nested conditions are flattened: each condition has all the queries that
// lead to its value.
addConditions(value, [], conditions, process);
hasConditionalProps = true;
// The first condition has no query: it is the default value.
return new ConditionalProp(conditions[0].value, conditions.slice(1));
}

function processAttributeValue(
attributeConfig: unknown,
value: unknown,
): unknown {
const process: ?(value: unknown) => unknown =
typeof attributeConfig === 'object' &&
attributeConfig !== null &&
typeof attributeConfig.process === 'function'
? // $FlowFixMe[unclear-type] AnyAttributeType is defined upstream with $FlowFixMe
(attributeConfig.process as any)
: null;
if (isConditionalValue(value)) {
return createConditionalProp(value, process);
}
return process != null ? process(value) : value;
}

function moveConditionalProps(
payload: null | {[key: string]: unknown},
): null | {[key: string]: unknown} {
if (!hasConditionalProps) {
return payload;
}
hasConditionalProps = false;
if (payload == null) {
return payload;
}
const plainProps: {[key: string]: unknown} = {};
let conditionalProps: ?{[key: string]: unknown} = null;
for (const key in payload) {
const value = payload[key];
if (value instanceof ConditionalProp) {
conditionalProps = conditionalProps ?? ({} as {[key: string]: unknown});
conditionalProps[key] = value.definition;
} else {
plainProps[key] = value;
}
}
if (conditionalProps == null) {
return payload;
}
plainProps[CONDITIONAL_PROPS_KEY] = conditionalProps;
return plainProps;
}

/**
* Create a payload that contains all the updates between two sets of props.
*
Expand Down Expand Up @@ -82,19 +206,16 @@ function restoreDeletedValuesInNestedArray(
nextProp = null;
}

if (typeof attributeConfig !== 'object') {
// case: !Object is the default case
updatePayload[propKey] = nextProp;
} else if (
if (
typeof attributeConfig !== 'object' ||
typeof attributeConfig.diff === 'function' ||
typeof attributeConfig.process === 'function'
) {
// case: CustomAttributeConfiguration
const nextValue =
typeof attributeConfig.process === 'function'
? attributeConfig.process(nextProp)
: nextProp;
updatePayload[propKey] = nextValue;
// case: !Object | CustomAttributeConfiguration
updatePayload[propKey] = processAttributeValue(
attributeConfig,
nextProp,
);
}
// $FlowFixMe[incompatible-use] found when upgrading Flow
removedKeys[propKey] = false;
Expand Down Expand Up @@ -291,19 +412,16 @@ function diffProperties(
// value. It doesn't matter if nothing changed, we'll set it anyway.

// Pattern match on: attributeConfig
if (typeof attributeConfig !== 'object') {
// case: !Object is the default case
updatePayload[propKey] = nextProp;
} else if (
if (
typeof attributeConfig !== 'object' ||
typeof attributeConfig.diff === 'function' ||
typeof attributeConfig.process === 'function'
) {
// case: CustomAttributeConfiguration
const nextValue =
typeof attributeConfig.process === 'function'
? attributeConfig.process(nextProp)
: nextProp;
updatePayload[propKey] = nextValue;
// case: !Object | CustomAttributeConfiguration
updatePayload[propKey] = processAttributeValue(
attributeConfig,
nextProp,
);
}
continue;
}
Expand All @@ -319,27 +437,29 @@ function diffProperties(
// a normal leaf has changed
(updatePayload || (updatePayload = {} as {[key: string]: unknown}))[
propKey
] = nextProp;
] = processAttributeValue(attributeConfig, nextProp);
}
} else if (
typeof attributeConfig.diff === 'function' ||
typeof attributeConfig.process === 'function'
) {
// case: CustomAttributeConfiguration
// A custom differ does not know conditional values.
const customDiffer =
typeof attributeConfig.diff === 'function' &&
!isConditionalValue(prevProp) &&
!isConditionalValue(nextProp)
? attributeConfig.diff
: null;
const shouldUpdate =
prevProp === undefined ||
(typeof attributeConfig.diff === 'function'
? attributeConfig.diff(prevProp, nextProp)
(customDiffer != null
? customDiffer(prevProp, nextProp)
: defaultDiffer(prevProp, nextProp));
if (shouldUpdate) {
const nextValue =
typeof attributeConfig.process === 'function'
? // $FlowFixMe[incompatible-use] found when upgrading Flow
attributeConfig.process(nextProp)
: nextProp;
(updatePayload || (updatePayload = {} as {[key: string]: unknown}))[
propKey
] = nextValue;
] = processAttributeValue(attributeConfig, nextProp);
}
} else {
// default: fallthrough case when nested properties are defined
Expand Down Expand Up @@ -460,20 +580,21 @@ function addNestedProperty(
continue;
}
} else if (typeof attributeConfig === 'object') {
if (typeof attributeConfig.process === 'function') {
// An atomic prop with custom processing.
newValue = attributeConfig.process(prop);
} else if (typeof attributeConfig.diff === 'function') {
// An atomic prop with custom diffing. We don't need to do diffing when adding props.
newValue = prop;
if (
typeof attributeConfig.process === 'function' ||
typeof attributeConfig.diff === 'function'
) {
// An atomic prop with custom processing or diffing. We don't need to
// do diffing when adding props.
newValue = processAttributeValue(attributeConfig, prop);
}
} else {
if (typeof prop === 'function') {
// A function prop. It represents an event handler. Pass it to native as 'true'.
newValue = true;
} else {
// An atomic prop. Doesn't need to be flattened.
newValue = prop;
newValue = processAttributeValue(attributeConfig, prop);
}
}

Expand Down Expand Up @@ -512,18 +633,20 @@ export function create(
props: NestedNode,
validAttributes: AttributeConfiguration,
): null | {[key: string]: unknown} {
return addNestedProperty(null, props, validAttributes);
return moveConditionalProps(addNestedProperty(null, props, validAttributes));
}

export function diff(
prevProps: {[key: string]: unknown},
nextProps: {[key: string]: unknown},
validAttributes: AttributeConfiguration,
): null | {[key: string]: unknown} {
return diffProperties(
null, // updatePayload
prevProps,
nextProps,
validAttributes,
return moveConditionalProps(
diffProperties(
null, // updatePayload
prevProps,
nextProps,
validAttributes,
),
);
}
Loading