docs/.vitepress/theme/components/search/SearchBar.vue

315 lines · 6118 bytes

  1<script setup lang="ts">
  2import {
  3  Settings,
  4  X
  5} from 'lucide-vue-next'
  6import { ref } from 'vue'
  7
  8defineProps<{
  9  filterText: string
 10  searchLoading: boolean
 11  showSettingsPopup: boolean
 12  buttonText: string
 13  disableReset: boolean
 14  searchMotionForm: string | object
 15  formMotion: Record<string, unknown>
 16  translate: (key: string) => string
 17}>()
 18
 19const emit = defineEmits<{
 20  'update:filterText': [value: string]
 21  resetSearch: []
 22  searchBarClick: [event: PointerEvent]
 23  toggleSettings: []
 24  close: []
 25}>()
 26
 27const searchInput = ref<HTMLInputElement>()
 28const settingsButtonRef = ref<HTMLButtonElement>()
 29
 30defineExpose({ searchInput, settingsButtonRef })
 31</script>
 32
 33<template>
 34  <component
 35    :is="searchMotionForm"
 36    class="search-bar"
 37    v-bind="formMotion"
 38    @pointerup="emit('searchBarClick', $event)"
 39    @submit.prevent=""
 40  >
 41    <label
 42      :title="buttonText"
 43      id="localsearch-label"
 44      for="localsearch-input"
 45    >
 46      <span
 47        aria-hidden="true"
 48        class="vpi-search search-icon local-search-icon"
 49      />
 50    </label>
 51    <div class="search-actions before">
 52      <button
 53        class="back-button"
 54        :title="translate('modal.backButtonTitle')"
 55        @click="emit('close')"
 56      >
 57        <span class="vpi-arrow-left local-search-icon" />
 58      </button>
 59    </div>
 60    <input
 61      ref="searchInput"
 62      :value="filterText"
 63      @input="emit('update:filterText', ($event.target as HTMLInputElement).value)"
 64      aria-autocomplete="both"
 65      aria-labelledby="localsearch-label"
 66      autocapitalize="off"
 67      autocomplete="off"
 68      autocorrect="off"
 69      class="search-input"
 70      id="localsearch-input"
 71      enterkeyhint="go"
 72      maxlength="64"
 73      :placeholder="buttonText"
 74      spellcheck="false"
 75      type="search"
 76    />
 77    <div class="search-actions">
 78      <button
 79        v-if="searchLoading"
 80        class="search-loading-button"
 81        type="button"
 82      >
 83        <img
 84          class="search-loading-bubba"
 85          src="/bubba.webp"
 86          alt="Bubba loading"
 87        />
 88      </button>
 89
 90
 91      <button
 92        ref="settingsButtonRef"
 93        type="button"
 94        class="mode-btn settings-toggle-btn"
 95        :class="{ 'mode-active': showSettingsPopup }"
 96        title="Search settings"
 97        @click.stop="emit('toggleSettings')"
 98      >
 99        <Settings :size="18" stroke-width="2" />
100      </button>
101      <button
102        class="clear-button"
103        type="reset"
104        :disabled="disableReset"
105        :title="translate('modal.resetButtonTitle')"
106        @click="emit('resetSearch')"
107      >
108        <X :size="18" stroke-width="2" />
109      </button>
110    </div>
111  </component>
112</template>
113
114<style>
115.search-bar {
116  flex: 0 0 auto !important;
117  min-height: 44px !important;
118  border: 1px solid var(--vp-c-divider);
119  border-radius: 4px;
120  display: flex;
121  align-items: center;
122  padding: 0 12px;
123  cursor: text;
124}
125
126@media (max-width: 767px) {
127  .search-bar {
128    padding: 0 8px;
129    min-height: 40px !important;
130  }
131}
132
133.search-bar:focus-within {
134  border-color: var(--vp-c-brand-1);
135}
136
137.local-search-icon {
138  display: block;
139  font-size: 18px;
140}
141
142.search-icon {
143  margin: 8px;
144}
145
146@media (max-width: 767px) {
147  .search-icon {
148    display: none;
149  }
150}
151
152.search-input {
153  flex: 1 1 auto;
154  min-width: 0;
155  padding: 6px 12px;
156  font-size: inherit;
157  width: 100%;
158}
159
160.search-input::-webkit-search-cancel-button {
161  display: none;
162}
163
164@media (max-width: 767px) {
165  .search-input {
166    padding: 6px 4px;
167  }
168}
169
170.search-actions {
171  flex: 0 0 auto;
172  display: flex;
173  align-items: center;
174  gap: 4px;
175}
176
177.search-loading-button {
178  display: inline-flex;
179  align-items: center;
180  justify-content: center;
181  padding: 0 !important;
182  margin-inline-end: 6px;
183  border: 0;
184  background: transparent;
185  cursor: pointer;
186}
187
188.search-loading-bubba {
189  width: 32px;
190  height: 28px;
191  object-fit: contain;
192  image-rendering: pixelated;
193  user-select: none;
194}
195
196@media (any-pointer: coarse) {
197  .search-actions {
198    gap: 8px;
199  }
200}
201
202@media (min-width: 769px) {
203  .search-actions.before {
204    display: none;
205  }
206}
207
208.search-actions button {
209  padding: 8px;
210}
211
212.search-actions button:not([disabled]):hover {
213  color: var(--vp-c-brand-1);
214}
215
216.search-actions button.clear-button:disabled {
217  opacity: 0.37;
218}
219
220
221
222.mode-btn {
223  display: flex;
224  align-items: center;
225  justify-content: center;
226  padding: 5px 7px;
227  color: var(--vp-c-text-2);
228  background: transparent;
229  border: none;
230  cursor: pointer;
231  transition: color 0.15s, background-color 0.15s;
232  border-radius: 0;
233}
234
235.mode-btn:hover {
236  color: var(--vp-c-text-1);
237  background: var(--vp-c-default-soft);
238}
239
240.mode-btn.mode-active {
241  color: var(--vp-c-brand-1);
242  background: color-mix(in srgb, var(--vp-c-brand-1) 10%, transparent);
243}
244
245
246
247.search-actions .settings-toggle-btn,
248.search-actions .clear-button {
249  display: flex;
250  border: none;
251  border-radius: 0;
252  background: transparent;
253  color: var(--vp-c-text-1);
254  opacity: 0.55;
255  padding: 8px 4px;
256  transition:
257    opacity 0.25s,
258    transform 0.2s cubic-bezier(0.16, 1, 0.3, 1);
259}
260
261.search-actions button.settings-toggle-btn:not([disabled]):hover,
262.search-actions button.clear-button:not([disabled]):hover,
263.settings-toggle-btn.mode-active {
264  color: var(--vp-c-text-1);
265  opacity: 1;
266  background: transparent;
267}
268
269.settings-toggle-btn:active,
270.clear-button:not([disabled]):active {
271  transform: scale(0.94);
272}
273
274html.effects-disabled .settings-toggle-btn,
275html.effects-disabled .clear-button {
276  transition: none;
277}
278
279html.effects-disabled .settings-toggle-btn:active,
280html.effects-disabled .clear-button:active {
281  transform: none;
282}
283
284@media (max-width: 767px) {
285  .search-actions.before {
286    display: flex;
287  }
288
289  label[for="localsearch-input"] {
290    display: none;
291  }
292}
293
294.back-button {
295  display: none;
296  align-items: center;
297  justify-content: center;
298  padding: 8px;
299  border: none;
300  background: transparent;
301  color: var(--vp-c-text-1);
302  cursor: pointer;
303  transition: color 0.15s;
304}
305
306.back-button:hover {
307  color: var(--vp-c-brand-1);
308}
309
310@media (max-width: 767px) {
311  .back-button {
312    display: flex;
313  }
314}
315</style>