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

Focus first button when modal is opened #15

Open
jameshibbard opened this issue Jul 8, 2020 · 0 comments
Open

Focus first button when modal is opened #15

jameshibbard opened this issue Jul 8, 2020 · 0 comments

Comments

@jameshibbard
Copy link

jameshibbard commented Jul 8, 2020

Hi,

Currently, when the modal is opened it does not receive focus. This make is somewhat inaccessible for keyboard users.

It would be possible to make the first button receive focus like so:

In src/lib/Notification.vue:

<button
+  ref="notivuecationButton"
   v-for="button in buttons"
   :class="button.css"
   @click="resolve(button.value)"
>{{button.label}}</button>

In src/lib/componentMixin.js

onShowNotification(notification) {
  this.notifications.push(notification);
+ this.$nextTick(() => this.$refs.notivuecationButton[0].focus());
},

But this creates a new problem, that when the modal is shut, focus is returned to the top of the page and not wherever the user was previously.

This could be solved like so:

In src/lib/componentMixin.js

data() {
  return {
+   activeElement: null,
    notifications: [],
  };
},
methods: {
  onShowNotification(notification) {
    this.notifications.push(notification);
+   this.activeElement = document.activeElement;
+   this.$nextTick(() => this.$refs.notivuecationButton[0].focus());
  },
  onHideNotification(notification) {
    const index = this.notifications.indexOf(notification);
    if (index > -1) {
      this.notifications.splice(index, 1);
    }
+   if (this.activeElement) this.activeElement.focus();
  },

But now the plugin is reaching into the DOM, which feels a bit messy.

My question(s): would you be interested in adding this functionality to your plugin?
If so, and presuming the above is an acceptable way of approaching things, I'd be happy to send a PR.
Otherwise, do you have any thoughts on a better way to handle this?

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

No branches or pull requests

1 participant