Skip to content

Commit 353dbc0

Browse files
authored
fix(toggle): setting dir on ion-toggle to enable rtl mode now supported (#24600)
1 parent 5dba4e5 commit 353dbc0

File tree

3 files changed

+55
-4
lines changed

3 files changed

+55
-4
lines changed
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
import { newE2EPage } from '@stencil/core/testing';
3+
4+
test('toggle: RTL', async () => {
5+
const page = await newE2EPage({
6+
url: '/src/components/toggle/test/rtl?ionic:_testing=true'
7+
});
8+
9+
const compare = await page.compareScreenshot();
10+
expect(compare).toMatchScreenshot();
11+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<!DOCTYPE html>
2+
<html lang="en" dir="ltr">
3+
4+
<head>
5+
<meta charset="UTF-8">
6+
<title>Toggle - RTL</title>
7+
<meta name="viewport"
8+
content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
9+
<link href="../../../../../css/ionic.bundle.css" rel="stylesheet">
10+
<link href="../../../../../scripts/testing/styles.css" rel="stylesheet">
11+
<script src="../../../../../scripts/testing/scripts.js"></script>
12+
<script nomodule src="../../../../../dist/ionic/ionic.js"></script>
13+
<script type="module" src="../../../../../dist/ionic/ionic.esm.js"></script>
14+
</head>
15+
16+
<body>
17+
<ion-app>
18+
19+
<ion-header>
20+
<ion-toolbar>
21+
<ion-title>Toggle - RTL</ion-title>
22+
<ion-buttons slot="primary">
23+
<ion-toggle aria-label="Toggle"></ion-toggle>
24+
</ion-buttons>
25+
</ion-toolbar>
26+
</ion-header>
27+
28+
<ion-content id="content">
29+
30+
<ion-list>
31+
<ion-item>
32+
<ion-label>Toggle</ion-label>
33+
<ion-toggle dir="rtl" slot="start" name="item-1" checked></ion-toggle>
34+
</ion-item>
35+
36+
</ion-content>
37+
38+
</ion-app>
39+
</body>
40+
41+
</html>

core/src/components/toggle/toggle.tsx

+3-4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { getIonMode } from '../../global/ionic-global';
44
import { Color, Gesture, GestureDetail, StyleEventDetail, ToggleChangeEventDetail } from '../../interface';
55
import { getAriaLabel, renderHiddenInput } from '../../utils/helpers';
66
import { hapticSelection } from '../../utils/native/haptic';
7+
import { isRTL } from '../../utils/rtl';
78
import { createColorClasses, hostContext } from '../../utils/theme';
89

910
/**
@@ -138,7 +139,7 @@ export class Toggle implements ComponentInterface {
138139
}
139140

140141
private onMove(detail: GestureDetail) {
141-
if (shouldToggle(document, this.checked, detail.deltaX, -10)) {
142+
if (shouldToggle(isRTL(this.el), this.checked, detail.deltaX, -10)) {
142143
this.checked = !this.checked;
143144
hapticSelection();
144145
}
@@ -224,9 +225,7 @@ export class Toggle implements ComponentInterface {
224225
}
225226
}
226227

227-
const shouldToggle = (doc: HTMLDocument, checked: boolean, deltaX: number, margin: number): boolean => {
228-
const isRTL = doc.dir === 'rtl';
229-
228+
const shouldToggle = (isRTL: boolean, checked: boolean, deltaX: number, margin: number): boolean => {
230229
if (checked) {
231230
return (!isRTL && (margin > deltaX)) ||
232231
(isRTL && (- margin < deltaX));

0 commit comments

Comments
 (0)