infoalloggi-monorepo/apps/infoalloggi/src/components/area-riservata/sidebar.tsx

93 lines
2.4 KiB
TypeScript
Raw Normal View History

import { setCookie } from "cookies-next/client";
2025-08-28 18:27:07 +02:00
import { add } from "date-fns";
import { motion } from "framer-motion";
import { Expand, Minimize2 } from "lucide-react";
2025-08-04 17:45:44 +02:00
import { usePathname } from "next/navigation";
import { useState } from "react";
2025-08-04 17:45:44 +02:00
import { ARMinimizableLinks } from "~/components/navbar/mobile-nav";
import { Button } from "~/components/ui/button";
2025-08-28 18:27:07 +02:00
import { cn } from "~/lib/utils";
2025-08-04 17:45:44 +02:00
export const SIDEBAR_COOKIE_NAME = "sidebar_minimized";
2025-08-04 17:45:44 +02:00
export const Sidebar = ({
defaultOpen,
2025-08-28 18:27:07 +02:00
isAdmin,
className,
2025-08-04 17:45:44 +02:00
}: {
defaultOpen: boolean;
2025-08-28 18:27:07 +02:00
isAdmin: boolean;
className?: string;
2025-08-04 17:45:44 +02:00
}) => {
2025-08-28 18:27:07 +02:00
const pathname = usePathname();
const [isAnimating, setIsAnimating] = useState(false);
const [minimized, setMinimized] = useState(defaultOpen);
2025-08-04 17:45:44 +02:00
2025-08-28 18:27:07 +02:00
const toggleSidebar = (v: boolean) => {
setIsAnimating(true);
2025-08-28 18:27:07 +02:00
setMinimized(v);
setCookie(SIDEBAR_COOKIE_NAME, v.toString(), {
2025-08-28 18:27:07 +02:00
expires: add(new Date(), { days: 30 }),
});
setTimeout(() => {
setIsAnimating(false);
}, 300);
2025-08-28 18:27:07 +02:00
};
2025-08-28 18:27:07 +02:00
return (
<motion.nav
animate={{ width: minimized ? 55 : 192 }}
2025-08-29 16:18:32 +02:00
aria-label="Sidebar Menu"
2025-08-28 18:27:07 +02:00
className={cn(
"relative z-30 hidden h-auto w-48 shrink-0 space-y-4 border-muted border-r bg-background pt-3 md:block",
2025-08-28 18:27:07 +02:00
className,
minimized && "cursor-pointer",
)}
>
{minimized && (
<button
aria-label="Expand sidebar"
className="absolute inset-0 z-50 cursor-pointer border-none bg-transparent p-0 hover:bg-black/2"
onClick={(e) => {
e.preventDefault();
e.stopPropagation();
toggleSidebar(false);
}}
type="button"
/>
)}
2025-08-28 18:27:07 +02:00
<div
className={cn(
"flex h-8 items-center justify-between px-4",
2025-08-28 18:27:07 +02:00
minimized && "justify-center px-0",
)}
>
{!minimized && (
2025-10-10 16:18:43 +02:00
<h4 className={cn("w-full font-semibold text-xl")}>Menu</h4>
2025-08-28 18:27:07 +02:00
)}
<Button
aria-label="Toggle Sidebar"
className="px-0 text-base hover:bg-transparent focus-visible:bg-transparent focus-visible:ring-0 focus-visible:ring-offset-0"
2025-08-29 16:18:32 +02:00
onClick={() => toggleSidebar(!minimized)}
variant="ghost"
2025-08-28 18:27:07 +02:00
>
{minimized ? (
<Expand className="size-5" />
2025-08-28 18:27:07 +02:00
) : (
<Minimize2 className="size-5" />
2025-08-28 18:27:07 +02:00
)}
<span className="sr-only">Toggle Menu</span>
</Button>
</div>
<div className={cn("px-2 text-lg")}>
2025-08-28 18:27:07 +02:00
<ARMinimizableLinks
isAdmin={isAdmin}
isAnimating={isAnimating}
2025-08-28 18:27:07 +02:00
minimized={minimized}
2025-08-29 16:18:32 +02:00
pathname={pathname}
2025-08-28 18:27:07 +02:00
/>
</div>
</motion.nav>
);
2025-08-04 17:45:44 +02:00
};