Skip to content

Commit f8fa76f

Browse files
AnuMessi10meta-codesync[bot]
authored andcommitted
fix: forward style prop to Modal inner container View (#56181)
Summary: `Modal` accepts a `style` prop via `ViewProps` (since `ModalProps` spreads `...ViewProps`) but silently discards it. The inner container `<View>` only applies `styles.container` and `containerStyles` (which handles `backdropColor`/`transparent`), so any style passed by the consumer has no effect. For example, `<Modal style={{ padding: 20 }}>` compiles without errors but the padding is never applied. This PR forwards `this.props.style` to the inner container View with a carefully designed precedence chain: ``` styles.container (layout defaults + white background) → this.props.style (consumer overrides) → containerStyles (explicit transparent / backdropColor always win) ``` `containerStyles` now only sets `backgroundColor` when `transparent` or `backdropColor` are explicitly passed, ensuring these Modal-specific API props always take precedence over the generic `style` prop while still allowing consumers to customize other style properties. **Precedence examples:** | Usage | Result | |---|---| | `<Modal style={{ backgroundColor: 'red' }}>` | Red (user overrides default white) | | `<Modal transparent>` | Transparent (explicit prop wins) | | `<Modal transparent style={{ backgroundColor: 'red' }}>` | Transparent wins | | `<Modal backdropColor="blue" style={{ backgroundColor: 'red' }}>` | Blue wins | | `<Modal style={{ padding: 20 }}>` | Works, no conflicts | ## Changelog: [GENERAL] [FIXED] - Forward `style` prop to Modal's inner container View with correct precedence so consumer styles are applied without overriding `transparent` or `backdropColor` Pull Request resolved: #56181 Test Plan: 1. Render a Modal with a custom style prop: ```jsx <Modal visible style={{ padding: 40, backgroundColor: 'red' }}> <View style={{ flex: 1, backgroundColor: 'white' }}> <Text>Hello</Text> </View> </Modal> ``` 2. **Before fix:** `padding` and `backgroundColor` are silently ignored 3. **After fix:** The modal container has 40px padding and a red background Also verified that: - `transparent={true}` always produces a transparent background, even if `style={{ backgroundColor }}` is set - `backdropColor` always takes precedence over `style.backgroundColor` - Default behavior (no `style` prop) is unchanged — white background - Non-backgroundColor style properties (padding, margin, etc.) work without conflicts Reviewed By: javache Differential Revision: D101170804 Pulled By: zeyap fbshipit-source-id: b138648ce41b55eb794c79a0217f905d5cc9a5f2
1 parent c2280e3 commit f8fa76f

2 files changed

Lines changed: 15 additions & 10 deletions

File tree

packages/react-native/Libraries/Modal/Modal.js

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -288,12 +288,16 @@ class Modal extends React.Component<ModalProps, ModalState> {
288288
return null;
289289
}
290290

291-
const containerStyles = {
292-
backgroundColor:
293-
this.props.transparent === true
294-
? 'transparent'
295-
: (this.props.backdropColor ?? 'white'),
296-
};
291+
// Only override backgroundColor when transparent or backdropColor are
292+
// explicitly set, so that these Modal-specific props take precedence
293+
// over the generic style prop. The default backgroundColor ('white')
294+
// is defined in styles.container below.
295+
const containerStyles: {backgroundColor?: ColorValue} = {};
296+
if (this.props.transparent === true) {
297+
containerStyles.backgroundColor = 'transparent';
298+
} else if (this.props.backdropColor != null) {
299+
containerStyles.backgroundColor = this.props.backdropColor;
300+
}
297301

298302
let animationType = this.props.animationType || 'none';
299303

@@ -349,7 +353,7 @@ class Modal extends React.Component<ModalProps, ModalState> {
349353
<ScrollView.Context.Provider value={null}>
350354
<View
351355
// $FlowFixMe[incompatible-type]
352-
style={[styles.container, containerStyles]}
356+
style={[styles.container, this.props.style, containerStyles]}
353357
collapsable={false}>
354358
{innerChildren}
355359
</View>
@@ -380,6 +384,7 @@ const styles = StyleSheet.create({
380384
[side]: 0,
381385
top: 0,
382386
flex: 1,
387+
backgroundColor: 'white',
383388
},
384389
});
385390

packages/react-native/Libraries/Modal/__tests__/__snapshots__/Modal-test.js.snap

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@ exports[`Modal should render as <RCTModalHostView> when not mocked 1`] = `
2626
style={
2727
Array [
2828
Object {
29+
"backgroundColor": "white",
2930
"flex": 1,
3031
"left": 0,
3132
"top": 0,
3233
},
33-
Object {
34-
"backgroundColor": "white",
35-
},
34+
undefined,
35+
Object {},
3636
]
3737
}
3838
>

0 commit comments

Comments
 (0)