Skip to content

Commit 5688dd3

Browse files
committed
fix(cdk/text-field): avoid having to manually load text field styles
Reworks the text field so that users don't have to manually load its structural styles.
1 parent 25966fe commit 5688dd3

File tree

5 files changed

+36
-4
lines changed

5 files changed

+36
-4
lines changed

src/cdk/text-field/BUILD.bazel

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,13 @@ ng_module(
1616
["**/*.ts"],
1717
exclude = ["**/*.spec.ts"],
1818
),
19+
assets = [
20+
":text-field-prebuilt.css",
21+
],
1922
deps = [
2023
"//src/cdk/coercion",
2124
"//src/cdk/platform",
25+
"//src/cdk/private",
2226
"@npm//@angular/core",
2327
"@npm//rxjs",
2428
],

src/cdk/text-field/autofill.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,17 @@ import {
1111
Directive,
1212
ElementRef,
1313
EventEmitter,
14+
inject,
1415
Injectable,
1516
NgZone,
1617
OnDestroy,
1718
OnInit,
1819
Output,
1920
} from '@angular/core';
21+
import {_CdkPrivateStyleLoader} from '@angular/cdk/private';
2022
import {coerceElement} from '@angular/cdk/coercion';
2123
import {EMPTY, Observable, Subject} from 'rxjs';
24+
import {_CdkTextFieldStyleLoader} from './text-field-style-loader';
2225

2326
/** An event that is emitted when the autofill state of an input changes. */
2427
export type AutofillEvent = {
@@ -44,6 +47,7 @@ const listenerOptions = normalizePassiveListenerOptions({passive: true});
4447
*/
4548
@Injectable({providedIn: 'root'})
4649
export class AutofillMonitor implements OnDestroy {
50+
private _styleLoader = inject(_CdkPrivateStyleLoader);
4751
private _monitoredElements = new Map<Element, MonitoredElementInfo>();
4852

4953
constructor(
@@ -70,6 +74,8 @@ export class AutofillMonitor implements OnDestroy {
7074
return EMPTY;
7175
}
7276

77+
this._styleLoader.load(_CdkTextFieldStyleLoader);
78+
7379
const element = coerceElement(elementOrRef);
7480
const info = this._monitoredElements.get(element);
7581

src/cdk/text-field/autosize.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,14 @@ import {
1818
Optional,
1919
Inject,
2020
booleanAttribute,
21+
inject,
2122
} from '@angular/core';
23+
import {DOCUMENT} from '@angular/common';
2224
import {Platform} from '@angular/cdk/platform';
25+
import {_CdkPrivateStyleLoader} from '@angular/cdk/private';
2326
import {auditTime, takeUntil} from 'rxjs/operators';
2427
import {fromEvent, Subject} from 'rxjs';
25-
import {DOCUMENT} from '@angular/common';
28+
import {_CdkTextFieldStyleLoader} from './text-field-style-loader';
2629

2730
/** Directive to automatically resize a textarea to fit its content. */
2831
@Directive({
@@ -124,8 +127,9 @@ export class CdkTextareaAutosize implements AfterViewInit, DoCheck, OnDestroy {
124127
/** @breaking-change 11.0.0 make document required */
125128
@Optional() @Inject(DOCUMENT) document?: any,
126129
) {
130+
const styleLoader = inject(_CdkPrivateStyleLoader);
131+
styleLoader.load(_CdkTextFieldStyleLoader);
127132
this._document = document;
128-
129133
this._textareaElement = this._elementRef.nativeElement as HTMLTextAreaElement;
130134
}
131135

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
9+
import {ChangeDetectionStrategy, Component, ViewEncapsulation} from '@angular/core';
10+
11+
/** Component used to load the structural styles of the text field. */
12+
@Component({
13+
template: '',
14+
changeDetection: ChangeDetectionStrategy.OnPush,
15+
encapsulation: ViewEncapsulation.None,
16+
styleUrl: 'text-field-prebuilt.css',
17+
standalone: true,
18+
host: {'cdk-text-field-style-loader': ''},
19+
})
20+
export class _CdkTextFieldStyleLoader {}

src/material/core/_core.scss

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
}
1616

1717
@include cdk.a11y-visually-hidden();
18-
@include cdk.text-field-autosize();
19-
@include cdk.text-field-autofill();
2018
@include private.structural-styling('mat');
2119
@include private.structural-styling('mat-mdc');
2220

0 commit comments

Comments
 (0)