Skip to content

Commit ec3a56c

Browse files
committed
build: unable to render individual examples in demo app
Seems like something that was broken by angular#14522 by accident. Since the custom component is compiled inside the example list, we can't use individual examples anymore which breaks the ripple demo. These changes move the compilation into the `example` and simplify the setup a little bit.
1 parent d22f48c commit ec3a56c

File tree

2 files changed

+15
-27
lines changed

2 files changed

+15
-27
lines changed

src/dev-app/example/example-list.ts

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,9 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import {Component, Input, SimpleChanges, OnChanges, Injector} from '@angular/core';
9+
import {Component, Input} from '@angular/core';
1010
import {EXAMPLE_COMPONENTS} from '@angular/material-examples';
1111
import {coerceBooleanProperty} from '@angular/cdk/coercion';
12-
import {createCustomElement} from '@angular/elements';
1312

1413
/** Displays a set of material examples in a mat-accordion. */
1514
@Component({
@@ -33,6 +32,7 @@ import {createCustomElement} from '@angular/elements';
3332
styles: [`
3433
mat-expansion-panel {
3534
box-shadow: none !important;
35+
border-radius: 0 !important;
3636
background: transparent;
3737
border-top: 1px solid #CCC;
3838
}
@@ -52,10 +52,7 @@ import {createCustomElement} from '@angular/elements';
5252
}
5353
`]
5454
})
55-
export class ExampleList implements OnChanges {
56-
/** Keeps track of the example ids that have been compiled already. */
57-
private static _compiledComponents = new Set<string>();
58-
55+
export class ExampleList {
5956
/** Type of examples being displayed. */
6057
@Input() type: string;
6158

@@ -68,20 +65,4 @@ export class ExampleList implements OnChanges {
6865
_expandAll: boolean;
6966

7067
exampleComponents = EXAMPLE_COMPONENTS;
71-
72-
constructor(private _injector: Injector) {}
73-
74-
ngOnChanges(changes: SimpleChanges) {
75-
if (changes.ids) {
76-
(changes.ids.currentValue as string[])
77-
.filter(id => !ExampleList._compiledComponents.has(id))
78-
.forEach(id => {
79-
const element = createCustomElement(EXAMPLE_COMPONENTS[id].component, {
80-
injector: this._injector
81-
});
82-
customElements.define(id, element);
83-
ExampleList._compiledComponents.add(id);
84-
});
85-
}
86-
}
8768
}

src/dev-app/example/example.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import {coerceBooleanProperty} from '@angular/cdk/coercion';
1010
import {Component, ElementRef, Injector, Input, OnInit} from '@angular/core';
1111
import {EXAMPLE_COMPONENTS} from '@angular/material-examples';
12+
import {createCustomElement} from '@angular/elements';
1213

1314
@Component({
1415
selector: 'material-example',
@@ -54,14 +55,20 @@ export class Example implements OnInit {
5455

5556
title: string;
5657

57-
constructor(private elementRef: ElementRef, private injector: Injector) { }
58+
constructor(private _elementRef: ElementRef<HTMLElement>, private _injector: Injector) { }
5859

5960
ngOnInit() {
60-
// Should be created with this component's injector to capture the whole injector which may
61-
// include provided things like Directionality.
62-
const exampleElementCtor = customElements.get(this.id);
63-
this.elementRef.nativeElement.appendChild(new exampleElementCtor(this.injector));
61+
let exampleElementCtor = customElements.get(this.id);
6462

63+
if (!exampleElementCtor) {
64+
exampleElementCtor = createCustomElement(EXAMPLE_COMPONENTS[this.id].component, {
65+
injector: this._injector
66+
});
67+
68+
customElements.define(this.id, exampleElementCtor);
69+
}
70+
71+
this._elementRef.nativeElement.appendChild(new exampleElementCtor(this._injector));
6572
this.title = EXAMPLE_COMPONENTS[this.id] ? EXAMPLE_COMPONENTS[this.id].title : '';
6673
}
6774
}

0 commit comments

Comments
 (0)