Skip to content
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

Add rtl support with dynamic right & left properties #70

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
57 changes: 43 additions & 14 deletions src/components/CopilotModal.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
// @flow
import React, { Component } from 'react';
import { Animated, Easing, View, NativeModules, Modal, StatusBar, Platform } from 'react-native';
import {
Animated,
Easing,
View,
NativeModules,
Modal,
StatusBar,
Platform,
I18nManager,
} from 'react-native';
import Tooltip from './Tooltip';
import StepNumber from './StepNumber';
import styles, { MARGIN, ARROW_SIZE, STEP_NUMBER_DIAMETER, STEP_NUMBER_RADIUS } from './style';
Expand Down Expand Up @@ -36,6 +45,10 @@ type State = {

const noop = () => {};

const rtl = I18nManager.isRTL;
const start = rtl ? 'right' : 'left';
const end = rtl ? 'left' : 'right';

class CopilotModal extends Component<Props, State> {
static defaultProps = {
easing: Easing.elastic(0.7),
Expand Down Expand Up @@ -101,15 +114,31 @@ class CopilotModal extends Component<Props, State> {
obj.top -= StatusBar.currentHeight; // eslint-disable-line no-param-reassign
}

let stepNumberLeft = obj.left - STEP_NUMBER_RADIUS;
let stepNumberLeft;

const edgeCase = (stepLeft) => {
if (stepLeft > layout.width - STEP_NUMBER_DIAMETER) {
return layout.width - STEP_NUMBER_DIAMETER;
}
return stepLeft;
};

if (!rtl) {
stepNumberLeft = obj.left - STEP_NUMBER_RADIUS;

if (stepNumberLeft < 0) {
if (stepNumberLeft < 0) {
stepNumberLeft = (obj.left + obj.width) - STEP_NUMBER_RADIUS;
stepNumberLeft = edgeCase(stepNumberLeft);
}
} else {
stepNumberLeft = (obj.left + obj.width) - STEP_NUMBER_RADIUS;
if (stepNumberLeft > layout.width - STEP_NUMBER_DIAMETER) {
stepNumberLeft = layout.width - STEP_NUMBER_DIAMETER;
if (stepNumberLeft > layout.width) {
stepNumberLeft = obj.left - STEP_NUMBER_RADIUS;
stepNumberLeft = edgeCase(stepNumberLeft);
}
}


const center = {
x: obj.left + (obj.width / 2),
y: obj.top + (obj.height / 2),
Expand Down Expand Up @@ -137,15 +166,15 @@ class CopilotModal extends Component<Props, State> {
}

if (horizontalPosition === 'left') {
tooltip.right = Math.max(layout.width - (obj.left + obj.width), 0);
tooltip.right = tooltip.right === 0 ? tooltip.right + MARGIN : tooltip.right;
tooltip.maxWidth = layout.width - tooltip.right - MARGIN;
arrow.right = tooltip.right + MARGIN;
tooltip[end] = Math.max(layout.width - (obj.left + obj.width), 0);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason you changed the syntax, instead of using tooltip.end?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The bracket notation is for computed keys. tooltip.end = 1; results in { end: 1 } whereas const end = 'left'; tooltip[end] = 1 gives { left: 1 }, unless I misunderstood your question.

tooltip[end] = tooltip[end] === 0 ? tooltip[end] + MARGIN : tooltip[end];
tooltip.maxWidth = layout.width - tooltip[end] - MARGIN;
arrow[end] = tooltip[end] + MARGIN;
} else {
tooltip.left = Math.max(obj.left, 0);
tooltip.left = tooltip.left === 0 ? tooltip.left + MARGIN : tooltip.left;
tooltip.maxWidth = layout.width - tooltip.left - MARGIN;
arrow.left = tooltip.left + MARGIN;
tooltip[start] = Math.max(obj.left, 0);
tooltip[start] = tooltip.left === 0 ? tooltip[start] + MARGIN : tooltip[start];
tooltip.maxWidth = layout.width - tooltip[start] - MARGIN;
arrow[start] = tooltip[start] + MARGIN;
}

const animate = {
Expand Down Expand Up @@ -249,7 +278,7 @@ class CopilotModal extends Component<Props, State> {
style={[
styles.stepNumberContainer,
{
left: this.state.animatedValues.stepNumberLeft,
[start]: this.state.animatedValues.stepNumberLeft,
top: Animated.add(this.state.animatedValues.top, -STEP_NUMBER_RADIUS),
},
]}
Expand Down
19 changes: 12 additions & 7 deletions src/components/ViewMask.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
// @flow
import React, { Component } from 'react';

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

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


const rtl = I18nManager.isRTL;
const start = rtl ? 'right' : 'left';
const end = rtl ? 'left' : 'right';

type Props = {
size: valueXY,
position: valueXY,
Expand Down Expand Up @@ -78,23 +83,23 @@ class ViewMask extends Component<Props, State> {
style={[
styles.overlayRectangle,
{
right: leftOverlayRight,
[end]: leftOverlayRight,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this change of syntax, again? How do the brackets help?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@RichardLitt
This syntax reaches the variable instead of the stable key, so the 'end' variable may switch depending on the LTR or RTL orientations.

}]}
/>
<Animated.View
style={[
styles.overlayRectangle,
{
left: rightOverlayLeft,
[start]: rightOverlayLeft,
}]}
/>
<Animated.View
style={[
styles.overlayRectangle,
{
top: bottomOverlayTopBoundary,
left: verticalOverlayLeftBoundary,
right: verticalOverlayRightBoundary,
[start]: verticalOverlayLeftBoundary,
[end]: verticalOverlayRightBoundary,
},
]}
/>
Expand All @@ -103,8 +108,8 @@ class ViewMask extends Component<Props, State> {
styles.overlayRectangle,
{
bottom: topOverlayBottomBoundary,
left: verticalOverlayLeftBoundary,
right: verticalOverlayRightBoundary,
[start]: verticalOverlayLeftBoundary,
[end]: verticalOverlayRightBoundary,
},
]}
/>
Expand Down