Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

A cross-platform Collapsible Tab View component for React Native

License

NotificationsYou must be signed in to change notification settings

PedroBern/react-native-collapsible-tab-view

Repository files navigation

Build StatusVersionMIT LicenseRuns with Expo

🚀 Version 6 released with Reanimated v3 support

React Native Collapsible Tab View is a versatile library for creating collapsible tab views usingReanimated.

  • Explore theexamples for the source code of the Expo app.

Credits

Thereact-native-tab-view example app was used as a template for the demos.

Demo

DefaultSnaprevealHeaderOnScrollrevealHeaderOnScroll + Snap

Features

  • UI thread animations and interactions
  • High customizability
  • FullTypeScript support
  • Lazy loading with fade-in animation
  • DiffClamp header
  • Interpolated header
  • Scroll snap (with interpolated header)
  • Animated snap (with diffClamp header)
  • Scrollable tabs, inspired by thereact-native-tab-view tab bar

Installation

To install the library, open a terminal in your project's root directory and run:

yarn add react-native-collapsible-tab-view react-native-pager-view

Then, addReanimated,follow the official installation guide.

Quick Start

importReactfrom'react'import{View,StyleSheet,ListRenderItem}from'react-native'import{Tabs}from'react-native-collapsible-tab-view'constHEADER_HEIGHT=250constDATA=[0,1,2,3,4]constidentity=(v:unknown):string=>v+''constHeader=()=>{return<Viewstyle={styles.header}/>}constExample:React.FC=()=>{constrenderItem:ListRenderItem<number>=React.useCallback(({ index})=>{return(<Viewstyle={[styles.box,index%2===0 ?styles.boxB :styles.boxA]}/>)},[])return(<Tabs.ContainerrenderHeader={Header}headerHeight={HEADER_HEIGHT}// optional><Tabs.Tabname="A"><Tabs.FlatListdata={DATA}renderItem={renderItem}keyExtractor={identity}/></Tabs.Tab><Tabs.Tabname="B"><Tabs.ScrollView><Viewstyle={[styles.box,styles.boxA]}/><Viewstyle={[styles.box,styles.boxB]}/></Tabs.ScrollView></Tabs.Tab></Tabs.Container>)}conststyles=StyleSheet.create({box:{height:250,width:'100%',},boxA:{backgroundColor:'white',},boxB:{backgroundColor:'#D8D8D8',},header:{height:HEADER_HEIGHT,width:'100%',backgroundColor:'#2196f3',},})exportdefaultExample

Guides

Scrolling on the Header

To enable scrolling from the header, follow these steps:

  • If theHeaderComponentdoes not contain touchable components, set thepointerEvents prop to'none'.
  • If theHeaderComponentdoes contain touchable components, set thepointerEvents prop to'box-none' to ensure they function properly.

Note: If any child component within theHeaderComponent shouldnot respond to touches, such as an<Image /> element, set itspointerEvents prop to'none'. Otherwise, it may unintentionally become the target of a touch gesture on iOS devices and prevent scrolling.

API Reference

Core

Tabs.Container

Basic usage looks like this:

import{Tabs}from'react-native-collapsible-tab-view'constExample=()=>{return(<Tabs.ContainerrenderHeader={MyHeader}><Tabs.Tabname="A"><ScreenA/></Tabs.Tab><Tabs.Tabname="B"><ScreenB/></Tabs.Tab></Tabs.Container>)}

Props

nametypedefaultdescription
allowHeaderOverscrollboolean | undefinedfalseWhether the header moves down during overscrolling (for example on pull-to-refresh on iOS) or sticks to the top
cancelLazyFadeInboolean | undefined
cancelTranslationboolean | undefined
containerStyleStyleProp<ViewStyle>
headerContainerStyleStyleProp<AnimateStyle<ViewStyle>>
headerHeightnumber | undefinedIs optional, but will optimize the first render.
initialTabNamestring | undefined
lazyboolean | undefinedIf lazy, will mount the screens only when the tab is visited. There is a default fade in transition.
minHeaderHeightnumber | undefinedHeader minimum height when collapsed
onIndexChange((index: number) => void) | undefinedCallback fired when the index changes. It receives the current index.
onTabChange(data: { prevIndex: number index: number prevTabName: T tabName: T }) => voidCallback fired when the tab changes. It receives the previous and current index and tabnames.
pagerPropsOmit<FlatListProps<number>, 'data' | 'keyExtractor' | 'renderItem' | 'horizontal' | 'pagingEnabled' | 'onScroll' | 'showsHorizontalScrollIndicator' | 'getItemLayout'>Props passed to the pager. If you want for example to disable swiping, you can pass{ scrollEnabled: false }
renderHeader(props: TabBarProps<TabName>) => React.ReactElement | null
renderTabBar(props: TabBarProps<TabName>) => React.ReactElement | null(props: TabBarProps<TabName>) => MaterialTabBar
revealHeaderOnScrollboolean | undefinedReveal header when scrolling down. Implements diffClamp.
snapThresholdnumber | null | undefinednullPercentage of header height to define as the snap point. A number between 0 and 1, ornull to disable snapping.
tabBarHeightnumber | undefinedIs optional, but will optimize the first render.
widthnumber | undefinedCustom width of the container. Defaults to the window width.

Tabs.Tab

Wrap your screens withTabs.Tab. Basic usage looks like this:

<Tabs.Container...><Tabs.Tabname="A"label="First Tab"><ScreenA/></Tabs.Tab><Tabs.Tabname="B"><ScreenA/></Tabs.Tab></Tabs.Container>

Props

nametype
labelstring | ((props: TabItemProps<T>) => ReactNode) | undefined
nameT

Tabs.Lazy

Typically used internally, but if you want to mix lazy and regular screens you can wrap the lazy ones with this component.

Props

nametype
cancelLazyFadeInboolean | undefined
startMountedboolean | undefined

Tabs.FlatList

Use like a regular FlatList.

Tabs.FlashList

Use like a regular FlashList.

Tabs.MasonryFlashList

Use like a regular MasonryFlashList.

Tabs.ScrollView

Use like a regular ScrollView.

Tabs.SectionList

Use like a regular SectionList.

Ref

You can pass a ref toTabs.Container.

constref=React.useRef()<Tabs.Containerref={ref}>
methodtype
jumpToTab(name: T) => boolean
setIndex(index: number) => boolean
getFocusedTab() => T
getCurrentIndex() => number

Hooks

useCollapsibleStyle

This hook provides access to key styles for the collapsible tab view. It can be used to obtain theprogressViewOffset and pass it to theRefreshControl of the scroll view.

const{  contentContainerStyle,  progressViewOffset,  style,}=useCollapsibleStyle()

Values

nametype
contentContainerStyle{ minHeight: number; paddingTop: number; }
progressViewOffsetnumber
style{ width: number; }

useAnimatedTabIndex

This hook returns an animated value representing the current tab index. As the tab view can be in between panes while swiping, this value is a floating-point number.

consttabIndex=useAnimatedTabIndex()

useFocusedTab

This hook returns the name of the currently focused tab.

constfocusedTab=useFocusedTab()

useHeaderMeasurements

This hook returns the top distance and the header height. For an example of how to use this, check out the animated header example in the example folder.

const{ top, height}=useHeaderMeasurements()

useCurrentTabScrollY

This hook returns the vertical scroll position of the current tab as an Animated SharedValue.

Since this library requires handling theonScroll event for its functionality, this is the only way to react to changes in the scroll position of the underlying scrollable component.

constscrollY=useCurrentTabScrollY()

Default Tab Bar

MaterialTabItem

Any additional props are passed to the pressable component.

Props

nametypedescription
activeColorstring | undefinedColor applied to the label when active
inactiveColorstring | undefinedColor applied to the label when inactive
inactiveOpacitynumber | undefined
indexnumber
indexDecimalSharedValue<number>
labelstring | ((props: TabItemProps<T>) => ReactNode)
labelStyleStyleProp<AnimateStyle<TextStyle>>Style to apply to the tab item label
nameT
onLayout(((event: LayoutChangeEvent) => void) & ((event: LayoutChangeEvent) => void)) | undefinedInvoked on mount and layout changes with {nativeEvent: { layout: {x, y, width, height}}}.
onPress(name: T) => void
pressColorstring | undefined
pressOpacitynumber | undefined
scrollEnabledboolean | undefined
styleStyleProp<ViewStyle>Either view styles or a function that receives a boolean reflecting whether the component is currently pressed and returns view styles.

Known Issues

Android FlatList Pull to Refresh

Refer tothis open issue. We utilizescrollTo to synchronize the unfocused tabs. While it is intended for use withScrollView, it works well withFlatList, until theRefreshControl is added. Note that this issue occurs only on Android.

Workaround: Check out theAndroid Shared Pull To Refresh example in the expo app. You can implement a single pull-to-refresh for theTabs.Container.

iOS FlatList StickyHeaderIndices and iOS SectionList StickySectionHeadersEnabled

When using thestickyHeaderIndices prop on a FlatList orstickySectionHeadersEnabled on a SectionList, the sticky elements do not scroll up as the header collapses. This issue is specific to iOS.

See#136.

ref.setIndex

This is not an issue per se, but it's essential to be aware of it. When usingcontainerRef.current.setIndex(i), if you set it to the current index, the screen will scroll to the top. You can prevent this behavior as follows:

constindex=pageRef.current?.getCurrentIndex()if(index!==nextIndex){pageRef.current?.setIndex(nextIndex)}

Alternative Libraries

If you do not require a full-featured tab view, consider another option: a simple segmented control / material tab bar without swiping or snapping, using only the React Native Animated API.

Contributing and running the Example

While developing, you can run theexample app to test your changes.

First runyarn in root:

yarn

Then prepare the example:

cd exampleyarn

Then run the example:

yarn ios

Please follow theangular commit message format.

Make sure your code passes TypeScript and ESLint. Run the following to verify:

yarn typescriptyarn lint

To fix formatting errors, run the following:

yarn lint -- --fix

Documentation changes

Edit theREADME_TEMPLATE, or update the docstrings inside thesrc folder, and run:

yarn docs

About

A cross-platform Collapsible Tab View component for React Native

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp