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
Implements scrollTo, focus and blur standard actions #10398
Conversation
2a47957
to
f4efc1d
Compare
7e8af82
to
916bd58
Compare
Ready for review @aghassemi |
e8c9cb4
to
0ef99ef
Compare
spec/amp-actions-and-events.md
Outdated
<td>Toggles the visibility of the target element.</td> | ||
</tr> | ||
<tr> | ||
<td>`scrollTo(duration=INTEGER, position=STRING)`</td> | ||
<td>Scrolls the element into view with a smooth animation. If defined, |
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.
mentioned the defaults
for these params.
/cc @bpaduch
spec/amp-actions-and-events.md
Outdated
<td>Makes the target element gain focus.</td> | ||
</tr> | ||
<tr> | ||
<td>`blur`</td> |
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.
blur
is not good to expose because it breaks A11Y. It moves the focus back to body
which is almost never a desired behaviour. If author wants something to lose focus, they need to decide what makes sense to get the focus and use focus
action instead.
Was there a particular use-case you had in mind for this?
src/service/standard-actions-impl.js
Outdated
const permittedPosVals = ['top','bottom','center']; | ||
const pos = invocation.args | ||
&& invocation.args['position'] | ||
&& (permittedPosVals.indexOf(invocation.args['position']) + 1) ? |
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.
we polyfill array.includes, so you can use it here
src/service/standard-actions-impl.js
Outdated
invocation.args['duration'] : 500; | ||
|
||
// Position in the viewport at the end | ||
const permittedPosVals = ['top','bottom','center']; |
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.
move outside as a module level constant.
src/service/standard-actions-impl.js
Outdated
const duration = invocation.args | ||
&& invocation.args['duration'] | ||
&& invocation.args['duration'] >= 0 ? | ||
invocation.args['duration'] : 500; |
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.
please create an issue to make the default be based on some formula that account for the distance between source and target. Ideally whatever Chrome is using for their native implementation (https://bugs.chromium.org/p/chromium/issues/detail?id=243871)
spec/amp-actions-and-events.md
Outdated
@@ -89,7 +89,7 @@ For example, the following is possible in AMP. | |||
<th>Description</th> | |||
</tr> | |||
<tr> | |||
<td>tap</td> | |||
<td>`tap`</td> |
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.
Did you preview this file? I ask because you'll notice that the back ticks don't work as code formatting if it's in HTML code. Instead, please replace all back ticks with <code></code>
.
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.
My bad! Thanks :)
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.
Approval of .md file changes
examples/standard-actions.amp.html
Outdated
<div class="spacer"> | ||
</div> | ||
|
||
<h4>Subscribe to our weekly Newsletter Form</h4> |
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 this is too much and makes the example cluttered, let's just do a simple <input type="text" id="input-element">
for focus and a simple div
for scrollTo
, no need for unrelated stuff like amp-form
, CLIENT_ID
, etc...
examples/standard-actions.amp.html
Outdated
<button on="tap:form.scrollTo('position' = 'center')">ScrollTo Center</button> | ||
<button on="tap:form.scrollTo('duration' = 5000)">ScrollTo Slowly</button> | ||
<button on="tap:name1.focus">Focus</button> | ||
<button on="tap:name1.blur">Lose Focus</button> |
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.
remove
src/service/standard-actions-impl.js
Outdated
'scrollTo', this.handleScrollTo.bind(this)); | ||
actionService.addGlobalMethodHandler( | ||
'focus', this.handleFocus.bind(this)); | ||
actionService.addGlobalMethodHandler('blur', this.handleBlur.bind(this)); |
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.
remove
src/service/standard-actions-impl.js
Outdated
* Handles the `blur` action where given an element, we make it lose its focus | ||
* @param {!./action-impl.ActionInvocation} invocation | ||
*/ | ||
handleBlur(invocation) { |
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.
remove
src/service/viewport-impl.js
Outdated
@@ -430,8 +447,8 @@ export class Viewport { | |||
// TODO(erwinm): the duration should not be a constant and should | |||
// be done in steps for better transition experience when things | |||
// are closer vs farther. | |||
return Animation.animate(this.ampdoc.getRootNode(), pos => { | |||
this.binding_.setScrollTop(interpolate(pos)); | |||
return Animation.animate(this.ampdoc.getRootNode(), position => { |
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.
update the comment above to point to #10463 (assign to me)
examples/standard-actions.amp.html
Outdated
@@ -7,6 +7,7 @@ | |||
<meta name="viewport" content="width=device-width,minimum-scale=1,initial-scale=1"> | |||
<style amp-boilerplate>body{-webkit-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-moz-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-ms-animation:-amp-start 8s steps(1,end) 0s 1 normal both;animation:-amp-start 8s steps(1,end) 0s 1 normal both}@-webkit-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-moz-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-ms-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-o-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}</style><noscript><style amp-boilerplate>body{-webkit-animation:none;-moz-animation:none;-ms-animation:none;animation:none}</style></noscript> | |||
<script async src="https://cdn.ampproject.org/v0.js"></script> | |||
<script async custom-element="amp-form" src="https://cdn.ampproject.org/v0/amp-form-0.1.js"></script> |
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.
remove
src/service/viewport-impl.js
Outdated
// are closer vs farther. | ||
return Animation.animate(this.ampdoc.getRootNode(), pos => { | ||
this.binding_.setScrollTop(interpolate(pos)); | ||
// TODO(aghassemi): the duration should not be a constant and should |
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.
TODO(aghassemi, #10463)
(Dima has a parser that finds TODO issues of this format)
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.
Got it!
Example does not work. Is anything special needed? Action Error: Target element does not support provided action in [tap:normal-element2.scrollTo] on [[object HTMLDivElement]] |
That is odd. @pulitz I would make sure that your build is up to date and that the example page isn't being served production AMP (this feature is not in production yet) |
Oh. Sorry, I guess it was my mistake. I thought this feature was in production seeing that it was already merged. Hopefully it really does go on production soon. |
@pulitz sorry currently it is in Dev Channel, you can try it by enabling Dev Channel (https://cdn.ampproject.org/experiments.html) will be in production next week. |
This adds an action to dynamically focus and scroll to elements as detailed in #7626
Changes
animateScrollIntoView
inviewport-impl
specifying the final position of the element in the viewport after the scroll animation is overscrollTo
action on all elements with configurableduration
of animation andposition
inside the viewportfocus
action on all elementsblur
action on all elementsCloses #7626