Skip to content

Commit ac7a921

Browse files
authored
docs (#2194): fix issue with autofocus on custom directives page (#2994)
docs (#2914): fix autofocus issue on custom directives
1 parent b3b1b89 commit ac7a921

File tree

1 file changed

+77
-23
lines changed

1 file changed

+77
-23
lines changed

src/guide/reusability/custom-directives.md

+77-23
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,24 @@
11
# Custom Directives {#custom-directives}
22

33
<script setup>
4-
const vFocus = {
4+
const vHighlight = {
55
mounted: el => {
6-
el.focus()
6+
el.classList.add('is-highlight')
77
}
88
}
99
</script>
1010

11+
<style>
12+
.vt-doc p.is-highlight {
13+
margin-bottom: 0;
14+
}
15+
16+
.is-highlight {
17+
background-color: yellow;
18+
color: black;
19+
}
20+
</style>
21+
1122
## Introduction {#introduction}
1223

1324
In addition to the default set of directives shipped in core (like `v-model` or `v-show`), Vue also allows you to register your own custom directives.
@@ -20,14 +31,16 @@ A custom directive is defined as an object containing lifecycle hooks similar to
2031

2132
```vue
2233
<script setup>
23-
// enables v-focus in templates
24-
const vFocus = {
25-
mounted: (el) => el.focus()
34+
// enables v-highlight in templates
35+
const vHighlight = {
36+
mounted: (el) => {
37+
el.classList.add('is-highlight')
38+
}
2639
}
2740
</script>
2841
2942
<template>
30-
<input v-focus />
43+
<p v-highlight>This sentence is important!</p>
3144
</template>
3245
```
3346

@@ -36,44 +49,42 @@ const vFocus = {
3649
<div class="options-api">
3750

3851
```js
39-
const focus = {
40-
mounted: (el) => el.focus()
52+
const highlight = {
53+
mounted: (el) => el.classList.add('is-highlight')
4154
}
4255

4356
export default {
4457
directives: {
45-
// enables v-focus in template
46-
focus
58+
// enables v-highlight in template
59+
highlight
4760
}
4861
}
4962
```
5063

5164
```vue-html
52-
<input v-focus />
65+
<p v-highlight>This sentence is important!</p>
5366
```
5467

5568
</div>
5669

5770
<div class="demo">
58-
<input v-focus placeholder="This should be focused" />
71+
<p v-highlight>This sentence is important!</p>
5972
</div>
6073

61-
Assuming you haven't clicked elsewhere on the page, the input above should be auto-focused. This directive is more useful than the `autofocus` attribute because it works not just on page load - it also works when the element is dynamically inserted by Vue.
62-
6374
<div class="composition-api">
6475

65-
In `<script setup>`, any camelCase variable that starts with the `v` prefix can be used as a custom directive. In the example above, `vFocus` can be used in the template as `v-focus`.
76+
In `<script setup>`, any camelCase variable that starts with the `v` prefix can be used as a custom directive. In the example above, `vHighlight` can be used in the template as `v-highlight`.
6677

67-
If not using `<script setup>`, custom directives can be registered using the `directives` option:
78+
If you are not using `<script setup>`, custom directives can be registered using the `directives` option:
6879

6980
```js
7081
export default {
7182
setup() {
7283
/*...*/
7384
},
7485
directives: {
75-
// enables v-focus in template
76-
focus: {
86+
// enables v-highlight in template
87+
highlight: {
7788
/* ... */
7889
}
7990
}
@@ -94,14 +105,58 @@ It is also common to globally register custom directives at the app level:
94105
const app = createApp({})
95106

96107
// make v-focus usable in all components
97-
app.directive('focus', {
108+
app.directive('highlight', {
98109
/* ... */
99110
})
100111
```
101112

102-
:::tip
103-
Custom directives should only be used when the desired functionality can only be achieved via direct DOM manipulation. Prefer declarative templating using built-in directives such as `v-bind` when possible because they are more efficient and server-rendering friendly.
104-
:::
113+
## When to use custom directives {#when-to-use}
114+
115+
Custom directives should only be used when the desired functionality can only be achieved via direct DOM manipulation.
116+
117+
A common example of this is a `v-focus` custom directive that brings an element into focus.
118+
119+
<div class="composition-api">
120+
121+
```vue
122+
<script setup>
123+
// enables v-focus in templates
124+
const vFocus = {
125+
mounted: (el) => el.focus()
126+
}
127+
</script>
128+
129+
<template>
130+
<input v-focus />
131+
</template>
132+
```
133+
134+
</div>
135+
136+
<div class="options-api">
137+
138+
```js
139+
const focus = {
140+
mounted: (el) => el.focus()
141+
}
142+
143+
export default {
144+
directives: {
145+
// enables v-focus in template
146+
focus
147+
}
148+
}
149+
```
150+
151+
```vue-html
152+
<input v-focus />
153+
```
154+
155+
</div>
156+
157+
This directive is more useful than the `autofocus` attribute because it works not just on page load - it also works when the element is dynamically inserted by Vue!
158+
159+
Declarative templating with built-in directives such as `v-bind` is recommended when possible because they are more efficient and server-rendering friendly.
105160

106161
## Directive Hooks {#directive-hooks}
107162

@@ -214,7 +269,6 @@ app.directive('demo', (el, binding) => {
214269
Using custom directives on components is not recommended. Unexpected behaviour may occur when a component has multiple root nodes.
215270
:::
216271

217-
218272
When used on components, custom directives will always apply to a component's root node, similar to [Fallthrough Attributes](/guide/components/attrs).
219273

220274
```vue-html

0 commit comments

Comments
 (0)