From 0f18b9169087860803fd39d2ca47aa464cb42b2b Mon Sep 17 00:00:00 2001 From: Evan Bacon Date: Mon, 27 Jan 2020 11:10:06 -0800 Subject: [PATCH] refactor: split Overlay into a new component (#284) * refactor: split Overlay into a new component * Update packages/drawer/src/index.tsx Co-Authored-By: Satyajit Sahoo Co-authored-by: Satyajit Sahoo --- packages/drawer/src/views/Drawer.tsx | 29 ++------------- packages/drawer/src/views/Overlay.tsx | 51 +++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 27 deletions(-) create mode 100644 packages/drawer/src/views/Overlay.tsx diff --git a/packages/drawer/src/views/Drawer.tsx b/packages/drawer/src/views/Drawer.tsx index ce763995..00f20c5d 100644 --- a/packages/drawer/src/views/Drawer.tsx +++ b/packages/drawer/src/views/Drawer.tsx @@ -17,6 +17,7 @@ import { State, } from 'react-native-gesture-handler'; import Animated from 'react-native-reanimated'; +import Overlay from './Overlay'; const { Clock, @@ -25,7 +26,6 @@ const { clockRunning, startClock, stopClock, - interpolate, spring, abs, add, @@ -52,8 +52,6 @@ const FALSE = 0; const NOOP = 0; const UNSET = -1; -const PROGRESS_EPSILON = 0.05; - const DIRECTION_LEFT = 1; const DIRECTION_RIGHT = -1; @@ -577,26 +575,7 @@ export default class DrawerView extends React.PureComponent { {renderSceneContent({ progress: this.progress })} - + & { + progress: Animated.Node; +}; + +const Overlay = React.forwardRef(function Overlay( + { progress, style, ...props }: Props, + ref: React.Ref +) { + const animatedStyle = { + opacity: interpolate(progress, { + inputRange: [PROGRESS_EPSILON, 1], + outputRange: [0, 1], + }), + // We don't want the user to be able to press through the overlay when drawer is open + // One approach is to adjust the pointerEvents based on the progress + // But we can also send the overlay behind the screen, which works, and is much less code + zIndex: cond(greaterThan(progress, PROGRESS_EPSILON), 0, -1), + }; + + return ( + + ); +}); + +const styles = StyleSheet.create({ + overlay: { + ...StyleSheet.absoluteFillObject, + backgroundColor: 'rgba(0, 0, 0, 0.5)', + ...Platform.select({ + web: { + // Disable touch highlight on mobile Safari. + WebkitTapHighlightColor: 'transparent', + }, + default: {}, + }), + }, +}); + +export default Overlay;