Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scroll top #46

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"react": "18.3.1",
"react-dom": "18.3.1",
"react-hot-toast": "2.4.1",
"react-icons": "5.3.0",
"react-router-dom": "6.27.0"
},
"devDependencies": {
Expand Down
42 changes: 40 additions & 2 deletions src/pages/Home.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useState } from "react";
import { useState, useEffect } from "react";
import { toast } from "react-hot-toast";
import { FaCaretUp } from "react-icons/fa";

import { ALLOWED_COLOR_NAMES } from "../constants";
import { generateExportData } from "../utils/colorUtils";
Expand Down Expand Up @@ -75,7 +76,7 @@ const Home = () => {
[id]: colorData,
}));
};

const baseColor = Object.values(generatedColors)[0]?.baseColor;
/**
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a code comment explaining what is this for

* Exports all generated colors as a JSON string and copies it to the clipboard.
*/
Expand Down Expand Up @@ -120,6 +121,34 @@ const Home = () => {
() => toast.error("Failed to copy colors to clipboard."),
);
};

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

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can move this useState below line 22

useEffect(() => {
// Toggle the visibility of the button based on the scroll position
const toggleVisibility = () => {
if (window.scrollY > 300) {
setIsVisible(true);
} else {
setIsVisible(false);
}
};

window.addEventListener("scroll", toggleVisibility);

return () => {
window.removeEventListener("scroll", toggleVisibility);
};
}, []);

// Smooth scroll to the top when button is clicked
const scrollToTop = () => {
window.scrollTo({
top: 0,
behavior: "smooth",
});
};

return (
<main className="container mx-auto px-6 py-12">
{/* App title */}
Expand Down Expand Up @@ -167,6 +196,15 @@ const Home = () => {
} // Callback when colors are generated
/>
))}
{isVisible && (
<button
onClick={scrollToTop}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have a component in Shared, you can use that

className="fixed bottom-4 border-2 right-4 p-2 text-black rounded-sm shadow-md lg:hidden"
style={{ borderColor: baseColor }}
>
<FaCaretUp size={40} color={baseColor} />
</button>
)}
</main>
);
};
Expand Down
Loading