Skip to content

Commit ecfdeb8

Browse files
committed
Refactor (add useFadeView) and fix pointerEvents
1 parent 3a51d6c commit ecfdeb8

File tree

3 files changed

+74
-24
lines changed

3 files changed

+74
-24
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/// <reference types="react" />
2+
import { ModalProps } from '../../../components/modal';
3+
import { TransitionViewAnimationType } from '../../TransitionView';
4+
import { ImperativeDialogProps } from '../types';
5+
export declare type AnimationType = TransitionViewAnimationType;
6+
export declare type FadeViewProps = Pick<ImperativeDialogProps, 'initialVisibility' | 'testID'> & Pick<ModalProps, 'overlayBackgroundColor'>;
7+
export interface FadeViewMethods {
8+
hideNow: () => void;
9+
}
10+
declare const useFadeView: (props: FadeViewProps) => {
11+
FadeView: JSX.Element;
12+
hideNow: () => void;
13+
fade: (type: AnimationType) => void;
14+
};
15+
export default useFadeView;

src/incubator/Dialog/ImperativeDialog.tsx

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
11
import React, {useMemo, useCallback, useState, useImperativeHandle, forwardRef} from 'react';
22
import {StyleSheet} from 'react-native';
3-
import {useSharedValue, withTiming, useAnimatedStyle} from 'react-native-reanimated';
43
import View from '../../components/view';
54
import Modal from '../../components/modal';
65
import TransitionView, {TransitionViewAnimationType} from '../TransitionView';
76
import PanView from '../panView';
87
import useAlignmentStyle from './helpers/useAlignmentStyle';
8+
import useFadeView from './helpers/useFadeView';
99
import {ImperativeDialogProps, ImperativeDialogMethods, DialogDirections, DialogDirectionsEnum} from './types';
1010
export {DialogDirections, DialogDirectionsEnum};
1111

12-
import {Colors} from 'style';
13-
const DEFAULT_OVERLAY_BACKGROUND_COLORS = Colors.rgba(Colors.black, 0.2);
14-
1512
const ImperativeDialog = (props: ImperativeDialogProps, ref: any) => {
1613
const {
1714
initialVisibility = false,
@@ -23,10 +20,14 @@ const ImperativeDialog = (props: ImperativeDialogProps, ref: any) => {
2320
testID
2421
} = props;
2522
const transitionAnimatorRef = React.createRef<typeof TransitionView>();
26-
const {overlayBackgroundColor = DEFAULT_OVERLAY_BACKGROUND_COLORS, ...otherModalProps} = modalProps;
27-
const fadeOpacity = useSharedValue<number>(Number(initialVisibility));
23+
const {overlayBackgroundColor, ...otherModalProps} = modalProps;
2824
const [visible, setVisible] = useState(initialVisibility);
2925
const {alignmentStyle} = useAlignmentStyle(props);
26+
const {FadeView, hideNow, fade} = useFadeView({
27+
initialVisibility,
28+
testID: `${testID}.overlayFadingBackground`,
29+
overlayBackgroundColor
30+
});
3031

3132
const open = useCallback(() => {
3233
if (!visible) {
@@ -54,16 +55,10 @@ const ImperativeDialog = (props: ImperativeDialogProps, ref: any) => {
5455
}, [close]);
5556

5657
const onPanViewDismiss = useCallback(() => {
57-
fadeOpacity.value = 0;
58+
hideNow();
5859
setVisible(false);
5960
onDismiss?.();
60-
// eslint-disable-next-line react-hooks/exhaustive-deps
61-
}, [onDismiss, setVisible]);
62-
63-
const onTransitionAnimationStart = useCallback((type: TransitionViewAnimationType) => {
64-
fadeOpacity.value = withTiming(Number(type === 'enter'), {duration: 300});
65-
// eslint-disable-next-line react-hooks/exhaustive-deps
66-
}, []);
61+
}, [hideNow, onDismiss, setVisible]);
6762

6863
const onTransitionAnimationEnd = useCallback((type: TransitionViewAnimationType) => {
6964
if (type === 'exit') {
@@ -73,13 +68,6 @@ const ImperativeDialog = (props: ImperativeDialogProps, ref: any) => {
7368
},
7469
[onDismiss, setVisible]);
7570

76-
const fadeStyle = useAnimatedStyle(() => {
77-
return {
78-
opacity: fadeOpacity.value,
79-
backgroundColor: overlayBackgroundColor
80-
};
81-
}, [overlayBackgroundColor]);
82-
8371
const renderDialog = () => {
8472
{
8573
/* TODO: remove?
@@ -98,7 +86,7 @@ const ImperativeDialog = (props: ImperativeDialogProps, ref: any) => {
9886
ref={transitionAnimatorRef}
9987
enterFrom={direction}
10088
exitTo={direction}
101-
onAnimationStart={onTransitionAnimationStart}
89+
onAnimationStart={fade}
10290
onAnimationEnd={onTransitionAnimationEnd}
10391
>
10492
{children}
@@ -119,8 +107,10 @@ const ImperativeDialog = (props: ImperativeDialogProps, ref: any) => {
119107
onRequestClose={onBackgroundPress}
120108
onDismiss={undefined}
121109
>
122-
<View testID={`${testID}.overlayFadingBackground`} absF reanimated style={fadeStyle} pointerEvents="none"/>
123-
<View pointerEvents={'none'} style={alignmentStyle}>{renderDialog()}</View>
110+
{FadeView}
111+
<View pointerEvents={'box-none'} style={alignmentStyle}>
112+
{renderDialog()}
113+
</View>
124114
</Modal>
125115
);
126116
};
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/* eslint-disable react-hooks/exhaustive-deps */
2+
import React, {useCallback} from 'react';
3+
import {useSharedValue, withTiming, useAnimatedStyle} from 'react-native-reanimated';
4+
import {Colors} from 'style';
5+
import View from '../../../components/view';
6+
import {ModalProps} from '../../../components/modal';
7+
import {TransitionViewAnimationType} from '../../TransitionView';
8+
import {ImperativeDialogProps} from '../types';
9+
10+
const DEFAULT_OVERLAY_BACKGROUND_COLORS = Colors.rgba(Colors.black, 0.2);
11+
12+
export type AnimationType = TransitionViewAnimationType;
13+
14+
export type FadeViewProps = Pick<ImperativeDialogProps, 'initialVisibility' | 'testID'> &
15+
Pick<ModalProps, 'overlayBackgroundColor'>;
16+
17+
export interface FadeViewMethods {
18+
hideNow: () => void;
19+
}
20+
21+
const useFadeView = (props: FadeViewProps) => {
22+
const {initialVisibility, overlayBackgroundColor = DEFAULT_OVERLAY_BACKGROUND_COLORS, testID} = props;
23+
const fadeOpacity = useSharedValue<number>(Number(initialVisibility));
24+
25+
const hideNow = useCallback(() => {
26+
fadeOpacity.value = 0;
27+
}, []);
28+
29+
const fade = useCallback((type: AnimationType) => {
30+
fadeOpacity.value = withTiming(Number(type === 'enter'), {duration: 300});
31+
}, []);
32+
33+
const fadeStyle = useAnimatedStyle(() => {
34+
return {
35+
opacity: fadeOpacity.value,
36+
backgroundColor: overlayBackgroundColor
37+
};
38+
}, [overlayBackgroundColor]);
39+
40+
const FadeView = <View testID={testID} absF reanimated style={fadeStyle} pointerEvents="none"/>;
41+
42+
return {FadeView, hideNow, fade};
43+
};
44+
45+
export default useFadeView;

0 commit comments

Comments
 (0)