vue-material-kit/src/assets/js/useWindowsWidth.js
2022-08-01 15:53:34 +04:30

21 lines
672 B
JavaScript

import { computed, onMounted, onUnmounted, ref } from "vue";
export function useWindowsWidth() {
let windowWidth = ref(window.innerWidth);
const onWidthChange = () => (windowWidth.value = window.innerWidth);
onMounted(() => window.addEventListener("resize", onWidthChange));
onUnmounted(() => window.removeEventListener("resize", onWidthChange));
const type = computed(() => {
if (windowWidth.value < 992) return "mobile";
if (windowWidth.value >= 992) return "desktop";
return null; // This is an unreachable line, simply to keep eslint happy.
});
let width = computed(() => {
return windowWidth.value;
});
return { width, type };
}