-
Notifications
You must be signed in to change notification settings - Fork 6.8k
/
Copy pathnotched-outline.ts
99 lines (88 loc) · 3.01 KB
/
notched-outline.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.dev/license
*/
import {
AfterViewInit,
ChangeDetectionStrategy,
Component,
ElementRef,
Input,
NgZone,
OnChanges,
SimpleChanges,
ViewChild,
ViewEncapsulation,
inject,
} from '@angular/core';
/**
* Internal component that creates an instance of the MDC notched-outline component.
*
* The component sets up the HTML structure and styles for the notched-outline. It provides
* inputs to toggle the notch state and width.
*/
@Component({
selector: 'div[matFormFieldNotchedOutline]',
templateUrl: './notched-outline.html',
host: {
'class': 'mdc-notched-outline',
// Besides updating the notch state through the MDC component, we toggle this class through
// a host binding in order to ensure that the notched-outline renders correctly on the server.
'[class.mdc-notched-outline--notched]': 'open',
},
changeDetection: ChangeDetectionStrategy.OnPush,
encapsulation: ViewEncapsulation.None,
})
export class MatFormFieldNotchedOutline implements AfterViewInit, OnChanges {
private _elementRef = inject<ElementRef<HTMLElement>>(ElementRef);
private _ngZone = inject(NgZone);
/** Whether the notch should be opened. */
@Input('matFormFieldNotchedOutlineOpen') open: boolean = false;
/** Whether the floating label is present. */
@Input('matFormFieldHasFloatingLabel') hasFloatingLabel: boolean = false;
@ViewChild('notch') _notch: ElementRef;
/** Gets the HTML element for the floating label. */
get element(): HTMLElement {
return this._elementRef.nativeElement;
}
constructor(...args: unknown[]);
constructor() {}
ngAfterViewInit(): void {
const label = this.element.querySelector<HTMLElement>('.mdc-floating-label');
if (label) {
this.element.classList.add('mdc-notched-outline--upgraded');
if (typeof requestAnimationFrame === 'function') {
label.style.transitionDuration = '0s';
this._ngZone.runOutsideAngular(() => {
requestAnimationFrame(() => (label.style.transitionDuration = ''));
});
}
} else {
this.element.classList.add('mdc-notched-outline--no-label');
}
}
ngOnChanges(changes: SimpleChanges) {
if (
changes['hasFloatingLabel'] &&
this.hasFloatingLabel &&
this.element.classList.contains('mdc-notched-outline--no-label')
) {
this.element.classList.add('mdc-notched-outline--upgraded');
this.element.classList.remove('mdc-notched-outline--no-label');
}
}
_setNotchWidth(labelWidth: number) {
if (!this.open || !labelWidth) {
this._notch.nativeElement.style.width = '';
} else {
const NOTCH_ELEMENT_PADDING = 8;
const NOTCH_ELEMENT_BORDER = 1;
this._notch.nativeElement.style.width = `calc(${labelWidth}px * var(--mat-mdc-form-field-floating-label-scale, 0.75) + ${
NOTCH_ELEMENT_PADDING + NOTCH_ELEMENT_BORDER
}px)`;
}
}
}