Skip to content

Commit c37b691

Browse files
fix(): flip-rtl works if dir="rtl" is set on icon directly (#1142)
1 parent 0e3ba99 commit c37b691

File tree

6 files changed

+24
-4
lines changed

6 files changed

+24
-4
lines changed

src/components/icon/icon.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Build, Component, Element, Host, Prop, State, Watch, h } from '@stencil/core';
22
import { getSvgContent, ioniconContent } from './request';
3-
import { getName, getUrl, inheritAttributes } from './utils';
3+
import { getName, getUrl, inheritAttributes, isRTL } from './utils';
44

55
@Component({
66
tag: 'ion-icon',
@@ -158,7 +158,7 @@ export class Icon {
158158
}
159159

160160
render() {
161-
const { iconName, ariaLabel, inheritedAttributes } = this;
161+
const { iconName, ariaLabel, el, inheritedAttributes } = this;
162162
const mode = this.mode || 'md';
163163
const flipRtl =
164164
this.flipRtl ||
@@ -181,7 +181,7 @@ export class Icon {
181181
[mode]: true,
182182
...createColorClasses(this.color),
183183
[`icon-${this.size}`]: !!this.size,
184-
'flip-rtl': !!flipRtl && (this.el.ownerDocument as Document).dir === 'rtl',
184+
'flip-rtl': !!flipRtl && isRTL(el),
185185
}}
186186
{...inheritedAttributes}
187187
>
15.2 KB
Loading
4.33 KB
Loading
12 KB
Loading

src/components/icon/utils.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,4 +139,18 @@ export const inheritAttributes = (el: HTMLElement, attributes: string[] = []) =>
139139
});
140140

141141
return attributeObject;
142-
}
142+
}
143+
144+
/**
145+
* Returns `true` if the document or host element
146+
* has a `dir` set to `rtl`. The host value will always
147+
* take priority over the root document value.
148+
*/
149+
export const isRTL = (hostEl?: Pick<HTMLElement, 'dir'>) => {
150+
if (hostEl) {
151+
if (hostEl.dir !== '') {
152+
return hostEl.dir.toLowerCase() === 'rtl';
153+
}
154+
}
155+
return document?.dir.toLowerCase() === 'rtl';
156+
};

src/index.html

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,12 @@ <h2>Un-flip: chevrons</h2>
124124
<ion-icon name="chevron-down" flip-rtl="false"></ion-icon>
125125
<ion-icon name="chevron-back" flip-rtl="false"></ion-icon>
126126

127+
<h2>Auto Flip, RTL on components</h2>
128+
<ion-icon name="arrow-up" dir="rtl" flip-rtl></ion-icon>
129+
<ion-icon name="arrow-forward" dir="rtl" flip-rtl></ion-icon>
130+
<ion-icon name="arrow-down" dir="rtl" flip-rtl></ion-icon>
131+
<ion-icon name="arrow-back" dir="rtl" flip-rtl></ion-icon>
132+
127133
<h2>Sanitized (shouldn't show)</h2>
128134
<ion-icon src="./assets/sanitize.svg"></ion-icon>
129135

0 commit comments

Comments
 (0)