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
61 changes: 54 additions & 7 deletions packages/react-native/Libraries/Modal/Modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,13 @@ type ModalBasePropsCore = {
onShow?: ?DirectEventHandler<null>,

/**
* The backdrop color of the modal's container. Defaults to `white` if
* `transparent` is `false`. Ignored if `transparent` is `true`.
* The backdrop color of the modal's container. Ignored if `transparent` is
* `true`. On Android, this overrides the transparent background implied by
* an explicit `dimAmount`.
*
* @deprecated Prefer `style={{backgroundColor: ...}}` for the container
* color. On Android, keep using `backdropColor` if you need a custom color
* while setting `dimAmount`.
*/
backdropColor?: ColorValue,

Expand Down Expand Up @@ -155,6 +160,37 @@ export type ModalPropsIOS = {
};

export type ModalPropsAndroid = {
/**
* Size the native window to its content instead of filling the parent.
* The window is not shown and `onShow` does not fire until the content
* measures to a positive width and height.
*
* @default `false`
* @platform android
*/
contentSized?: ?boolean,

/**
* Request closing on a completed outside tap when `contentSized` is enabled.
* The callback must update `visible` to dismiss the modal. Outside taps
* do not reach the presenting screen, even when dismissal is disabled.
*
* @default `false`
* @platform android
*/
dismissOnClickOutside?: ?boolean,

/**
* Amount of system dimming behind the modal, between 0 and 1.
* Ignored if `transparent` is `true`. When set, the modal's container
* defaults to a transparent background instead of `white`, unless
* `backdropColor` is set.
*
* @default `0.5`
* @platform android
*/
dimAmount?: ?number,

/**
* Controls whether to force hardware acceleration for the underlying window.
*
Expand Down Expand Up @@ -285,15 +321,17 @@ class Modal extends React.Component<ModalProps, ModalState> {
return null;
}

// Only override backgroundColor when transparent or backdropColor are
// explicitly set, so that these Modal-specific props take precedence
// over the generic style prop. The default backgroundColor ('white')
// is defined in styles.container below.
// Only override backgroundColor when transparent, backdropColor or
// dimAmount are explicitly set, so that these Modal-specific props take
// precedence over the generic style prop. The default backgroundColor
// ('white') is defined in styles.container below.
const containerStyles: {backgroundColor?: ColorValue} = {};
if (this.props.transparent === true) {
containerStyles.backgroundColor = 'transparent';
} else if (this.props.backdropColor != null) {
containerStyles.backgroundColor = this.props.backdropColor;
} else if (Platform.OS === 'android' && this.props.dimAmount != null) {
containerStyles.backgroundColor = 'transparent';
}

let animationType = this.props.animationType || 'none';
Expand Down Expand Up @@ -330,6 +368,9 @@ class Modal extends React.Component<ModalProps, ModalState> {
animationType={animationType}
presentationStyle={presentationStyle}
transparent={this.props.transparent}
dimAmount={this.props.dimAmount}
contentSized={Platform.OS === 'android' && this.props.contentSized}
dismissOnClickOutside={this.props.dismissOnClickOutside}
hardwareAccelerated={this.props.hardwareAccelerated}
onRequestClose={this.props.onRequestClose}
onShow={this.props.onShow}
Expand All @@ -351,7 +392,13 @@ class Modal extends React.Component<ModalProps, ModalState> {
<ScrollView.Context.Provider value={null}>
<View
// $FlowFixMe[incompatible-type]
style={[styles.container, this.props.style, containerStyles]}
style={[
Platform.OS === 'android' && this.props.contentSized === true
? null
: styles.container,
this.props.style,
containerStyles,
]}
collapsable={false}>
{innerChildren}
</View>
Expand Down
143 changes: 143 additions & 0 deletions packages/react-native/Libraries/Modal/__tests__/Modal-itest.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,149 @@ describe('<Modal>', () => {
</rn-modalHostView>,
);
});

describe('dismissOnClickOutside', () => {
it('forwards outside-press dismissal and resets when removed', () => {
Fantom.runTask(() => {
root.render(<Modal dismissOnClickOutside={true} />);
});

expect(
root.getRenderedOutput({props: ['dismissOnClickOutside']}).toJSX(),
).toEqual(
<rn-modalHostView dismissOnClickOutside="true">
<rn-view />
</rn-modalHostView>,
);

Fantom.runTask(() => {
root.render(<Modal />);
});

expect(
root.getRenderedOutput({props: ['dismissOnClickOutside']}).toJSX(),
).toEqual(
<rn-modalHostView>
<rn-view />
</rn-modalHostView>,
);
});
});

describe('dimAmount', () => {
it('forwards a custom dim amount and resets to the default when removed', () => {
Fantom.runTask(() => {
root.render(<Modal dimAmount={0.6} />);
});

expect(root.getRenderedOutput({props: ['dimAmount']}).toJSX()).toEqual(
<rn-modalHostView dimAmount="0.6">
<rn-view />
</rn-modalHostView>,
);

Fantom.runTask(() => {
root.render(<Modal />);
});

expect(root.getRenderedOutput({props: ['dimAmount']}).toJSX()).toEqual(
<rn-modalHostView>
<rn-view />
</rn-modalHostView>,
);
});

it('defaults the container to a transparent background', () => {
Fantom.runTask(() => {
root.render(<Modal dimAmount={0.6} />);
});

expect(
root.getRenderedOutput({props: ['backgroundColor']}).toJSX(),
).toEqual(
<rn-modalHostView>
<rn-view />
</rn-modalHostView>,
);
});

it('keeps an explicit backdropColor', () => {
Fantom.runTask(() => {
root.render(<Modal dimAmount={0.6} backdropColor="red" />);
});

expect(
root.getRenderedOutput({props: ['backgroundColor']}).toJSX(),
).toEqual(
<rn-modalHostView>
<rn-view backgroundColor="rgba(255, 0, 0, 1)" />
</rn-modalHostView>,
);
});
});

describe('contentSized', () => {
it('forwards content sizing and resets when removed', () => {
Fantom.runTask(() => {
root.render(<Modal contentSized={true} />);
});

expect(
root.getRenderedOutput({props: ['contentSized']}).toJSX(),
).toEqual(
<rn-modalHostView contentSized="true">
<rn-view />
</rn-modalHostView>,
);

Fantom.runTask(() => {
root.render(<Modal />);
});

expect(
root.getRenderedOutput({props: ['contentSized']}).toJSX(),
).toEqual(
<rn-modalHostView>
<rn-view />
</rn-modalHostView>,
);
});

it('does not apply the default container style', () => {
Fantom.runTask(() => {
root.render(<Modal contentSized={true} />);
});

expect(
root
.getRenderedOutput({
props: ['backgroundColor', 'flex', 'left', 'top'],
})
.toJSX(),
).toEqual(
<rn-modalHostView>
<rn-view />
</rn-modalHostView>,
);
});

it('keeps a style backgroundColor', () => {
Fantom.runTask(() => {
root.render(
<Modal contentSized={true} style={{backgroundColor: 'red'}} />,
);
});

expect(
root.getRenderedOutput({props: ['backgroundColor']}).toJSX(),
).toEqual(
<rn-modalHostView>
<rn-view backgroundColor="rgba(255, 0, 0, 1)" />
</rn-modalHostView>,
);
});
});

describe('animationType', () => {
it('renders a Modal with animationType="none" by default', () => {
Fantom.runTask(() => {
Expand Down
3 changes: 3 additions & 0 deletions packages/react-native/ReactAndroid/api/ReactAndroid.api
Original file line number Diff line number Diff line change
Expand Up @@ -5128,6 +5128,9 @@ public class com/facebook/react/viewmanagers/ModalHostViewManagerDelegate : com/
public abstract interface class com/facebook/react/viewmanagers/ModalHostViewManagerInterface : com/facebook/react/uimanager/ViewManagerWithGeneratedInterface {
public abstract fun setAllowSwipeDismissal (Landroid/view/View;Z)V
public abstract fun setAnimationType (Landroid/view/View;Ljava/lang/String;)V
public abstract fun setContentSized (Landroid/view/View;Z)V
public abstract fun setDimAmount (Landroid/view/View;F)V
public abstract fun setDismissOnClickOutside (Landroid/view/View;Z)V
public abstract fun setHardwareAccelerated (Landroid/view/View;Z)V
public abstract fun setIdentifier (Landroid/view/View;I)V
public abstract fun setNavigationBarTranslucent (Landroid/view/View;Z)V
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ internal class ReactModalHostManager :
view.transparent = value
}

@ReactProp(name = "dimAmount", defaultFloat = 0.5f)
override fun setDimAmount(view: ReactModalHostView, value: Float) {
view.dimAmount = value
}

@ReactProp(name = "statusBarTranslucent")
override fun setStatusBarTranslucent(view: ReactModalHostView, value: Boolean) {
view.statusBarTranslucent = value
Expand All @@ -69,6 +74,16 @@ internal class ReactModalHostManager :
// iOS only
}

@ReactProp(name = "contentSized", defaultBoolean = false)
override fun setContentSized(view: ReactModalHostView, value: Boolean) {
view.contentSized = value
}

@ReactProp(name = "dismissOnClickOutside", defaultBoolean = false)
override fun setDismissOnClickOutside(view: ReactModalHostView, value: Boolean) {
view.dismissOnClickOutside = value
}

@ReactProp(name = "presentationStyle")
override fun setPresentationStyle(view: ReactModalHostView, value: String?): Unit = Unit

Expand Down
Loading
Loading