Skip to content

Commit

Permalink
feat: 게시글 프로그래스 바 생성
Browse files Browse the repository at this point in the history
  • Loading branch information
1eecan committed Jan 10, 2024
1 parent b193c8e commit 0d0cf8e
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
44 changes: 44 additions & 0 deletions src/app/(root)/post/[slug]/ProgressBar.tsx
Original file line number Diff line number Diff line change
@@ -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 (
<div className="h-[4px] fixed top-0 left-0 w-full bg-white dark:bg-black">
<div
style={{ width: currentProgress }}
className="bar h-full absolute z-10 bg-RED_600 rounded-r-lg transition-width duration-30"
></div>
</div>
);
}
5 changes: 4 additions & 1 deletion src/app/(root)/post/[slug]/page.tsx
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -8,8 +9,10 @@ import remarkToc from 'remark-toc';

export default function Page({ params }: { params: { slug: string } }) {
const mdxSource = getPostBySlug(params.slug).content;

return (
<div className="prose dark:text-white">
<ProgressBar />
<MDXRemote
source={mdxSource}
options={{
Expand All @@ -18,7 +21,7 @@ export default function Page({ params }: { params: { slug: string } }) {
remarkPlugins: [
remarkGfm,
remarkBreaks,
[remarkToc, { tight: true, maxDepth: 5 }],
[remarkToc, { tight: true, maxDepth: 3 }],
],
rehypePlugins: [
rehypeSlug,
Expand Down

0 comments on commit 0d0cf8e

Please sign in to comment.