elk/components/search/SearchWidget.vue

115 lines
3.1 KiB
Vue
Raw Permalink Normal View History

2022-12-18 00:35:07 +03:00
<script setup lang="ts">
2023-01-05 19:48:20 +03:00
import type { AccountResult, HashTagResult, StatusResult } from './types'
2022-12-18 00:35:07 +03:00
const query = ref('')
const { accounts, hashtags, loading, statuses } = useSearch(query)
2022-12-18 00:35:07 +03:00
const index = ref(0)
const { t } = useI18n()
const el = ref<HTMLElement>()
const router = useRouter()
const { focused } = useFocusWithin(el)
const results = computed(() => {
if (query.value.length === 0)
return []
const results = [
2023-01-05 19:48:20 +03:00
...hashtags.value.slice(0, 3).map<HashTagResult>(hashtag => ({
type: 'hashtag',
id: hashtag.id,
hashtag,
to: getTagRoute(hashtag.name),
})),
...accounts.value.map<AccountResult>(account => ({
type: 'account',
id: account.id,
account,
to: getAccountRoute(account),
})),
...statuses.value.map<StatusResult>(status => ({
type: 'status',
id: status.id,
status,
to: getStatusRoute(status),
})),
2022-12-18 00:35:07 +03:00
// Disable until search page is implemented
// {
// type: 'action',
// to: `/search?q=${query.value}`,
// action: {
// label: `Search for ${query.value}`,
// },
// },
]
return results
})
// Reset index when results change
watch([results, focused], () => index.value = -1)
const shift = (delta: number) => index.value = (index.value + delta % results.value.length + results.value.length) % results.value.length
const activate = () => {
(document.activeElement as HTMLElement).blur()
const currentIndex = index.value
index.value = -1
if (query.value.length === 0)
return
// Disable until search page is implemented
// if (currentIndex === -1)
// router.push(`/search?q=${query.value}`)
router.push(results.value[currentIndex].to)
}
</script>
<template>
2023-01-05 14:33:00 +03:00
<div ref="el" relative group>
2023-01-06 12:44:02 +03:00
<div bg-base border="~ base" h10 px-4 rounded-3 flex="~ row" items-center relative focus-within:box-shadow-outline gap-3>
2023-01-05 14:33:00 +03:00
<div i-ri:search-2-line pointer-events-none text-secondary mt="1px" class="rtl-flip" />
2022-12-18 00:35:07 +03:00
<input
ref="input"
v-model="query"
h-full
2023-01-05 02:17:30 +03:00
rounded-3
2022-12-18 00:35:07 +03:00
w-full
bg-transparent
outline="focus:none"
pe-4
2022-12-28 00:41:44 +03:00
:placeholder="t('nav.search')"
2022-12-18 00:35:07 +03:00
pb="1px"
placeholder-text-secondary
@keydown.down.prevent="shift(1)"
@keydown.up.prevent="shift(-1)"
@keypress.enter="activate"
>
</div>
<!-- Results -->
2023-01-06 01:50:14 +03:00
<div left-0 top-12 absolute w-full z10 group-focus-within="pointer-events-auto visible" invisible pointer-events-none>
2023-01-05 02:17:30 +03:00
<div w-full bg-base border="~ base" rounded-3 max-h-100 overflow-auto py2>
2022-12-18 00:35:07 +03:00
<span v-if="query.length === 0" block text-center text-sm text-secondary>
{{ t('search.search_desc') }}
</span>
<template v-if="!loading">
2023-01-05 19:48:20 +03:00
<SearchResult
v-for="(result, i) in results" :key="result.id"
:active="index === parseInt(i.toString())"
:result="result"
:tabindex="focused ? 0 : -1"
/>
2022-12-18 00:35:07 +03:00
</template>
<div v-else>
<SearchResultSkeleton />
<SearchResultSkeleton />
<SearchResultSkeleton />
</div>
</div>
</div>
</div>
</template>