UNPKG

@cran/vue.use

Version:

Cranberry Vue Use Utilities

28 lines (27 loc) 870 B
import { ref } from "@vue/runtime-dom"; import { useEventListener } from "./useEventListener"; export function useWindowLocation() { const location = ref(parseLocation()); function onChange({ type, }) { location.value = parseLocation(type); } useEventListener(window, "popstate", onChange, { capture: true, }); useEventListener(window, "hashchange", onChange, { capture: true, }); return location; } function parseLocation(trigger) { const url = new URL(window.location.href); return { trigger, hash: url.hash, host: url.host, hostname: url.hostname, href: url.href, origin: url.origin, pathname: url.pathname, port: url.port, protocol: url.protocol, search: url.search, searchParams: Object.fromEntries(url.searchParams.entries()), }; }