1
1
import React , { useEffect , useRef , useState } from 'react' ;
2
2
3
- import { SELECTORS } from '@/constants/dom' ;
4
-
5
3
import type { ReactNode } from 'react' ;
6
4
7
5
interface Props {
8
6
children : ReactNode ;
9
7
}
10
8
11
- const { GISCUS_IFRAME_SELECTOR , GISCUS_WIDGET_SELECTOR } = SELECTORS ;
12
-
13
9
const fixedClasses = [ 'opacity-1' , 'translate-y-0' ] ;
14
10
const hiddenClasses = [ 'opacity-0' , 'translate-y-full' ] ;
15
11
@@ -24,24 +20,22 @@ const hideLink = (linkRef: React.RefObject<HTMLAnchorElement>): void => {
24
20
} ;
25
21
26
22
const getHalfViewportHeight = ( window : Window ) => Math . floor ( window . innerHeight / 2 ) ;
27
- const getDocumentScrollHeight = ( document : Document ) => document . documentElement . scrollHeight ;
28
23
29
24
const ScrollToTop : React . FC < Props > = ( { children } ) => {
30
25
const linkRef = useRef < HTMLAnchorElement > ( null ) ;
31
26
const topRef = useRef < HTMLDivElement > ( null ) ;
32
27
const bottomRef = useRef < HTMLDivElement > ( null ) ;
33
28
34
29
const [ height , setHeight ] = useState ( getHalfViewportHeight ( window ) ) ;
35
- const [ scrollHeight , setScrollHeight ] = useState ( getDocumentScrollHeight ( document ) ) ;
36
-
37
- console . log ( 'height' , height , 'scrollHeight' , scrollHeight ) ;
38
30
39
31
useEffect ( ( ) => {
40
32
const callback : IntersectionObserverCallback = ( entries ) => {
41
- const isAtTopOrBottom = entries . every ( ( entry ) => ! entry . isIntersecting ) ;
33
+ const isAtTopOrBottom = entries . some ( ( entry ) => entry . isIntersecting ) ;
34
+
35
+ console . log ( 'isAtTopOrBottom' , isAtTopOrBottom ) ;
42
36
43
37
if ( linkRef . current ) {
44
- isAtTopOrBottom ? showLink ( linkRef ) : hideLink ( linkRef ) ;
38
+ isAtTopOrBottom ? hideLink ( linkRef ) : showLink ( linkRef ) ;
45
39
}
46
40
} ;
47
41
@@ -55,31 +49,6 @@ const ScrollToTop: React.FC<Props> = ({ children }) => {
55
49
} ;
56
50
} , [ ] ) ;
57
51
58
- useEffect ( ( ) => {
59
- const shadowHost = document . querySelector ( GISCUS_WIDGET_SELECTOR ) ;
60
- const shadowRoot = shadowHost ?. shadowRoot ;
61
- if ( ! shadowRoot ) return ;
62
-
63
- const iframe = shadowRoot . querySelector ( GISCUS_IFRAME_SELECTOR ) as HTMLIFrameElement ;
64
-
65
- console . log ( 'iframe' , iframe ) ;
66
- if ( ! iframe ) return ;
67
-
68
- console . log ( 'updated scrollHeight' , scrollHeight ) ;
69
-
70
- let timer : NodeJS . Timeout ;
71
- const handleLoad = ( ) =>
72
- ( timer = setTimeout ( ( ) => setScrollHeight ( getDocumentScrollHeight ( document ) ) , 3000 ) ) ;
73
-
74
- // not onLoad but MutationObserver to lose class="loading"
75
- iframe . addEventListener ( 'load' , handleLoad ) ;
76
-
77
- return ( ) => {
78
- clearTimeout ( timer ) ;
79
- window . removeEventListener ( 'load' , handleLoad ) ;
80
- } ;
81
- } , [ ] ) ;
82
-
83
52
// on resize only, vertical...?
84
53
useEffect ( ( ) => {
85
54
const handleResize = ( ) => {
@@ -102,8 +71,8 @@ const ScrollToTop: React.FC<Props> = ({ children }) => {
102
71
/>
103
72
< div
104
73
ref = { bottomRef }
105
- className = "pointer-events-none absolute w-10 bg-blue-500"
106
- style = { { height : `${ height } px` , top : ` ${ scrollHeight - height } px` } }
74
+ className = "pointer-events-none absolute bottom-0 w-10 bg-blue-500"
75
+ style = { { height : `${ height } px` } }
107
76
/>
108
77
< a
109
78
ref = { linkRef }
0 commit comments