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

Adds support for RTL navigation in amp-story pages ✨ #16381

Merged
merged 5 commits into from
Jun 29, 2018

Conversation

Enriqe
Copy link
Contributor

@Enriqe Enriqe commented Jun 26, 2018

Part of #11647

Flips the navigational click areas in an amp-story page when dir="rtl" is used in the document.

This means the next screen area that is positioned on the right for LTR languages will be on the left, and the previous screen area will be on the right. Same sizes of these areas will be taken into account.

A follow up PR will address the education overlay so that it matches these areas in RTL.

Copy link
Contributor

@newmuis newmuis left a comment

Choose a reason for hiding this comment

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

Overall, this leaves a lot of duplicated code. Is there some way where we can, for example, control the ratio between the two regions and also the action of each of the two regions?

For example:

const sections = {
  left: {
    width: isLtr ? 1/4 : 3/4,
    direction: isLtr ? TapNavigationDirection.PREVIOUS : TapNavigationDirection.NEXT,
  },
  right: {
    width: isLtr ? 3/4 : 1/4,
    direction: isLtr ? TapNavigationDirection.NEXT : TapNavigationDirection.PREVIOUS,
  },
};
const tapDirection = this.getTapDirection(clickPositionX, sections);

@Enriqe, @gmajoulet WDYT?

/**
* Checks if click is inside the previous screen area.
* @param {number} clickPositionX
* @param {DOMRect} pageRect
Copy link
Contributor

Choose a reason for hiding this comment

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

Explicitly use ? for nullable or ! for non-nullable.

let previousScreenAreaMin;
let previousScreenAreaMax;

if (isRTL(this.element_.ownerDocument)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Eventually you'll want to have a RTL_STATE in the store, so you can avoid duplicating this. It'll also make it easier to propagate the dir="rtl" attributes in the shadow roots.

@gmajoulet
Copy link
Contributor

+1 to @newmuis comments, that structure should allow you to make the code a lot easier to read.

TapNavigationDirection.NEXT : TapNavigationDirection.PREVIOUS,
},
right: {
width: isRtl ? smallArea : bigArea,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

For the moment this is not used, but thought i'd leave it in case we need it in the future.

Copy link
Contributor

@gmajoulet gmajoulet left a comment

Choose a reason for hiding this comment

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

A few nits

@@ -64,6 +65,7 @@ export const StateProperty = {
DESKTOP_STATE: 'desktopstate',
HAS_AUDIO_STATE: 'hasaudiostate',
INFO_DIALOG_STATE: 'infodialogstate',
IS_RTL_STATE: 'isrtlstate',
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: RTL_STATE would be shorter and provide the same information

@@ -580,6 +581,12 @@ export class AmpStory extends AMP.BaseElement {

this.initializeBookend_();

const {document} = this.win;
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: you don't need this extra const, please use directly this.win.document

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You're right, I thought we preferred destructuring object like in #15204, but it's not the case here.

const {document} = this.win;

if (isRTL(document)) {
this.storeService_.dispatch(Action.TOGGLE_RTL, true);
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we should try to set this state as soon as possible in the code execution, like we do for desktop.

Right now, the RTL state is set after some components are initialized, which means you'd have to watch for RTL_STATE updates from every component, to later set the dir="rtl" attributes.
This can be avoided if you can find a way to instantiate the state right away with the proper value, or set it first thing in the buildCallback of amp-story.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good idea

this.next_();
break;
const keyDirection = e.keyCode;
const isRtl = this.storeService_.get(StateProperty.IS_RTL_STATE);
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: to further avoid confusions between isRTL and isRtl, maybe RTLState?

Copy link
Contributor

Choose a reason for hiding this comment

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

Technically, I think it's supposed to be Rtl per the style guide (not that we strictly follow the style guide), since only the first letter of acronyms get capitalized.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Per styleguide I will stick to RtlState

const smallArea = (1 - NEXT_SCREEN_AREA_RATIO) * offsetWidth;
const isRtl = this.storeService_.get(StateProperty.IS_RTL_STATE);

const sections = {
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there any way we can avoid creating a new instance of this object on every user interaction?

Maybe we could store the width as a ratio, and then do the operation in the getTapDirection_ method.
ie:

left: {
  width: RTLState ? 0.75 : 0.25,
  direction: ...,
}

this.next_();
} else if ((keyDirection == KeyCodes.RIGHT_ARROW && isRtl) ||
(keyDirection == KeyCodes.LEFT_ARROW && !isRtl)) {
this.previous_();
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe the original version would be easier to read:

const RTLState = this.storeService_.get(StateProperty.RTL_STATE);

switch (e.keyCode) {
  case KeyCodes.LEFT_ARROW:
    RTLState ? this.next_() : this.previous_();
    break;
  case KeyCodes.RIGHT_ARROW:
    RTLState ? this.previous_() : this.next_();
    break;
}

clickEventX: event.pageX,
};

const sections = {
Copy link
Contributor

Choose a reason for hiding this comment

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

This object could be instantiated only once, from the constructor

const offsetLeft = ('x' in elRect) ? elRect.x : elRect.left;
const offsetWidth = elRect.width;
const offsetLeft = ('x' in pageRect) ? pageRect.x : pageRect.left;
const RtlState = this.storeService_.get(StateProperty.RTL_STATE);
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: lowercase 'r'

@codecov-io
Copy link

Codecov Report

Merging #16381 into master will increase coverage by 0.02%.
The diff coverage is 31.57%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master   #16381      +/-   ##
==========================================
+ Coverage   77.14%   77.17%   +0.02%     
==========================================
  Files         547      547              
  Lines       39934    39942       +8     
==========================================
+ Hits        30807    30824      +17     
+ Misses       9127     9118       -9
Impacted Files Coverage Δ
...xtensions/amp-story/1.0/amp-story-store-service.js 83.33% <0%> (-1.78%) ⬇️
extensions/amp-story/1.0/amp-story.js 61.74% <20%> (-0.56%) ⬇️
extensions/amp-story/1.0/page-advancement.js 29.74% <38.46%> (+2.47%) ⬆️
...p-story/1.0/amp-story-unsupported-browser-layer.js 71.42% <0%> (-9.53%) ⬇️
src/base-element.js 91.17% <0%> (-0.99%) ⬇️
src/custom-element.js 94.23% <0%> (-0.2%) ⬇️
extensions/amp-next-page/0.1/next-page-service.js 80% <0%> (+2.42%) ⬆️
src/curve.js 82.05% <0%> (+2.56%) ⬆️
extensions/amp-dailymotion/0.1/amp-dailymotion.js 45.45% <0%> (+2.72%) ⬆️
... and 1 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 5fefe7f...65ce3e2. Read the comment docs.

@Enriqe
Copy link
Contributor Author

Enriqe commented Jun 29, 2018

ready to merge @newmuis

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants