UNPKG

react-box-tools

Version:

Box tools react components, utils and hooks

197 lines (196 loc) 4.78 kB
import { u as w, b as m } from "./useOutClick-DP--F-Sn.js"; import { a as H } from "./useOutClick-DP--F-Sn.js"; import { i as p, p as S } from "./json-B_vrZjDX.js"; import { useCallback as d, useState as g, useEffect as f, useRef as h } from "react"; import { u as M } from "./useUpdateEffect-4dQLu1cU.js"; import { u as j, a as q } from "./useResizeObserver-7Snrkg9X.js"; import { u as C } from "./useDebounce-CqCa03F5.js"; const v = (t, e) => { const o = d(() => { if (!p) return e; try { const r = window.localStorage.getItem(t); return r ? S(r) : e; } catch (r) { return console.warn(`Error reading localStorage key “${t}”:`, r), e; } }, [e, t]), [n, c] = g(o), u = E((r) => { p || console.warn( `Tried setting localStorage key “${t}” even though environment is not a client` ); try { const i = r instanceof Function ? r(n) : r; window.localStorage.setItem(t, JSON.stringify(i)), c(i), window.dispatchEvent(new Event("local-storage")); } catch (i) { console.warn(`Error setting localStorage key “${t}”:`, i); } }); f(() => { c(o()); }, [o]); const l = d( (r) => { r != null && r.key && r.key !== t || c(o()); }, [t, o] ); return w("storage", l), w("local-storage", l), [n, u]; }; function E(t) { const e = h(() => { throw new Error("Cannot call an event handler while rendering."); }); return m(() => { e.current = t; }, [t]), d((...o) => e.current(...o), [e]); } function x() { const t = h(!0); return t.current ? (t.current = !1, !0) : t.current; } function z(t, e) { const o = h(t); m(() => { o.current = t; }, [t]), f(() => { if (!e && e !== 0) return; const n = setInterval(() => { o.current(); }, e); return () => { clearInterval(n); }; }, [e]); } function L(t, e) { const o = h(t); m(() => { o.current = t; }, [t]), f(() => { if (!e && e !== 0) return; const n = setTimeout(() => { o.current(); }, e); return () => { clearTimeout(n); }; }, [e]); } function R() { const [t, e] = g({ width: 0, height: 0 }), o = () => { e({ ...t, width: window.innerWidth, height: window.innerHeight }); }; return w("resize", o), m(() => { o(); }, []), t; } function W(t, e) { const o = d(() => { if (typeof window > "u") return e; try { const r = window.sessionStorage.getItem(t); return r ? S(r) : e; } catch (r) { return console.warn(`Error reading sessionStorage key “${t}”:`, r), e; } }, [e, t]), [n, c] = g(o), u = E((r) => { typeof window > "u" && console.warn( `Tried setting sessionStorage key “${t}” even though environment is not a client` ); try { const i = r instanceof Function ? r(n) : r; window.sessionStorage.setItem(t, JSON.stringify(i)), c(i), window.dispatchEvent(new Event("session-storage")); } catch (i) { console.warn(`Error setting sessionStorage key “${t}”:`, i); } }); f(() => { c(o()); }, [o]); const l = d( (r) => { r != null && r.key && r.key !== t || c(o()); }, [t, o] ); return w("storage", l), w("session-storage", l), [n, u]; } const F = (t, e = 500, o) => { const [n, c] = g(o), u = h(Date.now()); return f(() => { if (Date.now() >= u.current + e) u.current = Date.now(), n && t(n); else { const l = setTimeout(() => { u.current = Date.now(), n && t(n); }, e); return () => { clearTimeout(l); }; } }, [n, e]), c; }, J = (t, e) => { const [o, n] = g({ top: 0, height: 0, width: 0, left: 0 }), u = C(() => { if (t.current) { const { scrollTop: a, scrollHeight: s, scrollLeft: I, scrollWidth: T } = t.current; n({ top: a, height: s, width: T, left: I }); } }, e ?? 500); return w("scroll", u, t), { ...o, fn: (...a) => { var s; (s = t.current) == null || s.scroll(...a); }, to: (...a) => { var s; (s = t.current) == null || s.scrollTo(...a); }, by: (...a) => { var s; (s = t.current) == null || s.scrollBy(...a); }, intoView: (a) => { var s; (s = t.current) == null || s.scrollIntoView(a); } }; }; export { C as useDebounce, E as useEventCallback, w as useEventListener, x as useFirstRender, j as useIntersectionObserver, z as useInterval, m as useIsomorphicLayoutEffect, v as useLocalStorage, H as useOutClick, q as useResizeObserver, J as useScroll, W as useSessionStorage, F as useThrottle, L as useTimeout, M as useUpdateEffect, R as useWindowResize };