Skip to content

Commit acb7263

Browse files
bugfix(webClick) Sometimes on web the modal got opened and immediately closed again because the BackdropPressableComponent press was triggered
1 parent d756824 commit acb7263

1 file changed

Lines changed: 11 additions & 4 deletions

File tree

src/ModalView.web.tsx

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import { useMemo } from 'react';
2-
import type { FC } from 'react';
32

43
import { createPortal } from 'react-dom';
54
import { StyleSheet, View, Pressable } from 'react-native';
65

76
import { useID } from './hooks/useID';
87
import { useModalRegistry } from './hooks/useModalRegistry';
8+
9+
import type { MouseEvent } from 'react';
910
import type { ModalViewProps } from './types';
1011

1112
export type ModalViewWebProps = Omit<
@@ -24,7 +25,7 @@ export enum DismissalSource {
2425
Backdrop = 'Backdrop',
2526
}
2627

27-
export const ModalView: FC<ModalViewWebProps> = ({
28+
export const ModalView = ({
2829
modalId,
2930
children,
3031
renderBackdrop,
@@ -33,7 +34,7 @@ export const ModalView: FC<ModalViewWebProps> = ({
3334
BackdropPressableComponent = Pressable,
3435
backdropColor = defaultBackdropColor,
3536
animationType = 'none',
36-
}) => {
37+
}: ModalViewWebProps) => {
3738
const currentModalId = useID(modalId);
3839
const { modals, isBackdropVisible } = useModalRegistry(currentModalId);
3940
const modalIsOpen = modals.has(currentModalId);
@@ -65,7 +66,13 @@ export const ModalView: FC<ModalViewWebProps> = ({
6566
styles.backdropPressable,
6667
!isBackdropVisible && styles.backdropHidden,
6768
]}
68-
onPress={() => onRequestDismiss?.(DismissalSource.Backdrop)}
69+
onPress={(e) => {
70+
const event = e as unknown as MouseEvent<HTMLElement>;
71+
72+
if (event.target === event.currentTarget) {
73+
onRequestDismiss?.(DismissalSource.Backdrop);
74+
}
75+
}}
6976
>
7077
{renderBackdrop ? (
7178
renderBackdrop()

0 commit comments

Comments
 (0)