tooot/src/screens/Actions.tsx

114 lines
3.1 KiB
TypeScript
Raw Normal View History

2021-08-29 15:25:38 +02:00
import { RootStackScreenProps } from '@utils/navigation/navigators'
import { StyleConstants } from '@utils/styles/constants'
import { useTheme } from '@utils/styles/ThemeManager'
2022-06-03 23:18:24 +02:00
import React, { useCallback, useEffect } from 'react'
2021-08-29 15:25:38 +02:00
import { Dimensions, StyleSheet, View } from 'react-native'
2022-11-20 16:14:08 +01:00
import { PanGestureHandler, State, TapGestureHandler } from 'react-native-gesture-handler'
2021-08-29 15:25:38 +02:00
import Animated, {
Extrapolate,
interpolate,
runOnJS,
useAnimatedGestureHandler,
useAnimatedStyle,
useSharedValue,
withTiming
} from 'react-native-reanimated'
2022-11-20 16:14:08 +01:00
import { useSafeAreaInsets } from 'react-native-safe-area-context'
2022-06-03 23:18:24 +02:00
import ActionsAltText from './Actions/AltText'
2021-01-30 01:29:15 +01:00
2022-04-30 21:29:08 +02:00
const ScreenActions = ({
route: { params },
navigation
}: RootStackScreenProps<'Screen-Actions'>) => {
const { colors } = useTheme()
const insets = useSafeAreaInsets()
2021-08-29 15:25:38 +02:00
2022-04-30 21:29:08 +02:00
const DEFAULT_VALUE = 350
2022-12-18 21:16:21 +01:00
const screenHeight = Dimensions.get('window').height
2022-04-30 21:29:08 +02:00
const panY = useSharedValue(DEFAULT_VALUE)
useEffect(() => {
panY.value = withTiming(0)
}, [])
const styleTop = useAnimatedStyle(() => {
return {
2022-11-20 16:14:08 +01:00
bottom: interpolate(panY.value, [0, screenHeight], [0, -screenHeight], Extrapolate.CLAMP)
2022-04-30 21:29:08 +02:00
}
})
const dismiss = useCallback(() => {
navigation.goBack()
}, [])
const onGestureEvent = useAnimatedGestureHandler({
onActive: ({ translationY }) => {
panY.value = translationY
},
onEnd: ({ velocityY }) => {
if (velocityY > 500) {
runOnJS(dismiss)()
} else {
panY.value = withTiming(0)
2021-08-29 15:25:38 +02:00
}
2022-04-30 21:29:08 +02:00
}
})
2021-08-29 15:25:38 +02:00
2022-04-30 21:29:08 +02:00
const actions = () => {
switch (params.type) {
2022-06-03 23:18:24 +02:00
case 'alt_text':
return <ActionsAltText text={params.text} />
2022-04-30 21:29:08 +02:00
}
}
2021-08-29 15:25:38 +02:00
2022-04-30 21:29:08 +02:00
return (
2022-11-20 16:14:08 +01:00
<Animated.View style={{ flex: 1 }}>
<TapGestureHandler
onHandlerStateChange={({ nativeEvent }) => {
if (nativeEvent.state === State.ACTIVE) {
dismiss()
}
}}
>
<Animated.View
style={[styles.overlay, { backgroundColor: colors.backgroundOverlayInvert }]}
2022-04-30 21:29:08 +02:00
>
2022-11-20 16:14:08 +01:00
<PanGestureHandler onGestureEvent={onGestureEvent}>
<Animated.View
style={[
styles.container,
styleTop,
{
backgroundColor: colors.backgroundDefault,
paddingBottom: insets.bottom || StyleConstants.Spacing.L
}
]}
>
<View style={[styles.handle, { backgroundColor: colors.primaryOverlay }]} />
{actions()}
</Animated.View>
</PanGestureHandler>
</Animated.View>
</TapGestureHandler>
</Animated.View>
2022-04-30 21:29:08 +02:00
)
}
2021-01-30 01:29:15 +01:00
2021-08-29 15:25:38 +02:00
const styles = StyleSheet.create({
overlay: {
flex: 1,
justifyContent: 'flex-end'
},
container: {
paddingTop: StyleConstants.Spacing.M
},
handle: {
alignSelf: 'center',
width: StyleConstants.Spacing.S * 8,
height: StyleConstants.Spacing.S / 2,
borderRadius: 100,
top: -StyleConstants.Spacing.M * 2
},
button: {
marginHorizontal: StyleConstants.Spacing.Global.PagePadding * 2
}
})
2021-01-30 01:29:15 +01:00
export default ScreenActions