From 06de806cf5c7c0eeeebe1f1a1c11a1c22040f468 Mon Sep 17 00:00:00 2001 From: lidlanca <8693091+lidlanca@users.noreply.github.com> Date: Thu, 5 Aug 2021 03:28:56 -0400 Subject: [PATCH] directive install hook --- active-rfcs/0000-directive-install-hook.md | 82 ++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 active-rfcs/0000-directive-install-hook.md diff --git a/active-rfcs/0000-directive-install-hook.md b/active-rfcs/0000-directive-install-hook.md new file mode 100644 index 00000000..a192b54c --- /dev/null +++ b/active-rfcs/0000-directive-install-hook.md @@ -0,0 +1,82 @@ +- Start Date: 2021-08-02 +- Target Major Version: 3.x +- Reference Issues: +- Implementation PR: https://github.com/vuejs/vue-next/pull/4235 + +# Summary +add `install` hook for a custom directive + +# Basic example + +```js +export default { + directives: { + MyDir: { + install(){ return { mounted(){}, unmounted(){} } + } + } +} +``` + +# Motivation + +In current state when a function is passed as a directive, vue will register the function +to `mounted` and `updated` hooks only. +additionally there is no way to tell which hook invoked the function, or register to other hooks. + +having an `install` hook will provide 2 main advantages. +1. allows for function based directive, that can target any of the supported directive hooks +2. having access to the directive early enough, that a function can be used to create the directive dynamically, +with access to the directive binding(instance, value, etc) + + + + +Why are we doing this? What use cases does it support? What is the expected +outcome? + +1. The current function based directive is limited to 2 fixed hooks, and no ability to know the life cycle phase +when the function is invoked. + +2. consider provide/inject api, a parent component can actually provide a directive as an `install` function +where the child can then *inject and use. +(* currently inject() is not callable in a directive hook, however as a work around, a child can "resolve" the provided value via. `instance.$.provides` ) +which make this use case doable. even without full support from the inject functionality. + + + +# Detailed design + + +https://github.com/vuejs/vue-next/pull/4235 + + +```js +export default { + directives: { + myDir: { + install(binding : DirectiveBinding): ObjectDirective { + return { mounted(){}, unmounted(){}, ... } + } + } + } +} +``` + +if `install` hook is provided it will be called and the result will be merged into the directive hooks object. + + +# Drawbacks + + a function based directive might have additional overhead, however vue already supports a very limited function based directive. + + +# Alternatives +n/a + + +# Adoption strategy + +The feature is additive and should not have any impact on existing code. + +