-
Notifications
You must be signed in to change notification settings - Fork 15
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
base: main
Are you sure you want to change the base?
Scroll top #46
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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"; | ||
|
@@ -75,7 +76,7 @@ const Home = () => { | |
[id]: colorData, | ||
})); | ||
}; | ||
|
||
const baseColor = Object.values(generatedColors)[0]?.baseColor; | ||
/** | ||
* Exports all generated colors as a JSON string and copies it to the clipboard. | ||
*/ | ||
|
@@ -120,6 +121,34 @@ const Home = () => { | |
() => toast.error("Failed to copy colors to clipboard."), | ||
); | ||
}; | ||
|
||
const [isVisible, setIsVisible] = useState(false); | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can move this |
||
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 */} | ||
|
@@ -167,6 +196,15 @@ const Home = () => { | |
} // Callback when colors are generated | ||
/> | ||
))} | ||
{isVisible && ( | ||
<button | ||
onClick={scrollToTop} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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> | ||
); | ||
}; | ||
|
There was a problem hiding this comment.
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