-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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
Conversation
There was a problem hiding this 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 |
There was a problem hiding this comment.
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)) { |
There was a problem hiding this comment.
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.
+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, |
There was a problem hiding this comment.
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.
There was a problem hiding this 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', |
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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
.
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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
?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 = { |
There was a problem hiding this comment.
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_(); |
There was a problem hiding this comment.
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 = { |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: lowercase 'r'
Codecov Report
@@ 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
Continue to review full report at Codecov.
|
ready to merge @newmuis |
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.