Skip to content

fix(cdk/listbox): improve SSR compatibility by adding an _isBrowser check before calling _setPreviousActiveOptionAsActiveOptionOnWindowBlur #28746

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/cdk/listbox/listbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import {defer, fromEvent, merge, Observable, Subject} from 'rxjs';
import {filter, map, startWith, switchMap, takeUntil} from 'rxjs/operators';
import {ControlValueAccessor, NG_VALUE_ACCESSOR} from '@angular/forms';
import {Directionality} from '@angular/cdk/bidi';
import {Platform} from '@angular/cdk/platform';

/** The next id to use for creating unique DOM IDs. */
let nextId = 0;
Expand Down Expand Up @@ -402,6 +403,9 @@ export class CdkListbox<T = unknown> implements AfterContentInit, OnDestroy, Con
/** The directionality of the page. */
private readonly _dir = inject(Directionality, {optional: true});

/** Whether the component is being rendered in the browser. */
private readonly _isBrowser: boolean = inject(Platform).isBrowser;

/** A predicate that skips disabled options. */
private readonly _skipDisabledPredicate = (option: CdkOption<T>) => option.disabled;

Expand All @@ -415,7 +419,9 @@ export class CdkListbox<T = unknown> implements AfterContentInit, OnDestroy, Con
private _previousActiveOption: CdkOption<T> | null = null;

constructor() {
this._setPreviousActiveOptionAsActiveOptionOnWindowBlur();
if (this._isBrowser) {
this._setPreviousActiveOptionAsActiveOptionOnWindowBlur();
}
}

ngAfterContentInit() {
Expand Down
9 changes: 9 additions & 0 deletions src/universal-app/kitchen-sink/kitchen-sink.html
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,15 @@ <h2>Native table with sticky header and footer</h2>
<tr mat-footer-row *matFooterRowDef="tableColumns; sticky: true"></tr>
</table>

<h2>CDK Listbox</h2>
<div class="test-cdk-listbox">
<label>Favorite color</label>
<ul cdkListbox>
<li cdkOption="red">Red</li>
<li cdkOption="green">Green</li>
<li cdkOption="blue">Blue</li>
</ul>
</div>

<h2>Selection list</h2>
<mat-selection-list>
Expand Down
33 changes: 33 additions & 0 deletions src/universal-app/kitchen-sink/kitchen-sink.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {A11yModule, FocusMonitor} from '@angular/cdk/a11y';
import {DragDropModule} from '@angular/cdk/drag-drop';
import {CdkListboxModule} from '@angular/cdk/listbox';
import {ScrollingModule, ViewportRuler} from '@angular/cdk/scrolling';
import {CdkTableModule, DataSource} from '@angular/cdk/table';
import {Component, ElementRef, InjectionToken, inject} from '@angular/core';
Expand Down Expand Up @@ -95,6 +96,37 @@ export class TestEntryComponent {}
border: 1px solid black;
}

.test-cdk-listbox {
display: block;
width: 100%;

> label {
display: block;
padding: 5px;
}

> ul {
list-style: none;
padding: 0;
margin: 0;

> li {
position: relative;
padding: 5px 5px 5px 25px;

&:focus {
background: rgba(0, 0, 0, 0.2);
}

&[aria-selected='true']::before {
content: "✔";
position: absolute;
left: 2px;
}
}
}
}

.test-cdk-table {
display: table;
width: 100%;
Expand Down Expand Up @@ -146,6 +178,7 @@ export class TestEntryComponent {}
ScrollingModule,

// CDK Modules
CdkListboxModule,
CdkTableModule,
DragDropModule,
A11yModule,
Expand Down