|
| 1 | +import React, { |
| 2 | + forwardRef, |
| 3 | + ForwardedRef, |
| 4 | + useState, |
| 5 | + useCallback, |
| 6 | + useMemo, |
| 7 | +} from "react"; |
| 8 | +import { Text, View, Switch, StyleSheet } from "react-native"; |
| 9 | +import { FlashList, type FlashListRef } from "@shopify/flash-list"; |
| 10 | + |
| 11 | +// Define our data structure |
| 12 | +type Item = |
| 13 | + | { |
| 14 | + type: "basic"; |
| 15 | + title: string; |
| 16 | + } |
| 17 | + | { |
| 18 | + type: "header"; |
| 19 | + title: string; |
| 20 | + }; |
| 21 | + |
| 22 | +const data: Item[] = Array.from({ length: 300 }, (_, index) => |
| 23 | + index % 20 === 0 |
| 24 | + ? { |
| 25 | + type: "header", |
| 26 | + title: `Header ${index / 20 + 1}`, |
| 27 | + } |
| 28 | + : { |
| 29 | + type: "basic", |
| 30 | + title: `Item ${index - Math.floor(index / 20) + 1}`, |
| 31 | + } |
| 32 | +); |
| 33 | + |
| 34 | +const headerIndices = data |
| 35 | + .map((item, index) => (item.type === "header" ? index : null)) |
| 36 | + .filter((item) => item !== null) as number[]; |
| 37 | + |
| 38 | +interface ToggleProps { |
| 39 | + label: string; |
| 40 | + value: boolean; |
| 41 | + onChange: (value: boolean) => void; |
| 42 | +} |
| 43 | +const Toggle = ({ label, value, onChange }: ToggleProps) => { |
| 44 | + return ( |
| 45 | + <View style={styles.toggleContainer}> |
| 46 | + <Text>{label}</Text> |
| 47 | + <Switch value={value} onValueChange={onChange} /> |
| 48 | + </View> |
| 49 | + ); |
| 50 | +}; |
| 51 | + |
| 52 | +const ItemSeparator = () => { |
| 53 | + return <View style={styles.itemSeparator} />; |
| 54 | +}; |
| 55 | + |
| 56 | +// Create our masonry component |
| 57 | +export const StickyHeaderExample = forwardRef( |
| 58 | + (_: unknown, ref: ForwardedRef<unknown>) => { |
| 59 | + const [stickyHeadersEnabled, setStickyHeadersEnabled] = useState(false); |
| 60 | + const [withStickyHeaderOffset, setWithStickyHeaderOffset] = useState(false); |
| 61 | + const [withStickyHeaderBackground, setWithStickyHeaderBackground] = |
| 62 | + useState(false); |
| 63 | + |
| 64 | + // Memoize the renderItem function |
| 65 | + const renderItem = useCallback( |
| 66 | + ({ item }: { item: Item }) => ( |
| 67 | + <View style={item.type === "header" ? styles.headerItem : styles.item}> |
| 68 | + <Text>{item.title}</Text> |
| 69 | + </View> |
| 70 | + ), |
| 71 | + [] |
| 72 | + ); |
| 73 | + |
| 74 | + const stickyHeaderConfig = useMemo( |
| 75 | + () => ({ |
| 76 | + offset: withStickyHeaderOffset ? 44 : 0, |
| 77 | + backdropComponent: withStickyHeaderBackground ? ( |
| 78 | + <View style={styles.stickyHeaderBackdropContainer}> |
| 79 | + <View style={styles.stickyHeaderBackground} /> |
| 80 | + </View> |
| 81 | + ) : undefined, |
| 82 | + }), |
| 83 | + [withStickyHeaderOffset, withStickyHeaderBackground] |
| 84 | + ); |
| 85 | + |
| 86 | + return ( |
| 87 | + <View |
| 88 | + style={styles.container} |
| 89 | + key={`${stickyHeadersEnabled}-${withStickyHeaderOffset}-${withStickyHeaderBackground}`} |
| 90 | + > |
| 91 | + <View> |
| 92 | + <Toggle |
| 93 | + label="Enable Sticky Headers" |
| 94 | + value={stickyHeadersEnabled} |
| 95 | + onChange={setStickyHeadersEnabled} |
| 96 | + /> |
| 97 | + <Toggle |
| 98 | + label="Sticky Header Offset" |
| 99 | + value={withStickyHeaderOffset} |
| 100 | + onChange={setWithStickyHeaderOffset} |
| 101 | + /> |
| 102 | + <Toggle |
| 103 | + label="Sticky Header Background" |
| 104 | + value={withStickyHeaderBackground} |
| 105 | + onChange={setWithStickyHeaderBackground} |
| 106 | + /> |
| 107 | + </View> |
| 108 | + <View style={styles.listContainer}> |
| 109 | + <FlashList |
| 110 | + ref={ref as React.RefObject<FlashListRef<Item>>} |
| 111 | + renderItem={renderItem} |
| 112 | + alwaysBounceVertical |
| 113 | + data={data} |
| 114 | + stickyHeaderIndices={ |
| 115 | + stickyHeadersEnabled ? headerIndices : undefined |
| 116 | + } |
| 117 | + stickyHeaderConfig={stickyHeaderConfig} |
| 118 | + ItemSeparatorComponent={ItemSeparator} |
| 119 | + /> |
| 120 | + </View> |
| 121 | + </View> |
| 122 | + ); |
| 123 | + } |
| 124 | +); |
| 125 | +StickyHeaderExample.displayName = "StickyHeaderExample"; |
| 126 | + |
| 127 | +const styles = StyleSheet.create({ |
| 128 | + container: { |
| 129 | + flex: 1, |
| 130 | + }, |
| 131 | + toggleContainer: { |
| 132 | + flexDirection: "row", |
| 133 | + justifyContent: "space-between", |
| 134 | + alignItems: "center", |
| 135 | + paddingHorizontal: 16, |
| 136 | + marginVertical: 8, |
| 137 | + }, |
| 138 | + listContainer: { |
| 139 | + borderRadius: 20, |
| 140 | + backgroundColor: "#C0C0CC", |
| 141 | + margin: 16, |
| 142 | + overflow: "hidden", |
| 143 | + flex: 1, |
| 144 | + }, |
| 145 | + itemSeparator: { |
| 146 | + height: 1, |
| 147 | + backgroundColor: "#CDCDCD", |
| 148 | + }, |
| 149 | + stickyHeaderBackdropContainer: { |
| 150 | + position: "absolute", |
| 151 | + width: "100%", |
| 152 | + inset: 0, |
| 153 | + }, |
| 154 | + stickyHeaderBackground: { |
| 155 | + height: 44, |
| 156 | + backgroundColor: "#40C4FF4C", |
| 157 | + }, |
| 158 | + headerItem: { |
| 159 | + height: 44, |
| 160 | + backgroundColor: "#FFAB40AA", |
| 161 | + paddingHorizontal: 16, |
| 162 | + justifyContent: "center", |
| 163 | + }, |
| 164 | + item: { |
| 165 | + height: 44, |
| 166 | + backgroundColor: "#E0E0E0", |
| 167 | + paddingHorizontal: 16, |
| 168 | + justifyContent: "center", |
| 169 | + }, |
| 170 | +}); |
0 commit comments