5
5
* Use of this source code is governed by an MIT-style license that can be
6
6
* found in the LICENSE file at https://angular.dev/license
7
7
*/
8
+ import { _IdGenerator } from '@angular/cdk/a11y' ;
8
9
import { Directionality } from '@angular/cdk/bidi' ;
9
10
import { BooleanInput , coerceBooleanProperty } from '@angular/cdk/coercion' ;
10
11
import { Platform } from '@angular/cdk/platform' ;
@@ -27,16 +28,17 @@ import {
27
28
QueryList ,
28
29
ViewChild ,
29
30
ViewEncapsulation ,
30
- afterRender ,
31
+ afterRenderEffect ,
31
32
computed ,
32
33
contentChild ,
33
34
inject ,
35
+ signal ,
36
+ viewChild ,
34
37
} from '@angular/core' ;
35
38
import { AbstractControlDirective , ValidatorFn } from '@angular/forms' ;
36
- import { _animationsDisabled , ThemePalette } from '../core' ;
37
- import { _IdGenerator } from '@angular/cdk/a11y' ;
38
39
import { Subject , Subscription , merge } from 'rxjs' ;
39
- import { map , pairwise , takeUntil , filter , startWith } from 'rxjs/operators' ;
40
+ import { filter , map , pairwise , startWith , takeUntil } from 'rxjs/operators' ;
41
+ import { ThemePalette , _animationsDisabled } from '../core' ;
40
42
import { MAT_ERROR , MatError } from './directives/error' ;
41
43
import {
42
44
FLOATING_LABEL_PARENT ,
@@ -203,6 +205,21 @@ export class MatFormField
203
205
@ViewChild ( MatFormFieldNotchedOutline ) _notchedOutline : MatFormFieldNotchedOutline | undefined ;
204
206
@ViewChild ( MatFormFieldLineRipple ) _lineRipple : MatFormFieldLineRipple | undefined ;
205
207
208
+ private _iconPrefixContainerSignal = viewChild < ElementRef < HTMLElement > > ( 'iconPrefixContainer' ) ;
209
+ private _textPrefixContainerSignal = viewChild < ElementRef < HTMLElement > > ( 'textPrefixContainer' ) ;
210
+ private _iconSuffixContainerSignal = viewChild < ElementRef < HTMLElement > > ( 'iconSuffixContainer' ) ;
211
+ private _textSuffixContainerSignal = viewChild < ElementRef < HTMLElement > > ( 'textSuffixContainer' ) ;
212
+ private _prefixSuffixContainers = computed ( ( ) => {
213
+ return [
214
+ this . _iconPrefixContainerSignal ( ) ,
215
+ this . _textPrefixContainerSignal ( ) ,
216
+ this . _iconSuffixContainerSignal ( ) ,
217
+ this . _textSuffixContainerSignal ( ) ,
218
+ ]
219
+ . map ( container => container ?. nativeElement )
220
+ . filter ( e => e !== undefined ) ;
221
+ } ) ;
222
+
206
223
@ContentChild ( _MatFormFieldControl ) _formFieldControl : MatFormFieldControl < any > ;
207
224
@ContentChildren ( MAT_PREFIX , { descendants : true } ) _prefixChildren : QueryList < MatPrefix > ;
208
225
@ContentChildren ( MAT_SUFFIX , { descendants : true } ) _suffixChildren : QueryList < MatSuffix > ;
@@ -250,10 +267,9 @@ export class MatFormField
250
267
/** The form field appearance style. */
251
268
@Input ( )
252
269
get appearance ( ) : MatFormFieldAppearance {
253
- return this . _appearance ;
270
+ return this . _appearanceSignal ( ) ;
254
271
}
255
272
set appearance ( value : MatFormFieldAppearance ) {
256
- const oldValue = this . _appearance ;
257
273
const newAppearance = value || this . _defaults ?. appearance || DEFAULT_APPEARANCE ;
258
274
if ( typeof ngDevMode === 'undefined' || ngDevMode ) {
259
275
if ( newAppearance !== 'fill' && newAppearance !== 'outline' ) {
@@ -262,15 +278,9 @@ export class MatFormField
262
278
) ;
263
279
}
264
280
}
265
- this . _appearance = newAppearance ;
266
- if ( this . _appearance === 'outline' && this . _appearance !== oldValue ) {
267
- // If the appearance has been switched to `outline`, the label offset needs to be updated.
268
- // The update can happen once the view has been re-checked, but not immediately because
269
- // the view has not been updated and the notched-outline floating label is not present.
270
- this . _needsOutlineLabelOffsetUpdate = true ;
271
- }
281
+ this . _appearanceSignal . set ( newAppearance ) ;
272
282
}
273
- private _appearance : MatFormFieldAppearance = DEFAULT_APPEARANCE ;
283
+ private _appearanceSignal = signal ( DEFAULT_APPEARANCE ) ;
274
284
275
285
/**
276
286
* Whether the form field should reserve space for one line of hint/error text (default)
@@ -319,7 +329,6 @@ export class MatFormField
319
329
private _destroyed = new Subject < void > ( ) ;
320
330
private _isFocused : boolean | null = null ;
321
331
private _explicitFormFieldControl : MatFormFieldControl < any > ;
322
- private _needsOutlineLabelOffsetUpdate = false ;
323
332
private _previousControl : MatFormFieldControl < unknown > | null = null ;
324
333
private _previousControlValidatorFn : ValidatorFn | null = null ;
325
334
private _stateChanges : Subscription | undefined ;
@@ -341,6 +350,8 @@ export class MatFormField
341
350
this . color = defaults . color ;
342
351
}
343
352
}
353
+
354
+ this . _syncOutlineLabelOffset ( ) ;
344
355
}
345
356
346
357
ngAfterViewInit ( ) {
@@ -366,7 +377,6 @@ export class MatFormField
366
377
this . _assertFormFieldControl ( ) ;
367
378
this . _initializeSubscript ( ) ;
368
379
this . _initializePrefixAndSuffix ( ) ;
369
- this . _initializeOutlineLabelOffsetSubscriptions ( ) ;
370
380
}
371
381
372
382
ngAfterContentChecked ( ) {
@@ -399,6 +409,7 @@ export class MatFormField
399
409
}
400
410
401
411
ngOnDestroy ( ) {
412
+ this . _outlineLabelOffsetResizeObserver ?. disconnect ( ) ;
402
413
this . _stateChanges ?. unsubscribe ( ) ;
403
414
this . _valueChanges ?. unsubscribe ( ) ;
404
415
this . _describedByChanges ?. unsubscribe ( ) ;
@@ -546,34 +557,42 @@ export class MatFormField
546
557
) ;
547
558
}
548
559
560
+ private _outlineLabelOffsetResizeObserver : ResizeObserver | null = null ;
561
+
549
562
/**
550
563
* The floating label in the docked state needs to account for prefixes. The horizontal offset
551
564
* is calculated whenever the appearance changes to `outline`, the prefixes change, or when the
552
565
* form field is added to the DOM. This method sets up all subscriptions which are needed to
553
566
* trigger the label offset update.
554
567
*/
555
- private _initializeOutlineLabelOffsetSubscriptions ( ) {
556
- // Whenever the prefix changes, schedule an update of the label offset.
557
- // TODO(mmalerba): Use ResizeObserver to better support dynamically changing prefix content.
558
- this . _prefixChildren . changes . subscribe ( ( ) => ( this . _needsOutlineLabelOffsetUpdate = true ) ) ;
568
+ private _syncOutlineLabelOffset ( ) {
569
+ // Skip measuring & sizing on server.
570
+ if ( ! inject ( Platform ) . isBrowser ) {
571
+ return ;
572
+ }
559
573
574
+ // Whenever the prefix changes, schedule an update of the label offset.
560
575
// TODO(mmalerba): Split this into separate `afterRender` calls using the `EarlyRead` and
561
576
// `Write` phases.
562
- afterRender (
563
- ( ) => {
564
- if ( this . _needsOutlineLabelOffsetUpdate ) {
565
- this . _needsOutlineLabelOffsetUpdate = false ;
566
- this . _updateOutlineLabelOffset ( ) ;
577
+ afterRenderEffect ( ( ) => {
578
+ if ( this . _appearanceSignal ( ) === 'outline' ) {
579
+ this . _updateOutlineLabelOffset ( ) ;
580
+ if ( ! globalThis . ResizeObserver ) {
581
+ return ;
567
582
}
568
- } ,
569
- {
570
- injector : this . _injector ,
571
- } ,
572
- ) ;
573
583
574
- this . _dir . change
575
- . pipe ( takeUntil ( this . _destroyed ) )
576
- . subscribe ( ( ) => ( this . _needsOutlineLabelOffsetUpdate = true ) ) ;
584
+ // Setup a resize observer to monitor changes to the size of the prefix / suffix and
585
+ // readjust the label offset.
586
+ this . _outlineLabelOffsetResizeObserver ||= new globalThis . ResizeObserver ( ( ) =>
587
+ this . _updateOutlineLabelOffset ( ) ,
588
+ ) ;
589
+ for ( const el of this . _prefixSuffixContainers ( ) ) {
590
+ this . _outlineLabelOffsetResizeObserver . observe ( el , { box : 'border-box' } ) ;
591
+ }
592
+ } else {
593
+ this . _outlineLabelOffsetResizeObserver ?. disconnect ( ) ;
594
+ }
595
+ } ) ;
577
596
}
578
597
579
598
/** Whether the floating label should always float or not. */
@@ -719,6 +738,7 @@ export class MatFormField
719
738
* incorporate the horizontal offset into their default text-field styles.
720
739
*/
721
740
private _updateOutlineLabelOffset ( ) {
741
+ const dir = this . _dir . valueSignal ( ) ;
722
742
if ( ! this . _hasOutline ( ) || ! this . _floatingLabel ) {
723
743
return ;
724
744
}
@@ -732,7 +752,6 @@ export class MatFormField
732
752
// If the form field is not attached to the DOM yet (e.g. in a tab), we defer
733
753
// the label offset update until the zone stabilizes.
734
754
if ( ! this . _isAttachedToDom ( ) ) {
735
- this . _needsOutlineLabelOffsetUpdate = true ;
736
755
return ;
737
756
}
738
757
const iconPrefixContainer = this . _iconPrefixContainer ?. nativeElement ;
@@ -745,7 +764,7 @@ export class MatFormField
745
764
const textSuffixContainerWidth = textSuffixContainer ?. getBoundingClientRect ( ) . width ?? 0 ;
746
765
// If the directionality is RTL, the x-axis transform needs to be inverted. This
747
766
// is because `transformX` does not change based on the page directionality.
748
- const negate = this . _dir . value === 'rtl' ? '-1' : '1' ;
767
+ const negate = dir === 'rtl' ? '-1' : '1' ;
749
768
const prefixWidth = `${ iconPrefixContainerWidth + textPrefixContainerWidth } px` ;
750
769
const labelOffset = `var(--mat-mdc-form-field-label-offset-x, 0px)` ;
751
770
const labelHorizontalOffset = `calc(${ negate } * (${ prefixWidth } + ${ labelOffset } ))` ;
0 commit comments