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

feat: initial keyboard shortcut support #263

Merged
merged 14 commits into from
Jul 22, 2022

Conversation

gkatsev
Copy link
Contributor

@gkatsev gkatsev commented Jul 11, 2022

Kicking off the work for #257

@vercel
Copy link

vercel bot commented Jul 11, 2022

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated
media-chrome ✅ Ready (Inspect) Visit Preview Jul 22, 2022 at 7:07PM (UTC)

src/js/media-container.js Outdated Show resolved Hide resolved
Copy link
Collaborator

@cjpillsbury cjpillsbury left a comment

Choose a reason for hiding this comment

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

a couple of asks, but this looks close to done for a good mvp spike.

src/js/media-container.js Outdated Show resolved Hide resolved
src/js/media-container.js Outdated Show resolved Hide resolved
src/js/media-container.js Outdated Show resolved Hide resolved
src/js/media-container.js Outdated Show resolved Hide resolved
src/js/media-controller.js Outdated Show resolved Hide resolved
Comment on lines +54 to +57
/**
* Media Controller should not mimic the HTMLMediaElement API.
* @see https://github.com/muxinc/media-chrome/pull/182#issuecomment-1067370339
*/
Copy link
Contributor Author

Choose a reason for hiding this comment

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

moved from the bottom of the class here, which seems more appropriate.

Copy link
Contributor

Choose a reason for hiding this comment

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

src/js/media-controller.js Outdated Show resolved Hide resolved
src/js/media-container.js Outdated Show resolved Hide resolved
Copy link
Collaborator

@cjpillsbury cjpillsbury left a comment

Choose a reason for hiding this comment

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

One docs chore and a handful of non-blocking callouts. Otherwise, 👌. Did basic smoke testing across available browsers.

* @see https://github.com/muxinc/media-chrome/pull/182#issuecomment-1067370339
*/
keyboardShortcutHandler(e) {
if (this.contains(e.target) || this.shadowRoot.contains(e.target)) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

question(non-blocking): Do we need these checks?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Maybe not, since I guess the event bubbling should guarantee that it's coming from a child element?

Copy link
Collaborator

Choose a reason for hiding this comment

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

yup that's what I was thinking. We may also get into some weirdness with contains checks for nested web component use cases.

// keysUsed is either an attribute or a property.
// The attribute is a DOM array and the property is a JS array
// In the attribute Space represents the space key and gets convered to ' '
const keysUsed = (e.target.getAttribute('keysused')?.split(' ') ?? e.target?.keysUsed ?? [])
Copy link
Collaborator

Choose a reason for hiding this comment

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

thought(non-blocking): As a followup, we'll need to figure out how this should work for non-nested components based on the current architecture (See code/architecture for associatedElements and how event (re)propagation works).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yup, this got called out in the discussion #257

return;
}

if (!this.hasAttribute('nohotkeys')) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

suggestion(non-blocking): Instead of continuing to listen to irrelevant keyboard presses when "nohotkeys" is set, we should probably:

  1. add "nohotkeys" to observedAttributes.
  2. add/remove event handlers for "keyup"/"keydown" based on attributeChangedCallback
    1. This entails moving the event callbacks to methods instead of inline.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

updated

| ⬅ | Seek back 10s |
| ➡ | Seek forward 10s |

If you are implementing an interactive element that uses any of these keys, you can stopPropagation in your `keyup` handler. Alternatively, you can add a `keysUsed` property on the element or a `keysused` attribute.
Copy link
Collaborator

Choose a reason for hiding this comment

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

chore(blocking):

  1. Discuss and link to https://developer.mozilla.org/en-US/docs/Web/API/UI_Events/Keyboard_event_key_values
  2. Callout the "Space" keyword mapping

Copy link
Contributor Author

Choose a reason for hiding this comment

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

updated

If you are implementing an interactive element that uses any of these keys, you can stopPropagation in your `keyup` handler. Alternatively, you can add a `keysUsed` property on the element or a `keysused` attribute. The values are those that match the `key` property on the KeyboardEvent. You can find a list of those values [on mdn](https://developer.mozilla.org/en-US/docs/Web/API/UI_Events/Keyboard_event_key_values). Additionally, since the DOM list can't have the Space key represented as `" "`, we will accept `Space` as an alternative name for it.
Example (`keysused` attribute):
```html
<media-time-range keysused="ArrowLeft ArrowRight Space"></media-time-range>
Copy link
Collaborator

Choose a reason for hiding this comment

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

😎

Copy link
Collaborator

@cjpillsbury cjpillsbury left a comment

Choose a reason for hiding this comment

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

pre-approving with the minor callout and recommend to remove the contains et al. check.

#keyUpHandler(e) {
const { key } = e;
if (!ButtonPressedKeys.includes(key)) {
this.removeEventListener('keyup', keyUpHandler);
Copy link
Collaborator

Choose a reason for hiding this comment

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

issue(blocker): missed this.#keyUpHandler refactor.

Example (hotkeys disabled):

```html
<media-controller nohotkeys>
Copy link
Contributor

Choose a reason for hiding this comment

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

👍

Copy link
Contributor

@luwes luwes left a comment

Choose a reason for hiding this comment

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

LGTM! 🎉

@gkatsev gkatsev merged commit 7542ce7 into muxinc:main Jul 22, 2022
@gkatsev gkatsev deleted the keyboard-shortcuts-poc branch July 22, 2022 20:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants