Skip to content

Commit

Permalink
feat: animate backToTop btn (#171)
Browse files Browse the repository at this point in the history
* feat: animate backToTop btn

* fix npm problem with a new transition package

* floated btn add animation
  • Loading branch information
Lourvens authored Feb 16, 2023
1 parent fe20fab commit 153c52e
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 17 deletions.
46 changes: 29 additions & 17 deletions components/BackToTop/BackToTopButton.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import React, { useEffect, useState } from "react";
import { useTransition } from "react-transition-state";
import { FaArrowUp } from "react-icons/fa";
import { SCROLL_LIMIT } from "app/constants";

export const BackToTopButton = () => {
const [scrollY, setScrollY] = useState(0);
const [{ isMounted, status }, toggle] = useTransition({
mountOnEnter: true,
unmountOnExit: true,
preEnter: true,
});

useEffect(() => {
const handleScroll = () => {
Expand All @@ -19,28 +25,34 @@ export const BackToTopButton = () => {
};
}, []);

useEffect(() => {
toggle(scrollY > SCROLL_LIMIT);
}, [scrollY]);

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

if (scrollY > SCROLL_LIMIT) {
return (
<div className="group fixed bottom-12 right-12 z-20">
<button
className="focus:animate-button-press rounded-full border border-white bg-violet-600 p-4 text-white shadow-xl focus:ring group-hover:border-dashed group-hover:border-violet-400 group-hover:bg-white dark:drop-shadow-[5px_5px_8px_rgba(124,58,237,0.25)] dark:group-hover:bg-[#101623] md:border-violet-600"
onClick={handleClick}
>
<FaArrowUp className="group-hover:text-violet-500" />
</button>
<span className="absolute left-1/2 top-1/2 -z-10 hidden -translate-x-1/2 -translate-y-1/2 rotate-0 text-2xl transition-all duration-100 ease-in-out group-hover:ml-8 group-hover:block group-hover:rotate-45">
👾
</span>
</div>
);
}

return null;
return isMounted ? (
<div
className={`group fixed z-20 bottom-12 right-12 transform transition duration-300${
status === "preEnter" || status === "exiting"
? " opacity-0 translate-y-3"
: ""
}`}
>
<button
className="focus:animate-button-press rounded-full border border-white bg-violet-600 p-4 text-white shadow-xl duration-300 transition-colors focus:ring group-hover:border-dashed group-hover:border-violet-400 group-hover:bg-white dark:drop-shadow-[5px_5px_8px_rgba(124,58,237,0.25)] dark:group-hover:bg-[#101623] md:border-violet-600"
onClick={handleClick}
>
<FaArrowUp className="group-hover:text-violet-500" />
</button>
<span className="absolute left-1/2 top-1/2 -z-10 hidden -translate-x-1/2 -translate-y-1/2 rotate-0 text-2xl transition-all duration-100 ease-in-out group-hover:ml-8 group-hover:block group-hover:rotate-45">
👾
</span>
</div>
) : null;
};
16 changes: 16 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 @@ -26,6 +26,7 @@
"react-dom": "18.2.0",
"react-icons": "^4.7.1",
"react-spinners": "^0.13.8",
"react-transition-state": "^2.1.0",
"typescript": "4.9.4"
},
"devDependencies": {
Expand Down

1 comment on commit 153c52e

@vercel
Copy link

@vercel vercel bot commented on 153c52e Feb 16, 2023

Choose a reason for hiding this comment

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

Successfully deployed to the following URLs:

linkshub – ./

linkshub.vercel.app
linkshub-git-main-rupali-codes.vercel.app
linkshub-rupali-codes.vercel.app

Please sign in to comment.