Vue Directive Pattern
Create custom Vue directives for low-level DOM manipulation and reusable DOM behavior
When to Use
- You need reusable DOM manipulation logic (auto-focus, click-outside, intersection observer, tooltips)
- The behavior is purely DOM-level and does not involve component state or rendering
- Integrating third-party libraries that need direct DOM access
Instructions
- Define a directive as an object with lifecycle hooks (
mounted, updated, unmounted).
- Register globally via
app.directive('name', directiveObj) or locally in <script setup> with vName convention.
- Access the element as the first argument and binding value as the second.
- Use directives only for DOM manipulation — for logic, prefer composables.
// v-focus directive — auto-focuses an input on mount
const vFocus = {
mounted(el: HTMLElement) {
el.focus();
},
};
// Usage in template: <input v-focus />
- Clean up event listeners and observers in the
unmounted hook to prevent memory leaks.
- Use
binding.value to pass dynamic data: <div v-tooltip="'Hello'">.
Details
Custom directives give you low-level access to DOM elements. They run at specific lifecycle points (created, beforeMount, mounted, beforeUpdate, updated, beforeUnmount, unmounted) and receive the element and binding information. Vue's built-in directives (v-if, v-show, v-model) are implemented the same way.
Trade-offs:
- Directives are harder to test than composables — they require a real or mocked DOM
- No reactive return value — directives modify the DOM imperatively, not declaratively
- Overusing directives leads to "jQuery-style" code that bypasses Vue's reactivity
When NOT to use:
- When a composable can achieve the same result — composables are easier to test and compose
- For complex component behavior — use a component, not a directive
- When the DOM manipulation is a one-time setup — inline it in
onMounted
Source
https://patterns.dev/vue/directive-pattern
Process
- Read the instructions and examples in this document.
- Apply the patterns to your implementation, adapting to your specific context.
- Verify your implementation against the details and edge cases listed above.
Harness Integration
- Type: knowledge — this skill is a reference document, not a procedural workflow.
- No tools or state — consumed as context by other skills and agents.
Success Criteria
- The patterns described in this document are applied correctly in the implementation.
- Edge cases and anti-patterns listed in this document are avoided.
1---2name: vue-directive-pattern3description: Vue Directive Pattern4metadata:5 source: https://skillmd.com/skills/intense-visions/vue-directive-pattern6---7# Vue Directive Pattern89> Create custom Vue directives for low-level DOM manipulation and reusable DOM behavior1011## When to Use1213- You need reusable DOM manipulation logic (auto-focus, click-outside, intersection observer, tooltips)14- The behavior is purely DOM-level and does not involve component state or rendering15- Integrating third-party libraries that need direct DOM access1617## Instructions18191. Define a directive as an object with lifecycle hooks (`mounted`, `updated`, `unmounted`).202. Register globally via `app.directive('name', directiveObj)` or locally in `<script setup>` with `vName` convention.213. Access the element as the first argument and binding value as the second.224. Use directives only for DOM manipulation — for logic, prefer composables.2324```typescript25// v-focus directive — auto-focuses an input on mount26const vFocus = {27 mounted(el: HTMLElement) {28 el.focus();29 },30};3132// Usage in template: <input v-focus />33```34355. Clean up event listeners and observers in the `unmounted` hook to prevent memory leaks.366. Use `binding.value` to pass dynamic data: `<div v-tooltip="'Hello'">`.3738## Details3940Custom directives give you low-level access to DOM elements. They run at specific lifecycle points (created, beforeMount, mounted, beforeUpdate, updated, beforeUnmount, unmounted) and receive the element and binding information. Vue's built-in directives (`v-if`, `v-show`, `v-model`) are implemented the same way.4142**Trade-offs:**4344- Directives are harder to test than composables — they require a real or mocked DOM45- No reactive return value — directives modify the DOM imperatively, not declaratively46- Overusing directives leads to "jQuery-style" code that bypasses Vue's reactivity4748**When NOT to use:**4950- When a composable can achieve the same result — composables are easier to test and compose51- For complex component behavior — use a component, not a directive52- When the DOM manipulation is a one-time setup — inline it in `onMounted`5354## Source5556https://patterns.dev/vue/directive-pattern5758## Process59601. Read the instructions and examples in this document.612. Apply the patterns to your implementation, adapting to your specific context.623. Verify your implementation against the details and edge cases listed above.6364## Harness Integration6566- **Type:** knowledge — this skill is a reference document, not a procedural workflow.67- **No tools or state** — consumed as context by other skills and agents.6869## Success Criteria7071- The patterns described in this document are applied correctly in the implementation.72- Edge cases and anti-patterns listed in this document are avoided.