1.1.1 • Published 2 years ago

react-native-collapsible-pages v1.1.1

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

react-native-collapsible-pages

The Collapsible Pages component is a complex UI component designed to display primary and secondary views on the same screen, allowing seamless transition between the two views. It provides a user-friendly interface for handling hardware back actions and detecting the active index on scrolling.

Key Features:

  • Display primary and secondary views with smooth transitions.
  • Automatic handling of hardware back actions and active index detection on scrolling.
  • Automatic detection of next open view and scrolling there on call of onNext function from any position.
  • Support for specifying different heights for each data item, with internal handling for smooth transitions.

Installation

yarn add react-native-collapsible-pages react-native-reanimated react-native-gesture-handler

Demo

Animation

Usage

import CollapsiblePages from 'react-native-collapsible-pages';

// Check example directory in github repo.

import * as React from 'react'

import CollapsiblePages, {
    type IItemsListAnimation,
    type IViewProps,
} from 'react-native-collapsible-pages'
import { GestureHandlerRootView } from 'react-native-gesture-handler'
import data, {
    type IStepDetails,
    type IYogaData,
} from './screens/collapsable-list/data'
import PrimaryViewN from './screens/collapsable-list/views/PrimaryView'
import SecondaryViewN from './screens/collapsable-list/views/SecondaryView'
import { StatusBar, StyleSheet } from 'react-native'

// Define the component outside of the parent component
const PrimaryViewWrapper =
    (rest: { activeIndex: number; stepDetails: IStepDetails }) =>
    (props: IViewProps) => {
        const { stepDetails } = rest
        return <PrimaryViewN {...props} stepDetails={stepDetails} />
    }

const SecondaryViewWrapper =
    (rest: { stepDetails: IStepDetails }) => (props: IViewProps) => {
        const { stepDetails } = rest
        return <SecondaryViewN {...props} stepDetails={stepDetails} />
    }

export default function App() {
    const [activeIndex, setActiveIndex] = React.useState(0)

    // Inside the parent component
    const views: IItemsListAnimation[] = React.useMemo(
        () =>
            data.map((item: IYogaData) => ({
                primaryView: PrimaryViewWrapper({
                    activeIndex: activeIndex,
                    stepDetails: item.stepDetails,
                }),

                secondaryView: SecondaryViewWrapper({
                    stepDetails: item.stepDetails,
                }),
                ...item.viewDetails,
            })),
        [activeIndex],
    )

    console.log('active index', activeIndex)
    return (
        <GestureHandlerRootView style={styles.container}>
            <CollapsiblePages
                data={views}
                activeIndex={activeIndex}
                setActiveIndex={setActiveIndex}
                callBack={(props: any, index: number) => {
                    // Callback when onNext function is triggered from primary or secondary view
                    // to set any outside props that are shared among different component
                    console.log(props, index)
                }}
                showPreviousSecodaryViewThreshold={0.6}
            />
        </GestureHandlerRootView>
    )
}

const styles = StyleSheet.create({
    container: {
        flex: 1,
        marginTop: StatusBar.currentHeight,
        backgroundColor: '#ffffff33',
    },
})

Props and Types

ZeroToOne Type

PropertyTypeDescription
ZeroToOne0 \| 0.1 \| 0.2 \| 0.3 \| 0.4 \| 0.5 \| 0.6 \| 0.7 \| 0.8 \| 0.9 \| 1A type representing a value between 0 and 1.

IViewProps Interface

PropertyTypeDescriptionRequiredDefault
onNextFunctionA function to handle the next action. You can call this on any button action inside primary or secondary view.Yes-
activebooleanA boolean indicating whether the view is active (if primary view is opened) or not.Yes-

IItemsListAnimation Props

PropertyTypeDescriptionRequiredDefault
primaryView(props: IViewProps) => React.ReactElementA function that takes props and returns a React element representing the primary view of the animation.Yes-
secondaryView(props: IViewProps) => React.ReactElementA function that takes props and returns a React element representing the secondary view of the animation.Yes-
secondaryViewHeightnumberThe height of the secondary view.Yes-
primaryViewHeightnumberThe height of the primary view.Yes-

For Component Props

PropertyTypeDescriptionRequiredDefault
dataIItemsListAnimation[]An array of IItemsListAnimation objects representing the animations to be displayed.Yes-
callBack(props: any, index: number) => voidA function that takes props and an index, and you can add any side effect here when a onNext is triggered and transition happens.Yes-
activeIndexnumberThe index of the active animation.Yes-
setActiveIndexFunctionA function that sets the active index of the animation.Yes-
initialCollapsedStateboolean[]An optional array of booleans representing the initial collapsed state of each animation.Noall false according to length of data
showAllbooleanAn optional boolean indicating whether to show all view at once.Nofalse
onPressHardwareBackFunctionAn optional function to be called when the hardware back button is pressed. This will overide default behaviourNo-
showPreviousSecodaryViewThresholdZeroToOneA value indicating the threshold for showing the previous secondary view.Yes0.5
activeViewDetectionBottomThresholdZeroToOneAn optional value indicating the threshold for detecting the bottom of the active view.No0.3

Contributing

See the contributing guide to learn how to contribute to the repository and the development workflow.

License

MIT


Made with create-react-native-library

1.1.1

2 years ago

1.1.0

2 years ago