Skip to content

Instantly share code, notes, and snippets.

@LukaszWiktor
Last active January 28, 2017 17:40
Show Gist options
  • Save LukaszWiktor/a019aec4475b11ee5a9bf234f9463289 to your computer and use it in GitHub Desktop.
Save LukaszWiktor/a019aec4475b11ee5a9bf234f9463289 to your computer and use it in GitHub Desktop.
Vue 2.x directive - automatic focus when element gets visible (works well with v-show)
Vue.directive('focus', {
update: function (el) {
if (!el.offsetParent) { // element not visible
el.dataset.focused=false;
} else if (el.dataset.focused!=="true") { // focus hasn't been forced yet
el.dataset.focused=true;
el.focus();
}
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment