diff --git a/example/src/App.tsx b/example/src/App.tsx index 49d58c656..005947d19 100644 --- a/example/src/App.tsx +++ b/example/src/App.tsx @@ -7,16 +7,12 @@ const Stack = createStackNavigator(); function App() { return ( - + require('./screens/Root').default} options={{ headerShown: false }} /> - require('./screens/BasicExample').default} - /> {/* basic examples */} JSX.Element) | null; style?: ViewStyle; + header?: (() => JSX.Element) | null; + onLayout?: () => void; } const ContactList = ({ @@ -26,6 +27,7 @@ const ContactList = ({ count = 50, header = null, style, + onLayout, }: ContactListProps) => { // hooks const { bottom: bottomSafeArea } = useSafeArea(); @@ -136,6 +138,7 @@ const ContactList = ({ {header && header()} {data.map(renderScrollViewItem)} + ); } diff --git a/example/src/components/handle/Handle.tsx b/example/src/components/handle/Handle.tsx index c0ffb8c40..7c59618ac 100644 --- a/example/src/components/handle/Handle.tsx +++ b/example/src/components/handle/Handle.tsx @@ -2,51 +2,39 @@ import React, { useMemo } from 'react'; import { StyleProp, StyleSheet, ViewStyle } from 'react-native'; import { BottomSheetHandleProps } from '@gorhom/bottom-sheet'; import Animated, { - interpolate, Extrapolate, + interpolate, useAnimatedStyle, useDerivedValue, } from 'react-native-reanimated'; +import { toRad } from 'react-native-redash'; +import { transformOrigin } from '../../utilities/transformOrigin'; interface HandleProps extends BottomSheetHandleProps { style?: StyleProp; } -const toRad = (degrees: number) => { - 'worklet'; - return degrees * (Math.PI / 180); -}; - -export const transformOrigin = ({ x, y }, ...transformations) => { - 'worklet'; - return [ - { translateX: x }, - { translateY: y }, - ...transformations, - { translateX: x * -1 }, - { translateY: y * -1 }, - ]; -}; - const Handle: React.FC = ({ style, animatedPositionIndex }) => { //#region animations const indicatorTransformOriginY = useDerivedValue(() => - interpolate(animatedPositionIndex.value, [0, 1, 2], [-1, 0, 1]) + interpolate( + animatedPositionIndex.value, + [0, 1, 2], + [-1, 0, 1], + Extrapolate.CLAMP + ) ); //#endregion //#region styles - const containerStyle = useMemo( - () => [styles.header, style], - // eslint-disable-next-line react-hooks/exhaustive-deps - [style] - ); + const containerStyle = useMemo(() => [styles.header, style], [style]); const containerAnimatedStyle = useAnimatedStyle(() => { const borderTopRadius = interpolate( animatedPositionIndex.value, [1, 2], - [20, 0] + [20, 0], + Extrapolate.CLAMP ); return { borderTopLeftRadius: borderTopRadius, @@ -64,13 +52,14 @@ const Handle: React.FC = ({ style, animatedPositionIndex }) => { const leftIndicatorRotate = interpolate( animatedPositionIndex.value, [0, 1, 2], - [toRad(-30), 0, toRad(30)] + [toRad(-30), 0, toRad(30)], + Extrapolate.CLAMP ); return { transform: transformOrigin( { x: 0, y: indicatorTransformOriginY.value }, { - rotate: leftIndicatorRotate, + rotate: `${leftIndicatorRotate}rad`, }, { translateX: -5, @@ -89,13 +78,14 @@ const Handle: React.FC = ({ style, animatedPositionIndex }) => { const rightIndicatorRotate = interpolate( animatedPositionIndex.value, [0, 1, 2], - [toRad(30), 0, toRad(-30)] + [toRad(30), 0, toRad(-30)], + Extrapolate.CLAMP ); return { transform: transformOrigin( { x: 0, y: indicatorTransformOriginY.value }, { - rotate: rightIndicatorRotate, + rotate: `${rightIndicatorRotate}rad`, }, { translateX: 5, diff --git a/example/src/screens/BasicExample.tsx b/example/src/screens/BasicExample.tsx index 561619fd7..9b9536d3e 100644 --- a/example/src/screens/BasicExample.tsx +++ b/example/src/screens/BasicExample.tsx @@ -1,11 +1,8 @@ import React, { useCallback, useMemo, useRef } from 'react'; import { View, StyleSheet, Text } from 'react-native'; import { useHeaderHeight } from '@react-navigation/stack'; -import { useValue, concat } from 'react-native-reanimated'; import BottomSheet from '@gorhom/bottom-sheet'; -import Handle from '../components/handle'; import Button from '../components/button'; -import { ReText } from 'react-native-redash'; import ContactList from '../components/contactList'; const BasicExample = () => { @@ -14,14 +11,10 @@ const BasicExample = () => { const headerHeight = useHeaderHeight(); // variables - const snapPoints = useMemo(() => [150, 450], []); - const position = useValue(0); + const snapPoints = useMemo(() => [150, 300, 450], []); // styles // callbacks - const handleSheetChanges = useCallback((index: number) => { - console.log('handleSheetChanges', index); - }, []); const handleSnapPress = useCallback(index => { bottomSheetRef.current?.snapTo(index); }, []); @@ -39,6 +32,11 @@ const BasicExample = () => { ); }, []); + const renderSheetContent = useCallback( + () => , + [renderHeader] + ); + return (