v-offline
Version:
Offline and Online components for Vue
50 lines (49 loc) • 1.43 kB
JavaScript
import d from "ping.js";
import { defineComponent as g, ref as i, computed as v, onBeforeUnmount as p, h as m } from "vue";
const C = g({
name: "VOffline",
props: {
onlineClass: {
type: String,
required: !1,
default: ""
},
offlineClass: {
type: String,
required: !1,
default: ""
},
pingUrl: {
type: String,
required: !1,
default: "https://google.com"
}
},
emits: ["detected-condition"],
setup(e, { slots: f, emit: o }) {
const n = i(navigator.onLine || !1), l = i(["online", "offline", "load"]), c = i(e.pingUrl || "https://google.com"), u = v(() => n.value ? typeof e.onlineClass == "string" ? e.onlineClass : "" : typeof e.offlineClass == "string" ? e.offlineClass : "");
l.value.forEach((t) => window.addEventListener(t, a)), p(() => {
l.value.forEach(
(t) => window.removeEventListener(t, a)
);
});
async function a() {
const t = new d();
try {
(await t.ping(c.value) || navigator.onLine) && (n.value = !0, o("detected-condition", n.value));
} catch (s) {
(s || !navigator.onLine) && (n.value = !1, o("detected-condition", n.value));
}
}
return () => m("div", { class: u.value }, f);
}
});
let r = !1;
const y = (e) => {
r || (e.component("VOffline", C), r = !0);
};
export {
C as VOffline,
y as default
};
//# sourceMappingURL=v-offline.js.map