Skip to content

Commit 1ba1a39

Browse files
committed
fix(form-field): update label gap for outline style
1 parent 3a84259 commit 1ba1a39

File tree

4 files changed

+41
-41
lines changed

4 files changed

+41
-41
lines changed

src/lib/form-field/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ ng_module(
1818
deps = [
1919
"//src/lib/core",
2020
"//src/cdk/coercion",
21+
"//src/cdk/observers",
2122
"//src/cdk/platform",
2223
],
2324
tsconfig = "//src/lib:tsconfig-build.json",

src/lib/form-field/form-field-module.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import {CommonModule} from '@angular/common';
1010
import {NgModule} from '@angular/core';
11+
import {ObserversModule} from '@angular/cdk/observers';
1112
import {MatError} from './error';
1213
import {MatFormField} from './form-field';
1314
import {MatHint} from './hint';
@@ -27,7 +28,10 @@ import {MatSuffix} from './suffix';
2728
MatPrefix,
2829
MatSuffix,
2930
],
30-
imports: [CommonModule],
31+
imports: [
32+
CommonModule,
33+
ObserversModule,
34+
],
3135
exports: [
3236
MatError,
3337
MatFormField,

src/lib/form-field/form-field.html

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55
<!-- Outline used for outline appearance. -->
66
<ng-container *ngIf="appearance == 'outline'">
77
<div class="mat-form-field-outline">
8-
<div class="mat-form-field-outline-start" [style.width.px]="_outlineGapStart"></div>
9-
<div class="mat-form-field-outline-gap" [style.width.px]="_outlineGapWidth"></div>
8+
<div class="mat-form-field-outline-start"></div>
9+
<div class="mat-form-field-outline-gap"></div>
1010
<div class="mat-form-field-outline-end"></div>
1111
</div>
1212
<div class="mat-form-field-outline mat-form-field-outline-thick">
13-
<div class="mat-form-field-outline-start" [style.width.px]="_outlineGapStart"></div>
14-
<div class="mat-form-field-outline-gap" [style.width.px]="_outlineGapWidth"></div>
13+
<div class="mat-form-field-outline-start"></div>
14+
<div class="mat-form-field-outline-gap"></div>
1515
<div class="mat-form-field-outline-end"></div>
1616
</div>
1717
</ng-container>
@@ -27,6 +27,7 @@
2727
<!-- We add aria-owns as a workaround for an issue in JAWS & NVDA where the label isn't
2828
read if it comes before the control in the DOM. -->
2929
<label class="mat-form-field-label"
30+
(cdkObserveContent)="updateOutlineGap()"
3031
[id]="_labelId"
3132
[attr.for]="_control.id"
3233
[attr.aria-owns]="_control.id"

src/lib/form-field/form-field.ts

Lines changed: 30 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -148,12 +148,13 @@ export class MatFormField extends _MatFormFieldMixinBase
148148
return this._appearance || this._defaultOptions && this._defaultOptions.appearance || 'legacy';
149149
}
150150
set appearance(value: MatFormFieldAppearance) {
151-
// If we're switching to `outline` from another appearance, we have to recalculate the gap.
152-
if (value !== this._appearance && value === 'outline') {
153-
this._initialGapCalculated = false;
154-
}
155-
151+
const oldValue = this._appearance;
156152
this._appearance = value;
153+
if (this._appearance === 'outline' && oldValue !== value) {
154+
this._ngZone!.onStable.pipe(take(1)).subscribe(() => {
155+
this._ngZone!.runOutsideAngular(() => this.updateOutlineGap());
156+
});
157+
}
157158
}
158159
_appearance: MatFormFieldAppearance;
159160

@@ -217,17 +218,13 @@ export class MatFormField extends _MatFormFieldMixinBase
217218
/** Whether the Angular animations are enabled. */
218219
_animationsEnabled: boolean;
219220

220-
_outlineGapWidth = 0;
221-
_outlineGapStart = 0;
222-
_initialGapCalculated = false;
223-
224221
/**
225222
* @deprecated
226223
* @breaking-change 7.0.0
227224
*/
228225
@ViewChild('underline') underlineRef: ElementRef;
229226

230-
@ViewChild('connectionContainer') _connectionContainerRef: ElementRef;
227+
@ViewChild('connectionContainer') _connectionContainerRef: ElementRef<HTMLElement>;
231228
@ViewChild('inputContainer') _inputContainerRef: ElementRef;
232229
@ViewChild('label') private _label: ElementRef;
233230
@ContentChild(MatFormFieldControl) _control: MatFormFieldControl<any>;
@@ -245,7 +242,7 @@ export class MatFormField extends _MatFormFieldMixinBase
245242
@Optional() private _dir: Directionality,
246243
@Optional() @Inject(MAT_FORM_FIELD_DEFAULT_OPTIONS) private _defaultOptions:
247244
MatFormFieldDefaultOptions,
248-
// @breaking-change 7.0.0 _platform, _ngZone and _animationMode to be made required.
245+
// @breaking-change 7.0.0 _platform and _animationMode to be made required.
249246
private _platform?: Platform,
250247
private _ngZone?: NgZone,
251248
@Optional() @Inject(ANIMATION_MODULE_TYPE) _animationMode?: string) {
@@ -298,20 +295,6 @@ export class MatFormField extends _MatFormFieldMixinBase
298295

299296
ngAfterContentChecked() {
300297
this._validateControlChild();
301-
302-
if (!this._initialGapCalculated) {
303-
// @breaking-change 7.0.0 Remove this check and else block once _ngZone is required.
304-
if (this._ngZone) {
305-
// It's important that we run this outside the `_ngZone`, because the `Promise.resolve`
306-
// can kick us into an infinite change detection loop, if the `_initialGapCalculated`
307-
// wasn't flipped on for some reason.
308-
this._ngZone.runOutsideAngular(() => {
309-
Promise.resolve().then(() => this.updateOutlineGap());
310-
});
311-
} else {
312-
Promise.resolve().then(() => this.updateOutlineGap());
313-
}
314-
}
315298
}
316299

317300
ngAfterViewInit() {
@@ -422,9 +405,9 @@ export class MatFormField extends _MatFormFieldMixinBase
422405
let ids: string[] = [];
423406

424407
if (this._getDisplayedMessages() === 'hint') {
425-
let startHint = this._hintChildren ?
408+
const startHint = this._hintChildren ?
426409
this._hintChildren.find(hint => hint.align === 'start') : null;
427-
let endHint = this._hintChildren ?
410+
const endHint = this._hintChildren ?
428411
this._hintChildren.find(hint => hint.align === 'end') : null;
429412

430413
if (startHint) {
@@ -456,10 +439,19 @@ export class MatFormField extends _MatFormFieldMixinBase
456439
* appearance.
457440
*/
458441
updateOutlineGap() {
459-
if (this.appearance === 'outline' && this._label && this._label.nativeElement.children.length) {
442+
if (this.appearance !== 'outline') {
443+
return;
444+
}
445+
446+
let startWidth = 0;
447+
let gapWidth = 0;
448+
const startEls = this._connectionContainerRef.nativeElement.querySelectorAll<HTMLElement>(
449+
'.mat-form-field-outline-start');
450+
const gapEls = this._connectionContainerRef.nativeElement.querySelectorAll<HTMLElement>(
451+
'.mat-form-field-outline-gap');
452+
if (this._label && this._label.nativeElement.children.length) {
460453
if (this._platform && !this._platform.isBrowser) {
461454
// getBoundingClientRect isn't available on the server.
462-
this._initialGapCalculated = true;
463455
return;
464456
}
465457
if (!document.documentElement.contains(this._elementRef.nativeElement)) {
@@ -474,14 +466,16 @@ export class MatFormField extends _MatFormFieldMixinBase
474466
for (const child of this._label.nativeElement.children) {
475467
labelWidth += child.offsetWidth;
476468
}
477-
this._outlineGapStart = labelStart - containerStart - outlineGapPadding;
478-
this._outlineGapWidth = labelWidth * floatingLabelScale + outlineGapPadding * 2;
479-
} else {
480-
this._outlineGapStart = 0;
481-
this._outlineGapWidth = 0;
469+
startWidth = labelStart - containerStart - outlineGapPadding;
470+
gapWidth = labelWidth * floatingLabelScale + outlineGapPadding * 2;
471+
}
472+
473+
for (let i = 0; i < startEls.length; i++) {
474+
startEls.item(i).style.width = `${startWidth}px`;
475+
}
476+
for (let i = 0; i < gapEls.length; i++) {
477+
gapEls.item(i).style.width = `${gapWidth}px`;
482478
}
483-
this._initialGapCalculated = true;
484-
this._changeDetectorRef.markForCheck();
485479
}
486480

487481
/** Gets the start end of the rect considering the current directionality. */

0 commit comments

Comments
 (0)