Skip to content

Commit 756fb69

Browse files
committed
cards again
1 parent 9c6c661 commit 756fb69

File tree

2 files changed

+44
-4
lines changed

2 files changed

+44
-4
lines changed

src/components/PostMetadata.js

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
11
import React from 'react';
22
import Label from './Label';
33

4-
const PostMetadata = ({ metadata }) => {
4+
const PostMetadata = ({ metadata, readingProgress, isAtTop }) => {
55
if (!metadata) {
66
return null;
77
}
88

9+
const handleButtonClick = () => {
10+
if (isAtTop) {
11+
window.scrollTo({ top: document.documentElement.scrollHeight, behavior: 'smooth' });
12+
} else {
13+
window.scrollTo({ top: 0, behavior: 'smooth' });
14+
}
15+
};
16+
917
return (
1018
<aside className="sticky top-24">
1119
<div className="p-6 bg-gray-800/50 rounded-lg border border-gray-700/50">
@@ -32,6 +40,20 @@ const PostMetadata = ({ metadata }) => {
3240
</div>
3341
)}
3442
</div>
43+
<div className="mt-6">
44+
<Label className="mb-1">Reading Progress</Label>
45+
<div className="w-full bg-gray-700 rounded-full h-2.5 mt-2">
46+
<div className="bg-primary-500 h-2.5 rounded-full" style={{width: `${readingProgress}%`}}></div>
47+
</div>
48+
</div>
49+
<div className="mt-6 text-center">
50+
<button
51+
onClick={handleButtonClick}
52+
className="text-primary-400 hover:text-primary-500 transition-colors text-sm font-medium flex items-center justify-center w-full py-2 rounded-md bg-gray-700/50 hover:bg-gray-700"
53+
>
54+
<span className="mr-1">{isAtTop ? '↓' : '↑'}</span> {isAtTop ? 'To the bottom' : 'To the top'}
55+
</button>
56+
</div>
3557
</div>
3658
</aside>
3759
);

src/pages/BlogPostPage.js

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useState, useEffect } from 'react';
1+
import React, { useState, useEffect, useRef } from 'react';
22
import { useParams, Link } from 'react-router-dom';
33
import ReactMarkdown from 'react-markdown';
44
import fm from 'front-matter';
@@ -8,6 +8,9 @@ const BlogPostPage = () => {
88
const { slug } = useParams();
99
const [post, setPost] = useState(null);
1010
const [loading, setLoading] = useState(true);
11+
const [readingProgress, setReadingProgress] = useState(0);
12+
const [isAtTop, setIsAtTop] = useState(true); // New state for tracking if at top
13+
const contentRef = useRef(null);
1114

1215
useEffect(() => {
1316
const fetchPost = async () => {
@@ -31,6 +34,21 @@ const BlogPostPage = () => {
3134
fetchPost();
3235
}, [slug]);
3336

37+
useEffect(() => {
38+
const handleScroll = () => {
39+
if (contentRef.current) {
40+
const { scrollTop, scrollHeight, clientHeight } = document.documentElement;
41+
const totalHeight = scrollHeight - clientHeight;
42+
const currentProgress = (scrollTop / totalHeight) * 100;
43+
setReadingProgress(currentProgress);
44+
setIsAtTop(scrollTop === 0); // Update isAtTop based on scroll position
45+
}
46+
};
47+
48+
window.addEventListener('scroll', handleScroll);
49+
return () => window.removeEventListener('scroll', handleScroll);
50+
}, [post]); // Re-attach scroll listener if post changes
51+
3452
if (loading) {
3553
return <div className="text-center py-16">Loading...</div>;
3654
}
@@ -47,12 +65,12 @@ const BlogPostPage = () => {
4765
<Link to="/blog" className="text-primary-400 hover:text-primary-500 transition-colors mb-8 block">
4866
&larr; Back to Blog
4967
</Link>
50-
<div className="prose prose-xl prose-dark max-w-none">
68+
<div ref={contentRef} className="prose prose-xl prose-dark max-w-none">
5169
<ReactMarkdown>{post.body}</ReactMarkdown>
5270
</div>
5371
</div>
5472
<div className="hidden lg:block">
55-
<PostMetadata metadata={post.attributes} />
73+
<PostMetadata metadata={post.attributes} readingProgress={readingProgress} isAtTop={isAtTop} />
5674
</div>
5775
</div>
5876
</div>

0 commit comments

Comments
 (0)