Skip to content

Commit

Permalink
fix: move focusmonitor from constructor to ngAfterViewInit (#1169)
Browse files Browse the repository at this point in the history
  • Loading branch information
[email protected] authored and GitHub Enterprise committed Apr 23, 2024
1 parent 4a4ad48 commit 2118d17
Show file tree
Hide file tree
Showing 25 changed files with 192 additions and 99 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { FocusMonitor } from '@angular/cdk/a11y';
import { BooleanInput, coerceBooleanProperty } from '@angular/cdk/coercion';
import {
AfterViewInit,
Component,
ElementRef,
Input,
Expand Down Expand Up @@ -50,7 +51,11 @@ export class MyTel {
},
})
export class FormfieldCustomTelInputExampleComponent
implements ControlValueAccessor, NxFormfieldControl<MyTel>, OnDestroy
implements
ControlValueAccessor,
NxFormfieldControl<MyTel>,
OnDestroy,
AfterViewInit
{
static nextId = 0;

Expand Down Expand Up @@ -155,17 +160,6 @@ export class FormfieldCustomTelInputExampleComponent
],
});

_focusMonitor
.monitor(_elementRef, true)
.pipe(takeUntil(this._destroyed))
.subscribe(origin => {
if (this.focused && !origin) {
this.onTouched();
}
this.focused = !!origin;
this.stateChanges.next();
});

if (this.ngControl != null) {
this.ngControl.valueAccessor = this;
}
Expand All @@ -179,6 +173,19 @@ export class FormfieldCustomTelInputExampleComponent
return this._elementRef;
}

ngAfterViewInit(): void {
this._focusMonitor
.monitor(this._elementRef, true)
.pipe(takeUntil(this._destroyed))
.subscribe(origin => {
if (this.focused && !origin) {
this.onTouched();
}
this.focused = !!origin;
this.stateChanges.next();
});
}

ngOnDestroy(): void {
this._destroyed.next();
this._destroyed.complete();
Expand Down
6 changes: 4 additions & 2 deletions projects/ng-aquila/src/accordion/expansion-panel-header.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { FocusMonitor } from '@angular/cdk/a11y';
import { ENTER, SPACE } from '@angular/cdk/keycodes';
import { ChangeDetectionStrategy, ChangeDetectorRef, Component, Directive, ElementRef, Host, OnDestroy } from '@angular/core';
import { AfterViewInit, ChangeDetectionStrategy, ChangeDetectorRef, Component, Directive, ElementRef, Host, OnDestroy } from '@angular/core';
import { merge, Subject } from 'rxjs';
import { filter, takeUntil } from 'rxjs/operators';

Expand All @@ -27,7 +27,7 @@ import { NxExpansionPanelComponent } from './expansion-panel';
'(click)': 'toggle()',
},
})
export class NxExpansionPanelHeaderComponent implements OnDestroy {
export class NxExpansionPanelHeaderComponent implements OnDestroy, AfterViewInit {
private readonly _destroyed = new Subject<void>();

constructor(
Expand All @@ -39,7 +39,9 @@ export class NxExpansionPanelHeaderComponent implements OnDestroy {
merge(panel.opened, panel.closed, panel._inputChanges.pipe(filter(changes => !!(changes.hideToggle || changes.disabled))))
.pipe(takeUntil(this._destroyed))
.subscribe(() => this._cdr.markForCheck());
}

ngAfterViewInit(): void {
this._focusMonitor.monitor(this._elementRef);
}

Expand Down
8 changes: 5 additions & 3 deletions projects/ng-aquila/src/action/action.component.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { FocusMonitor } from '@angular/cdk/a11y';
import { BooleanInput, coerceBooleanProperty } from '@angular/cdk/coercion';
import { ChangeDetectionStrategy, ChangeDetectorRef, Component, ContentChild, ElementRef, Input, OnDestroy } from '@angular/core';
import { AfterViewInit, ChangeDetectionStrategy, ChangeDetectorRef, Component, ContentChild, ElementRef, Input, OnDestroy } from '@angular/core';

import { NxActionIconDirective } from './action-icon.directive';

Expand All @@ -16,7 +16,7 @@ import { NxActionIconDirective } from './action-icon.directive';
},
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class NxActionComponent implements OnDestroy {
export class NxActionComponent implements OnDestroy, AfterViewInit {
@ContentChild(NxActionIconDirective) _iconChild!: NxActionIconDirective;

/** Whether this action is selected or not. */
Expand Down Expand Up @@ -57,7 +57,9 @@ export class NxActionComponent implements OnDestroy {
private readonly _cdr: ChangeDetectorRef,
private readonly _elementRef: ElementRef,
private readonly _focusMonitor: FocusMonitor,
) {
) {}

ngAfterViewInit(): void {
this._focusMonitor.monitor(this._elementRef);
}

Expand Down
8 changes: 5 additions & 3 deletions projects/ng-aquila/src/avatar/avatar.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { FocusMonitor } from '@angular/cdk/a11y';
import { ChangeDetectionStrategy, ChangeDetectorRef, Component, Directive, ElementRef, Input, OnDestroy } from '@angular/core';
import { AfterViewInit, ChangeDetectionStrategy, ChangeDetectorRef, Component, Directive, ElementRef, Input, OnDestroy } from '@angular/core';

/** Size of an avatar. */
export type NxAvatarSize = 'xsmall' | 'small' | 'small-medium' | 'medium' | 'large' | 'xlarge';
Expand Down Expand Up @@ -42,11 +42,13 @@ export class NxAvatarComponent {
'[class.is-button]': 'true',
},
})
export class NxAvatarButtonDirective implements OnDestroy {
export class NxAvatarButtonDirective implements OnDestroy, AfterViewInit {
constructor(
private readonly _elementRef: ElementRef,
private readonly _focusMonitor: FocusMonitor,
) {
) {}

ngAfterViewInit(): void {
this._focusMonitor.monitor(this._elementRef);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { FocusMonitor } from '@angular/cdk/a11y';
import { ChangeDetectionStrategy, Component, ElementRef, OnDestroy, Renderer2 } from '@angular/core';
import { AfterViewInit, ChangeDetectionStrategy, Component, ElementRef, OnDestroy, Renderer2 } from '@angular/core';

@Component({
selector: 'a[nxBreadcrumbItem]',
Expand All @@ -10,7 +10,7 @@ import { ChangeDetectionStrategy, Component, ElementRef, OnDestroy, Renderer2 }
class: 'nx-breadcrumb-item',
},
})
export class NxBreadcrumbItemComponent implements OnDestroy {
export class NxBreadcrumbItemComponent implements OnDestroy, AfterViewInit {
constructor(
private readonly _renderer: Renderer2,
private readonly _elemRef: ElementRef,
Expand All @@ -19,6 +19,10 @@ export class NxBreadcrumbItemComponent implements OnDestroy {
this._focusMonitor.monitor(this._elemRef);
}

ngAfterViewInit(): void {
this._focusMonitor.monitor(this._elemRef);
}

ngOnDestroy(): void {
this._focusMonitor.stopMonitoring(this._elemRef);
}
Expand Down
8 changes: 5 additions & 3 deletions projects/ng-aquila/src/button/button-base.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { FocusMonitor } from '@angular/cdk/a11y';
import { BooleanInput, coerceBooleanProperty } from '@angular/cdk/coercion';
import { ChangeDetectorRef, Directive, ElementRef, HostBinding, Input, NgZone, OnDestroy } from '@angular/core';
import { AfterViewInit, ChangeDetectorRef, Directive, ElementRef, HostBinding, Input, NgZone, OnDestroy } from '@angular/core';
import { NxTriggerButton } from '@aposin/ng-aquila/overlay';

/** Type of a button. */
Expand All @@ -14,7 +14,7 @@ const DEFAULT_TYPE = 'primary';

/** @docs-private */
@Directive()
export class NxButtonBase implements NxTriggerButton, OnDestroy {
export class NxButtonBase implements NxTriggerButton, OnDestroy, AfterViewInit {
private _classNames = '';

/** @docs-private */
Expand Down Expand Up @@ -135,7 +135,9 @@ export class NxButtonBase implements NxTriggerButton, OnDestroy {
private readonly _cdr: ChangeDetectorRef,
private readonly _elementRef: ElementRef,
private readonly _focusMonitor: FocusMonitor,
) {
) {}

ngAfterViewInit(): void {
this._focusMonitor.monitor(this._elementRef);
}

Expand Down
8 changes: 5 additions & 3 deletions projects/ng-aquila/src/button/plain-button.component.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { FocusMonitor } from '@angular/cdk/a11y';
import { BooleanInput, coerceBooleanProperty } from '@angular/cdk/coercion';
import { ChangeDetectionStrategy, ChangeDetectorRef, Component, ElementRef, HostBinding, Input, OnDestroy } from '@angular/core';
import { AfterViewInit, ChangeDetectionStrategy, ChangeDetectorRef, Component, ElementRef, HostBinding, Input, OnDestroy } from '@angular/core';
import { NxTriggerButton } from '@aposin/ng-aquila/overlay';

/** Please note: small is only for meant for the One Allianz Design */
Expand All @@ -22,7 +22,7 @@ export type NxPlainButtonVariant = 'primary' | 'secondary';
},
providers: [{ provide: NxTriggerButton, useExisting: NxPlainButtonComponent }],
})
export class NxPlainButtonComponent implements NxTriggerButton, OnDestroy {
export class NxPlainButtonComponent implements NxTriggerButton, OnDestroy, AfterViewInit {
/** @docs-private */
@HostBinding('attr.disabled') get isDisabled(): boolean | null {
return this.disabled || null;
Expand Down Expand Up @@ -80,7 +80,9 @@ export class NxPlainButtonComponent implements NxTriggerButton, OnDestroy {
private readonly _cdr: ChangeDetectorRef,
private readonly _elementRef: ElementRef,
private readonly _focusMonitor: FocusMonitor,
) {
) {}

ngAfterViewInit(): void {
this._focusMonitor.monitor(this._elementRef);
}

Expand Down
12 changes: 7 additions & 5 deletions projects/ng-aquila/src/card/card.component.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { FocusMonitor } from '@angular/cdk/a11y';
import { BooleanInput, coerceBooleanProperty } from '@angular/cdk/coercion';
import { booleanAttribute, ChangeDetectionStrategy, ChangeDetectorRef, Component, ElementRef, Input, OnDestroy } from '@angular/core';
import { AfterViewInit, booleanAttribute, ChangeDetectionStrategy, ChangeDetectorRef, Component, ElementRef, Input, OnDestroy } from '@angular/core';

@Component({
templateUrl: './card.component.html',
Expand All @@ -14,20 +14,22 @@ import { booleanAttribute, ChangeDetectionStrategy, ChangeDetectorRef, Component
'[class.is-disabled]': 'disabled',
},
})
export class NxCardComponent implements OnDestroy {
export class NxCardComponent implements OnDestroy, AfterViewInit {
constructor(
private readonly _elementRef: ElementRef,
private readonly _focusMonitor: FocusMonitor,
private readonly _cdr: ChangeDetectorRef,
) {
) {}

private _highlight = false;

ngAfterViewInit(): void {
// we still listen for focus in case the user set a tabindex on the element
// the focus monitor only adds the cdk-keyboard-focus class if the element is focusable
// meaning it needs a tabindex in this case
this._focusMonitor.monitor(this._elementRef);
}

private _highlight = false;

ngOnDestroy(): void {
this._focusMonitor.stopMonitoring(this._elementRef);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { FocusMonitor } from '@angular/cdk/a11y';
import { Component, ElementRef, Input, OnDestroy } from '@angular/core';
import { AfterViewInit, Component, ElementRef, Input, OnDestroy } from '@angular/core';
import { Subject } from 'rxjs';
import { takeUntil } from 'rxjs/operators';

Expand All @@ -24,7 +24,7 @@ import { NxComparisonTableRowDirective } from '../comparison-table-row.directive
'[class.has-intersection]': 'row.intersectionCell',
},
})
export class NxComparisonTableFlexRow implements OnDestroy {
export class NxComparisonTableFlexRow implements OnDestroy, AfterViewInit {
@Input() row!: NxComparisonTableRowDirective;

private readonly _destroyed = new Subject<void>();
Expand All @@ -33,7 +33,9 @@ export class NxComparisonTableFlexRow implements OnDestroy {
readonly _table: NxComparisonTableBase,
private readonly _elementRef: ElementRef,
private readonly _focusMonitor: FocusMonitor,
) {
) {}

ngAfterViewInit(): void {
this._focusMonitor
.monitor(this._elementRef, true)
.pipe(takeUntil(this._destroyed))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { FocusMonitor, FocusOrigin } from '@angular/cdk/a11y';
import { BooleanInput, coerceBooleanProperty } from '@angular/cdk/coercion';
import { DOCUMENT } from '@angular/common';
import {
AfterViewInit,
ChangeDetectionStrategy,
ChangeDetectorRef,
Component,
Expand Down Expand Up @@ -41,7 +42,7 @@ import { Subject } from 'rxjs';
`,
styleUrls: ['./context-menu-item.component.scss'],
})
export class NxContextMenuItemComponent implements OnDestroy {
export class NxContextMenuItemComponent implements OnDestroy, AfterViewInit {
/** Stream that emits when the context menu item is hovered. */
readonly _hovered = new Subject<NxContextMenuItemComponent>();

Expand Down Expand Up @@ -76,9 +77,7 @@ export class NxContextMenuItemComponent implements OnDestroy {
@Optional() @Inject(DOCUMENT) private readonly _document: Document | null,
private readonly _cdr: ChangeDetectorRef,
private readonly _focusMonitor: FocusMonitor,
) {
this._focusMonitor.monitor(this._elementRef);
}
) {}

/** Focuses this context menu item. */
focus(origin?: FocusOrigin): void {
Expand All @@ -89,6 +88,10 @@ export class NxContextMenuItemComponent implements OnDestroy {
}
}

ngAfterViewInit(): void {
this._focusMonitor.monitor(this._elementRef);
}

ngOnDestroy(): void {
this._hovered.complete();
this._focusMonitor.stopMonitoring(this._elementRef);
Expand Down
8 changes: 5 additions & 3 deletions projects/ng-aquila/src/footer/footer.component.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { FocusMonitor } from '@angular/cdk/a11y';
import { ChangeDetectionStrategy, Component, ContentChild, Directive, ElementRef, Input, OnDestroy } from '@angular/core';
import { AfterViewInit, ChangeDetectionStrategy, Component, ContentChild, Directive, ElementRef, Input, OnDestroy } from '@angular/core';

@Directive({
selector: 'nx-footer-copyright',
Expand Down Expand Up @@ -28,11 +28,13 @@ export class NxFooterNavigationDirective {}
role: 'listitem',
},
})
export class NxFooterLinkDirective implements OnDestroy {
export class NxFooterLinkDirective implements OnDestroy, AfterViewInit {
constructor(
private readonly _elementRef: ElementRef,
private readonly _focusMonitor: FocusMonitor,
) {
) {}

ngAfterViewInit(): void {
this._focusMonitor.monitor(this._elementRef, true);
}

Expand Down
8 changes: 5 additions & 3 deletions projects/ng-aquila/src/header/header.component.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { FocusMonitor } from '@angular/cdk/a11y';
import { BooleanInput, coerceBooleanProperty } from '@angular/cdk/coercion';
import { ChangeDetectionStrategy, Component, ContentChildren, Directive, ElementRef, Input, OnDestroy, QueryList } from '@angular/core';
import { AfterViewInit, ChangeDetectionStrategy, Component, ContentChildren, Directive, ElementRef, Input, OnDestroy, QueryList } from '@angular/core';

/** This directive defines a header row within the `<nx-header>` component. */
@Directive({
Expand Down Expand Up @@ -91,11 +91,13 @@ export class NxHeaderNavigationItemDirective {}
},
template: '<span class="nx-header__link-title"><ng-content></ng-content></span>',
})
export class NxHeaderLinkComponent implements OnDestroy {
export class NxHeaderLinkComponent implements OnDestroy, AfterViewInit {
constructor(
private readonly _elementRef: ElementRef,
private readonly _focusMonitor: FocusMonitor,
) {
) {}

ngAfterViewInit(): void {
this._focusMonitor.monitor(this._elementRef);
}

Expand Down
6 changes: 3 additions & 3 deletions projects/ng-aquila/src/input/password-toggle.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ export class NxPasswordToggleComponent implements AfterViewInit, OnDestroy {
private readonly _cdr: ChangeDetectorRef,
private readonly _elementRef: ElementRef,
private readonly _focusMonitor: FocusMonitor,
) {
this._focusMonitor.monitor(this._elementRef);
}
) {}

ngAfterViewInit(): void {
this._focusMonitor.monitor(this._elementRef);

if (!this.control) {
console.warn('You need to pass an input as a control to the password toggle.');
return;
Expand Down
Loading

0 comments on commit 2118d17

Please sign in to comment.