Custom directives let you encapsulate low-level DOM manipulation as a reusable v-* attribute. Use them when you need direct access to an element (focus, scroll, third-party DOM libraries) — things components/props don't naturally cover.
Defining a local directive
<script setup>
// a directive is an object of lifecycle hooks; in <script setup>, name it vXxx
const vFocus = {
mounted(el) {
el.focus(); // `el` is the raw DOM element
},
};
</script>
<template>
<input v-focus /> <!-- autofocuses on mount -->
</template>
