Question presented to candidate:
"Build me a custom directive, say v-click-outside or v-focus. Show how you define and register it, what arguments the hooks receive, and when you would choose a directive over a component or a composable."
What a strong answer should cover:
- A directive is an object of lifecycle hooks (
created,beforeMount,mounted,beforeUpdate,updated,beforeUnmount,unmounted) or a function shorthand that runs onmountedandupdated. - Hooks receive
(el, binding, vnode, prevVnode);bindinghasvalue,oldValue,arg,modifiers,instanceanddir. - Registration: in
<script setup>any camelCase variable starting withv(vFocusbecomesv-focus);directives: {}in the Options API;app.directive('name', def)globally. - Cleanup: anything added in
mounted(listeners, observers, timers) must be removed inunmounted. - Directives are for direct DOM access only; reusable logic belongs in composables and reusable UI in components. Using them on components is discouraged (multi-root components warn).
- SSR: hooks do not run on the server;
getSSRPropscan return attributes for the HTML.
Clarifying questions expected:
- "Does this need direct DOM access, or could a component or composable do it?"
- "Will the app be server-rendered?"
Code / implementation expected: Yes β a working directive with an argument or modifier and proper cleanup.