Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0ab768d

Browse files
Doctor-wuantfu
andauthored
feat(useTitle): restore title on unmounted (#3570)
Co-authored-by: Anthony Fu <[email protected]>
1 parent be3ccc7 commit 0ab768d

File tree

1 file changed

+36
-18
lines changed

1 file changed

+36
-18
lines changed

packages/core/useTitle/index.ts

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,38 @@
11
import type { MaybeRef, MaybeRefOrGetter, ReadonlyRefOrGetter } from '@vueuse/shared'
2-
import { toRef, toValue } from '@vueuse/shared'
2+
import { toRef, toValue, tryOnBeforeUnmount } from '@vueuse/shared'
33
import type { ComputedRef, Ref } from 'vue-demi'
44
import { watch } from 'vue-demi'
55
import { useMutationObserver } from '../useMutationObserver'
66
import type { ConfigurableDocument } from '../_configurable'
77
import { defaultDocument } from '../_configurable'
88

9-
export type UseTitleOptionsBase =
10-
{
9+
export type UseTitleOptionsBase = {
1110
/**
12-
* Observe `document.title` changes using MutationObserve
13-
* Cannot be used together with `titleTemplate` option.
14-
*
15-
* @default false
11+
* Restore the original title when unmounted
12+
* @param originTitle original title
13+
* @returns restored title
1614
*/
17-
observe?: boolean
18-
}
19-
| {
20-
/**
21-
* The template string to parse the title (e.g., '%s | My Website')
22-
* Cannot be used together with `observe` option.
23-
*
24-
* @default '%s'
25-
*/
26-
titleTemplate?: MaybeRef<string> | ((title: string) => string)
27-
}
15+
restoreOnUnmount?: false | ((originalTitle: string, currentTitle: string) => string | null | undefined)
16+
} & (
17+
{
18+
/**
19+
* Observe `document.title` changes using MutationObserve
20+
* Cannot be used together with `titleTemplate` option.
21+
*
22+
* @default false
23+
*/
24+
observe?: boolean
25+
}
26+
| {
27+
/**
28+
* The template string to parse the title (e.g., '%s | My Website')
29+
* Cannot be used together with `observe` option.
30+
*
31+
* @default '%s'
32+
*/
33+
titleTemplate?: MaybeRef<string> | ((title: string) => string)
34+
}
35+
)
2836

2937
export type UseTitleOptions = ConfigurableDocument & UseTitleOptionsBase
3038

@@ -56,7 +64,9 @@ export function useTitle(
5664
*/
5765
const {
5866
document = defaultDocument,
67+
restoreOnUnmount = t => t,
5968
} = options
69+
const originalTitle = document?.title ?? ''
6070

6171
const title: Ref<string | null | undefined> = toRef(newTitle ?? document?.title ?? null)
6272
const isReadonly = newTitle && typeof newTitle === 'function'
@@ -90,6 +100,14 @@ export function useTitle(
90100
)
91101
}
92102

103+
tryOnBeforeUnmount(() => {
104+
if (restoreOnUnmount) {
105+
const restoredTitle = restoreOnUnmount(originalTitle, title.value || '')
106+
if (restoredTitle != null && document)
107+
document.title = restoredTitle
108+
}
109+
})
110+
93111
return title
94112
}
95113

0 commit comments

Comments
 (0)