Skip to content
This repository was archived by the owner on Dec 18, 2024. It is now read-only.

Commit 4197b8e

Browse files
authored
refactor: switch to signal queries (#1271)
* refactor: switch to signal queries Runs the signal queries migration in this repository. * fixup! refactor: switch to signal queries Fix type * fixup! refactor: switch to signal queries Fix type
1 parent 0b9a906 commit 4197b8e

File tree

14 files changed

+65
-66
lines changed

14 files changed

+65
-66
lines changed

scenes/src/app/scene-viewer/scene-viewer.ts

+4-5
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import {
44
HostBinding,
55
Input,
66
OnInit,
7-
ViewChild,
87
ViewContainerRef,
9-
ViewEncapsulation
8+
ViewEncapsulation,
9+
viewChild
1010
} from '@angular/core';
1111
import {ActivatedRoute} from '@angular/router';
1212
import {DomSanitizer, SafeStyle} from '@angular/platform-browser';
@@ -44,8 +44,7 @@ export class SceneViewer implements OnInit {
4444
/** Component of scene to display */
4545
@Input() component: any;
4646

47-
@ViewChild('scene', {read: ViewContainerRef, static: true})
48-
scene!: ViewContainerRef;
47+
readonly scene = viewChild.required('scene', { read: ViewContainerRef });
4948

5049
constructor(private readonly componentFactoryResolver: ComponentFactoryResolver,
5150
private route: ActivatedRoute,
@@ -57,7 +56,7 @@ export class SceneViewer implements OnInit {
5756

5857
ngOnInit() {
5958
const componentFactory = this.componentFactoryResolver.resolveComponentFactory(this.component);
60-
this.scene.createComponent(componentFactory);
59+
this.scene().createComponent(componentFactory);
6160
const container = document.querySelector('#scene-content-container') as HTMLElement;
6261
container.style.transform = `scale(${this.scale})`;
6362
container.style.transformOrigin = 'center';

scenes/src/app/scenes/autocomplete/autocomplete-scene.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import {
22
AfterViewInit,
33
Component,
4-
ViewChild,
5-
ViewEncapsulation
4+
ViewEncapsulation,
5+
viewChild
66
} from '@angular/core';
77
import {FormControl, FormsModule, ReactiveFormsModule} from '@angular/forms';
88
import {MatFormFieldModule} from '@angular/material/form-field';
@@ -27,10 +27,10 @@ export class AutocompleteScene implements AfterViewInit {
2727
myControl = new FormControl('');
2828
options: string[] = ['hello', 'hello world'];
2929

30-
@ViewChild(MatInput) input!: MatInput;
30+
readonly input = viewChild.required(MatInput);
3131

3232
ngAfterViewInit() {
33-
this.input.focus();
33+
this.input().focus();
3434
}
3535
}
3636

scenes/src/app/scenes/menu/menu-scene.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {AfterViewInit, Component, ViewChild, ViewEncapsulation} from '@angular/core';
1+
import {AfterViewInit, Component, ViewEncapsulation, viewChild} from '@angular/core';
22
import {MatButtonModule} from '@angular/material/button';
33
import {MatIconModule} from '@angular/material/icon';
44
import {MatMenuModule, MatMenuTrigger} from '@angular/material/menu';
@@ -12,9 +12,9 @@ import {MatMenuModule, MatMenuTrigger} from '@angular/material/menu';
1212
imports: [MatButtonModule, MatMenuModule, MatIconModule]
1313
})
1414
export class MenuScene implements AfterViewInit {
15-
@ViewChild('menuTrigger') trigger!: MatMenuTrigger;
15+
readonly trigger = viewChild.required<MatMenuTrigger>('menuTrigger');
1616

1717
ngAfterViewInit() {
18-
this.trigger.openMenu();
18+
this.trigger().openMenu();
1919
}
2020
}

scenes/src/app/scenes/ripples/ripples-scene.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {AfterViewInit, Component, ViewChild, ViewEncapsulation} from '@angular/core';
1+
import {AfterViewInit, Component, ViewEncapsulation, viewChild} from '@angular/core';
22
import {MatButtonModule} from '@angular/material/button';
33
import {MatRipple, MatRippleModule} from '@angular/material/core';
44

@@ -11,17 +11,17 @@ import {MatRipple, MatRippleModule} from '@angular/material/core';
1111
imports: [MatRippleModule, MatButtonModule]
1212
})
1313
export class RipplesScene implements AfterViewInit {
14-
@ViewChild('button', {read: MatRipple}) buttonRipple!: MatRipple;
15-
@ViewChild('wrapper', {read: MatRipple}) wrapperRipple!: MatRipple;
14+
readonly buttonRipple = viewChild.required('button', { read: MatRipple });
15+
readonly wrapperRipple = viewChild.required('wrapper', { read: MatRipple });
1616

1717
ngAfterViewInit() {
18-
this.buttonRipple.launch(140, 100, {
18+
this.buttonRipple().launch(140, 100, {
1919
persistent: true,
2020
animation: {enterDuration: 0},
2121
radius: 50,
2222
});
2323

24-
this.wrapperRipple.launch(300, 100, {
24+
this.wrapperRipple().launch(300, 100, {
2525
persistent: true,
2626
animation: {enterDuration: 0},
2727
radius: 150,

scenes/src/app/scenes/select/select-scene.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {AfterViewInit, Component, ViewChild, ViewEncapsulation} from '@angular/core';
1+
import {AfterViewInit, Component, ViewEncapsulation, viewChild} from '@angular/core';
22
import {MatFormFieldModule} from '@angular/material/form-field';
33
import {MatSelect, MatSelectModule} from '@angular/material/select';
44
import {MatOptionModule} from '@angular/material/core';
@@ -12,9 +12,9 @@ import {MatOptionModule} from '@angular/material/core';
1212
imports: [MatFormFieldModule, MatSelectModule, MatOptionModule]
1313
})
1414
export class SelectScene implements AfterViewInit {
15-
@ViewChild(MatSelect) select!: MatSelect;
15+
readonly select = viewChild.required(MatSelect);
1616

1717
ngAfterViewInit() {
18-
this.select.open();
18+
this.select().open();
1919
}
2020
}

scenes/src/app/scenes/tooltip/tooltip-scene.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {Component, ViewChild, AfterViewInit, ViewEncapsulation} from '@angular/core';
1+
import {Component, AfterViewInit, ViewEncapsulation, viewChild} from '@angular/core';
22
import {MatButtonModule} from '@angular/material/button';
33
import {MatTooltipModule, MatTooltip} from '@angular/material/tooltip';
44
import {MatIconModule} from '@angular/material/icon';
@@ -16,9 +16,9 @@ import {MatIconModule} from '@angular/material/icon';
1616
],
1717
})
1818
export class TooltipScene implements AfterViewInit {
19-
@ViewChild(MatTooltip) tooltip!: MatTooltip;
19+
readonly tooltip = viewChild.required(MatTooltip);
2020

2121
ngAfterViewInit() {
22-
this.tooltip.toggle();
22+
this.tooltip().toggle();
2323
}
2424
}

src/app/pages/component-sidenav/component-sidenav.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
</mat-sidenav>
1111
}
1212
<div class="docs-component-sidenav-content">
13-
<component-page-header (toggleSidenav)="toggleSidenav(sidenav)"></component-page-header>
13+
<component-page-header (toggleSidenav)="toggleSidenav(sidenav())"></component-page-header>
1414
<div class="docs-component-sidenav-inner-content">
1515
<main class="docs-component-sidenav-body-content">
1616
<!-- If on large screen, menu resides to left of content -->

src/app/pages/component-sidenav/component-sidenav.spec.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ describe('ComponentSidenav', () => {
2828

2929
// TODO refactor this as none of these expectations are ever verified
3030
waitForAsync(() => {
31-
expect(component.sidenav instanceof MatSidenav).toBeTruthy();
31+
expect(component.sidenav() instanceof MatSidenav).toBeTruthy();
3232
component.isScreenSmall.pipe(take(1)).subscribe(isSmall => expect(isSmall).toBeTruthy());
33-
expect(component.sidenav.opened).toBe(false);
33+
expect(component.sidenav().opened).toBe(false);
3434
});
3535
});
3636

src/app/pages/component-sidenav/component-sidenav.ts

+6-5
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ import {
44
NgZone,
55
OnDestroy,
66
OnInit,
7-
ViewChild,
87
ViewEncapsulation,
98
forwardRef,
10-
input
9+
input,
10+
viewChild
1111
} from '@angular/core';
1212
import {animate, state, style, transition, trigger} from '@angular/animations';
1313
import {CdkAccordionModule} from '@angular/cdk/accordion';
@@ -82,7 +82,7 @@ const SMALL_WIDTH_BREAKPOINT = 959;
8282
],
8383
})
8484
export class ComponentSidenav implements OnInit, OnDestroy {
85-
@ViewChild(MatSidenav) sidenav!: MatSidenav;
85+
readonly sidenav = viewChild.required(MatSidenav);
8686
params: Observable<Params> | undefined;
8787
isExtraScreenSmall: Observable<boolean>;
8888
isScreenSmall: Observable<boolean>;
@@ -108,8 +108,9 @@ export class ComponentSidenav implements OnInit, OnDestroy {
108108
this.subscriptions.add(
109109
this._navigationFocusService.navigationEndEvents.pipe(map(() => this.isScreenSmall))
110110
.subscribe((shouldCloseSideNav) => {
111-
if (shouldCloseSideNav && this.sidenav) {
112-
this.sidenav.close();
111+
const sidenav = this.sidenav();
112+
if (shouldCloseSideNav && sidenav) {
113+
sidenav.close();
113114
}
114115
}
115116
));

src/app/pages/component-viewer/component-viewer.ts

+13-12
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,9 @@ import {
77
NgModule,
88
OnDestroy,
99
OnInit,
10-
QueryList,
11-
ViewChild,
12-
ViewChildren,
1310
ViewEncapsulation,
11+
viewChild,
12+
viewChildren
1413
} from '@angular/core';
1514
import {MatTabsModule} from '@angular/material/tabs';
1615
import {ActivatedRoute,
@@ -95,8 +94,8 @@ export class ComponentViewer implements OnDestroy {
9594
*/
9695
@Directive()
9796
export class ComponentBaseView implements OnInit, OnDestroy {
98-
@ViewChild('toc') tableOfContents!: TableOfContents;
99-
@ViewChildren(DocViewer) viewers!: QueryList<DocViewer>;
97+
readonly tableOfContents = viewChild.required<TableOfContents>('toc');
98+
readonly viewers = viewChildren(DocViewer);
10099

101100
showToc: Observable<boolean>;
102101
private _destroyed = new Subject();
@@ -116,17 +115,18 @@ export class ComponentBaseView implements OnInit, OnDestroy {
116115

117116
ngOnInit() {
118117
this.componentViewer.componentDocItem.pipe(takeUntil(this._destroyed)).subscribe(() => {
119-
if (this.tableOfContents) {
120-
this.tableOfContents.resetHeaders();
118+
const tableOfContents = this.tableOfContents();
119+
if (tableOfContents) {
120+
tableOfContents.resetHeaders();
121121
}
122122
});
123123

124124
this.showToc.pipe(
125125
skip(1),
126126
takeUntil(this._destroyed)
127127
).subscribe(() => {
128-
if (this.tableOfContents) {
129-
this.viewers.forEach(viewer => {
128+
if (this.tableOfContents()) {
129+
this.viewers().forEach(viewer => {
130130
viewer.contentRendered.emit(viewer._elementRef.nativeElement);
131131
});
132132
}
@@ -139,9 +139,10 @@ export class ComponentBaseView implements OnInit, OnDestroy {
139139
}
140140

141141
updateTableOfContents(sectionName: string, docViewerContent: HTMLElement, sectionIndex = 0) {
142-
if (this.tableOfContents) {
143-
this.tableOfContents.addHeaders(sectionName, docViewerContent, sectionIndex);
144-
this.tableOfContents.updateScrollPosition();
142+
const tableOfContents = this.tableOfContents();
143+
if (tableOfContents) {
144+
tableOfContents.addHeaders(sectionName, docViewerContent, sectionIndex);
145+
tableOfContents.updateScrollPosition();
145146
}
146147
}
147148
}

src/app/shared/carousel/carousel.ts

+12-13
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
import {
22
AfterContentInit,
33
Component,
4-
ContentChildren,
54
Directive,
65
ElementRef,
76
HostBinding,
87
Inject,
98
Optional,
10-
QueryList,
11-
ViewChild,
129
ViewEncapsulation,
13-
input
10+
input,
11+
contentChildren,
12+
viewChild
1413
} from '@angular/core';
1514
import {FocusableOption, FocusKeyManager} from '@angular/cdk/a11y';
1615
import {LEFT_ARROW, RIGHT_ARROW, TAB} from '@angular/cdk/keycodes';
@@ -43,8 +42,8 @@ export class CarouselItem implements FocusableOption {
4342
})
4443
export class Carousel implements AfterContentInit {
4544
readonly ariaLabel = input<string|undefined>(undefined, { alias: 'aria-label' });
46-
@ContentChildren(CarouselItem) items!: QueryList<CarouselItem>;
47-
@ViewChild('list') list!: ElementRef<HTMLElement>;
45+
readonly items = contentChildren(CarouselItem);
46+
readonly list = viewChild.required<ElementRef<HTMLElement>>('list');
4847
@HostBinding('class.animations-disabled') readonly animationsDisabled: boolean;
4948
position = 0;
5049
showPrevArrow = false;
@@ -79,12 +78,12 @@ export class Carousel implements AfterContentInit {
7978
}
8079

8180
ngAfterContentInit(): void {
82-
this._keyManager = new FocusKeyManager<CarouselItem>(this.items);
81+
this._keyManager = new FocusKeyManager<CarouselItem>(this.items());
8382
}
8483

8584
/** Goes to the next set of items */
8685
next() {
87-
for (let i = this.index; i < this.items.length; i++) {
86+
for (let i = this.index; i < this.items().length; i++) {
8887
if (this._isOutOfView(i)) {
8988
this.index = i;
9089
this._scrollToActiveItem();
@@ -106,7 +105,7 @@ export class Carousel implements AfterContentInit {
106105

107106
/** Updates the `tabindex` of each of the items based on their active state. */
108107
private _updateItemTabIndices() {
109-
this.items.forEach((item: CarouselItem) => {
108+
this.items().forEach((item: CarouselItem) => {
110109
if (this._keyManager != null) {
111110
item.tabindex = item === this._keyManager.activeItem ? '0' : '-1';
112111
}
@@ -119,7 +118,7 @@ export class Carousel implements AfterContentInit {
119118
return;
120119
}
121120

122-
const itemsArray = this.items.toArray();
121+
const itemsArray = this.items();
123122
let targetItemIndex = this.index;
124123

125124
// Only shift the carousel by one if we're going forwards. This
@@ -129,7 +128,7 @@ export class Carousel implements AfterContentInit {
129128
}
130129

131130
this.position = itemsArray[targetItemIndex].element.nativeElement.offsetLeft;
132-
this.list.nativeElement.style.transform = `translateX(-${this.position}px)`;
131+
this.list().nativeElement.style.transform = `translateX(-${this.position}px)`;
133132
this.showPrevArrow = this.index > 0;
134133
this.showNextArrow = false;
135134

@@ -143,15 +142,15 @@ export class Carousel implements AfterContentInit {
143142

144143
/** Checks whether an item at a specific index is outside of the viewport. */
145144
private _isOutOfView(index: number, side?: 'start' | 'end') {
146-
const {offsetWidth, offsetLeft} = this.items.toArray()[index].element.nativeElement;
145+
const {offsetWidth, offsetLeft} = this.items()[index].element.nativeElement;
147146

148147
if ((!side || side === 'start') && offsetLeft - this.position < 0) {
149148
return true;
150149
}
151150

152151
return (
153152
(!side || side === 'end') &&
154-
offsetWidth + offsetLeft - this.position > this.list.nativeElement.clientWidth
153+
offsetWidth + offsetLeft - this.position > this.list().nativeElement.clientWidth
155154
);
156155
}
157156
}

src/app/shared/example-viewer/code-snippet.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import {
22
ChangeDetectionStrategy,
33
Component,
4-
ViewChild,
54
forwardRef,
6-
input
5+
input,
6+
viewChild
77
} from '@angular/core';
88
import {DocViewer} from '../doc-viewer/doc-viewer';
99

@@ -17,5 +17,5 @@ import {DocViewer} from '../doc-viewer/doc-viewer';
1717
})
1818
export class CodeSnippet {
1919
readonly source = input<string>();
20-
@ViewChild('viewer') viewer!: DocViewer;
20+
readonly viewer = viewChild.required<DocViewer>('viewer');
2121
}

src/app/shared/example-viewer/example-viewer.html

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
@if (view === 'snippet') {
33
<div class="docs-example-viewer-source-compact">
44
<div class="button-bar">
5-
<button mat-icon-button type="button" (click)="copySource(snippet)"
5+
<button mat-icon-button type="button" (click)="copySource(snippet())"
66
class="docs-example-source-copy docs-example-button" matTooltip="Copy snippet"
77
title="Copy example source" aria-label="Copy example source to clipboard">
88
<mat-icon>content_copy</mat-icon>
@@ -65,7 +65,7 @@
6565
@for (tabName of _getExampleTabNames(); track tabName) {
6666
<mat-tab [label]="tabName">
6767
<div class="button-bar">
68-
<button mat-icon-button type="button" (click)="copySource(snippet, selectedTab)"
68+
<button mat-icon-button type="button" (click)="copySource(snippet(), selectedTab)"
6969
class="docs-example-source-copy docs-example-button" matTooltip="Copy example source"
7070
title="Copy example source" aria-label="Copy example source to clipboard">
7171
<mat-icon>content_copy</mat-icon>

0 commit comments

Comments
 (0)