Implement Vue custom directives: hook order in the renderer, binding values, v-click-outside and v-intersect

hardAbout 60 minutesUpdated CodingProvuedirectivesrendererclick-outsideintersection-observer
Not solved

Share this question

Problem

A custom directive is an object of lifecycle hooks that Vue calls with the real element. They are the right tool for low-level DOM behaviour that does not need its own component: closing a menu on an outside click, lazy-loading an image when it scrolls into view, autofocus. Interviewers ask about them to see whether you know when each hook runs and what the element looks like at that moment. Build the runtime part and two directives on a small renderer.

const menu = withDirectives(h("ul", null, items), [[vClickOutside, close]]);
render(menu, container);
dispatch(document.body, "click"); // close(event)

const img = withDirectives(h("img"), [[vIntersect, { handler: load, options: { rootMargin: "200px" } }, undefined, { once: true }]]);

Ref. UR-1464 · © UIRound · uiround.com/questions/implement-vue-custom-directives-hook-order-in-the-renderer-binding-values-v-click-outside-and-v-intersect

Stuck? Take a hint

Try it in the editor

Run your code as often as you like. Submitting it to the full test suite is part of Pro.

Private, and saved in this browser as you type. All your notes are on My lists.

Every question and answer7 days, one payment