UNPKG

@primer/react

Version:

An implementation of GitHub's Primer Design System using React

105 lines (101 loc) 2.38 kB
'use strict'; var reactCompilerRuntime = require('react-compiler-runtime'); var React = require('react'); function useDetails(t0) { const $ = reactCompilerRuntime.c(15); const { ref, closeOnOutsideClick, defaultOpen, onClickOutside } = t0; const [open, setOpen] = React.useState(defaultOpen); const backupRef = React.useRef(null); const customRef = ref !== null && ref !== void 0 ? ref : backupRef; let t1; if ($[0] !== customRef || $[1] !== onClickOutside) { t1 = event => { const { current } = customRef; const eventTarget = event.target; const closest = eventTarget.closest("details"); if (closest !== current) { onClickOutside && onClickOutside(event); if (!event.defaultPrevented) { setOpen(false); } } }; $[0] = customRef; $[1] = onClickOutside; $[2] = t1; } else { t1 = $[2]; } const onClickOutsideInternal = t1; let t2; let t3; if ($[3] !== closeOnOutsideClick || $[4] !== onClickOutsideInternal || $[5] !== open) { t2 = () => { if (open && closeOnOutsideClick) { document.addEventListener("click", onClickOutsideInternal); return () => { document.removeEventListener("click", onClickOutsideInternal); }; } }; t3 = [open, closeOnOutsideClick, onClickOutsideInternal]; $[3] = closeOnOutsideClick; $[4] = onClickOutsideInternal; $[5] = open; $[6] = t2; $[7] = t3; } else { t2 = $[6]; t3 = $[7]; } React.useEffect(t2, t3); let t4; if ($[8] === Symbol.for("react.memo_cache_sentinel")) { t4 = e => { if (!e.defaultPrevented) { const eventTarget_0 = e.target; setOpen(eventTarget_0.open); } }; $[8] = t4; } else { t4 = $[8]; } const handleToggle = t4; let t5; if ($[9] !== customRef || $[10] !== open) { t5 = () => ({ onToggle: handleToggle, open, ref: customRef }); $[9] = customRef; $[10] = open; $[11] = t5; } else { t5 = $[11]; } const getDetailsProps = t5; let t6; if ($[12] !== getDetailsProps || $[13] !== open) { t6 = { open, setOpen, getDetailsProps }; $[12] = getDetailsProps; $[13] = open; $[14] = t6; } else { t6 = $[14]; } return t6; } module.exports = useDetails;