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

Implements scrollTo, focus and blur standard actions #10398

Merged
merged 6 commits into from Jul 18, 2017

Conversation

wassgha
Copy link
Contributor

@wassgha wassgha commented Jul 13, 2017

This adds an action to dynamically focus and scroll to elements as detailed in #7626

Changes

  • Added position parameter to animateScrollIntoView in viewport-impl specifying the final position of the element in the viewport after the scroll animation is over
  • Implemented scrollTo action on all elements with configurable duration of animation and position inside the viewport
  • Implemented focus action on all elements
  • Implemented blur action on all elements
  • Added example that implements the changes
  • Made changes to standard action spec
  • Added unit tests

Closes #7626

@wassgha wassgha requested a review from aghassemi July 13, 2017 02:34
@wassgha wassgha force-pushed the git branch 4 times, most recently from 2a47957 to f4efc1d Compare July 13, 2017 05:13
@wassgha wassgha changed the title Implements scrollTo and blur standard actions Implements scrollTo, focus and blur standard actions Jul 13, 2017
@wassgha wassgha force-pushed the git branch 4 times, most recently from 7e8af82 to 916bd58 Compare July 13, 2017 23:13
@wassgha wassgha added this to the Fixit Week EOQ2 milestone Jul 13, 2017
@wassgha
Copy link
Contributor Author

wassgha commented Jul 13, 2017

Ready for review @aghassemi

@wassgha wassgha force-pushed the git branch 2 times, most recently from e8c9cb4 to 0ef99ef Compare July 14, 2017 01:23
<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,
Copy link
Contributor

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

<td>Makes the target element gain focus.</td>
</tr>
<tr>
<td>`blur`</td>
Copy link
Contributor

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?

const permittedPosVals = ['top','bottom','center'];
const pos = invocation.args
&& invocation.args['position']
&& (permittedPosVals.indexOf(invocation.args['position']) + 1) ?
Copy link
Contributor

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

invocation.args['duration'] : 500;

// Position in the viewport at the end
const permittedPosVals = ['top','bottom','center'];
Copy link
Contributor

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.

const duration = invocation.args
&& invocation.args['duration']
&& invocation.args['duration'] >= 0 ?
invocation.args['duration'] : 500;
Copy link
Contributor

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)

@@ -89,7 +89,7 @@ For example, the following is possible in AMP.
<th>Description</th>
</tr>
<tr>
<td>tap</td>
<td>`tap`</td>
Copy link

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>.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

My bad! Thanks :)

Copy link

@ghost ghost left a 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

<div class="spacer">
</div>

<h4>Subscribe to our weekly Newsletter Form</h4>
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 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...

<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>
Copy link
Contributor

Choose a reason for hiding this comment

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

remove

'scrollTo', this.handleScrollTo.bind(this));
actionService.addGlobalMethodHandler(
'focus', this.handleFocus.bind(this));
actionService.addGlobalMethodHandler('blur', this.handleBlur.bind(this));
Copy link
Contributor

Choose a reason for hiding this comment

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

remove

* Handles the `blur` action where given an element, we make it lose its focus
* @param {!./action-impl.ActionInvocation} invocation
*/
handleBlur(invocation) {
Copy link
Contributor

Choose a reason for hiding this comment

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

remove

@@ -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 => {
Copy link
Contributor

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)

@@ -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>
Copy link
Contributor

Choose a reason for hiding this comment

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

remove

// 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
Copy link
Contributor

@aghassemi aghassemi Jul 18, 2017

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)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Got it!

@aghassemi aghassemi merged commit e589398 into ampproject:master Jul 18, 2017
@wassgha wassgha deleted the git branch July 18, 2017 16:40
@pulitz
Copy link

pulitz commented Jul 27, 2017

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]]

@wassgha
Copy link
Contributor Author

wassgha commented Jul 27, 2017

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)
@aghassemi can you test on your end? It works for me.

@pulitz
Copy link

pulitz commented Jul 27, 2017

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.

@aghassemi
Copy link
Contributor

@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.

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

Successfully merging this pull request may close these issues.

None yet

5 participants