@@ -19,24 +19,37 @@ export type Direction = 'ltr' | 'rtl';
1919 */
2020@Injectable ( { providedIn : 'root' } )
2121export 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 ( ) ;
0 commit comments