feat: Add ReadProgressBar component to DefaultLayout
This commit is contained in:
parent
74f45b7d64
commit
3d2056c3d9
@ -22,8 +22,8 @@ export function DocsLayout(props: DocsLayoutProps) {
|
|||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<div class="max-w-2xl min-w-0 flex-auto px-4 py-16 lg:max-w-none lg:pr-0 lg:pl-8 xl:px-16 grow">
|
<main class="max-w-2xl min-w-0 flex-auto px-4 py-16 lg:max-w-none lg:pr-0 lg:pl-8 xl:px-16 grow">
|
||||||
<article>
|
<article id="article-content">
|
||||||
<DocsHeader
|
<DocsHeader
|
||||||
title={pageContext.exports.frontmatter?.title}
|
title={pageContext.exports.frontmatter?.title}
|
||||||
estimatedReadingTime={pageContext.exports.readingTime?.text}
|
estimatedReadingTime={pageContext.exports.readingTime?.text}
|
||||||
@ -31,7 +31,7 @@ export function DocsLayout(props: DocsLayoutProps) {
|
|||||||
<Prose>{props.children}</Prose>
|
<Prose>{props.children}</Prose>
|
||||||
</article>
|
</article>
|
||||||
<PrevNextLinks />
|
<PrevNextLinks />
|
||||||
</div>
|
</main>
|
||||||
|
|
||||||
<TableOfContents />
|
<TableOfContents />
|
||||||
</>
|
</>
|
||||||
|
|||||||
@ -3,6 +3,7 @@ import type { JSXElement } from "solid-js";
|
|||||||
import { usePageContext } from "vike-solid/usePageContext";
|
import { usePageContext } from "vike-solid/usePageContext";
|
||||||
import { HeroSection } from "@/partials/HeroSection";
|
import { HeroSection } from "@/partials/HeroSection";
|
||||||
import { Navigation } from "@/partials/Navigation";
|
import { Navigation } from "@/partials/Navigation";
|
||||||
|
import { clientOnly } from "vike-solid/clientOnly";
|
||||||
import { Header } from "@/partials/Header";
|
import { Header } from "@/partials/Header";
|
||||||
import { Footer } from "@/partials/Footer";
|
import { Footer } from "@/partials/Footer";
|
||||||
import { DocsLayout } from "./DocsLayout";
|
import { DocsLayout } from "./DocsLayout";
|
||||||
@ -10,6 +11,10 @@ import { Toaster } from "solid-toast";
|
|||||||
|
|
||||||
import "./tailwind.css";
|
import "./tailwind.css";
|
||||||
|
|
||||||
|
const ReadProgressBar = clientOnly(
|
||||||
|
async () => (await import("@/partials/ReadProgressBar")).ReadProgressBar,
|
||||||
|
);
|
||||||
|
|
||||||
type DefaultLayoutProps = {
|
type DefaultLayoutProps = {
|
||||||
children: JSXElement;
|
children: JSXElement;
|
||||||
};
|
};
|
||||||
@ -21,6 +26,7 @@ export default function DefaultLayout(props: DefaultLayoutProps) {
|
|||||||
<>
|
<>
|
||||||
<div class="flex w-full flex-col font-sans">
|
<div class="flex w-full flex-col font-sans">
|
||||||
<Header />
|
<Header />
|
||||||
|
<ReadProgressBar />
|
||||||
|
|
||||||
{pageContext.urlPathname === "/" && <HeroSection />}
|
{pageContext.urlPathname === "/" && <HeroSection />}
|
||||||
|
|
||||||
|
|||||||
44
app/partials/ReadProgressBar.tsx
Normal file
44
app/partials/ReadProgressBar.tsx
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
import { createEffect, createSignal } from "solid-js";
|
||||||
|
|
||||||
|
export function ReadProgressBar() {
|
||||||
|
const articleContentElement: HTMLElement | null =
|
||||||
|
document.getElementById("article-content");
|
||||||
|
if (!articleContentElement) return null;
|
||||||
|
|
||||||
|
const [width, setWidth] = createSignal("0%");
|
||||||
|
|
||||||
|
const handleScroll = () => {
|
||||||
|
const scrollTop = window.scrollY;
|
||||||
|
const pageHeight = document.documentElement.scrollHeight;
|
||||||
|
const scrollHeight = articleContentElement.scrollHeight;
|
||||||
|
|
||||||
|
const gapWithBottom = pageHeight - scrollHeight;
|
||||||
|
const scrollableHeight = pageHeight - gapWithBottom;
|
||||||
|
|
||||||
|
const scrollPercentage = Math.round((scrollTop / scrollableHeight) * 100);
|
||||||
|
|
||||||
|
setWidth(`${scrollPercentage}%`);
|
||||||
|
};
|
||||||
|
|
||||||
|
createEffect(() => {
|
||||||
|
window.addEventListener("scroll", handleScroll);
|
||||||
|
|
||||||
|
handleScroll();
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
window.removeEventListener("scroll", handleScroll);
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div
|
||||||
|
aria-hidden
|
||||||
|
class="sticky inset-x-0 top-20 z-50 h-1 w-full bg-violet-50"
|
||||||
|
>
|
||||||
|
<div
|
||||||
|
class="bg-violet-300 h-full transition-all duration-75"
|
||||||
|
style={{ width: width() }}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user