A sidebar component from Aceternity UI.
From Aceternity UI
1"use client";
2import { cn } from "@/lib/utils";
3import React, { useState, createContext, useContext } from "react";
4import { AnimatePresence, motion } from "motion/react";
5import { IconMenu2, IconX } from "@tabler/icons-react";
6
7interface Links {
8 label: string;
9 href: string;
10 icon: React.JSX.Element | React.ReactNode;
11}
12
13interface SidebarContextProps {
14 open: boolean;
15 setOpen: React.Dispatch<React.SetStateAction<boolean>>;
16 animate: boolean;
17}
18
19const SidebarContext = createContext<SidebarContextProps | undefined>(
20 undefined
21);
22
23export const useSidebar = () => {
24 const context = useContext(SidebarContext);
25 if (!context) {
26 throw new Error("useSidebar must be used within a SidebarProvider");
27 }
28 return context;
29};
30
31export const SidebarProvider = ({
32 children,
33 open: openProp,
34 setOpen: setOpenProp,
35 animate = true,
36}: {
37 children: React.ReactNode;
38 open?: boolean;
39 setOpen?: React.Dispatch<React.SetStateAction<boolean>>;
40 animate?: boolean;
41}) => {
42 const [openState, setOpenState] = useState(false);
43
44 const open = openProp !== undefined ? openProp : openState;
45 const setOpen = setOpenProp !== undefined ? setOpenProp : setOpenState;
46
47 return (
48 <SidebarContext.Provider value={{ open, setOpen, animate: animate }}>
49 {children}
50 </SidebarContext.Provider>
51 );
52};
53
54export const Sidebar = ({
55 children,
56 open,
57 setOpen,
58 animate,
59}: {
60 children: React.ReactNode;
61 open?: boolean;
62 setOpen?: React.Dispatch<React.SetStateAction<boolean>>;
63 animate?: boolean;
64}) => {
65 return (
66 <SidebarProvider open={open} setOpen={setOpen} animate={animate}>
67 {children}
68 </SidebarProvider>
69 );
70};
71
72export const SidebarBody = (props: React.ComponentProps<typeof motion.div>) => {
73 return (
74 <>
75 <DesktopSidebar {...props} />
76 <MobileSidebar {...(props as React.ComponentProps<"div">)} />
77 </>
78 );
79};
80
81export const DesktopSidebar = ({
82 className,
83 children,
84 ...props
85}: React.ComponentProps<typeof motion.div>) => {
86 const { open, setOpen, animate } = useSidebar();
87 return (
88 <>
89 <motion.div
90 className={cn(
91 "h-full px-4 py-4 hidden md:flex md:flex-col bg-neutral-100 dark:bg-neutral-800 w-[300px] shrink-0",
92 className
93 )}
94 animate={{
95 width: animate ? (open ? "300px" : "60px") : "300px",
96 }}
97 onMouseEnter={() => setOpen(true)}
98 onMouseLeave={() => setOpen(false)}
99 {...props}
100 >
101 {children}
102 </motion.div>
103 </>
104 );
105};
106
107export const MobileSidebar = ({
108 className,
109 children,
110 ...props
111}: React.ComponentProps<"div">) => {
112 const { open, setOpen } = useSidebar();
113 return (
114 <>
115 <div
116 className={cn(
117 "h-10 px-4 py-4 flex flex-row md:hidden items-center justify-between bg-neutral-100 dark:bg-neutral-800 w-full"
118 )}
119 {...props}
120 >
121 <div className="flex justify-end z-20 w-full">
122 <IconMenu2
123 className="text-neutral-800 dark:text-neutral-200"
124 onClick={() => setOpen(!open)}
125 />
126 </div>
127 <AnimatePresence>
128 {open && (
129 <motion.div
130 initial={{ x: "-100%", opacity: 0 }}
131 animate={{ x: 0, opacity: 1 }}
132 exit={{ x: "-100%", opacity: 0 }}
133 transition={{
134 duration: 0.3,
135 ease: "easeInOut",
136 }}
137 className={cn(
138 "fixed h-full w-full inset-0 bg-white dark:bg-neutral-900 p-10 z-[100] flex flex-col justify-between",
139 className
140 )}
141 >
142 <div
143 className="absolute right-10 top-10 z-50 text-neutral-800 dark:text-neutral-200"
144 onClick={() => setOpen(!open)}
145 >
146 <IconX />
147 </div>
148 {children}
149 </motion.div>
150 )}
151 </AnimatePresence>
152 </div>
153 </>
154 );
155};
156
157export const SidebarLink = ({
158 link,
159 className,
160 ...props
161}: {
162 link: Links;
163 className?: string;
164}) => {
165 const { open, animate } = useSidebar();
166 return (
167 <a
168 href={link.href}
169 className={cn(
170 "flex items-center justify-start gap-2 group/sidebar py-2",
171 className
172 )}
173 {...props}
174 >
175 {link.icon}
176
177 <motion.span
178 animate={{
179 display: animate ? (open ? "inline-block" : "none") : "inline-block",
180 opacity: animate ? (open ? 1 : 0) : 1,
181 }}
182 className="text-neutral-700 dark:text-neutral-200 text-sm group-hover/sidebar:translate-x-1 transition duration-150 whitespace-pre inline-block !p-0 !m-0"
183 >
184 {link.label}
185 </motion.span>
186 </a>
187 );
188};
189