- Notifications
You must be signed in to change notification settings - Fork39
A reusable focus directive for reusable Vue.js components
License
simplesmiler/vue-focus
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
A reusable focus directive for reusableVue.js components
It can be tricky to manage input focus. You always have to fall back to accessing DOM elements and calling.focus or.blur on them.
Well not anymore.vue-focus lets you manage focus from the safety of your view model.
Check out the examples,read the docs orfile an issue.
- Focus the field when the modal windows appears
- Track the focus to show a hint for the focused field
- Move between fields with cursor keys
- Implement custom focus-related controls (e.g labels)
- vue: ^2.0.0
If you need a version for Vue 1, tryvue-focus@1.0.
From npm:
$ npm install vue-focus --save
From CDN:
<scriptsrc="https://cdnjs.cloudflare.com/ajax/libs/vue-focus/2.1.0/vue-focus.js"></script><!-- OR --><scriptsrc="https://cdnjs.cloudflare.com/ajax/libs/vue-focus/2.1.0/vue-focus.min.js"></script>
A directive that binds focus to the expression in a one-way manner, so that the element receives focus when the expression istruthy and loses focus when the expression isfalsy.
import{focus}from'vue-focus';exportdefault{directives:{focus:focus},template:'<input type="text" v-focus="focused" @focus="focused = true" @blur="focused = false">',data:function(){return{focused:false,};},};
NOTE: As opposed to 1.x, in Vue 2.0, directives are updated every time the host component rerenders, not just when the directive expression chages. Somethimes this may be undesirable, especially for the "autofocus" use case. If you want to mimic the 1.x behavior, then add the
.lazymodifier to the directive, e.g.v-focus.lazy="true".
A mixin that makes thev-focus directive available to the component under the default name.
import{mixinasfocusMixin}from'vue-focus';exportdefault{mixins:[focusMixin],template:'<input type="text" v-focus="focused" @focus="focused = true" @blur="focused = false">',data:function(){return{focused:false,};},};
- Although you can write an input that gains focus immediately after loosing it, this is not recommended, as two such inputs will fall into infinite recursion and freeze the browser.
Form elements are not the only elements that are able to receive focus. The list also includes links, elements withtabindex attribute set and elements withcontentEditable set totrue.
About
A reusable focus directive for reusable Vue.js components
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.