import React, {
useCallback,
useEffect,
useMemo,
useRef,
useState,
} from 'react';
import { Pressable, ScrollView, StyleSheet, Text, View } from 'react-native';
import { SafeAreaView } from 'react-native-safe-area-context';
import PagerView, { type PagerViewRef } from 'react-native-pager-view';
import { Button } from './component/Button';
import { colors, radius } from './theme';
import { BGCOLOR, padNumber } from './utils';
const PAGE_COUNT = 20;
const PRELOAD_DISTANCES = [0, 1, 2, 3];
type LazyPageProps = {
index: number;
onMountChange: (index: number, mounted: boolean) => void;
};
function LazyPage({ index, onMountChange }: LazyPageProps) {
useEffect(() => {
onMountChange(index, true);
return () => onMountChange(index, false);
}, [index, onMountChange]);
return (
{`Mounted page ${index + 1}`}
{Array.from({ length: 30 }, (_, row) => (
{`Page ${index + 1} row ${
row + 1
}`}
))}
);
}
type SegmentedProps = {
label: string;
swatchColor: string;
testID: string;
options: readonly number[];
value: number;
onChange: (value: number) => void;
};
function Segmented({
label,
swatchColor,
testID,
options,
value,
onChange,
}: SegmentedProps) {
return (
{label}
{options.map((option) => {
const isActive = option === value;
return (
onChange(option)}
style={[styles.segment, isActive && styles.segmentActive]}
>
{option}
);
})}
);
}
export function LazyPagerViewExample() {
const ref = useRef(null);
const [page, setPage] = useState(0);
const [preloadDistance, setPreloadDistance] = useState(1);
const [mounted, setMounted] = useState>(new Set());
const onMountChange = useCallback(
(index: number, isMounted: boolean) =>
setMounted((prev) => {
const next = new Set(prev);
if (isMounted) {
next.add(index);
} else {
next.delete(index);
}
return next;
}),
[]
);
const pages = useMemo(
() =>
Array.from({ length: PAGE_COUNT }, (_, index) => (
)),
[onMountChange]
);
const isFirst = page === 0;
const isLast = page === PAGE_COUNT - 1;
return (
(
{`Loading page ${
index + 1
}`}
)}
onPageSelected={(e) => setPage(e.nativeEvent.position)}
>
{pages}
{padNumber(page + 1)}
{' / '}
{padNumber(PAGE_COUNT)}
{Array.from({ length: PAGE_COUNT }, (_, index) => (
{index === page && }
))}
mounted
{mounted.size}
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: colors.background,
},
pager: {
flex: 1,
},
pageContent: {
alignItems: 'center',
padding: 20,
},
title: {
fontSize: 22,
fontWeight: '600',
color: colors.foreground,
marginBottom: 12,
},
row: {
paddingVertical: 8,
color: colors.foreground,
},
placeholder: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
backgroundColor: colors.muted,
},
placeholderText: {
color: colors.mutedForeground,
},
panel: {
paddingHorizontal: 12,
paddingTop: 8,
paddingBottom: 8,
},
buttons: {
flexDirection: 'row',
alignItems: 'center',
},
navigation: {
marginTop: 4,
paddingTop: 8,
borderTopWidth: 1,
borderTopColor: colors.border,
},
segmentedRow: {
flexDirection: 'row',
alignItems: 'center',
marginVertical: 4,
paddingHorizontal: 4,
},
segmentedLabel: {
width: 72,
flexDirection: 'row',
alignItems: 'center',
gap: 6,
},
swatch: {
width: 10,
height: 4,
borderRadius: radius.full,
},
segmented: {
flex: 1,
flexDirection: 'row',
padding: 3,
borderRadius: radius.lg,
backgroundColor: colors.muted,
},
segment: {
flex: 1,
alignItems: 'center',
paddingVertical: 6,
borderRadius: radius.md - 1,
},
segmentActive: {
backgroundColor: colors.background,
shadowColor: '#000',
shadowOpacity: 0.08,
shadowRadius: 2,
shadowOffset: { width: 0, height: 1 },
elevation: 1,
},
segmentText: {
fontSize: 13,
fontWeight: '500',
color: colors.mutedForeground,
},
segmentTextActive: {
color: colors.foreground,
},
pill: {
height: 36,
minWidth: 56,
margin: 4,
paddingHorizontal: 12,
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'center',
borderRadius: radius.md,
backgroundColor: colors.muted,
},
mountedPill: {
margin: 0,
gap: 8,
},
pillLabel: {
fontSize: 11,
color: colors.mutedForeground,
},
pillValue: {
fontSize: 12,
fontWeight: '600',
color: colors.foreground,
},
progress: {
flexDirection: 'row',
alignItems: 'center',
gap: 12,
height: 36,
marginTop: 4,
paddingHorizontal: 4,
},
progressText: {
fontVariant: ['tabular-nums'],
fontSize: 13,
fontWeight: '600',
color: colors.foreground,
},
progressTotal: {
fontWeight: '400',
color: colors.mutedForeground,
},
map: {
flex: 1,
flexDirection: 'row',
gap: 2,
},
mapColumn: {
flex: 1,
gap: 3,
},
mountedCell: {
height: 10,
alignItems: 'center',
justifyContent: 'center',
borderRadius: radius.full,
backgroundColor: colors.muted,
},
mountedCellActive: {
backgroundColor: colors.primary,
},
currentDot: {
width: 4,
height: 4,
borderRadius: radius.full,
backgroundColor: colors.background,
},
rangeCell: {
height: 4,
borderRadius: radius.full,
},
rangeCellPreload: {
backgroundColor: colors.mutedForeground,
},
});