feat(search): improve search UI with sticky bar and keyboard handling

- Add fixed search bar with glassmorphism effect (backdrop blur, semi-transparent)
- Implement clear search button in DaisyUI style
- Auto-hide keyboard on Enter key press and scroll events
- Remove search page title for cleaner UI
- Use DaisyUI theme-aware background colors instead of fixed white
- Add fixed padding offset for content below search bar
This commit is contained in:
2025-12-01 21:55:16 +03:00
parent cedc49f0d5
commit 64ead29583
6 changed files with 210 additions and 53 deletions

View File

@@ -1,47 +1,55 @@
<template>
<BaseViewWrapper title="Поиск">
<label class="input w-full mb-4">
<svg class="h-[1em] opacity-50" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24">
<g
stroke-linejoin="round"
stroke-linecap="round"
stroke-width="2.5"
fill="none"
stroke="currentColor"
<div>
<div class="bg-base-100/80 backdrop-blur-md z-10 fixed left-0 right-0 px-4 pt-4 shadow-sm" :style="searchWrapperStyle">
<div class="flex gap-2 mb-4">
<label class="input grow">
<svg class="h-[1em] opacity-50" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24">
<g
stroke-linejoin="round"
stroke-linecap="round"
stroke-width="2.5"
fill="none"
stroke="currentColor"
>
<circle cx="11" cy="11" r="8"></circle>
<path d="m21 21-4.3-4.3"></path>
</g>
</svg>
<input
ref="searchInput"
type="search"
class="grow input-lg"
placeholder="Поиск по магазину"
v-model="searchStore.search"
@search="handleSearch"
@keydown.enter="handleEnter"
@input="debouncedSearch"
/>
</label>
<button
v-if="searchStore.search"
@click="clearSearch"
class="btn btn-circle btn-ghost"
type="button"
aria-label="Очистить поиск"
>
<circle cx="11" cy="11" r="8"></circle>
<path d="m21 21-4.3-4.3"></path>
</g>
</svg>
<input
ref="searchInput"
type="search"
class="grow input-lg"
placeholder="Поиск по магазину"
v-model="searchStore.search"
@search="debouncedSearch"
@input="debouncedSearch"
<svg xmlns="http://www.w3.org/2000/svg" class="h-6 w-6" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2">
<path stroke-linecap="round" stroke-linejoin="round" d="M6 18L18 6M6 6l12 12" />
</svg>
</button>
</div>
</div>
<BaseViewWrapper>
<div class="pt-24">
<div v-if="searchStore.isLoading === false && searchStore.products.data.length > 0">
<ProductsList
:products="searchStore.products.data"
:hasMore="searchStore.hasMore"
:isLoading="searchStore.isLoading"
:isLoadingMore="searchStore.isLoadingMore"
@loadMore="searchStore.loadMore"
/>
</label>
<div v-if="searchStore.isLoading === false && searchStore.products.data.length > 0">
<RouterLink
v-for="product in searchStore.products.data"
:key="product.id"
class="flex mb-5"
:to="{name: 'product.show', params: {id: product.id}}"
>
<div v-if="product.images && product.images.length > 0" class="avatar">
<div class="w-24 rounded">
<img :src="product.images[0].url" :alt="product.images[0].alt"/>
</div>
</div>
<div class="ml-5 p-0">
<h2 class="card-title">{{ product.name }}</h2>
<Price :price="product.price" :special="product.special"/>
</div>
</RouterLink>
</div>
<div v-if="searchStore.isLoading === true">
@@ -63,24 +71,97 @@
<h2 class="text-xl font-semibold mb-2">Товары не найдены</h2>
<p class="text-sm mb-4">Попробуйте изменить или уточнить запрос</p>
</div>
</BaseViewWrapper>
<div
v-if="! searchStore.isSearchPerformed && searchStore.isLoading === false && searchStore.products.data.length === 0"
class="flex flex-col items-center justify-center text-center py-16 px-10"
>
<div class="avatar-group -space-x-6 mb-4">
<template v-for="product in preloadedProducts" :key="product.id">
<div v-if="product.image" class="avatar">
<div class="w-12">
<img :src="product.image" :alt="product.name"/>
</div>
</div>
<div v-else class="avatar avatar-placeholder">
<div class="bg-neutral text-neutral-content w-12 rounded-full">
<span class="text-3xl">{{ product.name.charAt(0).toUpperCase() }}</span>
</div>
</div>
</template>
<div class="avatar avatar-placeholder">
<div class="bg-neutral text-neutral-content w-12">
<span>{{ renderSmartNumber(productsTotal) }}</span>
</div>
</div>
</div>
<h2 class="text-xl font-semibold mb-2">Поиск товаров</h2>
<p class="text-sm mb-4">Введите запрос, чтобы отобразить подходящие товары.</p>
</div>
</div>
</BaseViewWrapper>
</div>
</template>
<script setup>
import {useSearchStore} from "@/stores/SearchStore.js";
import {useDebounceFn} from "@vueuse/core";
import {onMounted, ref} from "vue";
import {computed, onMounted, onUnmounted, ref} from "vue";
import {useYaMetrikaStore} from "@/stores/yaMetrikaStore.js";
import {useRoute} from "vue-router";
import {YA_METRIKA_GOAL} from "@/constants/yaMetrikaGoals.js";
import BaseViewWrapper from "@/views/BaseViewWrapper.vue";
import Price from "@/components/ProductItem/Price.vue";
import ProductsList from "@/components/ProductsList.vue";
import {renderSmartNumber} from "../helpers.js";
const route = useRoute();
const yaMetrika = useYaMetrikaStore();
const searchStore = useSearchStore();
const searchInput = ref(null);
const debouncedSearch = useDebounceFn(() => searchStore.performSearch(), 500);
const debouncedSearch = useDebounceFn(() => searchStore.performSearch(), 800);
const clearSearch = () => {
searchStore.reset();
if (searchInput.value) {
searchInput.value.focus();
}
};
const hideKeyboard = () => {
if (window.Telegram?.WebApp?.hideKeyboard) {
window.Telegram.WebApp.hideKeyboard();
}
};
const handleEnter = (event) => {
hideKeyboard();
};
const handleSearch = () => {
hideKeyboard();
};
const handleScroll = useDebounceFn(() => {
hideKeyboard();
}, 100);
const preloadedProducts = ref([]);
const productsTotal = ref(0);
const searchWrapperStyle = computed(() => {
const safeTop = getComputedStyle(document.documentElement)
.getPropertyValue('--tg-content-safe-area-inset-top') || '0px';
const tgSafeTop = getComputedStyle(document.documentElement)
.getPropertyValue('--tg-safe-area-inset-top') || '0px';
// Учитываем safe area и отступ для header (если есть app-header)
const topValue = `calc(${safeTop} + ${tgSafeTop})`;
return {
top: topValue
};
});
onMounted(async () => {
window.document.title = 'Поиск';
@@ -88,5 +169,15 @@ onMounted(async () => {
title: 'Поиск',
});
yaMetrika.reachGoal(YA_METRIKA_GOAL.VIEW_SEARCH);
const response = await searchStore.fetchProducts('', 1, 3);
productsTotal.value = response?.meta?.total || 0;
preloadedProducts.value = response.data;
window.addEventListener('scroll', handleScroll, { passive: true });
});
onUnmounted(() => {
window.removeEventListener('scroll', handleScroll);
});
</script>