1313import { LitElement , ReactiveElement } from 'lit' ;
1414
1515import { version } from './version.js' ;
16- type ThemeRoot = HTMLElement & {
17- startManagingContentDirection : ( el : HTMLElement ) => void ;
18- stopManagingContentDirection : ( el : HTMLElement ) => void ;
19- } ;
2016
2117type Constructor < T = Record < string , unknown > > = {
2218 // eslint-disable-next-line @typescript-eslint/no-explicit-any
@@ -28,36 +24,8 @@ export interface SpectrumInterface {
2824 shadowRoot : ShadowRoot ;
2925 isLTR : boolean ;
3026 hasVisibleFocusInTree ( ) : boolean ;
31- dir : 'ltr' | 'rtl' ;
3227}
3328
34- const observedForElements : Set < HTMLElement > = new Set ( ) ;
35-
36- const updateRTL = ( ) : void => {
37- const dir =
38- document . documentElement . dir === 'rtl'
39- ? document . documentElement . dir
40- : 'ltr' ;
41- observedForElements . forEach ( ( el ) => {
42- el . setAttribute ( 'dir' , dir ) ;
43- } ) ;
44- } ;
45-
46- const rtlObserver = new MutationObserver ( updateRTL ) ;
47-
48- rtlObserver . observe ( document . documentElement , {
49- attributes : true ,
50- attributeFilter : [ 'dir' ] ,
51- } ) ;
52-
53- type ContentDirectionManager = HTMLElement & {
54- startManagingContentDirection ?( ) : void ;
55- } ;
56-
57- const canManageContentDirection = ( el : ContentDirectionManager ) : boolean =>
58- typeof el . startManagingContentDirection !== 'undefined' ||
59- el . tagName === 'SP-THEME' ;
60-
6129export function SpectrumMixin < T extends Constructor < ReactiveElement > > (
6230 constructor : T
6331) : T & Constructor < SpectrumInterface > {
@@ -66,12 +34,6 @@ export function SpectrumMixin<T extends Constructor<ReactiveElement>>(
6634 * @private
6735 */
6836 public override shadowRoot ! : ShadowRoot ;
69- private _dirParent ?: HTMLElement ;
70-
71- /**
72- * @private
73- */
74- public override dir ! : 'ltr' | 'rtl' ;
7537
7638 /**
7739 * @private
@@ -126,68 +88,16 @@ export function SpectrumMixin<T extends Constructor<ReactiveElement>>(
12688 return activeElement . matches ( '.focus-visible' ) ;
12789 }
12890 }
129-
130- public override connectedCallback ( ) : void {
131- if ( ! this . hasAttribute ( 'dir' ) ) {
132- let dirParent = ( ( this as HTMLElement ) . assignedSlot ||
133- this . parentNode ) as HTMLElement ;
134- while (
135- dirParent !== document . documentElement &&
136- ! canManageContentDirection (
137- dirParent as ContentDirectionManager
138- )
139- ) {
140- dirParent = ( ( dirParent as HTMLElement ) . assignedSlot || // step into the shadow DOM of the parent of a slotted node
141- dirParent . parentNode || // DOM Element detected
142- ( dirParent as unknown as ShadowRoot )
143- . host ) as HTMLElement ;
144- }
145- this . dir =
146- dirParent . dir === 'rtl' ? dirParent . dir : this . dir || 'ltr' ;
147- if ( dirParent === document . documentElement ) {
148- observedForElements . add ( this ) ;
149- } else {
150- const { localName } = dirParent ;
151- if (
152- localName . search ( '-' ) > - 1 &&
153- ! customElements . get ( localName )
154- ) {
155- /* c8 ignore next 5 */
156- customElements . whenDefined ( localName ) . then ( ( ) => {
157- (
158- dirParent as ThemeRoot
159- ) . startManagingContentDirection ( this ) ;
160- } ) ;
161- } else {
162- ( dirParent as ThemeRoot ) . startManagingContentDirection (
163- this
164- ) ;
165- }
166- }
167- this . _dirParent = dirParent as HTMLElement ;
168- }
169- super . connectedCallback ( ) ;
170- }
171-
172- public override disconnectedCallback ( ) : void {
173- super . disconnectedCallback ( ) ;
174- if ( this . _dirParent ) {
175- if ( this . _dirParent === document . documentElement ) {
176- observedForElements . delete ( this ) ;
177- } else {
178- ( this . _dirParent as ThemeRoot ) . stopManagingContentDirection (
179- this
180- ) ;
181- }
182- this . removeAttribute ( 'dir' ) ;
183- }
184- }
18591 }
18692 return SpectrumMixinElement ;
18793}
18894
18995export class SpectrumElement extends SpectrumMixin ( LitElement ) {
19096 static VERSION = version ;
97+
98+ public override get dir ( ) : CSSStyleDeclaration [ 'direction' ] {
99+ return getComputedStyle ( this ) . direction ?? 'ltr' ;
100+ }
191101}
192102
193103if ( process . env . NODE_ENV === 'development' ) {
0 commit comments