UNPKG

react-extended-hooks

Version:

A collection of React custom hooks providing extended functionality for geolocation, platform details, battery status, and more.

449 lines (448 loc) 11.6 kB
import { useState as h, useEffect as I, useCallback as ge, useRef as ve } from "react"; function Ee({ onChargingChange: e, onLevelChange: t, onChargingTimeChange: i, onDischargingTimeChange: o } = {}) { const [a, l] = h( null ); return I(() => { const u = (async () => { if ("getBattery" in navigator) try { const r = await navigator.getBattery(), n = () => { const s = { charging: r.charging, level: Math.round(r.level * 100), chargingTime: r.chargingTime || null, dischargingTime: r.dischargingTime || null }; l(s), e && s.charging !== (a == null ? void 0 : a.charging) && e(s.charging), t && s.level !== (a == null ? void 0 : a.level) && t(s.level), i && s.chargingTime !== (a == null ? void 0 : a.chargingTime) && i(s.chargingTime), o && s.dischargingTime !== (a == null ? void 0 : a.dischargingTime) && o(s.dischargingTime); }; return n(), r.addEventListener("chargingchange", n), r.addEventListener("levelchange", n), r.addEventListener("chargingtimechange", n), r.addEventListener( "dischargingtimechange", n ), () => { r.removeEventListener("chargingchange", n), r.removeEventListener("levelchange", n), r.removeEventListener( "chargingtimechange", n ), r.removeEventListener( "dischargingtimechange", n ); }; } catch (r) { console.error("Failed to get battery details:", r), l(null); } else console.warn("Battery Status API is not supported in this browser."), l(null); })(); return () => { u instanceof Function && u(); }; }, [ e, t, i, o, a ]), a; } var _ = typeof globalThis < "u" ? globalThis : typeof window < "u" ? window : typeof global < "u" ? global : typeof self < "u" ? self : {}; function me(e) { return e && e.__esModule && Object.prototype.hasOwnProperty.call(e, "default") ? e.default : e; } var R, H; function oe() { if (H) return R; H = 1; function e(t) { var i = typeof t; return t != null && (i == "object" || i == "function"); } return R = e, R; } var E, X; function be() { if (X) return E; X = 1; var e = typeof _ == "object" && _ && _.Object === Object && _; return E = e, E; } var x, z; function se() { if (z) return x; z = 1; var e = be(), t = typeof self == "object" && self && self.Object === Object && self, i = e || t || Function("return this")(); return x = i, x; } var k, J; function he() { if (J) return k; J = 1; var e = se(), t = function() { return e.Date.now(); }; return k = t, k; } var G, K; function pe() { if (K) return G; K = 1; var e = /\s/; function t(i) { for (var o = i.length; o-- && e.test(i.charAt(o)); ) ; return o; } return G = t, G; } var P, Q; function ye() { if (Q) return P; Q = 1; var e = pe(), t = /^\s+/; function i(o) { return o && o.slice(0, e(o) + 1).replace(t, ""); } return P = i, P; } var A, Y; function ce() { if (Y) return A; Y = 1; var e = se(), t = e.Symbol; return A = t, A; } var B, Z; function Te() { if (Z) return B; Z = 1; var e = ce(), t = Object.prototype, i = t.hasOwnProperty, o = t.toString, a = e ? e.toStringTag : void 0; function l(c) { var u = i.call(c, a), r = c[a]; try { c[a] = void 0; var n = !0; } catch { } var s = o.call(c); return n && (u ? c[a] = r : delete c[a]), s; } return B = l, B; } var N, ee; function we() { if (ee) return N; ee = 1; var e = Object.prototype, t = e.toString; function i(o) { return t.call(o); } return N = i, N; } var F, ne; function Se() { if (ne) return F; ne = 1; var e = ce(), t = Te(), i = we(), o = "[object Null]", a = "[object Undefined]", l = e ? e.toStringTag : void 0; function c(u) { return u == null ? u === void 0 ? a : o : l && l in Object(u) ? t(u) : i(u); } return F = c, F; } var C, te; function je() { if (te) return C; te = 1; function e(t) { return t != null && typeof t == "object"; } return C = e, C; } var D, re; function _e() { if (re) return D; re = 1; var e = Se(), t = je(), i = "[object Symbol]"; function o(a) { return typeof a == "symbol" || t(a) && e(a) == i; } return D = o, D; } var M, ie; function Ie() { if (ie) return M; ie = 1; var e = ye(), t = oe(), i = _e(), o = NaN, a = /^[-+]0x[0-9a-f]+$/i, l = /^0b[01]+$/i, c = /^0o[0-7]+$/i, u = parseInt; function r(n) { if (typeof n == "number") return n; if (i(n)) return o; if (t(n)) { var s = typeof n.valueOf == "function" ? n.valueOf() : n; n = t(s) ? s + "" : s; } if (typeof n != "string") return n === 0 ? n : +n; n = e(n); var v = l.test(n); return v || c.test(n) ? u(n.slice(2), v ? 2 : 8) : a.test(n) ? o : +n; } return M = r, M; } var W, ae; function Oe() { if (ae) return W; ae = 1; var e = oe(), t = he(), i = Ie(), o = "Expected a function", a = Math.max, l = Math.min; function c(u, r, n) { var s, v, y, p, g, d, m = 0, S = !1, T = !1, O = !0; if (typeof u != "function") throw new TypeError(o); r = i(r) || 0, e(n) && (S = !!n.leading, T = "maxWait" in n, y = T ? a(i(n.maxWait) || 0, r) : y, O = "trailing" in n ? !!n.trailing : O); function q(f) { var b = s, w = v; return s = v = void 0, m = f, p = u.apply(w, b), p; } function ue(f) { return m = f, g = setTimeout(j, r), S ? q(f) : p; } function le(f) { var b = f - d, w = f - m, $ = r - b; return T ? l($, y - w) : $; } function U(f) { var b = f - d, w = f - m; return d === void 0 || b >= r || b < 0 || T && w >= y; } function j() { var f = t(); if (U(f)) return V(f); g = setTimeout(j, le(f)); } function V(f) { return g = void 0, O && s ? q(f) : (s = v = void 0, p); } function de() { g !== void 0 && clearTimeout(g), m = 0, s = d = v = g = void 0; } function fe() { return g === void 0 ? p : V(t()); } function L() { var f = t(), b = U(f); if (s = arguments, v = this, d = f, b) { if (g === void 0) return ue(d); if (T) return clearTimeout(g), g = setTimeout(j, r), q(d); } return g === void 0 && (g = setTimeout(j, r)), p; } return L.cancel = de, L.flush = fe, L; } return W = c, W; } var qe = Oe(); const Le = /* @__PURE__ */ me(qe); function xe({ trackOnlineStatus: e = !0, debounceDelay: t = 0, initialDetails: i = {} } = {}) { const [o, a] = h( () => { if (typeof navigator < "u") { const { userAgent: c, language: u, onLine: r } = navigator; return { userAgent: c, language: u, onLine: r, ...i }; } return { userAgent: "", language: "", onLine: !1, ...i }; } ), l = ge(() => { a((c) => ({ ...c, onLine: navigator.onLine })); }, []); return I(() => { if (!e) return; const c = Le(l, t); return window.addEventListener("online", c), window.addEventListener("offline", c), () => { window.removeEventListener("online", c), window.removeEventListener("offline", c), c.cancel(); }; }, [l, t, e]), o; } function ke({ enableHighAccuracy: e = !1, timeout: t = 1 / 0, maximumAge: i = 0, enableWatch: o = !1 } = {}) { const [a, l] = h({ latitude: null, longitude: null, accuracy: null, altitude: null, altitudeAccuracy: null, heading: null, speed: null, timestamp: null, error: null }); return I(() => { if (!("geolocation" in navigator)) { console.warn("Geolocation API is not supported in this browser."), l({ latitude: null, longitude: null, accuracy: null, altitude: null, altitudeAccuracy: null, heading: null, speed: null, timestamp: null, error: null }); return; } const c = (n) => { const { coords: s, timestamp: v } = n; l({ latitude: s.latitude, longitude: s.longitude, accuracy: s.accuracy, altitude: s.altitude ?? null, altitudeAccuracy: s.altitudeAccuracy ?? null, heading: s.heading ?? null, speed: s.speed ?? null, timestamp: v, error: null }); }, u = (n) => { l({ latitude: null, longitude: null, accuracy: null, altitude: null, altitudeAccuracy: null, heading: null, speed: null, timestamp: null, error: n }); }, r = { enableHighAccuracy: e, timeout: t, maximumAge: i }; if (o) { const n = navigator.geolocation.watchPosition( c, u, r ); return () => navigator.geolocation.clearWatch(n); } else navigator.geolocation.getCurrentPosition( c, u, r ); }, [e, t, i, o]), a; } function Ge({ onStart: e, onEnd: t, onError: i, rate: o = 1, pitch: a = 1, volume: l = 1, voiceURI: c } = {}) { const [u, r] = h(!1), [n, s] = h([]), v = ve(null); return I(() => { const g = () => { const d = window.speechSynthesis.getVoices(); s(d); }; return "speechSynthesis" in window ? (window.speechSynthesis.addEventListener( "voiceschanged", g ), g()) : console.warn("SpeechSynthesis API is not supported in this browser."), () => { "speechSynthesis" in window && window.speechSynthesis.removeEventListener( "voiceschanged", g ); }; }, []), { speak: (g) => { if (!("speechSynthesis" in window)) { console.warn("SpeechSynthesis API is not supported in this browser."); return; } if (u) { console.warn("Speech is already in progress."); return; } const d = new SpeechSynthesisUtterance(g); if (v.current = d, d.rate = o, d.pitch = a, d.volume = l, c) { const m = n.find((S) => S.voiceURI === c); m ? d.voice = m : console.warn( "Specified voiceURI not found. Default voice will be used." ); } d.onstart = () => { r(!0), e == null || e(); }, d.onend = () => { r(!1), t == null || t(); }, d.onerror = (m) => { r(!1), i == null || i(m); }, window.speechSynthesis.speak(d); }, cancel: () => { "speechSynthesis" in window && (window.speechSynthesis.cancel(), r(!1)); }, isSpeaking: u, voices: n }; } function Pe() { const [e, t] = h(""), [i, o] = h(!1), [a, l] = h(""); return { copiedText: e, isCopied: i, clipboardContent: a, copyToClipboard: async (r) => { try { await navigator.clipboard.writeText(r), t(r), o(!0), setTimeout(() => o(!1), 2e3); } catch (n) { console.error("Failed to copy text: ", n), o(!1); } }, readFromClipboard: async () => { try { const r = await navigator.clipboard.readText(); l(r); } catch (r) { console.error("Failed to read from clipboard: ", r); } } }; } export { Ee as useBatteryDetails, Pe as useClipBoardDetails, ke as useGeolocationDetails, xe as usePlatformDetails, Ge as useSpeechSynthesis }; //# sourceMappingURL=react-extended-hooks-es.js.map