import React, { useImperativeHandle, useRef } from 'react'; import { I18nManager, Keyboard, Platform } from 'react-native'; import type * as ReactNative from 'react-native'; import { useLazyPages } from './useLazyPages'; import { childrenWithOverriddenStyle } from './utils'; import PagerViewNativeComponent, { Commands as PagerViewNativeCommands, type OnPageScrollEventData, type OnPageScrollStateChangedEventData, type OnPageSelectedEventData, type NativeProps, } from './PagerViewNativeComponent'; export type SetPageOptions = { /** * `'scroll'` scrolls past every page between the current one and the target. * `'direct'` slides the target in next to the current page, as if they were * neighbours. Defaults to `'scroll'`. */ animation?: 'scroll' | 'direct'; }; export type PagerViewMethods = { /** * A helper function to scroll to a specific page in the PagerView. * The transition between pages will be animated. */ setPage: (selectedPage: number, options?: SetPageOptions) => void; /** * A helper function to scroll to a specific page in the PagerView. * The transition between pages will *not* be animated. */ setPageWithoutAnimation: (selectedPage: number) => void; /** * A helper function to enable/disable scroll imperatively * The recommended way is using the scrollEnabled prop, however, there might be a case where a * imperative solution is more useful (e.g. for not blocking an animation) */ setScrollEnabled: (scrollEnabled: boolean) => void; /** * @deprecated The ref is the native view itself, use it directly. */ pagerView: React.ComponentRef; }; export type PagerViewRef = React.ComponentRef & PagerViewMethods; type LazyProps = { /** * Mounts a page's content only when the pager gets near it. Set it once: * the pager isn't tracked while `lazy` is off, so turning it on later starts * from `initialPage`. */ lazy?: boolean; /** With `lazy`, how many pages on each side are mounted ahead of time. */ lazyPreloadDistance?: number; /** With `lazy`, what a page shows before its content is mounted. */ renderLazyPlaceholder?: (index: number) => React.ReactNode; }; export type PagerViewProps = Omit & LazyProps & { /** * `'locale'` follows `I18nManager.isRTL`; native only receives `'ltr' | 'rtl'`. */ layoutDirection?: 'ltr' | 'rtl' | 'locale'; ref?: React.Ref; }; type NativeInstance = React.ComponentRef; function bindCommand( ref: React.RefObject, command: (view: NativeInstance, ...args: Args) => void ) { return (...args: Args) => { if (ref.current) { command(ref.current, ...args); } }; } function deduceLayoutDirection( layoutDirection: PagerViewProps['layoutDirection'] ) { if (!layoutDirection || layoutDirection === 'locale') { return I18nManager.isRTL ? 'rtl' : 'ltr'; } return layoutDirection; } /** * Container that allows to flip left and right between child views. Each * child view of the `PagerView` will be treated as a separate page * and will be stretched to fill the `PagerView`. * * It is important all children are ``s and not composite components. * You can set style properties like `padding` or `backgroundColor` for each * child. It is also important that each child have a `key` prop. * * ```tsx * * * First page * * * Second page * * * ``` */ export function PagerView({ ref, lazy = false, lazyPreloadDistance = 1, renderLazyPlaceholder, ...props }: PagerViewProps) { const nativeRef = useRef(null); const isScrolling = useRef(false); const { children, setCurrent, onScroll, onSelect } = useLazyPages({ lazy, preloadDistance: lazyPreloadDistance, initialPage: props.initialPage ?? 0, children: props.children, renderPlaceholder: renderLazyPlaceholder, }); // Methods go on the native view, not a wrapper, which Animated needs. useImperativeHandle( ref, () => { const instance = nativeRef.current; if (!instance) { return null; } const setPage = bindCommand(nativeRef, PagerViewNativeCommands.setPage); return Object.assign(instance, { setPage: (selectedPage: number, options?: SetPageOptions) => { setCurrent?.(selectedPage); setPage(selectedPage, options?.animation ?? 'scroll'); }, setPageWithoutAnimation: (selectedPage: number) => { setCurrent?.(selectedPage); bindCommand( nativeRef, PagerViewNativeCommands.setPageWithoutAnimation )(selectedPage); }, setScrollEnabled: bindCommand( nativeRef, PagerViewNativeCommands.setScrollEnabledImperatively ), pagerView: instance, }); }, [setCurrent] ); const onPageScroll = ( e: ReactNative.NativeSyntheticEvent ) => { props.onPageScroll?.(e); onScroll(e.nativeEvent.position, e.nativeEvent.offset, isScrolling.current); }; const onPageScrollStateChanged = ( e: ReactNative.NativeSyntheticEvent ) => { props.onPageScrollStateChanged?.(e); isScrolling.current = e.nativeEvent.pageScrollState === 'dragging'; if ( isScrolling.current && Platform.OS === 'android' && props.keyboardDismissMode === 'on-drag' ) { Keyboard.dismiss(); } }; const onPageSelected = ( e: ReactNative.NativeSyntheticEvent ) => { props.onPageSelected?.(e); onSelect(e.nativeEvent.position); }; const onMoveShouldSetResponderCapture = ( e: ReactNative.GestureResponderEvent ) => isScrolling.current || !!props.onMoveShouldSetResponderCapture?.(e); const iosPageMargin = Platform.OS === 'ios' ? props.pageMargin ?? 0 : 0; return ( ); } /** Keeps `useRef(null)` working. */ export type PagerView = PagerViewRef;