Advertisement
Guest User

Untitled

a guest
Feb 20th, 2020
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { useEffect, useState, useRef } from 'react';
  2.  
  3. export default function ({ className, children, target }: Props) {
  4.   const targetElRef = useRef<HTMLElement>(null);
  5.   const addonRef = useRef<HTMLDivElement>(null);
  6.   const [showing, setShowing] = useState<boolean>(false);
  7.  
  8.   const toggleShowing = () => setShowing(s => !s);
  9.  
  10.   useEffect(() => {
  11.     let targetEl = document.getElementById(target.key);
  12.     console.log("got target el", targetEl, target);
  13.     if(targetEl) {
  14.       console.log("got targetEl", targetEl);
  15.       targetElRef.current = targetEl;
  16.       targetElRef.current.addEventListener('click', toggleShowing);
  17.     }
  18.   }, [target]);
  19.  
  20.   useEffect(() => {
  21.     console.log("Received new showing", showing);
  22.     if(addonRef && addonRef.current) {
  23.       addonRef.current.style.opacity = showing ? 1 : 0;
  24.     }
  25.   }, [showing])
  26.  
  27.   return (
  28.     <div className={className} ref={addonRef}>
  29.       <div className="fill">{children}</div>
  30.     </div>
  31.   );
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement