51 lines
2.2 KiB
Vue
51 lines
2.2 KiB
Vue
<script setup>
|
||
import { onMounted, ref, watch } from 'vue';
|
||
import { LoaderCircle, Search } from '@lucide/vue';
|
||
import { api } from '../lib/api';
|
||
|
||
const emit = defineEmits(['pick']);
|
||
const query = ref('');
|
||
const gifs = ref([]);
|
||
const giphy = ref(false);
|
||
const loading = ref(false);
|
||
let timer;
|
||
|
||
async function load() {
|
||
loading.value = true;
|
||
|
||
try {
|
||
const data = await api.gifs(query.value);
|
||
gifs.value = data.gifs;
|
||
giphy.value = data.giphy;
|
||
} finally {
|
||
loading.value = false;
|
||
}
|
||
}
|
||
|
||
watch(query, () => {
|
||
clearTimeout(timer);
|
||
timer = setTimeout(load, 300);
|
||
});
|
||
onMounted(load);
|
||
</script>
|
||
|
||
<template>
|
||
<div>
|
||
<div class="ih:border-b ih:p-2">
|
||
<div class="ih:relative">
|
||
<Search class="ih:pointer-events-none ih:absolute ih:top-1/2 ih:left-2.5 ih:size-4 ih:-translate-y-1/2 ih:text-muted-foreground" />
|
||
<input v-model="query" type="search" placeholder="GIFs suchen" aria-label="GIFs suchen" autofocus class="ih:h-9 ih:w-full ih:rounded-md ih:bg-muted ih:pr-3 ih:pl-8 ih:text-sm ih:focus-visible:outline-none" />
|
||
</div>
|
||
</div>
|
||
<div class="ih:max-h-72 ih:overflow-y-auto ih:p-2">
|
||
<div v-if="loading && !gifs.length" class="ih:grid ih:place-items-center ih:py-8"><LoaderCircle class="ih:size-5 ih:animate-spin ih:text-muted-foreground" /></div>
|
||
<div v-else-if="gifs.length" class="ih:columns-2 ih:gap-1.5">
|
||
<button v-for="gif in gifs" :key="gif.url" type="button" class="ih:mb-1.5 ih:block ih:w-full ih:overflow-hidden ih:rounded ih:bg-muted ih:hover:ring-2 ih:hover:ring-brand" :title="gif.title" @click="emit('pick', gif)">
|
||
<img :src="gif.preview" :alt="gif.title" loading="lazy" class="ih:block ih:w-full" :style="gif.width && gif.height ? { aspectRatio: `${gif.width} / ${gif.height}` } : {}" />
|
||
</button>
|
||
</div>
|
||
<p v-else class="ih:py-6 ih:text-center ih:text-sm ih:text-muted-foreground">{{ query ? 'Keine GIFs gefunden.' : 'Noch keine GIFs in der Sammlung.' }}</p>
|
||
</div>
|
||
<p v-if="giphy" class="ih:border-t ih:px-3 ih:py-1.5 ih:text-[10px] ih:text-muted-foreground">Mit Suche über GIPHY – Suchbegriffe gehen an GIPHY.</p>
|
||
</div>
|
||
</template>
|