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