Skip to content

Commit 501b6be

Browse files
committed
fix(form-field): FormField with outline doesn't work well with dir="rtl" #14944
1 parent 2f61607 commit 501b6be

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

src/cdk/bidi/directionality.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,24 +19,37 @@ export type Direction = 'ltr' | 'rtl';
1919
*/
2020
@Injectable({providedIn: 'root'})
2121
export class Directionality implements OnDestroy {
22+
private _document: Document;
2223
/** The current 'ltr' or 'rtl' value. */
23-
readonly value: Direction = 'ltr';
24+
value: Direction = 'ltr';
2425

2526
/** Stream that emits whenever the 'ltr' / 'rtl' state changes. */
2627
readonly change = new EventEmitter<Direction>();
2728

2829
constructor(@Optional() @Inject(DIR_DOCUMENT) _document?: any) {
2930
if (_document) {
31+
this._document = _document;
32+
this.setDirection();
33+
}
34+
}
35+
36+
/** Get the current direction. */
37+
get currentDirection(): Direction {
38+
this.setDirection();
39+
return this.value;
40+
}
41+
42+
/** Set the current direction. */
43+
setDirection(): void {
3044
// TODO: handle 'auto' value -
3145
// We still need to account for dir="auto".
3246
// It looks like HTMLElemenet.dir is also "auto" when that's set to the attribute,
3347
// but getComputedStyle return either "ltr" or "rtl". avoiding getComputedStyle for now
34-
const bodyDir = _document.body ? _document.body.dir : null;
35-
const htmlDir = _document.documentElement ? _document.documentElement.dir : null;
48+
const bodyDir = this._document.body ? this._document.body.dir : null;
49+
const htmlDir = this._document.documentElement ? this._document.documentElement.dir : null;
3650
const value = bodyDir || htmlDir;
3751
this.value = (value === 'ltr' || value === 'rtl') ? value : 'ltr';
3852
}
39-
}
4053

4154
ngOnDestroy() {
4255
this.change.complete();

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -558,7 +558,7 @@ export class MatFormField extends _MatFormFieldMixinBase
558558

559559
/** Gets the start end of the rect considering the current directionality. */
560560
private _getStartEnd(rect: ClientRect): number {
561-
return this._dir && this._dir.value === 'rtl' ? rect.right : rect.left;
561+
return this._dir && this._dir.currentDirection && this._dir.currentDirection === 'rtl' ? rect.right : rect.left;
562562
}
563563

564564
/**

0 commit comments

Comments
 (0)