Skip to content

Commit 94430c8

Browse files
authored
docs: fix issue with autofocus on custom directives page (#2330)
* docs: fix issue with autofocus on custom directives page * chore: update CSS for custom directives page * docs: remove unnecessary Japanese translation in custom directives page * Fix base on vuejs/docs#2994 * feat: Add v-focus directive for input autofocus
1 parent 10277bd commit 94430c8

File tree

1 file changed

+77
-23
lines changed

1 file changed

+77
-23
lines changed

src/guide/reusability/custom-directives.md

Lines changed: 77 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,24 @@
11
# カスタムディレクティブ {#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}
1223

1324
コアに設定されているデフォルトのディレクティブのセット(`v-model``v-show` など)に加え、Vue では独自のカスタムディレクティブを登録することができます。
@@ -20,14 +31,16 @@ Vue におけるコードの再利用として 2 つの方法を紹介してき
2031

2132
```vue
2233
<script setup>
23-
// テンプレート内で v-focus が有効になります
24-
const vFocus = {
25-
mounted: (el) => el.focus()
34+
// テンプレート内で v-highlight が有効になります
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,33 +49,31 @@ 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-
// テンプレート内で v-focus が有効になります
46-
focus
58+
// テンプレート内で v-highlight が有効になります
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-
ページの他の場所をクリックしていないと仮定すると、上の入力欄は自動的にフォーカスされるはずです。このディレクティブはページロード時だけでなく、Vue によって要素が動的に挿入されたときにも機能するため、`autofocus` 属性よりも便利です。
62-
6374
<div class="composition-api">
6475

65-
`<script setup>` では、接頭辞が `v` で始まるキャメルケースの変数をカスタムディレクティブとして使用することができます。上の例では、`vFocus` はテンプレート内で `v-focus` として使用できます。
76+
`<script setup>` では、接頭辞が `v` で始まるキャメルケースの変数をカスタムディレクティブとして使用することができます。上の例では、`vHighlight` はテンプレート内で `v-highlight` として使用できます。
6677

6778
`<script setup>` を使用しない場合、カスタムディレクティブは `directives` オプションを使用して登録することができます:
6879

@@ -72,8 +83,8 @@ export default {
7283
/*...*/
7384
},
7485
directives: {
75-
// テンプレート内で v-focus が有効になります
76-
focus: {
86+
// テンプレート内で v-highlight が有効になります
87+
highlight: {
7788
/* ... */
7889
}
7990
}
@@ -93,15 +104,59 @@ export default {
93104
```js
94105
const app = createApp({})
95106

96-
// 全てのコンポーネントで v-focus が使用可能
97-
app.directive('focus', {
107+
// 全てのコンポーネントで v-highlight が使用可能
108+
app.directive('highlight', {
98109
/* ... */
99110
})
100111
```
101112

102-
:::tip
103-
カスタムディレクティブは DOM を直接操作することでしか必要な機能を実現できない場合にのみ使用してください。`v-bind` のような組み込みディレクティブを使用した宣言的なテンプレートは、効率的かつサーバーレンダリングフレンドリーです。可能なかぎり組み込みディレクティブを使用することをおすすめします。
104-
:::
113+
## カスタムディレクティブを使用するタイミング {#when-to-use}
114+
115+
カスタムディレクティブは DOM を直接操作することでしか必要な機能を実現できない場合にのみ使用してください。
116+
117+
一般的な例として、要素にフォーカスを当てる `v-focus` カスタムディレクティブがあります。
118+
119+
<div class="composition-api">
120+
121+
```vue
122+
<script setup>
123+
// テンプレート内で v-focus が有効になります
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+
// テンプレート内で v-focus が有効になります
146+
focus
147+
}
148+
}
149+
```
150+
151+
```vue-html
152+
<input v-focus />
153+
```
154+
155+
</div>
156+
157+
このディレクティブは `autofocus` 属性よりも便利です。ページ読み込み時だけでなく、Vue によって要素が動的に挿入されたときにも機能するからです!
158+
159+
可能な限り `v-bind` のような組み込みディレクティブを使用した宣言的なテンプレートを推奨します。これらはより効率的で、サーバーレンダリングにも適しているためです。
105160

106161
## ディレクティブフック {#directive-hooks}
107162

@@ -214,7 +269,6 @@ app.directive('demo', (el, binding) => {
214269
コンポーネントにカスタムディレクティブを使用することは推奨しません。コンポーネントに複数のルートノードがある場合、予期しない動作が発生する可能性があります。
215270
:::
216271

217-
218272
コンポーネントで使用すると、[フォールスルー属性](/guide/components/attrs)と同様にカスタムディレクティブは常にコンポーネントのルートノードに適用されます。
219273

220274
```vue-html

0 commit comments

Comments
 (0)