koel/resources/assets/js/components/ui/SearchForm.vue

104 lines
2.1 KiB
Vue
Raw Normal View History

2022-04-15 14:24:30 +00:00
<template>
2022-10-13 15:18:47 +00:00
<form id="searchForm" role="search" @submit.prevent="onSubmit">
2022-04-15 14:24:30 +00:00
<input
2022-04-24 20:23:20 +00:00
ref="input"
v-model="q"
2022-04-15 14:24:30 +00:00
:class="{ dirty: q }"
autocorrect="false"
2022-04-24 20:23:20 +00:00
name="q"
2022-10-13 15:18:47 +00:00
:placeholder="placeholder"
2022-04-15 14:24:30 +00:00
spellcheck="false"
2022-04-24 20:23:20 +00:00
type="search"
2022-10-13 15:18:47 +00:00
@focus="maybeGoToSearchScreen"
2022-04-24 20:23:20 +00:00
@input="onInput"
2022-04-15 14:24:30 +00:00
>
2022-10-13 15:18:47 +00:00
<button type="submit" title="Search">
<icon :icon="faSearch"/>
</button>
</form>
2022-04-15 14:24:30 +00:00
</template>
2022-04-15 17:00:08 +00:00
<script lang="ts" setup>
2022-10-13 15:18:47 +00:00
import isMobile from 'ismobilejs'
import { faSearch } from '@fortawesome/free-solid-svg-icons'
2022-04-15 17:00:08 +00:00
import { ref } from 'vue'
2022-04-15 14:24:30 +00:00
import { debounce } from 'lodash'
import { eventBus, requireInjection } from '@/utils'
import { RouterKey } from '@/symbols'
2022-10-13 15:18:47 +00:00
const placeholder = isMobile.any ? 'Search' : 'Press F to search'
const router = requireInjection(RouterKey)
2022-04-15 14:24:30 +00:00
2022-04-24 20:23:20 +00:00
const input = ref<HTMLInputElement>()
2022-04-15 17:00:08 +00:00
const q = ref('')
2022-04-15 14:24:30 +00:00
let onInput = () => {
2022-04-15 17:00:08 +00:00
const _q = q.value.trim()
_q && eventBus.emit('SEARCH_KEYWORDS_CHANGED', _q)
}
if (process.env.NODE_ENV !== 'test') {
onInput = debounce(onInput, 500)
}
2022-04-15 14:24:30 +00:00
2022-10-13 15:18:47 +00:00
const onSubmit = () => {
eventBus.emit('TOGGLE_SIDEBAR')
maybeGoToSearchScreen()
}
const maybeGoToSearchScreen = () => isMobile.any || router.go('search')
2022-04-15 14:24:30 +00:00
2022-04-15 17:00:08 +00:00
eventBus.on({
FOCUS_SEARCH_FIELD: () => {
2022-04-24 20:23:20 +00:00
input.value?.focus()
input.value?.select()
2022-04-15 14:24:30 +00:00
}
})
</script>
<style lang="scss">
#searchForm {
2022-10-13 15:18:47 +00:00
display: flex;
align-items: stretch;
color: var(--color-text-secondary);
border: 1px solid transparent;
border-radius: 5px;
transition: border .2s ease-in-out;
overflow: hidden;
2022-04-15 14:24:30 +00:00
2022-10-13 15:18:47 +00:00
button {
display: none;
padding: 0 1.5rem;
background: rgba(255, 255, 255, .05);
border-radius: 0;
@media screen and (max-width: 768px) {
display: block;
}
2022-04-15 14:24:30 +00:00
}
2022-10-13 15:18:47 +00:00
&:focus-within {
border: 1px solid rgba(255, 255, 255, .2);
}
input[type="search"] {
2022-04-15 14:24:30 +00:00
width: 100%;
2022-10-13 15:18:47 +00:00
border-radius: 0;
height: 36px;
background: rgba(0, 0, 0, .2);
transition: .3s background-color;
padding: 0 1rem;
color: var(--color-text-primary);
2022-04-15 14:24:30 +00:00
2022-10-13 15:18:47 +00:00
&:focus {
background: rgba(0, 0, 0, .5);
2022-04-15 14:24:30 +00:00
}
2022-10-13 15:18:47 +00:00
&::placeholder {
color: rgba(255, 255, 255, .5);
2022-04-15 14:24:30 +00:00
}
}
}
</style>