Skip to content

Commit a5d5ed8

Browse files
committed
refactor(multiple): clean up usages of DOCUMENT (#31461)
We had several places that were optionally injecting `DOCUMENT`, because historically it wasn't guaranteed to be there. That shouldn't be the case anymore so we can clean them up. (cherry picked from commit a6a385c)
1 parent a6f4499 commit a5d5ed8

File tree

7 files changed

+11
-19
lines changed

7 files changed

+11
-19
lines changed

goldens/cdk/a11y/index.api.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ export class FocusKeyManager<T> extends ListKeyManager<FocusableOption & T> {
175175
// @public
176176
export class FocusMonitor implements OnDestroy {
177177
constructor(...args: unknown[]);
178-
protected _document?: Document | null | undefined;
178+
protected _document: Document;
179179
focusVia(element: HTMLElement, origin: FocusOrigin, options?: FocusOptions_2): void;
180180
focusVia(element: ElementRef<HTMLElement>, origin: FocusOrigin, options?: FocusOptions_2): void;
181181
monitor(element: HTMLElement, checkChildren?: boolean): Observable<FocusOrigin>;

goldens/cdk/text-field/index.api.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ export class CdkAutofill implements OnDestroy, OnInit {
5151
// @public
5252
export class CdkTextareaAutosize implements AfterViewInit, DoCheck, OnDestroy {
5353
constructor(...args: unknown[]);
54-
protected _document?: Document | null | undefined;
54+
protected _document: Document;
5555
get enabled(): boolean;
5656
set enabled(value: boolean);
5757
get maxRows(): number;

src/cdk/a11y/focus-monitor/focus-monitor.ts

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ export class FocusMonitor implements OnDestroy {
142142
};
143143

144144
/** Used to reference correct document/window */
145-
protected _document? = inject(DOCUMENT, {optional: true});
145+
protected _document = inject(DOCUMENT);
146146

147147
/** Subject for stopping our InputModalityDetector subscription. */
148148
private readonly _stopInputModalityDetector = new Subject<void>();
@@ -206,7 +206,7 @@ export class FocusMonitor implements OnDestroy {
206206
// If the element is inside the shadow DOM, we need to bind our focus/blur listeners to
207207
// the shadow root, rather than the `document`, because the browser won't emit focus events
208208
// to the `document`, if focus is moving within the same shadow root.
209-
const rootNode = _getShadowRoot(nativeElement) || this._getDocument();
209+
const rootNode = _getShadowRoot(nativeElement) || this._document;
210210
const cachedInfo = this._elementInfo.get(nativeElement);
211211

212212
// Check if we're already monitoring this element.
@@ -280,7 +280,7 @@ export class FocusMonitor implements OnDestroy {
280280
options?: FocusOptions,
281281
): void {
282282
const nativeElement = coerceElement(element);
283-
const focusedElement = this._getDocument().activeElement;
283+
const focusedElement = this._document.activeElement;
284284

285285
// If the element is focused already, calling `focus` again won't trigger the event listener
286286
// which means that the focus classes won't be updated. If that's the case, update the classes
@@ -303,15 +303,9 @@ export class FocusMonitor implements OnDestroy {
303303
this._elementInfo.forEach((_info, element) => this.stopMonitoring(element));
304304
}
305305

306-
/** Access injected document if available or fallback to global document reference */
307-
private _getDocument(): Document {
308-
return this._document || document;
309-
}
310-
311306
/** Use defaultView of injected document if available or fallback to global window reference */
312307
private _getWindow(): Window {
313-
const doc = this._getDocument();
314-
return doc.defaultView || window;
308+
return this._document.defaultView || window;
315309
}
316310

317311
private _getFocusOrigin(focusEventTarget: HTMLElement | null): FocusOrigin {

src/cdk/dialog/dialog-container.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ export class CdkDialogContainer<C extends DialogConfig = DialogConfig>
8282
private _renderer = inject(Renderer2);
8383

8484
private _platform = inject(Platform);
85-
protected _document = inject(DOCUMENT, {optional: true})!;
85+
protected _document = inject(DOCUMENT);
8686

8787
/** The portal outlet inside of this container into which the dialog content will be loaded. */
8888
@ViewChild(CdkPortalOutlet, {static: true}) _portalOutlet: CdkPortalOutlet;

src/cdk/scrolling/viewport-ruler.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export class ViewportRuler implements OnDestroy {
3636
private readonly _change = new Subject<Event>();
3737

3838
/** Used to reference correct document/window */
39-
protected _document = inject(DOCUMENT, {optional: true})!;
39+
protected _document = inject(DOCUMENT);
4040

4141
constructor(...args: unknown[]);
4242

src/cdk/text-field/autosize.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ export class CdkTextareaAutosize implements AfterViewInit, DoCheck, OnDestroy {
122122
private _cachedScrollTop: number;
123123

124124
/** Used to reference correct document/window */
125-
protected _document? = inject(DOCUMENT, {optional: true});
125+
protected _document = inject(DOCUMENT);
126126

127127
private _hasFocus: boolean;
128128

src/material/sidenav/drawer.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ export class MatDrawer implements AfterViewInit, OnDestroy {
182182
private _ngZone = inject(NgZone);
183183
private _renderer = inject(Renderer2);
184184
private readonly _interactivityChecker = inject(InteractivityChecker);
185-
private _doc = inject(DOCUMENT, {optional: true})!;
185+
private _doc = inject(DOCUMENT);
186186
_container? = inject<MatDrawerContainer>(MAT_DRAWER_CONTAINER, {optional: true});
187187

188188
private _focusTrap: FocusTrap | null = null;
@@ -347,9 +347,7 @@ export class MatDrawer implements AfterViewInit, OnDestroy {
347347
constructor() {
348348
this.openedChange.pipe(takeUntil(this._destroyed)).subscribe((opened: boolean) => {
349349
if (opened) {
350-
if (this._doc) {
351-
this._elementFocusedBeforeDrawerWasOpened = this._doc.activeElement as HTMLElement;
352-
}
350+
this._elementFocusedBeforeDrawerWasOpened = this._doc.activeElement as HTMLElement;
353351
this._takeFocus();
354352
} else if (this._isFocusWithinDrawer()) {
355353
this._restoreFocus(this._openedVia || 'program');

0 commit comments

Comments
 (0)