Last active
January 28, 2017 17:40
-
-
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)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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