39 lines
1.3 KiB
JavaScript
Raw Permalink Normal View History

2024-01-29 09:26:07 +08:00
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
2024-08-02 18:19:39 +08:00
exports.useBrowserLocation = useBrowserLocation;
2024-01-29 09:26:07 +08:00
const vue_1 = require("vue");
const is_browser_1 = require("../env/is-browser");
2024-08-02 18:19:39 +08:00
function useBrowserLocation(customWindow = is_browser_1.isBrowser ? window : null) {
2024-01-29 09:26:07 +08:00
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
};
};
2024-08-02 18:19:39 +08:00
const locationState = (0, vue_1.ref)(getWindowLocation());
2024-01-29 09:26:07 +08:00
const updateLocation = () => {
locationState.value = 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;
2024-08-02 18:19:39 +08:00
}