Skip to content

fix rtl: flip rtl when the document dir #1046

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion src/components/icon/icon.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { getName, getUrl, inheritAttributes } from './utils';
shadow: true,
})
export class Icon {
private documentDirectionObserver?: MutationObserver;
private io?: IntersectionObserver;
private iconName: string | null = null;
private inheritedAttributes: { [k: string]: any } = {};
Expand All @@ -18,6 +19,7 @@ export class Icon {
@State() private svgContent?: string;
@State() private isVisible = false;
@State() private ariaLabel?: string;
@State() private direction?: string;

/**
* The mode determines which platform styles to use.
Expand Down Expand Up @@ -93,13 +95,36 @@ export class Icon {
this.isVisible = true;
this.loadIcon();
});
this.observeDocumentDirection(() => {
this.direction = (this.el.ownerDocument as Document).dir;
});
}

disconnectedCallback() {
if (this.io) {
this.io.disconnect();
this.io = undefined;
}
if (this.documentDirectionObserver) {
this.documentDirectionObserver.disconnect();
this.documentDirectionObserver = undefined;
}
}

private observeDocumentDirection(cb: () => void) {
if (Build.isBrowser && typeof window !== 'undefined' && (window as any).MutationObserver) {
this.documentDirectionObserver = new MutationObserver(() => {
this.direction = (this.el.ownerDocument as Document).dir;
});
this.documentDirectionObserver.observe((this.el.ownerDocument as Document).documentElement, {
attributes: true,
attributeFilter: ['dir'],
});
} else {
// browser doesn't support MutationObserver
// so just fallback to always show it
cb();
}
}

private waitUntilVisible(el: HTMLElement, rootMargin: string, cb: () => void) {
Expand Down Expand Up @@ -181,7 +206,7 @@ export class Icon {
[mode]: true,
...createColorClasses(this.color),
[`icon-${this.size}`]: !!this.size,
'flip-rtl': !!flipRtl && (this.el.ownerDocument as Document).dir === 'rtl',
'flip-rtl': !!flipRtl && this.direction === 'rtl',
}}
{...inheritedAttributes}
>
Expand Down