Browse Source

[feat] smarter timers

Signed-off-by: Muthu Kumar <muthukumar@thefeathers.in>
pull/1/head
Muthu Kumar 4 years ago
parent
commit
cfce084f90
Signed by: mkrhere GPG Key ID: 3FD688398897097E
  1. 22
      src/components/Container.js
  2. 14
      src/util/index.js

22
src/components/Container.js

@ -4,6 +4,7 @@ import { Link, navigate } from "@reach/router";
import { ReactComponent as Logo } from "../assets/logo.svg"; import { ReactComponent as Logo } from "../assets/logo.svg";
import { ReactComponent as Right } from "../assets/arrow-right.svg"; import { ReactComponent as Right } from "../assets/arrow-right.svg";
import { getTimeout } from "../util";
const flash = css` const flash = css`
span& { span& {
@ -15,7 +16,7 @@ const flash = css`
} }
`; `;
const clearable = new Set(); const [timer, clear] = getTimeout();
const Container = ({ children, hideNav = false, next, ...props }) => { const Container = ({ children, hideNav = false, next, ...props }) => {
const logoContainer = useRef(); const logoContainer = useRef();
@ -33,36 +34,31 @@ const Container = ({ children, hideNav = false, next, ...props }) => {
useEffect(() => { useEffect(() => {
if (highlightCircle.current) { if (highlightCircle.current) {
highlightCircle.current.classList.add(flash); highlightCircle.current.classList.add(flash);
clearable.add( timer(() => highlightCircle.current && highlightCircle.current.classList.remove(flash), 1500);
setTimeout(
() => highlightCircle.current && highlightCircle.current.classList.remove(flash),
1500,
),
);
} }
if (nextBtn.current) { if (nextBtn.current) {
nextBtn.current.style.width = "4rem"; nextBtn.current.style.width = "4rem";
setTimeout(() => nextBtn.current && (nextBtn.current.style.right = "10vw"), 300); timer(() => nextBtn.current && (nextBtn.current.style.right = "10vw"), 300);
} }
if (containerChild.current) { if (containerChild.current) {
const containerChildren = [...containerChild.current.children]; const containerChildren = [...containerChild.current.children];
containerChildren.forEach((child, idx) => { containerChildren.forEach((child, idx) => {
setTimeout(() => { timer(() => {
child.style.removeProperty("opacity"); child.style.removeProperty("opacity");
child.style.removeProperty("margin-bottom"); child.style.removeProperty("margin-bottom");
}, 100 * idx); }, 100 * idx);
}); });
setTimeout(() => { timer(() => {
document.body.style.maxHeight = "auto"; document.body.style.maxHeight = "auto";
document.body.style.overflow = "auto"; document.body.style.overflow = "auto";
}, containerChildren.length * 100); }, containerChildren.length * 100);
} }
// cleanup // cleanup
return () => (clearable.forEach(item => clearTimeout(item)), clearable.clear()); return clear;
}, []); }, []);
const handleResize = () => { const handleResize = () => {
@ -90,7 +86,7 @@ const Container = ({ children, hideNav = false, next, ...props }) => {
document.body.style.maxHeight = "100vh"; document.body.style.maxHeight = "100vh";
document.body.style.overflow = "hidden"; document.body.style.overflow = "hidden";
e.currentTarget.style.width = 0; e.currentTarget.style.width = 0;
setTimeout(() => navigate(next), 300); timer(() => navigate(next), 300);
}; };
return ( return (
@ -131,7 +127,7 @@ const Container = ({ children, hideNav = false, next, ...props }) => {
z-index: 0; z-index: 0;
opacity: 0%; opacity: 0%;
transition: all 300ms; transition: all 600ms;
`, `,
"logo-highlight", "logo-highlight",
)}></span> )}></span>

14
src/util/index.js

@ -0,0 +1,14 @@
export const getTimeout = () => {
const set = new Set();
const timeout = (f, t) => {
const self = set.add(setTimeout(() => (f(), set.delete(self)), t));
};
const clearTimers = () => {
set.forEach(timer => clearTimeout(timer));
set.clear();
};
return [timeout, clearTimers];
};
Loading…
Cancel
Save