Skip to content

Commit

Permalink
fix(cdk/a11y): resolve hydration error in focus trap
Browse files Browse the repository at this point in the history
Disables focus traps on the server, because they insert DOM nodes that can throw off hydration.
  • Loading branch information
crisbeto committed Dec 28, 2023
1 parent a8b8e62 commit 10fbe36
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 11 deletions.
25 changes: 16 additions & 9 deletions src/cdk/a11y/focus-trap/focus-trap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* found in the LICENSE file at https://angular.io/license
*/

import {_getFocusedElementPierceShadowDom} from '@angular/cdk/platform';
import {Platform, _getFocusedElementPierceShadowDom} from '@angular/cdk/platform';
import {DOCUMENT} from '@angular/common';
import {
AfterContentInit,
Expand All @@ -21,6 +21,7 @@ import {
SimpleChanges,
OnChanges,
booleanAttribute,
inject,
} from '@angular/core';
import {take} from 'rxjs/operators';
import {InteractivityChecker} from '../interactivity-checker/interactivity-checker';
Expand Down Expand Up @@ -416,10 +417,12 @@ export class CdkTrapFocus implements OnDestroy, AfterContentInit, OnChanges, DoC
/** Whether the focus trap is active. */
@Input({alias: 'cdkTrapFocus', transform: booleanAttribute})
get enabled(): boolean {
return this.focusTrap.enabled;
return this.focusTrap?.enabled || false;
}
set enabled(value: boolean) {
this.focusTrap.enabled = value;
if (this.focusTrap) {
this.focusTrap.enabled = value;
}
}

/**
Expand All @@ -437,11 +440,15 @@ export class CdkTrapFocus implements OnDestroy, AfterContentInit, OnChanges, DoC
*/
@Inject(DOCUMENT) _document: any,
) {
this.focusTrap = this._focusTrapFactory.create(this._elementRef.nativeElement, true);
const platform = inject(Platform);

if (platform.isBrowser) {
this.focusTrap = this._focusTrapFactory.create(this._elementRef.nativeElement, true);
}
}

ngOnDestroy() {
this.focusTrap.destroy();
this.focusTrap?.destroy();

// If we stored a previously focused element when using autoCapture, return focus to that
// element now that the trapped region is being destroyed.
Expand All @@ -452,15 +459,15 @@ export class CdkTrapFocus implements OnDestroy, AfterContentInit, OnChanges, DoC
}

ngAfterContentInit() {
this.focusTrap.attachAnchors();
this.focusTrap?.attachAnchors();

if (this.autoCapture) {
this._captureFocus();
}
}

ngDoCheck() {
if (!this.focusTrap.hasAttached()) {
if (this.focusTrap && !this.focusTrap.hasAttached()) {
this.focusTrap.attachAnchors();
}
}
Expand All @@ -472,14 +479,14 @@ export class CdkTrapFocus implements OnDestroy, AfterContentInit, OnChanges, DoC
autoCaptureChange &&
!autoCaptureChange.firstChange &&
this.autoCapture &&
this.focusTrap.hasAttached()
this.focusTrap?.hasAttached()
) {
this._captureFocus();
}
}

private _captureFocus() {
this._previouslyFocusedElement = _getFocusedElementPierceShadowDom();
this.focusTrap.focusInitialElementWhenReady();
this.focusTrap?.focusInitialElementWhenReady();
}
}
2 changes: 1 addition & 1 deletion src/universal-app/kitchen-sink/kitchen-sink.html
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ <h2>Horizontal Stepper</h2>

<h2>Focus trap</h2>

<div cdkTrapFocus cdkTrapFocusAutoCapture>
<div cdkTrapFocus [cdkTrapFocusAutoCapture]="isAutomated">
<button>Oh no, I'm trapped!</button>
</div>

Expand Down
3 changes: 2 additions & 1 deletion src/universal-app/kitchen-sink/kitchen-sink.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {FocusMonitor} from '@angular/cdk/a11y';
import {A11yModule, FocusMonitor} from '@angular/cdk/a11y';
import {DragDropModule} from '@angular/cdk/drag-drop';
import {ScrollingModule, ViewportRuler} from '@angular/cdk/scrolling';
import {CdkTableModule, DataSource} from '@angular/cdk/table';
Expand Down Expand Up @@ -122,6 +122,7 @@ export class TestEntryComponent {}
// CDK Modules
CdkTableModule,
DragDropModule,
A11yModule,

// Other modules
YouTubePlayer,
Expand Down

0 comments on commit 10fbe36

Please sign in to comment.