Skip to content

Commit 1fbf8fd

Browse files
committed
Add rtl support with dynamic right & left properties
1 parent 46c205e commit 1fbf8fd

File tree

2 files changed

+51
-17
lines changed

2 files changed

+51
-17
lines changed

src/components/CopilotModal.js

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
// @flow
22
import React, { Component } from 'react';
3-
import { Animated, Easing, View, NativeModules, Modal, StatusBar, Platform } from 'react-native';
3+
import {
4+
Animated,
5+
Easing,
6+
View,
7+
NativeModules,
8+
Modal,
9+
StatusBar,
10+
Platform,
11+
I18nManager,
12+
} from 'react-native';
413
import Tooltip from './Tooltip';
514
import StepNumber from './StepNumber';
615
import styles, { MARGIN, ARROW_SIZE, STEP_NUMBER_DIAMETER, STEP_NUMBER_RADIUS } from './style';
@@ -36,6 +45,10 @@ type State = {
3645

3746
const noop = () => {};
3847

48+
const rtl = I18nManager.isRTL;
49+
const start = rtl ? 'right' : 'left';
50+
const end = rtl ? 'left' : 'right';
51+
3952
class CopilotModal extends Component<Props, State> {
4053
static defaultProps = {
4154
easing: Easing.elastic(0.7),
@@ -101,15 +114,31 @@ class CopilotModal extends Component<Props, State> {
101114
obj.top -= StatusBar.currentHeight; // eslint-disable-line no-param-reassign
102115
}
103116

104-
let stepNumberLeft = obj.left - STEP_NUMBER_RADIUS;
117+
let stepNumberLeft;
118+
119+
const edgeCase = (stepLeft) => {
120+
if (stepLeft > layout.width - STEP_NUMBER_DIAMETER) {
121+
return layout.width - STEP_NUMBER_DIAMETER;
122+
}
123+
return stepLeft;
124+
};
125+
126+
if (!rtl) {
127+
stepNumberLeft = obj.left - STEP_NUMBER_RADIUS;
105128

106-
if (stepNumberLeft < 0) {
129+
if (stepNumberLeft < 0) {
130+
stepNumberLeft = (obj.left + obj.width) - STEP_NUMBER_RADIUS;
131+
stepNumberLeft = edgeCase(stepNumberLeft);
132+
}
133+
} else {
107134
stepNumberLeft = (obj.left + obj.width) - STEP_NUMBER_RADIUS;
108-
if (stepNumberLeft > layout.width - STEP_NUMBER_DIAMETER) {
109-
stepNumberLeft = layout.width - STEP_NUMBER_DIAMETER;
135+
if (stepNumberLeft > layout.width) {
136+
stepNumberLeft = obj.left - STEP_NUMBER_RADIUS;
137+
stepNumberLeft = edgeCase(stepNumberLeft);
110138
}
111139
}
112140

141+
113142
const center = {
114143
x: obj.left + (obj.width / 2),
115144
y: obj.top + (obj.height / 2),
@@ -137,15 +166,15 @@ class CopilotModal extends Component<Props, State> {
137166
}
138167

139168
if (horizontalPosition === 'left') {
140-
tooltip.right = Math.max(layout.width - (obj.left + obj.width), 0);
169+
tooltip[end] = Math.max(layout.width - (obj.left + obj.width), 0);
141170
tooltip.right = tooltip.right === 0 ? tooltip.right + MARGIN : tooltip.right;
142171
tooltip.maxWidth = layout.width - tooltip.right - MARGIN;
143-
arrow.right = tooltip.right + MARGIN;
172+
arrow[end] = tooltip[end] + MARGIN;
144173
} else {
145-
tooltip.left = Math.max(obj.left, 0);
174+
tooltip[start] = Math.max(obj.left, 0);
146175
tooltip.left = tooltip.left === 0 ? tooltip.left + MARGIN : tooltip.left;
147176
tooltip.maxWidth = layout.width - tooltip.left - MARGIN;
148-
arrow.left = tooltip.left + MARGIN;
177+
arrow[start] = tooltip[start] + MARGIN;
149178
}
150179

151180
const animate = {
@@ -249,7 +278,7 @@ class CopilotModal extends Component<Props, State> {
249278
style={[
250279
styles.stepNumberContainer,
251280
{
252-
left: this.state.animatedValues.stepNumberLeft,
281+
[start]: this.state.animatedValues.stepNumberLeft,
253282
top: Animated.add(this.state.animatedValues.top, -STEP_NUMBER_RADIUS),
254283
},
255284
]}

src/components/ViewMask.js

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
// @flow
22
import React, { Component } from 'react';
33

4-
import { View, Animated } from 'react-native';
4+
import { View, Animated, I18nManager } from 'react-native';
55
import styles from './style';
66

77
import type { valueXY } from '../types';
88

9+
10+
const rtl = I18nManager.isRTL;
11+
const start = rtl ? 'right' : 'left';
12+
const end = rtl ? 'left' : 'right';
13+
914
type Props = {
1015
size: valueXY,
1116
position: valueXY,
@@ -78,23 +83,23 @@ class ViewMask extends Component<Props, State> {
7883
style={[
7984
styles.overlayRectangle,
8085
{
81-
right: leftOverlayRight,
86+
[end]: leftOverlayRight,
8287
}]}
8388
/>
8489
<Animated.View
8590
style={[
8691
styles.overlayRectangle,
8792
{
88-
left: rightOverlayLeft,
93+
[start]: rightOverlayLeft,
8994
}]}
9095
/>
9196
<Animated.View
9297
style={[
9398
styles.overlayRectangle,
9499
{
95100
top: bottomOverlayTopBoundary,
96-
left: verticalOverlayLeftBoundary,
97-
right: verticalOverlayRightBoundary,
101+
[start]: verticalOverlayLeftBoundary,
102+
[end]: verticalOverlayRightBoundary,
98103
},
99104
]}
100105
/>
@@ -103,8 +108,8 @@ class ViewMask extends Component<Props, State> {
103108
styles.overlayRectangle,
104109
{
105110
bottom: topOverlayBottomBoundary,
106-
left: verticalOverlayLeftBoundary,
107-
right: verticalOverlayRightBoundary,
111+
[start]: verticalOverlayLeftBoundary,
112+
[end]: verticalOverlayRightBoundary,
108113
},
109114
]}
110115
/>

0 commit comments

Comments
 (0)