npm i v-0ffline
App.vue
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
<template> <v-offline online-class="online" offline-class="offline" @detected-condition="onNetworkChange" > <template v-if="online"> <div class="flex w-full h-screen justify-center items-center text-6xl"> ⚡️ Online </div> </template> <template v-if="!online"> <div class="flex w-full h-screen justify-center items-center text-6xl"> 💩 Offline </div> </template> </v-offline> <!-- Netlify Badge --> <div class="absolute bottom-4 right-4"> <a href="https://app.netlify.com/sites/v-offline/deploys" aria-label="View deploys on Netlify" target="_blank" rel="noopener noreferrer" class="gray-400" > <img src="https://www.netlify.com/img/global/badges/netlify-color-accent.svg" alt="Deploys by Netlify" /> </a> </div> </template> <script lang="ts"> import { defineComponent, ref, onMounted, onUnmounted } from "vue"; import type { Ref } from "vue"; import { VOffline } from "v-offline"; export default defineComponent({ components: { VOffline, }, setup() { const online: Ref<boolean> = ref(navigator.onLine); const onNetworkChange = (status: boolean) => { online.value = status; }; const updateOnlineStatus = () => { online.value = navigator.onLine; }; // Listen for online and offline events onMounted(() => { window.addEventListener("online", updateOnlineStatus); window.addEventListener("offline", updateOnlineStatus); }); // Cleanup the event listeners when the component is destroyed onUnmounted(() => { window.removeEventListener("online", updateOnlineStatus); window.removeEventListener("offline", updateOnlineStatus); }); return { online, onNetworkChange }; }, }); </script> <style> /* Full viewport height and centering the content */ .flex { display: flex; } .h-screen { height: 100vh; } .justify-center { justify-content: center; } .items-center { align-items: center; } .text-6xl { font-size: 4rem; } </style> |