diff --git a/src/app/(root)/post/[slug]/ProgressBar.tsx b/src/app/(root)/post/[slug]/ProgressBar.tsx new file mode 100644 index 0000000..4967cc0 --- /dev/null +++ b/src/app/(root)/post/[slug]/ProgressBar.tsx @@ -0,0 +1,44 @@ +'use client'; + +import { useEffect, useState } from 'react'; + +export default function ProgressBar() { + const [currentProgress, setCurrentProgress] = useState('0'); + + const scrollEvent = () => { + const progressBar = document.querySelector('.bar'); + + if (progressBar === null) return; + + let scrollNum = 0; + let documentHeight = 0; + + const getPercent = (scroll: number, total: number) => { + return (scroll / total) * 100; + }; + + scrollNum = document.documentElement.scrollTop; + + documentHeight = + document.documentElement.scrollHeight - + document.documentElement.clientHeight; + + setCurrentProgress(() => { + return getPercent(scrollNum, documentHeight) + '%'; + }); + }; + + useEffect(() => { + window.addEventListener('scroll', scrollEvent); + return () => window.removeEventListener('scroll', scrollEvent); + }, []); + + return ( +
+
+
+ ); +} diff --git a/src/app/(root)/post/[slug]/page.tsx b/src/app/(root)/post/[slug]/page.tsx index 8a532c1..4af69e6 100644 --- a/src/app/(root)/post/[slug]/page.tsx +++ b/src/app/(root)/post/[slug]/page.tsx @@ -1,3 +1,4 @@ +import ProgressBar from './ProgressBar'; import { getPostBySlug, getPostDatas } from '@/app/utils'; import { MDXRemote } from 'next-mdx-remote/rsc'; import rehypePrettyCode from 'rehype-pretty-code'; @@ -8,8 +9,10 @@ import remarkToc from 'remark-toc'; export default function Page({ params }: { params: { slug: string } }) { const mdxSource = getPostBySlug(params.slug).content; + return (
+