/*! For license information please see a602f988.da91ce19.js.LICENSE.txt */
"use strict";(self.webpackChunkweb_totals=self.webpackChunkweb_totals||[]).push([[8289],{542:(e,t,n)=>{n.r(t),n.d(t,{assets:()=>u,contentTitle:()=>c,default:()=>d,frontMatter:()=>r,metadata:()=>i,toc:()=>a});var s=n(85893),o=n(11151);const r={sidebar_position:5,title:"useClickOutside",comment:!0,tags:["Reactjs","Custom Hook","useClickOutside"]},c=void 0,i={id:"reactjs/custom-hook/use-click-outside",title:"useClickOutside",description:"",source:"@site/issues/reactjs/custom-hook/use-click-outside.md",sourceDirName:"reactjs/custom-hook",slug:"/reactjs/custom-hook/use-click-outside",permalink:"/issues/reactjs/custom-hook/use-click-outside",draft:!1,unlisted:!1,editUrl:"https://github.com/hunghg255/web-totals/tree/main/issues/reactjs/custom-hook/use-click-outside.md",tags:[{label:"Reactjs",permalink:"/issues/tags/reactjs"},{label:"Custom Hook",permalink:"/issues/tags/custom-hook"},{label:"useClickOutside",permalink:"/issues/tags/use-click-outside"}],version:"current",sidebarPosition:5,frontMatter:{sidebar_position:5,title:"useClickOutside",comment:!0,tags:["Reactjs","Custom Hook","useClickOutside"]},sidebar:"tutorialSidebar",previous:{title:"useFetch",permalink:"/issues/reactjs/custom-hook/use-fetch"},next:{title:"useCountDown",permalink:"/issues/reactjs/custom-hook/use-count-down"}},u={},a=[];function l(e){const t={code:"code",pre:"pre",...(0,o.a)(),...e.components};return(0,s.jsx)(t.pre,{children:(0,s.jsx)(t.code,{className:"language-jsx",metastring:"live noInline",live:!0,children:"const useEventListener = (eventType, callback, element = window) => {\n  const refCallback = useRef(callback);\n\n  useEffect(() => {\n    refCallback.current = callback;\n  }, [callback]);\n\n  useEffect(() => {\n    const handler = (e) => refCallback.current(e);\n    element.addEventListener(eventType, handler);\n    return () => element.removeEventListener(eventType, handler);\n  }, [eventType, element]);\n};\n\nconst useClickOutside = (ref, cb) => {\n  useEventListener('click', (e) => {\n    if (ref.current == null || ref.current.contains(e.target)) return;\n    cb(e);\n  });\n};\n\nconst App = () => {\n  const [open, setOpen] = useState(false);\n  const refModal = useRef();\n\n  useClickOutside(refModal, () => {\n    if (open) setOpen(false);\n  });\n\n  return (\n    <>\n      <button onClick={() => setOpen(true)}>Open</button>\n      <div\n        ref={refModal}\n        style={{\n          display: open ? 'block' : 'none',\n          backgroundColor: 'blue',\n          color: 'white',\n          width: 100,\n          height: 100,\n          position: 'fixed',\n          top: 'calc(50% - 50px)',\n          left: 'calc(50% - 50px)',\n        }}\n      >\n        <span>Modal</span>\n      </div>\n    </>\n  );\n};\n\nrender(<App />);\n"})})}function d(e={}){const{wrapper:t}={...(0,o.a)(),...e.components};return t?(0,s.jsx)(t,{...e,children:(0,s.jsx)(l,{...e})}):l(e)}},75251:(e,t,n)=>{var s=n(67294),o=Symbol.for("react.element"),r=Symbol.for("react.fragment"),c=Object.prototype.hasOwnProperty,i=s.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.ReactCurrentOwner,u={key:!0,ref:!0,__self:!0,__source:!0};function a(e,t,n){var s,r={},a=null,l=null;for(s in void 0!==n&&(a=""+n),void 0!==t.key&&(a=""+t.key),void 0!==t.ref&&(l=t.ref),t)c.call(t,s)&&!u.hasOwnProperty(s)&&(r[s]=t[s]);if(e&&e.defaultProps)for(s in t=e.defaultProps)void 0===r[s]&&(r[s]=t[s]);return{$$typeof:o,type:e,key:a,ref:l,props:r,_owner:i.current}}t.Fragment=r,t.jsx=a,t.jsxs=a},85893:(e,t,n)=>{e.exports=n(75251)},11151:(e,t,n)=>{n.d(t,{Z:()=>i,a:()=>c});var s=n(67294);const o={},r=s.createContext(o);function c(e){const t=s.useContext(r);return s.useMemo((function(){return"function"==typeof e?e(t):{...t,...e}}),[t,e])}function i(e){let t;return t=e.disableParentContext?"function"==typeof e.components?e.components(o):e.components||o:c(e.components),s.createElement(r.Provider,{value:t},e.children)}}}]);