1
1
<script setup lang="ts">
2
- import { focusIn , useNuxtApp , useRoute , useRouter } from " #imports"
3
- import { computed , nextTick , ref , SetupContext , watch } from " vue"
2
+ import { useNuxtApp , useRoute , useRouter } from " #imports"
3
+ import {
4
+ computed ,
5
+ nextTick ,
6
+ ref ,
7
+ type SetupContext ,
8
+ useTemplateRef ,
9
+ watch ,
10
+ } from " vue"
4
11
5
12
import { onClickOutside } from " @vueuse/core"
6
13
@@ -15,6 +22,7 @@ import {
15
22
getAllTabbableIn ,
16
23
getFirstTabbableIn ,
17
24
} from " ~/utils/reakit-utils/focus"
25
+ import { focusIn } from " ~/utils/focus-management"
18
26
import { useMediaStore } from " ~/stores/media"
19
27
import { useSearchStore } from " ~/stores/search"
20
28
import { useDialogControl } from " ~/composables/use-dialog-control"
@@ -34,16 +42,18 @@ import VSearchBarButton from "~/components/VHeader/VHeaderMobile/VSearchBarButto
34
42
* displayed in the bar itself.
35
43
*/
36
44
37
- const searchInputRef = ref <HTMLInputElement | null >(null )
38
- const headerRef = ref <HTMLElement | null >(null )
39
- const recentSearchesRef = ref <InstanceType <typeof VRecentSearches > | null >(null )
40
- const contentSettingsButtonRef = ref < InstanceType <
41
- typeof VContentSettingsButton
42
- > | null > (null )
45
+ const searchInputRef = useTemplateRef (" searchInputRef" )
46
+ const headerRef = useTemplateRef (" headerRef" )
47
+ const recentSearchesRef =
48
+ useTemplateRef <InstanceType <typeof VRecentSearches >>(" recentSearchesRef" )
49
+ const contentSettingsButtonRef = useTemplateRef <
50
+ InstanceType < typeof VContentSettingsButton >
51
+ > (" contentSettingsButtonRef" )
43
52
const contentSettingsButton = computed (
44
53
() => (contentSettingsButtonRef .value ?.$el as HTMLElement ) ?? undefined
45
54
)
46
- const clearButtonRef = ref <InstanceType <typeof VSearchBarButton > | null >(null )
55
+ const clearButtonRef =
56
+ useTemplateRef <InstanceType <typeof VSearchBarButton >>(" clearButtonRef" )
47
57
48
58
const mediaStore = useMediaStore ()
49
59
const searchStore = useSearchStore ()
@@ -67,7 +77,10 @@ const { updateSearchState, searchTerm, searchStatus } =
67
77
const localSearchTerm = ref (searchTerm .value )
68
78
69
79
const focusInput = () => {
70
- const input = searchInputRef .value as HTMLInputElement
80
+ const input = searchInputRef .value
81
+ if (! input ) {
82
+ return
83
+ }
71
84
ensureFocus (input )
72
85
input .selectionStart = selection .value .start
73
86
input .selectionEnd = selection .value .end
@@ -108,7 +121,10 @@ const deactivate = () => {
108
121
* the input field is blurred and focused again.
109
122
*/
110
123
const updateSelection = () => {
111
- const inputElement = searchInputRef .value as HTMLInputElement
124
+ const inputElement = searchInputRef .value
125
+ if (! inputElement ) {
126
+ return
127
+ }
112
128
const lastPos =
113
129
inputElement .value .length > 0 ? inputElement .value .length - 1 : 0
114
130
selection .value = {
@@ -125,7 +141,11 @@ const updateSelection = () => {
125
141
* and activate the search bar.
126
142
*/
127
143
const updateSearchText = () => {
128
- localSearchTerm .value = (searchInputRef .value as HTMLInputElement ).value
144
+ const inputElement = searchInputRef .value
145
+ if (! inputElement ) {
146
+ return
147
+ }
148
+ localSearchTerm .value = inputElement .value
129
149
updateSelection ()
130
150
if (isInputFocused .value && ! isSearchBarActive .value ) {
131
151
activate ()
0 commit comments