react-native-auto-scroll

repository·main·Indexed 42 days ago

https://github.com/homielab/react-native-auto-scroll

A high-performance React Native library for creating automatic horizontal scrolling effects, similar to the HTML marquee tag. Version 1.1.2 uses native driver animations via useNativeDriver to ensure smooth performance on the native UI thread. The <AutoScroll> component allows customization of scroll duration, delay, direction (LTR/RTL), and padding width.

Tokens
1.5K
Snippets
3
Records
5
Agent score
3%

What's inside react-native-auto-scroll

  1. Install @homielab/react-native-auto-scroll

    main

    Install the package using npm or yarn to enable automatic horizontal scrolling in your React Native application. The animations are optimized using useNativeDriver to run on the native UI thread for better performance.

    npm install --save @homielab/react-native-auto-scroll
    
    # or
    
    yarn add @homielab/react-native-auto-scroll
  2. Use the AutoScroll component

    main

    To implement auto-scrolling, wrap your target components (like Text or Image) inside the <AutoScroll> component. The component handles the animation logic automatically.

    import * as React from 'react';
    import { Text, View, Image, StyleSheet } from 'react-native';
    import AutoScroll from '@homielab/react-native-auto-scroll';
    
    export default class App extends React.Component {
      render() {
        return (
          <View style={styles.container}>
            {/* Example with an Image */}
            <AutoScroll style={styles.scrolling1}>
              <Image
                style={styles.image}
                source={require('./assets/merry-christmas-png.png')}
              />
            </AutoScroll>
    
            {/* Example with Text and custom padding */}
            <AutoScroll style={styles.scrolling2} endPaddingWidth={50}>
              <Text style={styles.welcome}>MERRY CHRISTMAS AND HAPPY NEW YEAR</Text>
            </AutoScroll>
          </View>
        );
      }
    }
    
    const styles = StyleSheet.create({
      container: {
        flex: 1,
        justifyContent: 'center',
        alignItems: 'center',
        backgroundColor: '#F5FCFF',
      },
      image: {
        width: 200,
        height: 200,
      },
      scrolling1: {
        width: 400,
        padding: 10,
        marginBottom: 10,
      },
      scrolling2: {
        backgroundColor: 'red',
        width: 400,
        padding: 10,
        marginBottom: 10,
      },
      welcome: {
        color: 'white',
        fontSize: 20,
        fontWeight: 'bold',
        textAlign: 'center',
        margin: 10,
      },
    });
  3. Configure AutoScroll properties

    main

    The <AutoScroll> component wraps your content to create a horizontal scrolling effect (similar to the HTML <marquee> tag). Use the following props to customize the behavior:

    PropTypeDescriptionDefault
    styleViewStyleStyles for the container viewundefined
    endPaddingWidthnumberThe padding width to the next round100
    durationnumberTime to finish a round (ms)undefined
    delaynumberDelay time before starting the auto scroll animation (ms)1000
    isLTRbooleanWhether the scroll direction is Left to Rightfalse
  4. Use the AutoScroll component

    main

    The AutoScroll component provides a smooth, looping horizontal auto-scroll effect for its children. It is designed to work with a single child element (typically a View containing text or images) and automatically handles the looping logic by cloning the child to create a seamless transition.

    Usage Requirements

    • The children prop must be a single React.ReactElement that can accept a ref and an onLayout prop (e.g., a View).
    • The component uses Animated for performance, utilizing the native driver on non-web platforms.

    Basic Example

    import AutoScroll from '@homielab/react-native-auto-scroll';
    import { View, Text } from 'react-native';
    
    const MyScrollingContent = () => (
      <AutoScroll style={{ width: 300 }}>
        <View style={{ flexDirection: 'row' }}>
          <Text> This is a scrolling message! </Text>
          <Text> This is a scrolling message! </Text>
        </View>
      </AutoScroll>
    );
  5. Configure AutoScrollProps

    main

    The AutoScroll component accepts the following props to control its behavior and appearance:

    PropTypeDefaultDescription
    childrenReact.ReactElement<ViewProps & { ref?: React.Ref<View> }>RequiredThe element to be scrolled. Must accept a ref and onLayout.
    styleStyleProp<ViewStyle>undefinedStyles for the outer container View.
    endPaddingWidthnumber100The width of the divider/padding used to separate the original content from its clone during the loop.
    durationnumberCalculatedThe duration of one full scroll cycle in milliseconds. If not provided, it is calculated as max(3000, 50 * contentWidth).
    delaynumber0The delay (in ms) before the very first scroll cycle starts. Subsequent loops have 0 delay.
    isLTRbooleanfalseDirectionality. false (default) treats the layout as RTL. true treats it as LTR.