forked from npmx-dev/npmx.dev
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathusePermalink.ts
More file actions
26 lines (24 loc) · 734 Bytes
/
usePermalink.ts
File metadata and controls
26 lines (24 loc) · 734 Bytes
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
/**
* Creates a computed property that uses route query parameters by default,
* with an option to use local state instead.
*/
export function usePermalink<T extends string = string>(
queryKey: string,
defaultValue: T = '' as T,
options: { permanent?: boolean } = {},
): WritableComputedRef<T> {
const { permanent = true } = options
const localValue = shallowRef<T>(defaultValue)
const routeValue = useRouteQuery<T>(queryKey, defaultValue)
const permalinkValue = computed({
get: () => (permanent ? routeValue.value : localValue.value),
set: (value: T) => {
if (permanent) {
routeValue.value = value
} else {
localValue.value = value
}
},
})
return permalinkValue
}