Files
nahodka-front/src/components/feed/FeedTopBar.tsx
2026-10-09 00:23:16 +03:00

150 lines
5.2 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { useState, useRef, useEffect } from "react";
import { motion, AnimatePresence } from "framer-motion";
import { useTranslation } from "react-i18next";
import MenuIconBase from "../icons/MenuIcon";
import MainNahodkaLogoBase from "../MainNahodkaLogo";
import type { Ref, ComponentType } from "react";
// Legacy .jsx-компоненты: TS инферирует их сигнатуры из JS неточно —
// приводим к нормальному виду для корректной проверки пропсов.
const MainNahodkaLogo = MainNahodkaLogoBase as unknown as ComponentType<{
className?: string;
width?: number;
height?: number;
}>;
const MenuIcon = MenuIconBase as unknown as ComponentType<{ className?: string }>;
interface FeedTopBarProps {
onMenuClick: () => void;
typeFilter: "loss" | "found" | null;
onTypeFilterChange: (key: "loss" | "found" | null) => void;
hidden?: boolean;
/** Реф на сам стеклянный бар — родитель меряет его высоту для CSS-переменной --feed-fade-height */
topBarRef?: Ref<HTMLDivElement>;
}
/**
* FeedTopBar — «стеклянная» шапка ленты (Telegram-style).
*
* Эффекты (разделены, чтобы было понятно что за что отвечает):
* - стекло: backdrop-filter blur+saturate + полупрозрачный фон (см. .f-topbar в layout.css);
* - плавное скрытие/появление при скролле: родитель передаёт `hidden`
* (значение из useScrollDirection), CSS делает transform: translateY(-100%)
* — скрывается вверх, контент ленты проплывает под ним;
* - self-contained: вся логика дропдауна фильтра типа осталась здесь.
*/
export default function FeedTopBar({
onMenuClick,
typeFilter,
onTypeFilterChange,
hidden = false,
topBarRef,
}: FeedTopBarProps) {
const { t } = useTranslation();
const [open, setOpen] = useState(false);
const menuRef = useRef<HTMLDivElement>(null);
const btnRef = useRef<HTMLButtonElement>(null);
const options: { key: "loss" | "found" | null; label: string }[] = [
{ key: "loss", label: t("feed.filterLoss") },
{ key: null, label: t("feed.filterAll") },
{ key: "found", label: t("feed.filterFound") },
];
useEffect(() => {
if (!open) return;
const handler = (e: MouseEvent) => {
if (
menuRef.current && !menuRef.current.contains(e.target as Node) &&
btnRef.current && !btnRef.current.contains(e.target as Node)
) {
setOpen(false);
}
};
document.addEventListener("mousedown", handler);
return () => document.removeEventListener("mousedown", handler);
}, [open]);
const handleSelect = (key: "loss" | "found" | null) => {
onTypeFilterChange(key);
setOpen(false);
};
return (
<div className="f-topbar-dropdown-wrap">
<div className={`f-topbar${hidden ? " f-topbar-hidden" : ""}`} ref={topBarRef}>
<button
type="button"
className="f-menu-btn"
onClick={onMenuClick}
aria-label={String(t("feed.openMenu"))}
>
<MenuIcon />
</button>
<button
ref={btnRef}
type="button"
className="f-logo-btn"
onClick={() => setOpen((v) => !v)}
aria-label={String(t("feed.openFilter"))}
aria-haspopup="menu"
aria-expanded={open}
>
<MainNahodkaLogo className="f-topbar-logo" />
<svg
className={`f-topbar-chevron${open ? " open" : ""}`}
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
strokeWidth="2.5"
strokeLinecap="round"
strokeLinejoin="round"
aria-hidden="true"
>
<polyline points="6 9 12 15 18 9" />
</svg>
</button>
<span className="f-topbar-spacer" />
</div>
<AnimatePresence>
{open && (
<motion.div
className="f-topbar-dropdown-overlay"
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
exit={{ opacity: 0 }}
transition={{ duration: 0.15 }}
onClick={() => setOpen(false)}
/>
)}
</AnimatePresence>
<AnimatePresence>
{open && (
<motion.div
ref={menuRef}
className="f-topbar-dropdown-menu"
initial={{ opacity: 0, scale: 0.95, y: -8 }}
animate={{ opacity: 1, scale: 1, y: 0 }}
exit={{ opacity: 0, scale: 0.95, y: -8 }}
transition={{ duration: 0.18, ease: "easeOut" }}
>
{options.map((opt) => (
<button
key={opt.key ?? "all"}
type="button"
className={`f-topbar-dropdown-item${typeFilter === opt.key ? " active" : ""}`}
onClick={() => handleSelect(opt.key)}
>
{opt.label}
</button>
))}
</motion.div>
)}
</AnimatePresence>
</div>
);
}