2024-01-29 09:26:07 +08:00

40 lines
1.4 KiB
JavaScript

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.useBrowserLocation = void 0;
const vue_1 = require("vue");
const is_browser_1 = require("../env/is-browser");
const useBrowserLocation = (customWindow = is_browser_1.isBrowser ? window : null) => {
const getWindowLocation = () => {
const { hash, host, hostname, href, origin, pathname, port, protocol, search } = (customWindow === null || customWindow === void 0 ? void 0 : customWindow.location) || {};
return {
hash,
host,
hostname,
href,
origin,
pathname,
port,
protocol,
search
};
};
const updateLocation = () => {
locationState.value = getWindowLocation();
};
const locationState = (0, vue_1.ref)(getWindowLocation());
(0, vue_1.onMounted)(() => {
if (customWindow) {
customWindow.addEventListener('popstate', updateLocation);
customWindow.addEventListener('hashchange', updateLocation);
}
});
(0, vue_1.onUnmounted)(() => {
if (customWindow) {
customWindow.removeEventListener('popstate', updateLocation);
customWindow.removeEventListener('hashchange', updateLocation);
}
});
return locationState;
};
exports.useBrowserLocation = useBrowserLocation;