Skip to content

Instantly share code, notes, and snippets.

@ChaseH88
Created March 2, 2022 11:49
Show Gist options
  • Save ChaseH88/8fee18fd33dae424da65e402afb20700 to your computer and use it in GitHub Desktop.
Save ChaseH88/8fee18fd33dae424da65e402afb20700 to your computer and use it in GitHub Desktop.
Fade Out Element - Simply pass a ref into the div. Then onClick, you can utilize this function to fade out the element as it is removed from the DOM.
/**
* Fade out elements as they're deleted
* @param el - the element to focus
* @param speed - the speed of the focus
* @example
const ref = useRef<HTMLDivElement>(null);
...
fadeOut(ref.current, 500)
*/
const fadeOut = (el: HTMLDivElement, speed: number) => {
el.style.transition = `opacity ${speed / 1000}s ease`;
el.style.opacity = '0';
setTimeout(() => el.parentNode.removeChild(el), speed);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment