-
Notifications
You must be signed in to change notification settings - Fork 6.8k
/
Copy pathexample.ts
87 lines (77 loc) · 1.96 KB
/
example.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.dev/license
*/
import {
ChangeDetectionStrategy,
ChangeDetectorRef,
Component,
Injector,
Input,
OnInit,
ViewContainerRef,
inject,
} from '@angular/core';
import {BooleanInput, coerceBooleanProperty} from '@angular/cdk/coercion';
import {EXAMPLE_COMPONENTS} from '@angular/components-examples';
import {loadExample} from '@angular/components-examples/private';
@Component({
selector: 'material-example',
template: `
@if (showLabel) {
<div class="label">
<span class="title"> {{title}} </span>
<span class="id"> <{{id}}> </span>
</div>
}
@if (!id) {
<div>
Could not find example {{id}}
</div>
}
`,
styles: `
.label {
display: flex;
justify-content: space-between;
align-items: flex-end;
margin: 16px 0;
}
.title {
font-size: 20px;
font-weight: 500;
}
.id {
font-size: 13px;
font-family: monospace;
color: #666;
white-space: pre;
}
`,
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class Example implements OnInit {
private _injector = inject(Injector);
private _viewContainerRef = inject(ViewContainerRef);
private _changeDetectorRef = inject(ChangeDetectorRef);
/** ID of the material example to display. */
@Input() id: string;
@Input()
get showLabel(): boolean {
return this._showLabel;
}
set showLabel(v: BooleanInput) {
this._showLabel = coerceBooleanProperty(v);
}
_showLabel: boolean;
title: string;
async ngOnInit() {
this.title = EXAMPLE_COMPONENTS[this.id].title;
const example = await loadExample(this.id, this._injector);
this._viewContainerRef.createComponent(example.component, {injector: example.injector});
this._changeDetectorRef.markForCheck();
}
}