Skip to content

Commit

Permalink
refactor: update backdrop for android
Browse files Browse the repository at this point in the history
  • Loading branch information
enesozturk committed Jul 19, 2022
1 parent 6127770 commit ba2fc1f
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 10 deletions.
56 changes: 47 additions & 9 deletions src/components/backdrop/Backdrop.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { memo } from 'react';
import { StyleSheet } from 'react-native';
import React, { memo, useMemo } from 'react';
import Animated, {
useAnimatedGestureHandler,
useAnimatedProps,
useAnimatedStyle,
withDelay,
withTiming,
Expand All @@ -19,15 +19,22 @@ import { styles } from './styles';
import {
CONTEXT_MENU_STATE,
HOLD_ITEM_TRANSFORM_DURATION,
IS_IOS,
WINDOW_HEIGHT,
} from '../../constants';
import {
BACKDROP_LIGHT_BACKGROUND_COLOR,
BACKDROP_DARK_BACKGROUND_COLOR,
} from './constants';
import { useInternal } from '../../hooks';
import { Pressable, StyleSheet } from 'react-native';
import { GestureHandlerProps } from '../holdItem/types';

const AnimatedBlurView = Animated.createAnimatedComponent(BlurView);
const AnimatedPressable = Animated.createAnimatedComponent(Pressable);

const AnimatedBlurView = IS_IOS
? Animated.createAnimatedComponent(BlurView)
: Animated.View;

type Context = {
startPosition: {
Expand Down Expand Up @@ -88,6 +95,17 @@ const BackdropComponent = () => {
};
});

const animatedContainerProps = useAnimatedProps(() => {
return {
intensity: withTiming(
state.value === CONTEXT_MENU_STATE.ACTIVE ? 100 : 0,
{
duration: HOLD_ITEM_TRANSFORM_DURATION,
}
),
};
});

const animatedInnerContainerStyle = useAnimatedStyle(() => {
const backgroundColor =
theme.value === 'light'
Expand All @@ -97,15 +115,35 @@ const BackdropComponent = () => {
return { backgroundColor };
}, [theme]);

// TODO: TapGestureHandler is now working on Android. Check later.
const GestureHandler = useMemo(() => {
if (IS_IOS) {
return ({ children }: GestureHandlerProps) => (
<TapGestureHandler onHandlerStateChange={tapGestureEvent}>
{children}
</TapGestureHandler>
);
} else {
return ({ children }: GestureHandlerProps) => (
<AnimatedPressable
style={[styles.container, animatedContainerStyle]}
onPress={() => {
'worklet';
state.value = CONTEXT_MENU_STATE.END;
}}
>
{children}
</AnimatedPressable>
);
}
}, []);

return (
<TapGestureHandler
onGestureEvent={tapGestureEvent}
onHandlerStateChange={tapGestureEvent}
>
<GestureHandler>
<AnimatedBlurView
// @ts-ignore
intensity={100}
tint="default"
animatedProps={animatedContainerProps}
style={[styles.container, animatedContainerStyle]}
>
<Animated.View
Expand All @@ -115,7 +153,7 @@ const BackdropComponent = () => {
]}
/>
</AnimatedBlurView>
</TapGestureHandler>
</GestureHandler>
);
};

Expand Down
2 changes: 1 addition & 1 deletion src/components/menu/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { TransformOriginAnchorPosition } from '../../utils/calculations';

export type MenuItemProps = {
text: string;
icon?: string;
icon?: string | (() => React.ReactElement);
onPress?: (...args: any[]) => void;
isTitle?: boolean;
isDestructive?: boolean;
Expand Down

0 comments on commit ba2fc1f

Please sign in to comment.