aboutsummaryrefslogtreecommitdiff
path: root/resources/vue/directives/autofocus.ts
blob: e4c3d3f129718471fe76c122eab6f52df9bed039 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// Shamelessly copied from https://github.com/byteboomers/vue-autofocus-directive

import {DirectiveBinding, nextTick} from "vue";

function focusElement(el: HTMLElement, binding: DirectiveBinding) : void {
    if (binding.value !== undefined && !binding.value) {
        return;
    }

    nextTick().then(() => el.focus());
}

export default {
    mounted(el: HTMLElement, binding: DirectiveBinding) {
        // When the component of the element gets activated
        focusElement(el, binding);
    },
    inserted: focusElement
}