/** * Copyright (c) Paymium. * * This source code is licensed under the MIT license found in the * LICENSE file in the root of this projects source tree. */ import { ScrollViewProps } from 'react-native'; import Animated, { FadeIn, FadeOut, SharedValue, useAnimatedReaction, useSharedValue, } from 'react-native-reanimated'; import { composeStyles, createStyles, inlineStyle, isTouchable, } from '@crossed/styled'; import { XBox } from '../../layout/XBox'; import { ScrollView } from 'react-native-gesture-handler'; import { Box } from '../../layout/Box'; import { Button, ButtonProps, ButtonTextProps } from '../../buttons/Button'; import { useState } from 'react'; import { heightStyles, linearGradientRounded, linearGradientUnderline, } from './styles'; import { ChevronLeft, ChevronRight } from '@crossed/unicons'; import { TabsContext } from './context'; const styles = createStyles(() => ({ default: { base: { zIndex: 1 } }, })); const ButtonScroll = ({ children, style, ...rest }: Omit & Pick) => { return ( ({ base: { position: 'absolute', top: 0, bottom: 0, zIndex: 100, }, })), style ).style()} > ); }; export const createList = (useTabsContext: () => TabsContext) => { const PrevButton = ({ widthLayout, }: { widthLayout: SharedValue; }) => { const { listTabRef, scroll, variant } = useTabsContext(); const [disabled, setDisabled] = useState(false); useAnimatedReaction( () => { return scroll.value; }, (currentValue, previousValue) => { if (currentValue !== previousValue) { setDisabled(currentValue === 0); } }, [widthLayout, scroll] ); const style = variant === 'rounded' ? linearGradientRounded : linearGradientUnderline; return ( { listTabRef.current?.scrollTo({ x: scroll.value >= widthLayout.value ? scroll.value - widthLayout.value : 0, }); }} > ); }; const NextButton = ({ widthLayout, widthContent, }: { widthLayout: SharedValue; widthContent: SharedValue; }) => { const { listTabRef, scroll, variant } = useTabsContext(); const [disabled, setDisabled] = useState(false); useAnimatedReaction( () => { return scroll.value; }, (currentValue, previousValue) => { if (currentValue !== previousValue) { setDisabled(currentValue + widthLayout.value >= widthContent.value); } }, [widthLayout, scroll, widthContent] ); const style = variant === 'rounded' ? linearGradientRounded : linearGradientUnderline; return ( { scroll.value <= widthLayout.value ? listTabRef.current?.scrollTo({ x: scroll.value + widthLayout.value, }) : listTabRef.current?.scrollToEnd(); }} > ); }; return ({ children, ...props }: Omit) => { const { listTabRef, scroll, shouldShow, setShow, widthLayout, size } = useTabsContext(); const widthContent = useSharedValue(0); useAnimatedReaction( () => { return widthLayout.value; }, (currentValue, previousValue) => { if (currentValue !== previousValue && !isTouchable) { setShow(currentValue < widthContent.value); } }, [widthLayout, widthContent, setShow] ); return ( {shouldShow && } {shouldShow && ( )} { scroll.value = nativeEvent.contentOffset.x; }} onContentSizeChange={(width) => { widthContent.value = width - (shouldShow ? 60 : 0); }} onLayout={({ nativeEvent: { layout } }) => { widthLayout.value = layout.width; }} stickyHeaderIndices={shouldShow ? [0] : []} {...props} contentContainerStyle={ composeStyles( inlineStyle(() => ({ base: { alignItems: 'center' } })) ).rnw().style } {...composeStyles( shouldShow && inlineStyle(() => ({ base: { position: 'absolute', top: 0, bottom: 0, left: 0, right: 0, zIndex: 30, }, })) ).rnw()} > {shouldShow && ( ({ base: { width: 30 } }))} /> )} {children} {shouldShow && ( ({ base: { width: 30 } }))} /> )} ); }; };