From 9ce02dff5b995d384bdc5ea43323a0d03ca754cc Mon Sep 17 00:00:00 2001 From: Paul Frazee Date: Wed, 7 Dec 2022 15:51:06 -0600 Subject: Add HorzSwipe gesture and integrate it into the ViewSelector --- src/view/com/util/Selector.tsx | 60 ++++++++++++++++++++---------------------- 1 file changed, 28 insertions(+), 32 deletions(-) (limited to 'src/view/com/util/Selector.tsx') diff --git a/src/view/com/util/Selector.tsx b/src/view/com/util/Selector.tsx index 06e8cda80..ed042d7c1 100644 --- a/src/view/com/util/Selector.tsx +++ b/src/view/com/util/Selector.tsx @@ -1,10 +1,11 @@ import React, {createRef, useState, useMemo} from 'react' -import {StyleSheet, Text, TouchableWithoutFeedback, View} from 'react-native' -import Animated, { - SharedValue, - useAnimatedStyle, - interpolate, -} from 'react-native-reanimated' +import { + Animated, + StyleSheet, + Text, + TouchableWithoutFeedback, + View, +} from 'react-native' import {colors} from '../../lib/styles' interface Layout { @@ -12,17 +13,15 @@ interface Layout { width: number } -const DEFAULT_SWIPE_GESTURE_INTERP = {value: 0} - export function Selector({ selectedIndex, items, - swipeGestureInterp, + panX, onSelect, }: { selectedIndex: number items: string[] - swipeGestureInterp?: SharedValue + panX: Animated.Value onSelect?: (index: number) => void }) { const [itemLayouts, setItemLayouts] = useState( @@ -43,27 +42,24 @@ export function Selector({ return [left, middle, right] }, [selectedIndex, items, itemLayouts]) - const interp = swipeGestureInterp || DEFAULT_SWIPE_GESTURE_INTERP - const underlinePos = useAnimatedStyle(() => { - const other = - interp.value === 0 - ? currentLayouts[1] - : interp.value < 0 - ? currentLayouts[0] - : currentLayouts[2] - return { - left: interpolate( - Math.abs(interp.value), - [0, 1], - [currentLayouts[1].x, other.x], - ), - width: interpolate( - Math.abs(interp.value), - [0, 1], - [currentLayouts[1].width, other.width], - ), - } - }, [currentLayouts, interp]) + const underlineStyle = { + left: panX.interpolate({ + inputRange: [-1, 0, 1], + outputRange: [ + currentLayouts[0].x, + currentLayouts[1].x, + currentLayouts[2].x, + ], + }), + width: panX.interpolate({ + inputRange: [-1, 0, 1], + outputRange: [ + currentLayouts[0].width, + currentLayouts[1].width, + currentLayouts[2].width, + ], + }), + } const onLayout = () => { const promises = [] @@ -89,7 +85,7 @@ export function Selector({ return ( - + {items.map((item, i) => { const selected = i === selectedIndex return ( -- cgit 1.4.1