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 support for enter and space on amp-selector #9083

Merged
merged 8 commits into from
May 4, 2017

Conversation

kmh287
Copy link
Contributor

@kmh287 kmh287 commented May 2, 2017

Adds support for enter and space on amp-selector.

  • Enter and space should now select the focused option
  • This should work independent of the keyboard-select-mode attribute

Also fixes a couple of bugs with arrow key navigation.

  • Selecting an option should not reset the focus on a multi-selector
  • Focus should move to selected option if user selects an option with a tap/click

Partial for #8810

/to @aghassemi @camelburrito

Copy link
Contributor

@camelburrito camelburrito left a comment

Choose a reason for hiding this comment

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

Minor things , overall looks good.

@@ -106,10 +106,7 @@ export class AmpSelector extends AMP.BaseElement {
this.init_();
if (!this.isDisabled_) {
this.element.addEventListener('click', this.clickHandler_.bind(this));
if (this.kbSelectMode_ != KEYBOARD_SELECT_MODES.NONE) {
Copy link
Contributor

Choose a reason for hiding this comment

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

why are we removing this?

Copy link
Contributor

Choose a reason for hiding this comment

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

we definitely dont need this on mobile devices, seems to be unnecessary when some one does not set keyboard option

Copy link
Contributor Author

@kmh287 kmh287 May 2, 2017

Choose a reason for hiding this comment

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

Users who use a keyboard as a primary input device, on or off mobile, should be able to select an amp-selector option regardless of the keyboard-select-mode attribute.

Copy link
Contributor

Choose a reason for hiding this comment

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

the viewport/platform i think has an api to figureout if keyboard is available, in-fact it uses that to add css classes to the body.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not seeing that. Could you link to it?

Copy link
Contributor

@aghassemi aghassemi May 4, 2017

Choose a reason for hiding this comment

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

touch does not mean !keyboard. Folks with dexterity use physical keyboard on their smartphones. We should always assume a physical keyboard exists. Adding a listener that may not be called is not something to optimize for anyway. It should have ~0 perf impact.

Copy link
Contributor

Choose a reason for hiding this comment

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

@aghassemi - yes i do understand that, AMP has a way to detect if a device has a keyboard attached or not
@kmh287 - search for amp-mode-keyboard-active and you should be able to track it down.

Copy link
Contributor Author

@kmh287 kmh287 May 4, 2017

Choose a reason for hiding this comment

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

I believe that's for when the keyboard is presently in use i.e. user is typing, not for when there's a keyboard connected.

* @private
*/
updateFocus_() {
updateFocus_(opt_focusOn) {
Copy link
Contributor

Choose a reason for hiding this comment

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

optFocusEl

Copy link
Contributor Author

Choose a reason for hiding this comment

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

sure

initialFocusedElement = this.options_[0];
} else {
initialFocusedElement = this.selectedOptions_[0] || this.options_[0];
let focusElement = opt_focusOn;
Copy link
Contributor

Choose a reason for hiding this comment

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

why not use the param variable?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I would rather not redefine the parameter.

selectionKeyDownHandler_(event) {
const keyCode = event.keyCode;
if (keyCode == Keycodes.SPACE || keyCode == Keycodes.ENTER) {
event.preventDefault();
Copy link
Contributor

Choose a reason for hiding this comment

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

move preventDefault inside the if (this.options_.includes(event.target)) otherwise actionable elements inside option can be broken ( e.g. <div option> <button> </div>)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

}
}

navigationKeyDownHandler_(event) {
Copy link
Contributor

Choose a reason for hiding this comment

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

jsdoc here and below

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

Copy link
Contributor

@aghassemi aghassemi left a comment

Choose a reason for hiding this comment

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

awesome. now with multi-select and keyboard-select-mode='focus' one can arrow through options and use SPACE to toggle them on/off. Exactly how it should work. I wish he had integration tests for amp-selector to constantly verify these end-to-end. May not be too much work if you find free time sometime.

@kmh287
Copy link
Contributor Author

kmh287 commented May 3, 2017

I can definitely take a look in the near-future. 😄

@kmh287
Copy link
Contributor Author

kmh287 commented May 4, 2017

@camelburrito Let me know if you have any further comments.

@kmh287 kmh287 merged commit f8f8f5b into ampproject:master May 4, 2017
@kmh287
Copy link
Contributor Author

kmh287 commented May 4, 2017

Thanks!

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