@cran/vue.use
Version:
Cranberry Vue Use Utilities
32 lines (31 loc) • 1.1 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.useWindowLocation = void 0;
const runtime_dom_1 = require("@vue/runtime-dom");
const useEventListener_1 = require("./useEventListener");
function useWindowLocation() {
const location = (0, runtime_dom_1.ref)(parseLocation());
function onChange({ type, }) {
location.value = parseLocation(type);
}
(0, useEventListener_1.useEventListener)(window, "popstate", onChange, { capture: true, });
(0, useEventListener_1.useEventListener)(window, "hashchange", onChange, { capture: true, });
return location;
}
exports.useWindowLocation = useWindowLocation;
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()),
};
}