File tree 2 files changed +32
-3
lines changed
2 files changed +32
-3
lines changed Original file line number Diff line number Diff line change @@ -114,3 +114,20 @@ export const observeMutation = (
114
114
return ( ) => observer . disconnect ( )
115
115
} )
116
116
}
117
+
118
+ export function observeIntersection (
119
+ element : HTMLElement ,
120
+ options ?: IntersectionObserverInit ,
121
+ ) : Observable < IntersectionObserverEntry [ ] > {
122
+ return new Observable < IntersectionObserverEntry [ ] > ( ( observer ) => {
123
+ const intersectionObserver = new IntersectionObserver ( ( entries ) => {
124
+ observer . next ( entries )
125
+ } , options )
126
+
127
+ intersectionObserver . observe ( element )
128
+
129
+ return ( ) => {
130
+ intersectionObserver . disconnect ( )
131
+ }
132
+ } )
133
+ }
Original file line number Diff line number Diff line change 1
1
import { Box , Button } from "@chakra-ui/react"
2
- import type { SpineItem } from "@prose-reader/core"
2
+ import { type SpineItem , observeIntersection } from "@prose-reader/core"
3
3
import { memo } from "react"
4
4
import { useObserve , useSubscribe } from "reactjrx"
5
+ import { NEVER , finalize , switchMap } from "rxjs"
5
6
import { useMeasure } from "../common/useMeasure"
6
7
import {
7
8
DialogActionTrigger ,
@@ -23,8 +24,19 @@ const GalleryItem = memo(({ item }: { item: SpineItem }) => {
23
24
useAttachSnapshot ( element , item , measures )
24
25
25
26
useSubscribe (
26
- ( ) => reader ?. spine . spineItemsLoader . forceOpen ( [ item ] ) ,
27
- [ item , reader ] ,
27
+ ( ) =>
28
+ ( ! element ? NEVER : observeIntersection ( element as HTMLElement , { } ) ) . pipe (
29
+ switchMap ( ( entries ) => {
30
+ if ( entries . some ( ( e ) => e . isIntersecting ) ) {
31
+ const unlock = reader ?. spine . spineItemsLoader . forceOpen ( [ item ] )
32
+
33
+ return NEVER . pipe ( finalize ( ( ) => unlock ?.( ) ) )
34
+ }
35
+
36
+ return NEVER
37
+ } ) ,
38
+ ) ,
39
+ [ item , reader , element ] ,
28
40
)
29
41
30
42
return (
You can’t perform that action at this time.
0 commit comments