Skip to content

Commit

Permalink
feat: observe document.documentElement for dir value
Browse files Browse the repository at this point in the history
  • Loading branch information
Westbrook Johnson authored and Westbrook committed Aug 29, 2020
1 parent 1742d07 commit da84a9a
Showing 1 changed file with 33 additions and 2 deletions.
35 changes: 33 additions & 2 deletions packages/base/src/Base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,34 @@ export interface SpectrumInterface {
dir: 'ltr' | 'rtl';
}

const observedForElements: Set<HTMLElement> = new Set();

const updateRTL = (): void => {
const dir =
document.documentElement.dir === 'rtl'
? document.documentElement.dir
: 'ltr';
observedForElements.forEach((el) => {
el.setAttribute('dir', dir);
});
};

const rtlObserver = new MutationObserver(updateRTL);

rtlObserver.observe(document.documentElement, {
attributes: true,
attributeFilter: ['dir'],
});

export function SpectrumMixin<T extends Constructor<UpdatingElement>>(
constructor: T
): T & Constructor<SpectrumInterface> {
class SlotTextObservingElement extends constructor {
public shadowRoot!: ShadowRoot;

/**
* @private
*/
@property({ reflect: true })
public dir: 'ltr' | 'rtl' = 'ltr';

Expand All @@ -45,10 +67,19 @@ export function SpectrumMixin<T extends Constructor<UpdatingElement>>(
}

public connectedCallback(): void {
super.connectedCallback();
if (!this.hasAttribute('dir')) {
this.dir = document.dir === 'rtl' ? document.dir : 'ltr';
this.dir =
document.documentElement.dir === 'rtl'
? document.documentElement.dir
: 'ltr';
}
super.connectedCallback();
observedForElements.add(this);
}

public disconnectedCallback(): void {
super.disconnectedCallback();
observedForElements.delete(this);
}
}
return SlotTextObservingElement;
Expand Down

0 comments on commit da84a9a

Please sign in to comment.