Skip to content

Commit 29fe94e

Browse files
committed
move to Base layout, use position absolute, Explore list broken
1 parent f417f70 commit 29fe94e

File tree

3 files changed

+15
-43
lines changed

3 files changed

+15
-43
lines changed

src/components/Footer.astro

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import { Icon } from 'astro-icon/components';
33
44
import Link from '@/components/Link.astro';
5-
import ScrollToTop from '@/components/react/ScrollToTop';
65
import { ROUTES } from '@/constants/routes';
76
import { CONFIG } from '@/config';
87
import { getLatestCommitInfo } from '@/libs/git';
@@ -79,8 +78,4 @@ const trimmedMessage = limitString(message, messageLength);
7978
</ul>
8079
</div>
8180
</div>
82-
83-
<ScrollToTop client:only="react">
84-
<Icon name="mdi:arrow-up-thin" class="h-10 w-10 text-content hover:text-links-hover" />
85-
</ScrollToTop>
8681
</footer>

src/components/react/ScrollToTop.tsx

Lines changed: 6 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
11
import React, { useEffect, useRef, useState } from 'react';
22

3-
import { SELECTORS } from '@/constants/dom';
4-
53
import type { ReactNode } from 'react';
64

75
interface Props {
86
children: ReactNode;
97
}
108

11-
const { GISCUS_IFRAME_SELECTOR, GISCUS_WIDGET_SELECTOR } = SELECTORS;
12-
139
const fixedClasses = ['opacity-1', 'translate-y-0'];
1410
const hiddenClasses = ['opacity-0', 'translate-y-full'];
1511

@@ -24,24 +20,22 @@ const hideLink = (linkRef: React.RefObject<HTMLAnchorElement>): void => {
2420
};
2521

2622
const getHalfViewportHeight = (window: Window) => Math.floor(window.innerHeight / 2);
27-
const getDocumentScrollHeight = (document: Document) => document.documentElement.scrollHeight;
2823

2924
const ScrollToTop: React.FC<Props> = ({ children }) => {
3025
const linkRef = useRef<HTMLAnchorElement>(null);
3126
const topRef = useRef<HTMLDivElement>(null);
3227
const bottomRef = useRef<HTMLDivElement>(null);
3328

3429
const [height, setHeight] = useState(getHalfViewportHeight(window));
35-
const [scrollHeight, setScrollHeight] = useState(getDocumentScrollHeight(document));
36-
37-
console.log('height', height, 'scrollHeight', scrollHeight);
3830

3931
useEffect(() => {
4032
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);
4236

4337
if (linkRef.current) {
44-
isAtTopOrBottom ? showLink(linkRef) : hideLink(linkRef);
38+
isAtTopOrBottom ? hideLink(linkRef) : showLink(linkRef);
4539
}
4640
};
4741

@@ -55,31 +49,6 @@ const ScrollToTop: React.FC<Props> = ({ children }) => {
5549
};
5650
}, []);
5751

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-
8352
// on resize only, vertical...?
8453
useEffect(() => {
8554
const handleResize = () => {
@@ -102,8 +71,8 @@ const ScrollToTop: React.FC<Props> = ({ children }) => {
10271
/>
10372
<div
10473
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` }}
10776
/>
10877
<a
10978
ref={linkRef}

src/layouts/Base.astro

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
---
2+
import { Icon } from 'astro-icon/components';
3+
24
import BaseHead from '@/components/BaseHead.astro';
35
import Footer from '@/components/Footer.astro';
46
import Header from '@/components/Header.astro';
7+
import ScrollToTop from '@/components/react/ScrollToTop';
58
// import { Snackbar } from '@/components/Snackbar';
69
import ThemeScript from '@/components/ThemeScript.astro';
710
@@ -14,11 +17,16 @@ const { metadata } = Astro.props as BaseProps;
1417

1518
<html lang="en">
1619
<BaseHead {metadata} />
17-
<body class="flex flex-col min-h-screen bg-base-100 transition-colors duration-500">
20+
<body class="relative flex flex-col min-h-screen bg-base-100 transition-colors duration-500">
1821
<Header />
1922
<slot />
2023
<Footer />
2124
{/* <Snackbar client:idle /> */}
25+
26+
<ScrollToTop client:only="react">
27+
<Icon name="mdi:arrow-up-thin" class="h-10 w-10 text-content hover:text-links-hover" />
28+
</ScrollToTop>
29+
2230
<ThemeScript />
2331
</body>
2432
</html>

0 commit comments

Comments
 (0)