-
-
Notifications
You must be signed in to change notification settings - Fork 58
Expand file tree
/
Copy pathpage-title.ts
More file actions
68 lines (58 loc) · 1.68 KB
/
page-title.ts
File metadata and controls
68 lines (58 loc) · 1.68 KB
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
58
59
60
61
62
63
64
65
66
67
68
import Helper from '@ember/component/helper';
import { guidFor } from '@ember/object/internals';
import type Owner from '@ember/owner';
import type PageTitleManager from './manager.ts';
import type { PageTitleToken } from '../private-types.ts';
export type PageTitleHelperOptions = Pick<
PageTitleToken,
'prepend' | 'front' | 'replace' | 'separator'
>;
interface Signature {
Args: {
Positional: string[];
Named: PageTitleHelperOptions;
};
Return: void;
}
/**
* `{{pageTitle}}` helper used to set the title of the current route context.
*
* ```gjs
* import { pageTitle } from 'ember-page-title';
*
* <template>
* {{pageTitle "the text to set the tab's title to"}}
* {{pageTitle \@model.post.title}}
* </template>
* ```
*/
export default class PageTitle extends Helper<Signature> {
declare tokens: PageTitleManager;
tokenId = guidFor(this);
constructor(owner: Owner) {
super(owner);
this.tokens = new PageTitleManager(owner);
this.tokens.push({ id: this.tokenId });
}
compute(params: string[], userOptions: PageTitleHelperOptions) {
const options = {
...userOptions,
id: this.tokenId,
title: params.join(''),
};
this.tokens.push(options);
this.tokens.scheduleTitleUpdate();
// We must return an empty value here because otherwise
// invoking the pageTitle helper will render something
// in the component it's used in, and we don't want that.
//
// pageTitle is a side-effecting helper.
// We *synchronize* the document.title with our internal state.
return '';
}
willDestroy() {
super.willDestroy();
this.tokens.remove(this.tokenId);
this.tokens.scheduleTitleUpdate();
}
}