Skip to content

Commit

Permalink
feat: added a scroll to top button
Browse files Browse the repository at this point in the history
  • Loading branch information
coder-pink committed Oct 18, 2024
1 parent 73df15b commit 349a247
Show file tree
Hide file tree
Showing 19 changed files with 208 additions and 31 deletions.
174 changes: 154 additions & 20 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
"react": "^18.3.1",
"react-dom": "^18.3.1",
"react-error-boundary": "^4.1.0",
"react-icons": "^5.3.0",
"react-quill": "^2.0.0",
"react-router-dom": "^6.26.2"
},
"devDependencies": {
Expand Down
1 change: 1 addition & 0 deletions src/App.jsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import React from "react";
import { createBrowserRouter, RouterProvider } from "react-router-dom";
import Home from "./pages/Home";
import CreateBlogs from "./pages/CreateBlogs";
Expand Down
1 change: 1 addition & 0 deletions src/Layout.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Layout.jsx
import React from "react";
import { Outlet } from "react-router-dom";
import NavBar from "./components/NavBar";

Expand Down
2 changes: 1 addition & 1 deletion src/components/BlogForm.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useState } from "react";
import React, { useState } from "react";
import ReactQuill from 'react-quill';
import 'react-quill/dist/quill.snow.css';

Expand Down
1 change: 1 addition & 0 deletions src/components/Button.jsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import React from "react";
const Button = ({ label, onClick, className }) => {
return (
<button
Expand Down
2 changes: 1 addition & 1 deletion src/components/HeroSection.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useState, useEffect } from "react";
import React, { useState, useEffect } from "react";

const HeroSection = ({ posts, autoSlideInterval = 5000 }) => {
const [currentIndex, setCurrentIndex] = useState(0);
Expand Down
2 changes: 1 addition & 1 deletion src/components/LoadMoreButton.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// import React from "react";
import React from "react";

const LoadMoreButton = () => {
return (
Expand Down
2 changes: 1 addition & 1 deletion src/components/LoginForm.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useState } from "react";
import React, { useState } from "react";

const LoginForm = () => {
const [email, setEmail] = useState("");
Expand Down
2 changes: 1 addition & 1 deletion src/components/NavBar.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useEffect, useState } from "react";
import React, { useEffect, useState } from "react";
import { useNavigate } from "react-router-dom";
import Sun from "./svgs/Sun";
import Moon from "./svgs/Moon";
Expand Down
2 changes: 1 addition & 1 deletion src/components/PostCard.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// import React from "react";
import React from "react";

const PostCard = ({ post }) => {
return (
Expand Down
2 changes: 1 addition & 1 deletion src/components/RecentPosts.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// import React from "react";
import React from "react";
import PostCard from "./PostCard";
import LoadMoreButton from "./LoadMoreButton";

Expand Down
32 changes: 32 additions & 0 deletions src/components/ScrollToTopButton.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import React, { useState, useEffect } from 'react';
import { FaChevronUp } from "react-icons/fa";

const ScrollToTopButton = () => {
const [isVisible, setIsVisible] = useState(false);

const handleScroll = () => {
if (window.scrollY > 300) {
setIsVisible(true);
} else {
setIsVisible(false);
}
}

const scrollToTop = () => {
window.scrollTo({ top: 0, behavior: "smooth" })
}

useEffect(() => {
window.addEventListener('scroll', handleScroll);
return () => {
window.removeEventListener('scroll', handleScroll);
}
}, [])
return (
<>
{isVisible && (<button onClick={scrollToTop} className='fixed bottom-7 right-4 p-4 bg-purple-100 text-purple-600 rounded-full shadow-lg hover:bg-purple-200 focus:outline-none focus:ring-2 focus:ring-purple-400 transition-all duration-300 ease-in-out transform hover:scale-110'> <FaChevronUp className="w-8 h-8" aria-label="Scroll to top" /></button>)}
</>
)
}

export default ScrollToTopButton
4 changes: 2 additions & 2 deletions src/components/svgs/Moon.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ const Moon = (props) => {
xmlns="http://www.w3.org/2000/svg"
>
<path
fill-rule="evenodd"
clip-rule="evenodd"
fillRule="evenodd"
clipRule="evenodd"
d="M10.9994 3.3733C11.3211 3.69498 11.4469 4.16374 11.3295 4.60326C10.7419 6.80366 11.3123 9.24326 13.0345 10.9655C14.7567 12.6877 17.1963 13.2581 19.3967 12.6705C19.8363 12.5531 20.305 12.6789 20.6267 13.0006C20.9484 13.3223 21.0742 13.791 20.9568 14.2306C20.556 15.7314 19.7664 17.1501 18.5916 18.3249C15.0248 21.8917 9.24189 21.8917 5.67509 18.3249C2.1083 14.7581 2.1083 8.9752 5.67509 5.40841C6.84993 4.23357 8.26857 3.44398 9.76945 3.04318C10.209 2.9258 10.6777 3.05162 10.9994 3.3733Z"
></path>
</svg>
Expand Down
2 changes: 1 addition & 1 deletion src/main.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { StrictMode } from "react";
import React, { StrictMode } from "react";
import { createRoot } from "react-dom/client";
import App from "./App.jsx";
import "./index.css";
Expand Down
1 change: 1 addition & 0 deletions src/pages/CreateBlogs.jsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import React from "react";
import BlogForm from "../components/BlogForm";

const CreateBlogs = () => {
Expand Down
3 changes: 3 additions & 0 deletions src/pages/Home.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import React from "react";
import Footer from "../components/Footer";
import HeroSection from "../components/HeroSection";
import RecentPosts from "../components/RecentPosts";
import ScrollToTopButton from "../components/ScrollToTopButton";

const Home = () => {
const heroPosts = [
Expand Down Expand Up @@ -80,6 +82,7 @@ const Home = () => {
<RecentPosts posts={blogPosts} />

<Footer />
<ScrollToTopButton/>
</div>
);
};
Expand Down
1 change: 1 addition & 0 deletions src/pages/Login.jsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import React from "react";
import LoginForm from "../components/LoginForm";

const Login = () => {
Expand Down
3 changes: 2 additions & 1 deletion src/pages/PageNotFound.jsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import React from "react";
import { useNavigate } from "react-router-dom";

const NotFound = () => {
Expand All @@ -7,7 +8,7 @@ const NotFound = () => {
<div className="flex flex-col justify-center items-center bg-background h-screen">
<h4 className="text-7xl font-extrabold mb-4">404</h4>
<p className="text-lg mb-6">
We can't seem to find the page you're looking for.
We can not seem to find the page you are looking for.
</p>
<button
onClick={() => navigate("/")}
Expand Down

0 comments on commit 349a247

Please sign in to comment.