-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
index.ts
57 lines (50 loc) · 1.39 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/* this implementation is original ported from https://github.com/logaretm/vue-use-web by Abdelrahman Awad */
import { ref } from 'vue-demi'
import { useEventListener } from '../useEventListener'
import { ConfigurableWindow, defaultWindow } from '../_configurable'
export interface BrowserLocationState {
trigger: string
state?: any
length?: number
hash?: string
host?: string
hostname?: string
href?: string
origin?: string
pathname?: string
port?: string
protocol?: string
search?: string
}
/**
* Reactive browser location.
*
* @see https://vueuse.org/useBrowserLocation
* @param options
*/
export function useBrowserLocation({ window = defaultWindow }: ConfigurableWindow = {}) {
const buildState = (trigger: string): BrowserLocationState => {
const { state, length } = window?.history || {}
const { hash, host, hostname, href, origin, pathname, port, protocol, search } = window?.location || {}
return {
trigger,
state,
length,
hash,
host,
hostname,
href,
origin,
pathname,
port,
protocol,
search,
}
}
const state = ref(buildState('load'))
if (window) {
useEventListener(window, 'popstate', () => state.value = buildState('popstate'), { passive: true })
useEventListener(window, 'hashchange', () => state.value = buildState('hashchange'), { passive: true })
}
return state
}