Let's break it down simply
Only reach for a custom directive when you need to reuse low-level DOM behavior. If UI structure is involved, a component or composable is usually the better fit. Use a plugin's install(app) to bundle up app-wide services and directives.
vue
<script setup>
const vFocus = {
mounted(element) {
element.focus()
}
}
</script>
<template>
<label>
Search
<input v-focus placeholder="Type to search">
</label>
</template>You should see
Search input receives focus after mountTry it yourself
Write a v-click-outside custom directive that calls a callback when you click outside the element.
Custom Directives — Vue.js