Detect when elements are visible
Find out when an element scrolls onto or off the screen. Use it to render heavy content only when needed, or to track which items the user saw. Lynx calls this exposure (see Glossary).
Choose an API
All three follow the element as it scrolls and moves, not whether the app is in front.
On iOS, an element stays on screen while the app is in the background. It also stays on screen with Control Center, Notification Center or the app switcher over the app. A browser does the same when its window is covered.
Watch one element
Add lynxExposure to an element and read its visible() signal:
#exp="lynxExposure"gives the template a reference to the directive.exp.visible()istruewhile the element is on screen andfalseotherwise. It starts asfalse.- The element must have a size and not be fully transparent. Inside a
<scroll-view>or<list>, it counts as visible when it overlaps the visible part of the scroll area. - The directive cleans up after itself when the element is destroyed.
The directive switches content in and out as the user scrolls. To create a component the first time it appears and keep it, combine lynxExposure with @defer.
Use IntersectionObserver
IntersectionObserver takes Lynx elements, so code and libraries written for the browser work unchanged. Observe an element from its ElementRef:
- It reports each element once observing starts, as a browser does, then each time the element comes into view or leaves it. The first report waits for the element's first layout. After that, the same rules as
lynxExposuredecide what counts as visible. - Entries have the browser's fields:
isIntersecting,intersectionRatio,boundingClientRect,intersectionRect,rootBoundsandtarget. The boxes are measured when Lynx notices the change, so an element scrolling into view reports a smallintersectionRatio. - In tests,
fireEvent.uiappear()andfireEvent.uidisappear()bring an element into view and take it out. - It's in your app's bundle only when the app uses it. The build adds it for a file of your app or its libraries that names
IntersectionObserveror has an@defer (on viewport)block. Code that looks it up by a name it builds at runtime finds it missing, unless another file names it. In that case, callɵenableIntersectionObserver()from@blotch/angular-lynxfirst.
Thresholds, margins and a root
threshold, rootMargin, scrollMargin and root work as they do in the browser:
-
thresholdreports each time the visible part of the element crosses one of the values, with itsintersectionRatiothen. As in Chrome and Safari,isIntersectingistrueonly once the element has reached a threshold: withthreshold: 1, a half-visible element isn't intersecting. -
rootMargingrows the root's box before the element is checked against it, or shrinks it with negative values. It takespx, another absolute unit (in,cm,mm,Q,ptorpc) or%. Percentages are of the root's height for the top and bottom, and of its width for the sides. Every value needs its unit,0too, as in the spec, Chrome and Firefox. On iOS and Android, a bare0throws aSyntaxError. The observer'srootMarginreads an absolute length back in px, and a percentage as a percentage:'1in 10%'reads'96px 10% 96px 10%'. The web preview keeps the browser's own observer. There, Safari also takes a bare0, and Firefox takes onlypxand%. So write margins inpxand%to run the same everywhere. -
rootis an element around the observed one, such as a<scroll-view>or<list>. The element is checked against the root, clipped by the scroll containers between them, and not against the screen. -
rootMargingrows only the root, as in the browser, and not the scroll containers inside it. To load ahead in a<scroll-view>or<list>, make it the root, asnearEnddoes above, or usescrollMargin. -
scrollMargingrows every scroll container between the element and the root (<scroll-view>,<list>,<viewpager>and<scroll-coordinator>), and the root too when it scrolls. With no root, as foraheadabove, it also grows the page, as the browser grows the viewport, so it loads ahead in a scroll-view that fills the screen too. It takes the same units asrootMargin, each percentage of the box it grows: its height for the top and bottom, and its width for the sides.rootBoundsdoesn't include it.@defer (on viewport({ scrollMargin: '300px' }))uses it to create a block before it scrolls into view. -
With any of these options, the observer measures the element itself. It measures:
- when the element is laid out
- when it comes into view or leaves
- 20 times a second while a scroll container between it and the root scrolls
- when one of those scroll containers is laid out again, as one that grows or shrinks moves the edge that clips the element
That's a measurement per observed element each time. So leave the options out when all you need is to know when an element comes into view and leaves.
-
In tests,
fireEvent.uiappear()brings all of the element into view (anintersectionRatioof 1).fireEvent.uidisappear()takes all of it out. It's all or nothing because the test environment has no layout to measure.
Track impressions across the page
Give each element you want to track an exposure-id, then read the batches from LynxGlobalExposure:
exposure-idmarks an element for tracking. Bind[exposure-id]to set it from data.exposure-sceneis an optional label for grouping, such as the screen name.- Lynx checks visibility about 20 times a second and reports changes in batches.
exposures()holds the latest batch of elements that became visible.disexposures()holds the latest batch that left the screen.- Each signal holds only the latest batch. To count impressions, or to track everything that's on screen, add each batch to your own state in an
effect().
Pause and resume tracking
stopExposure()stops Lynx's visibility checks. By default, Lynx first reports every visible element as having left. Pass{ sendEvent: false }to skip that.active()is a signal that'sfalsewhile paused.
Related
- Exposure API reference: every signal, method and event field
@defer: create a component when it first scrolls into view- Threads: where your code runs
- Lynx's exposure guide: attributes such as
exposure-areathat change when an element counts as visible
