( node: Element, args: OnScreenArgs, changes?: Stream.Stream<OnScreenArgs> )
| 95 | * @since 4.3.0 |
| 96 | */ |
| 97 | export const onScreen = ( |
| 98 | node: Element, |
| 99 | args: OnScreenArgs, |
| 100 | changes?: Stream.Stream<OnScreenArgs> |
| 101 | ): Stream.Stream<boolean> => |
| 102 | Stream.changes( |
| 103 | Stream.switchMap(changes ?? Stream.succeed(args), ({ rootMargin = "0px", threshold = 0 }) => |
| 104 | Stream.callback<boolean>((queue) => { |
| 105 | const Observer = node.ownerDocument.defaultView?.IntersectionObserver |
| 106 | if (Observer === undefined) { |
| 107 | return Queue.offer(queue, true) |
| 108 | } |
| 109 | return Effect.acquireRelease( |
| 110 | Effect.sync(() => { |
| 111 | // One callback can carry several crossings. |
| 112 | const observer = new Observer((entries) => { |
| 113 | for (const entry of entries) { |
| 114 | Queue.offerUnsafe(queue, entry.isIntersecting && entry.intersectionRatio >= threshold) |
| 115 | } |
| 116 | }, { rootMargin, threshold }) |
| 117 | observer.observe(node) |
| 118 | return observer |
| 119 | }), |
| 120 | (observer) => |
| 121 | Effect.sync(() => observer.disconnect()) |
| 122 | ) |
| 123 | })) |
| 124 | ) |
| 125 | |
| 126 | /** |
| 127 | * Reports whether an element is on screen. Attach it with `onMount` and map |
nothing calls this directly
no test coverage detected