|
| 1 | +# useExposureForPage |
| 2 | + |
| 3 | +Page-level exposure hook that consumes `GlobalEventEmitter` events and manages multiple items with admission gating. |
| 4 | + |
| 5 | +## Usage |
| 6 | + |
| 7 | +```tsx |
| 8 | +import { useExposureForPage } from "@lynx-js/react-use"; |
| 9 | + |
| 10 | +const items = [ |
| 11 | + { id: "card-1", title: "A" }, |
| 12 | + { id: "card-2", title: "B" }, |
| 13 | +]; |
| 14 | + |
| 15 | +export function Feed() { |
| 16 | + const { isInView, exposureProps } = useExposureForPage({ |
| 17 | + attrs: { "exposure-scene": "feed" }, |
| 18 | + admissionTimeMs: 80, |
| 19 | + onAppear: (_detail, info) => console.log("appear", info.exposureId), |
| 20 | + onDisappear: (_detail, info) => console.log("disappear", info.exposureId), |
| 21 | + }); |
| 22 | + |
| 23 | + return ( |
| 24 | + <view> |
| 25 | + {items.map((item) => ( |
| 26 | + <view key={item.id} {...exposureProps({ id: item.id })}> |
| 27 | + <text> |
| 28 | + {item.title} {isInView(item.id) ? "(visible)" : "(hidden)"} |
| 29 | + </text> |
| 30 | + </view> |
| 31 | + ))} |
| 32 | + </view> |
| 33 | + ); |
| 34 | +} |
| 35 | +``` |
| 36 | + |
| 37 | +## Type Declarations |
| 38 | + |
| 39 | +```ts |
| 40 | +export interface IUseExposureForPageOptions< |
| 41 | + EA extends Record<string, string | number | boolean | undefined> |
| 42 | +> { |
| 43 | + attrs?: Omit<TExposureAttrBag, "exposure-id"> & EA; |
| 44 | + admissionMs?: number; |
| 45 | + admissionTimeMs?: number; |
| 46 | + onAppear?: ( |
| 47 | + e: UIAppearanceTargetDetail, |
| 48 | + info: { exposureId?: string; exposureScene?: string } |
| 49 | + ) => void; |
| 50 | + onDisappear?: ( |
| 51 | + e: UIAppearanceTargetDetail, |
| 52 | + info: { exposureId?: string; exposureScene?: string } |
| 53 | + ) => void; |
| 54 | + onChange?: ( |
| 55 | + e: UIAppearanceTargetDetail, |
| 56 | + info: { isInView: boolean; exposureId?: string; exposureScene?: string } |
| 57 | + ) => void; |
| 58 | +} |
| 59 | + |
| 60 | +export interface IUseExposureForPageReturn< |
| 61 | + EA extends Record<string, string | number | boolean | undefined> |
| 62 | +> { |
| 63 | + isInView: (exposureId: string) => boolean; |
| 64 | + exposureProps: (p: { id: string; extra?: EA }) => TExposureAttrBag & EA; |
| 65 | +} |
| 66 | +``` |
0 commit comments