|
| 1 | +import React, {useCallback, useEffect, useState} from 'react'; |
| 2 | +import {LayoutChangeEvent, LayoutRectangle, StyleSheet} from 'react-native'; |
| 3 | +import {useSharedValue, useAnimatedStyle, withTiming, withRepeat, Easing} from 'react-native-reanimated'; |
| 4 | +import View from '../view'; |
| 5 | +import Text from '../text'; |
| 6 | +import {MarqueeDirections, MarqueeProps} from './types'; |
| 7 | + |
| 8 | +const DEFAULT_DURATION = 3000; |
| 9 | +const DEFAULT_DURATION_PER_WORD = 250; |
| 10 | + |
| 11 | +function Marquee(props: MarqueeProps) { |
| 12 | + const {label, labelStyle, direction = MarqueeDirections.LEFT, duration, numberOfReps = -1, containerStyle} = props; |
| 13 | + |
| 14 | + const calcDuration = () => { |
| 15 | + const numOfWords = label.split(' ').length; |
| 16 | + return DEFAULT_DURATION + DEFAULT_DURATION_PER_WORD * numOfWords; |
| 17 | + }; |
| 18 | + |
| 19 | + const isHorizontal = direction === MarqueeDirections.LEFT || direction === MarqueeDirections.RIGHT; |
| 20 | + const fixedDuration = duration || (isHorizontal ? calcDuration() : DEFAULT_DURATION); |
| 21 | + |
| 22 | + const [viewLayout, setViewLayout] = useState<LayoutRectangle | undefined>(undefined); |
| 23 | + const [textLayout, setTextLayout] = useState<LayoutRectangle | undefined>(undefined); |
| 24 | + |
| 25 | + const offset = useSharedValue<number | undefined>(undefined); |
| 26 | + |
| 27 | + let initialOffset = 0; |
| 28 | + let axisX = false; |
| 29 | + let axisY = false; |
| 30 | + |
| 31 | + if (isHorizontal) { |
| 32 | + axisX = true; |
| 33 | + } else { |
| 34 | + axisY = true; |
| 35 | + } |
| 36 | + |
| 37 | + const onLayoutView = useCallback((event: LayoutChangeEvent) => { |
| 38 | + setViewLayout(event.nativeEvent.layout); |
| 39 | + }, []); |
| 40 | + |
| 41 | + const onLayoutText = useCallback((event: LayoutChangeEvent) => { |
| 42 | + setTextLayout(event.nativeEvent.layout); |
| 43 | + }, []); |
| 44 | + |
| 45 | + const startAnimation = (fromValue: number, toValue: number, backToValue: number) => { |
| 46 | + initialOffset = fromValue; |
| 47 | + offset.value = initialOffset; |
| 48 | + |
| 49 | + offset.value = withRepeat(withTiming(toValue, {duration: fixedDuration, easing: Easing.linear}), |
| 50 | + numberOfReps, |
| 51 | + false, |
| 52 | + finished => { |
| 53 | + if (finished) { |
| 54 | + offset.value = initialOffset; |
| 55 | + offset.value = withTiming(backToValue, {duration: fixedDuration, easing: Easing.linear}); |
| 56 | + } |
| 57 | + }); |
| 58 | + }; |
| 59 | + |
| 60 | + useEffect(() => { |
| 61 | + if (viewLayout && textLayout) { |
| 62 | + switch (direction) { |
| 63 | + case MarqueeDirections.RIGHT: |
| 64 | + startAnimation(-textLayout.width, viewLayout.width, 0); |
| 65 | + break; |
| 66 | + case MarqueeDirections.LEFT: |
| 67 | + startAnimation(viewLayout?.width, -textLayout.width, viewLayout.width - textLayout.width); |
| 68 | + break; |
| 69 | + case MarqueeDirections.UP: |
| 70 | + startAnimation(viewLayout.height, -textLayout.height, viewLayout.height - textLayout.height); |
| 71 | + break; |
| 72 | + case MarqueeDirections.DOWN: |
| 73 | + startAnimation(-textLayout.height, viewLayout.height, 0); |
| 74 | + break; |
| 75 | + } |
| 76 | + } |
| 77 | + }, [viewLayout, textLayout]); |
| 78 | + |
| 79 | + const translateStyle = useAnimatedStyle(() => { |
| 80 | + if (offset.value) { |
| 81 | + return { |
| 82 | + transform: [{translateX: axisX ? offset.value : 0}, {translateY: axisY ? offset.value : 0}], |
| 83 | + position: 'absolute', |
| 84 | + width: !isHorizontal || textLayout?.width ? textLayout?.width : '400%' |
| 85 | + }; |
| 86 | + } |
| 87 | + return {position: 'absolute', width: !isHorizontal || textLayout?.width ? textLayout?.width : '400%'}; |
| 88 | + }); |
| 89 | + |
| 90 | + return ( |
| 91 | + <View style={[styles.container, containerStyle]} onLayout={onLayoutView}> |
| 92 | + <View reanimated style={[translateStyle]}> |
| 93 | + <Text style={[styles.text, labelStyle]} onLayout={onLayoutText}> |
| 94 | + {label} |
| 95 | + </Text> |
| 96 | + </View> |
| 97 | + <Text style={[styles.text, labelStyle, styles.hiddenText]} numberOfLines={1}> |
| 98 | + {label} |
| 99 | + </Text> |
| 100 | + </View> |
| 101 | + ); |
| 102 | +} |
| 103 | + |
| 104 | +export {MarqueeProps, MarqueeDirections}; |
| 105 | + |
| 106 | +export default Marquee; |
| 107 | + |
| 108 | +const styles = StyleSheet.create({ |
| 109 | + container: {overflow: 'hidden'}, |
| 110 | + text: {alignSelf: 'center'}, |
| 111 | + hiddenText: {color: 'transparent'} |
| 112 | +}); |
0 commit comments