@impulse-ui-native/flat-list 17.0.0 → 17.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -19,7 +19,7 @@ import {
19
19
  } from "@impulse-ui-native/flat-list";
20
20
 
21
21
  function OptionsList({ options, selectedIndex, rowHeight, renderItem }) {
22
- const initialScroll = useFlatListInitialScroll({
22
+ const { scrollToInitialIndex, ...initialScroll } = useFlatListInitialScroll({
23
23
  itemSize: rowHeight,
24
24
  initialScrollIndex: selectedIndex,
25
25
  });
@@ -28,7 +28,7 @@ function OptionsList({ options, selectedIndex, rowHeight, renderItem }) {
28
28
  }
29
29
  ```
30
30
 
31
- The hook returns only native `initialScrollIndex` and a stable `getItemLayout`. FlatList itself performs initial scrolling and virtualization. `FlatList` is the native component, so all native props, callbacks, and refs work directly.
31
+ Destructure `scrollToInitialIndex` before spreading the remaining result onto the list. The remaining props are a list `ref`, native `initialScrollIndex`, and a stable `getItemLayout`. Call `scrollToInitialIndex()` to reposition an existing list without animation, using the latest target index. FlatList itself performs initial scrolling and virtualization. `FlatList` is the native component, so all native props, callbacks, and refs work directly.
32
32
 
33
33
  | Hook option | Meaning |
34
34
  | -------------------- | ---------------------------------------------------------------------------------- |
@@ -39,7 +39,7 @@ The hook returns only native `initialScrollIndex` and a stable `getItemLayout`.
39
39
 
40
40
  Use the same size for rendered rows and `itemSize`. For multiple columns, use the native row index, not an individual item index. For rows with different known sizes, provide your own accurate native `getItemLayout` instead of this fixed-size helper.
41
41
 
42
- Initial positioning follows native FlatList's mount behavior. Remount the list when a fresh initial position is needed, and mount it after asynchronously loaded data is available. Changing the selected value after initialization does not scroll an existing list.
42
+ Initial positioning follows native FlatList's mount behavior. For asynchronous data, mount the list after data is available, or call `scrollToInitialIndex()` once the target row is available and the list has laid out. Changing the target index alone does not scroll an existing list; call the method explicitly when repositioning is intended. An omitted or negative target is ignored. If you supply your own list ref, use its native scrolling methods instead.
43
43
 
44
44
  Exports: `FlatList`, `useFlatListInitialScroll`, `FlatListProps`, `FlatListRef`, and `UseFlatListInitialScrollOptions`.
45
45
 
@@ -1,3 +1,4 @@
1
1
  export { useFlatListInitialScroll } from './use-flat-list-initial-scroll.hook.mjs';
2
2
  import 'react-native';
3
+ import 'react';
3
4
  import '../types/flat-list.types.mjs';
@@ -1,13 +1,16 @@
1
- import 'react-native';
1
+ import { FlatList } from 'react-native';
2
+ import * as react from 'react';
2
3
  import { UseFlatListInitialScrollOptions } from '../types/flat-list.types.mjs';
3
4
 
4
5
  declare function useFlatListInitialScroll<Item>(options: UseFlatListInitialScrollOptions): {
6
+ ref: react.RefObject<FlatList<Item> | null>;
5
7
  initialScrollIndex: number | undefined;
6
8
  getItemLayout: (data: Readonly<ArrayLike<Item>> | undefined, index: number) => {
7
9
  length: number;
8
10
  offset: number;
9
11
  index: number;
10
12
  };
13
+ scrollToInitialIndex: () => void;
11
14
  };
12
15
 
13
16
  export { useFlatListInitialScroll };
@@ -1,4 +1,4 @@
1
- import { useCallback } from "react";
1
+ import { useCallback, useRef } from "react";
2
2
  function useFlatListInitialScroll(options) {
3
3
  const {
4
4
  itemSize,
@@ -6,6 +6,13 @@ function useFlatListInitialScroll(options) {
6
6
  separatorSize = 0,
7
7
  headerSize = 0
8
8
  } = options;
9
+ const ref = useRef(null);
10
+ const scrollToInitialIndex = useCallback(() => {
11
+ if (initialScrollIndex == null || initialScrollIndex < 0) {
12
+ return;
13
+ }
14
+ ref.current?.scrollToIndex({ index: initialScrollIndex, animated: false });
15
+ }, [initialScrollIndex]);
9
16
  const getItemLayout = useCallback(
10
17
  (_, index) => ({
11
18
  length: itemSize,
@@ -14,7 +21,12 @@ function useFlatListInitialScroll(options) {
14
21
  }),
15
22
  [itemSize, separatorSize, headerSize]
16
23
  );
17
- return { initialScrollIndex: initialScrollIndex ?? void 0, getItemLayout };
24
+ return {
25
+ ref,
26
+ initialScrollIndex: initialScrollIndex ?? void 0,
27
+ getItemLayout,
28
+ scrollToInitialIndex
29
+ };
18
30
  }
19
31
  export {
20
32
  useFlatListInitialScroll
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/hooks/use-flat-list-initial-scroll.hook.ts"],"sourcesContent":["import type { FlatListProps } from \"react-native\";\nimport { useCallback } from \"react\";\n\nimport type { UseFlatListInitialScrollOptions } from \"../types\";\n\nexport function useFlatListInitialScroll<Item>(\n options: UseFlatListInitialScrollOptions,\n) {\n const {\n itemSize,\n initialScrollIndex,\n separatorSize = 0,\n headerSize = 0,\n } = options;\n\n const getItemLayout = useCallback<\n NonNullable<FlatListProps<Item>[\"getItemLayout\"]>\n >(\n (_, index) => ({\n length: itemSize,\n offset: headerSize + (itemSize + separatorSize) * index,\n index,\n }),\n [itemSize, separatorSize, headerSize],\n );\n\n return { initialScrollIndex: initialScrollIndex ?? undefined, getItemLayout };\n}\n"],"mappings":"AACA,SAAS,mBAAmB;AAIrB,SAAS,yBACd,SACA;AACA,QAAM;AAAA,IACJ;AAAA,IACA;AAAA,IACA,gBAAgB;AAAA,IAChB,aAAa;AAAA,EACf,IAAI;AAEJ,QAAM,gBAAgB;AAAA,IAGpB,CAAC,GAAG,WAAW;AAAA,MACb,QAAQ;AAAA,MACR,QAAQ,cAAc,WAAW,iBAAiB;AAAA,MAClD;AAAA,IACF;AAAA,IACA,CAAC,UAAU,eAAe,UAAU;AAAA,EACtC;AAEA,SAAO,EAAE,oBAAoB,sBAAsB,QAAW,cAAc;AAC9E;","names":[]}
1
+ {"version":3,"sources":["../../src/hooks/use-flat-list-initial-scroll.hook.ts"],"sourcesContent":["import type { FlatList, FlatListProps } from \"react-native\";\r\nimport { useCallback, useRef } from \"react\";\r\n\r\nimport type { UseFlatListInitialScrollOptions } from \"../types\";\r\n\r\nexport function useFlatListInitialScroll<Item>(\r\n options: UseFlatListInitialScrollOptions,\r\n) {\r\n const {\r\n itemSize,\r\n initialScrollIndex,\r\n separatorSize = 0,\r\n headerSize = 0,\r\n } = options;\r\n\r\n const ref = useRef<FlatList<Item>>(null);\r\n\r\n const scrollToInitialIndex = useCallback(() => {\r\n if (initialScrollIndex == null || initialScrollIndex < 0) {\r\n return;\r\n }\r\n\r\n ref.current?.scrollToIndex({ index: initialScrollIndex, animated: false });\r\n }, [initialScrollIndex]);\r\n\r\n const getItemLayout = useCallback<\r\n NonNullable<FlatListProps<Item>[\"getItemLayout\"]>\r\n >(\r\n (_, index) => ({\r\n length: itemSize,\r\n offset: headerSize + (itemSize + separatorSize) * index,\r\n index,\r\n }),\r\n [itemSize, separatorSize, headerSize],\r\n );\r\n\r\n return {\r\n ref,\r\n initialScrollIndex: initialScrollIndex ?? undefined,\r\n getItemLayout,\r\n scrollToInitialIndex,\r\n };\r\n}\r\n"],"mappings":"AACA,SAAS,aAAa,cAAc;AAI7B,SAAS,yBACd,SACA;AACA,QAAM;AAAA,IACJ;AAAA,IACA;AAAA,IACA,gBAAgB;AAAA,IAChB,aAAa;AAAA,EACf,IAAI;AAEJ,QAAM,MAAM,OAAuB,IAAI;AAEvC,QAAM,uBAAuB,YAAY,MAAM;AAC7C,QAAI,sBAAsB,QAAQ,qBAAqB,GAAG;AACxD;AAAA,IACF;AAEA,QAAI,SAAS,cAAc,EAAE,OAAO,oBAAoB,UAAU,MAAM,CAAC;AAAA,EAC3E,GAAG,CAAC,kBAAkB,CAAC;AAEvB,QAAM,gBAAgB;AAAA,IAGpB,CAAC,GAAG,WAAW;AAAA,MACb,QAAQ;AAAA,MACR,QAAQ,cAAc,WAAW,iBAAiB;AAAA,MAClD;AAAA,IACF;AAAA,IACA,CAAC,UAAU,eAAe,UAAU;AAAA,EACtC;AAEA,SAAO;AAAA,IACL;AAAA,IACA,oBAAoB,sBAAsB;AAAA,IAC1C;AAAA,IACA;AAAA,EACF;AACF;","names":[]}
package/dist/index.d.mts CHANGED
@@ -1,3 +1,4 @@
1
1
  export { FlatList, FlatListProps } from 'react-native';
2
2
  export { useFlatListInitialScroll } from './hooks/use-flat-list-initial-scroll.hook.mjs';
3
3
  export { FlatListRef, UseFlatListInitialScrollOptions } from './types/flat-list.types.mjs';
4
+ import 'react';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@impulse-ui-native/flat-list",
3
- "version": "17.0.0",
3
+ "version": "17.2.0",
4
4
  "description": "Native FlatList and a small hook for initial positioning with known row sizes.",
5
5
  "keywords": [
6
6
  "react-native",