Skip to content

Commit 55fcc7c

Browse files
committed
chore: refactor dir management in SpectrumElement
1 parent 0ea3419 commit 55fcc7c

File tree

2 files changed

+7
-97
lines changed

2 files changed

+7
-97
lines changed

1st-gen/tools/base/test/base.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,16 @@ describe('Base', () => {
2121
after(() => {
2222
document.dir = '';
2323
});
24-
it('sets `dir` from `document`', async () => {
24+
it('component understands `dir` from `document`', async () => {
2525
document.dir = 'rtl';
2626
const el = await fixture<DirElement>(html`
2727
<dir-element></dir-element>
2828
`);
2929

3030
await elementUpdated(el);
3131

32-
expect(el.dir).to.equal('rtl');
33-
expect(el.isLTR).to.be.false;
32+
const dir = getComputedStyle(el).direction;
33+
expect(dir).to.equal('rtl');
3434
});
3535
it('has a static VERSION property', () => {
3636
expect(DirElement.VERSION).to.equal(version);

2nd-gen/packages/core/shared/base/Base.ts

Lines changed: 4 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,6 @@
1313
import { LitElement, ReactiveElement } from 'lit';
1414

1515
import { version } from './version.js';
16-
type ThemeRoot = HTMLElement & {
17-
startManagingContentDirection: (el: HTMLElement) => void;
18-
stopManagingContentDirection: (el: HTMLElement) => void;
19-
};
2016

2117
type 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-
6129
export 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

18995
export 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

193103
if (process.env.NODE_ENV === 'development') {

0 commit comments

Comments
 (0)