The user wants to implement custom directives in Vue 3 to manage complex user interactions, which feels quite broad! I need to explain Vue 3 directives first, including how to create them, and provide usage examples that highlight complex interactions—especially with the composition API and lifecycle hooks like beforeMount and mounted.
I should clarify when to use a directive over a component or composable and demonstrate the basic structure. I'll also cover global and local registration, along with examples like click-outside or debounce input. Additionally, it's important to note that Vue 3 changes some hook names to camelCase.Proposing SSR Patterns
I think I need to caution the user about SSR, considering the open nature of their question. I’ll start with a summary of patterns and then provide basic examples, focusing on directives such as:
Click-outside (for closing popups)
Long-press
Drag
I want to demonstrate how to hook events, use binding.value options, and manage cleanup. It's also important to show the TypeScript version and how to handle arguments and modifiers. I should keep examples concise while coordinating multiple events for complex interactions, like v-drag. Lastly, I’ll mention how to register directives in Vue 3.Clarifying Directives in <script setup>
I realized it’s actually defineProps we're working with; there’s no defineDirective. When using <script setup>, local directives can be registered by naming conventions. If I import vClickOutside, I can't directly use it as a directive in the template, but there is a way! I can define them and expose them with defineExpose({}). In <script setup>, I can reference them with shorthand like v-my-directive, and I remember the pattern: create a local directive with a variable that capitalizes the name for auto-registration.Reviewing Usage in <script setup>
I need to check the Vue 3 docs for using directives in <script setup>. I can import a directive, then apply it in the template with the v- prefix, like this:
This works because <script setup> registers variables automatically. I'll also mention that using app.directive is suitable for global directives, while customizing arguments or modifiers can be advanced. To demonstrate advanced usage, I might show a directive with a config object for events. I'll also discuss event listener cleanup to prevent leaks, especially when binding changes. Finally, I’ll consider TypeScript adaptation.Introducing Directives in Vue 3
I want to start with a brief intro to what directives are and when to use them. Then, I’ll provide a basic directive skeleton in Vue 3:
Next, I'll show how to register a global directive and provide some examples, like v-click-outside for closing dropdowns when clicking outside an element. I'll also cover v-long-press and v-drag for handling user interactions. Since directives are low-level, I'll emphasize that for logic within stateful components, composables could be a better choice. In Vue 3, custom directives are the right tool when you need low‑level, DOM-based behavior that’s awkward to express as a component or composable: complex mouse interactions, gestures, click‑outside, long‑press, drag, etc.
Below are practical patterns and examples for implementing such directives.
1. Custom directive basics in Vue 3
A directive is an object with lifecycle hooks, very similar to component hooks:
In <script setup>, imported vFocus can be used as v-focus.
2. Example 1: v-click-outside (closing menus, dialogs)
Handle clicks outside an element to close dropdowns, modals, etc.
js
// directives/v-click-outside.js
export const vClickOutside = {
mounted(el, binding) {
const handler = (event) => {
// Ignore clicks on the element or its children
if (el.contains(event.target)) return
// If a callback was provided, call it
if (typeof binding.value === 'function') {
binding.value(event)
}
}
// Store handler on the element so we can remove it later
el.__vueClickOutside__ = handler
document.addEventListener('mousedown', handler)
document.addEventListener('touchstart', handler)
},
unmounted(el) {
const handler = el.__vueClickOutside__
if (handler) {
document.removeEventListener('mousedown', handler)
document.removeEventListener('touchstart', handler)
delete el.__vueClickOutside__
}
}
}
Usage:
vue
<script setup>
import { ref } from 'vue'
import { vClickOutside } from '@/directives/v-click-outside'
const open = ref(false)
const close = () => (open.value = false)
</script>
<template>
<div>
<button @click="open = !open">Toggle menu</button>
<div v-if="open" v-click-outside="close" class="menu">
Menu content...
</div>
</div>
</template>
This encapsulates the global event listeners and cleanup in a reusable way.
3. Example 2: v-long-press (press-and-hold interactions)
Recognize a long press (mouse or touch) and trigger an action. Useful for context menus, power‑user actions, etc.
6. When to choose a directive vs component/composable
Use a directive when:
You need direct, imperative DOM access (measuring, low‑level events).
Behavior is tightly bound to a single element’s interaction.
You want to reuse logic across many differing components without adding wrapper components.
Use a component if you need:
Template/markup, slots, or internal reactive state.
Use a composable (useXxx) if:
You can stay in Vue’s reactive world without touching the raw DOM directly.
If you describe a specific interaction you’re targeting (e.g. “drag to select range”, “swipe with inertia”, “press-and-hold to repeat action”), I can sketch a directive tailored exactly to that pattern.