sidebar.tsx 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723
  1. 'use client';
  2. import * as React from 'react';
  3. import { mergeProps } from '@base-ui/react/merge-props';
  4. import { useRender } from '@base-ui/react/use-render';
  5. import { cva, type VariantProps } from 'class-variance-authority';
  6. import { useIsMobile } from '@/hooks/use-mobile';
  7. import { cn } from '@/lib/utils';
  8. import { Button } from '@/components/ui/button';
  9. import { Input } from '@/components/ui/input';
  10. import { Separator } from '@/components/ui/separator';
  11. import {
  12. Sheet,
  13. SheetContent,
  14. SheetDescription,
  15. SheetHeader,
  16. SheetTitle,
  17. } from '@/components/ui/sheet';
  18. import { Skeleton } from '@/components/ui/skeleton';
  19. import {
  20. Tooltip,
  21. TooltipContent,
  22. TooltipTrigger,
  23. } from '@/components/ui/tooltip';
  24. import { PanelLeftIcon } from 'lucide-react';
  25. const SIDEBAR_COOKIE_NAME = 'sidebar_state';
  26. const SIDEBAR_COOKIE_MAX_AGE = 60 * 60 * 24 * 7;
  27. const SIDEBAR_WIDTH = '16rem';
  28. const SIDEBAR_WIDTH_MOBILE = '18rem';
  29. const SIDEBAR_WIDTH_ICON = '3rem';
  30. const SIDEBAR_KEYBOARD_SHORTCUT = 'b';
  31. type SidebarContextProps = {
  32. state: 'expanded' | 'collapsed';
  33. open: boolean;
  34. setOpen: (open: boolean) => void;
  35. openMobile: boolean;
  36. setOpenMobile: (open: boolean) => void;
  37. isMobile: boolean;
  38. toggleSidebar: () => void;
  39. };
  40. const SidebarContext = React.createContext<SidebarContextProps | null>(null);
  41. function useSidebar() {
  42. const context = React.useContext(SidebarContext);
  43. if (!context) {
  44. throw new Error('useSidebar must be used within a SidebarProvider.');
  45. }
  46. return context;
  47. }
  48. function SidebarProvider({
  49. defaultOpen = true,
  50. open: openProp,
  51. onOpenChange: setOpenProp,
  52. className,
  53. style,
  54. children,
  55. ...props
  56. }: React.ComponentProps<'div'> & {
  57. defaultOpen?: boolean;
  58. open?: boolean;
  59. onOpenChange?: (open: boolean) => void;
  60. }) {
  61. const isMobile = useIsMobile();
  62. const [openMobile, setOpenMobile] = React.useState(false);
  63. // This is the internal state of the sidebar.
  64. // We use openProp and setOpenProp for control from outside the component.
  65. const [_open, _setOpen] = React.useState(defaultOpen);
  66. const open = openProp ?? _open;
  67. const setOpen = React.useCallback(
  68. (value: boolean | ((value: boolean) => boolean)) => {
  69. const openState = typeof value === 'function' ? value(open) : value;
  70. if (setOpenProp) {
  71. setOpenProp(openState);
  72. } else {
  73. _setOpen(openState);
  74. }
  75. // This sets the cookie to keep the sidebar state.
  76. document.cookie = `${SIDEBAR_COOKIE_NAME}=${openState}; path=/; max-age=${SIDEBAR_COOKIE_MAX_AGE}`;
  77. },
  78. [setOpenProp, open],
  79. );
  80. // Helper to toggle the sidebar.
  81. const toggleSidebar = React.useCallback(() => {
  82. return isMobile ? setOpenMobile((open) => !open) : setOpen((open) => !open);
  83. }, [isMobile, setOpen, setOpenMobile]);
  84. // Adds a keyboard shortcut to toggle the sidebar.
  85. React.useEffect(() => {
  86. const handleKeyDown = (event: KeyboardEvent) => {
  87. if (
  88. event.key === SIDEBAR_KEYBOARD_SHORTCUT &&
  89. (event.metaKey || event.ctrlKey)
  90. ) {
  91. event.preventDefault();
  92. toggleSidebar();
  93. }
  94. };
  95. window.addEventListener('keydown', handleKeyDown);
  96. return () => window.removeEventListener('keydown', handleKeyDown);
  97. }, [toggleSidebar]);
  98. // We add a state so that we can do data-state="expanded" or "collapsed".
  99. // This makes it easier to style the sidebar with Tailwind classes.
  100. const state = open ? 'expanded' : 'collapsed';
  101. const contextValue = React.useMemo<SidebarContextProps>(
  102. () => ({
  103. state,
  104. open,
  105. setOpen,
  106. isMobile,
  107. openMobile,
  108. setOpenMobile,
  109. toggleSidebar,
  110. }),
  111. [state, open, setOpen, isMobile, openMobile, setOpenMobile, toggleSidebar],
  112. );
  113. return (
  114. <SidebarContext.Provider value={contextValue}>
  115. <div
  116. data-slot="sidebar-wrapper"
  117. style={
  118. {
  119. '--sidebar-width': SIDEBAR_WIDTH,
  120. '--sidebar-width-icon': SIDEBAR_WIDTH_ICON,
  121. ...style,
  122. } as React.CSSProperties
  123. }
  124. className={cn(
  125. 'group/sidebar-wrapper flex min-h-svh w-full has-data-[variant=inset]:bg-sidebar',
  126. className,
  127. )}
  128. {...props}
  129. >
  130. {children}
  131. </div>
  132. </SidebarContext.Provider>
  133. );
  134. }
  135. function Sidebar({
  136. side = 'left',
  137. variant = 'sidebar',
  138. collapsible = 'offcanvas',
  139. className,
  140. children,
  141. dir,
  142. ...props
  143. }: React.ComponentProps<'div'> & {
  144. side?: 'left' | 'right';
  145. variant?: 'sidebar' | 'floating' | 'inset';
  146. collapsible?: 'offcanvas' | 'icon' | 'none';
  147. }) {
  148. const { isMobile, state, openMobile, setOpenMobile } = useSidebar();
  149. if (collapsible === 'none') {
  150. return (
  151. <div
  152. data-slot="sidebar"
  153. className={cn(
  154. 'flex h-full w-(--sidebar-width) flex-col bg-sidebar text-sidebar-foreground',
  155. className,
  156. )}
  157. {...props}
  158. >
  159. {children}
  160. </div>
  161. );
  162. }
  163. if (isMobile) {
  164. return (
  165. <Sheet open={openMobile} onOpenChange={setOpenMobile} {...props}>
  166. <SheetContent
  167. dir={dir}
  168. data-sidebar="sidebar"
  169. data-slot="sidebar"
  170. data-mobile="true"
  171. className="w-(--sidebar-width) bg-sidebar p-0 text-sidebar-foreground [&>button]:hidden"
  172. style={
  173. {
  174. '--sidebar-width': SIDEBAR_WIDTH_MOBILE,
  175. } as React.CSSProperties
  176. }
  177. side={side}
  178. >
  179. <SheetHeader className="sr-only">
  180. <SheetTitle>Sidebar</SheetTitle>
  181. <SheetDescription>Displays the mobile sidebar.</SheetDescription>
  182. </SheetHeader>
  183. <div className="flex h-full w-full flex-col">{children}</div>
  184. </SheetContent>
  185. </Sheet>
  186. );
  187. }
  188. return (
  189. <div
  190. className="group peer hidden text-sidebar-foreground md:block"
  191. data-state={state}
  192. data-collapsible={state === 'collapsed' ? collapsible : ''}
  193. data-variant={variant}
  194. data-side={side}
  195. data-slot="sidebar"
  196. >
  197. {/* This is what handles the sidebar gap on desktop */}
  198. <div
  199. data-slot="sidebar-gap"
  200. className={cn(
  201. 'transition-[width] duration-200 ease-linear relative w-(--sidebar-width) bg-transparent',
  202. 'group-data-[collapsible=offcanvas]:w-0',
  203. 'group-data-[side=right]:rotate-180',
  204. variant === 'floating' || variant === 'inset'
  205. ? 'group-data-[collapsible=icon]:w-[calc(var(--sidebar-width-icon)+(--spacing(4)))]'
  206. : 'group-data-[collapsible=icon]:w-(--sidebar-width-icon)',
  207. )}
  208. />
  209. <div
  210. data-slot="sidebar-container"
  211. data-side={side}
  212. className={cn(
  213. 'fixed inset-y-0 z-10 hidden h-svh w-(--sidebar-width) transition-[left,right,width] duration-200 ease-linear data-[side=left]:left-0 data-[side=left]:group-data-[collapsible=offcanvas]:left-[calc(var(--sidebar-width)*-1)] data-[side=right]:right-0 data-[side=right]:group-data-[collapsible=offcanvas]:right-[calc(var(--sidebar-width)*-1)] md:flex',
  214. // Adjust the padding for floating and inset variants.
  215. variant === 'floating' || variant === 'inset'
  216. ? 'p-2 group-data-[collapsible=icon]:w-[calc(var(--sidebar-width-icon)+(--spacing(4))+2px)]'
  217. : 'group-data-[collapsible=icon]:w-(--sidebar-width-icon) group-data-[side=left]:border-r group-data-[side=right]:border-l',
  218. className,
  219. )}
  220. {...props}
  221. >
  222. <div
  223. data-sidebar="sidebar"
  224. data-slot="sidebar-inner"
  225. className="bg-sidebar group-data-[variant=floating]:ring-sidebar-border group-data-[variant=floating]:rounded-lg group-data-[variant=floating]:shadow-sm group-data-[variant=floating]:ring-1 flex size-full flex-col"
  226. >
  227. {children}
  228. </div>
  229. </div>
  230. </div>
  231. );
  232. }
  233. function SidebarTrigger({
  234. className,
  235. onClick,
  236. ...props
  237. }: React.ComponentProps<typeof Button>) {
  238. const { toggleSidebar } = useSidebar();
  239. return (
  240. <Button
  241. data-sidebar="trigger"
  242. data-slot="sidebar-trigger"
  243. variant="ghost"
  244. size="icon-sm"
  245. className={cn(className)}
  246. onClick={(event) => {
  247. onClick?.(event);
  248. toggleSidebar();
  249. }}
  250. {...props}
  251. >
  252. <PanelLeftIcon className="cn-rtl-flip" />
  253. <span className="sr-only">Toggle Sidebar</span>
  254. </Button>
  255. );
  256. }
  257. function SidebarRail({ className, ...props }: React.ComponentProps<'button'>) {
  258. const { toggleSidebar } = useSidebar();
  259. return (
  260. <button
  261. data-sidebar="rail"
  262. data-slot="sidebar-rail"
  263. aria-label="Toggle Sidebar"
  264. tabIndex={-1}
  265. onClick={toggleSidebar}
  266. title="Toggle Sidebar"
  267. className={cn(
  268. 'hover:after:bg-sidebar-border absolute inset-y-0 z-20 hidden w-4 transition-all ease-linear group-data-[side=left]:-right-4 group-data-[side=right]:left-0 after:absolute after:inset-y-0 after:start-1/2 after:w-[2px] sm:flex ltr:-translate-x-1/2 rtl:-translate-x-1/2',
  269. 'in-data-[side=left]:cursor-w-resize in-data-[side=right]:cursor-e-resize',
  270. '[[data-side=left][data-state=collapsed]_&]:cursor-e-resize [[data-side=right][data-state=collapsed]_&]:cursor-w-resize',
  271. 'group-data-[collapsible=offcanvas]:translate-x-0 group-data-[collapsible=offcanvas]:after:left-full hover:group-data-[collapsible=offcanvas]:bg-sidebar',
  272. '[[data-side=left][data-collapsible=offcanvas]_&]:-right-2',
  273. '[[data-side=right][data-collapsible=offcanvas]_&]:-left-2',
  274. className,
  275. )}
  276. {...props}
  277. />
  278. );
  279. }
  280. function SidebarInset({ className, ...props }: React.ComponentProps<'main'>) {
  281. return (
  282. <main
  283. data-slot="sidebar-inset"
  284. className={cn(
  285. 'bg-background md:peer-data-[variant=inset]:m-2 md:peer-data-[variant=inset]:ml-0 md:peer-data-[variant=inset]:rounded-xl md:peer-data-[variant=inset]:shadow-sm md:peer-data-[variant=inset]:peer-data-[state=collapsed]:ml-2 relative flex w-full flex-1 flex-col',
  286. className,
  287. )}
  288. {...props}
  289. />
  290. );
  291. }
  292. function SidebarInput({
  293. className,
  294. ...props
  295. }: React.ComponentProps<typeof Input>) {
  296. return (
  297. <Input
  298. data-slot="sidebar-input"
  299. data-sidebar="input"
  300. className={cn('bg-background h-8 w-full shadow-none', className)}
  301. {...props}
  302. />
  303. );
  304. }
  305. function SidebarHeader({ className, ...props }: React.ComponentProps<'div'>) {
  306. return (
  307. <div
  308. data-slot="sidebar-header"
  309. data-sidebar="header"
  310. className={cn('gap-2 p-2 flex flex-col', className)}
  311. {...props}
  312. />
  313. );
  314. }
  315. function SidebarFooter({ className, ...props }: React.ComponentProps<'div'>) {
  316. return (
  317. <div
  318. data-slot="sidebar-footer"
  319. data-sidebar="footer"
  320. className={cn('gap-2 p-2 flex flex-col', className)}
  321. {...props}
  322. />
  323. );
  324. }
  325. function SidebarSeparator({
  326. className,
  327. ...props
  328. }: React.ComponentProps<typeof Separator>) {
  329. return (
  330. <Separator
  331. data-slot="sidebar-separator"
  332. data-sidebar="separator"
  333. className={cn('bg-sidebar-border mx-2 w-auto', className)}
  334. {...props}
  335. />
  336. );
  337. }
  338. function SidebarContent({ className, ...props }: React.ComponentProps<'div'>) {
  339. return (
  340. <div
  341. data-slot="sidebar-content"
  342. data-sidebar="content"
  343. className={cn(
  344. 'no-scrollbar gap-0 flex min-h-0 flex-1 flex-col overflow-auto group-data-[collapsible=icon]:overflow-hidden',
  345. className,
  346. )}
  347. {...props}
  348. />
  349. );
  350. }
  351. function SidebarGroup({ className, ...props }: React.ComponentProps<'div'>) {
  352. return (
  353. <div
  354. data-slot="sidebar-group"
  355. data-sidebar="group"
  356. className={cn('p-2 relative flex w-full min-w-0 flex-col', className)}
  357. {...props}
  358. />
  359. );
  360. }
  361. function SidebarGroupLabel({
  362. className,
  363. render,
  364. ...props
  365. }: useRender.ComponentProps<'div'> & React.ComponentProps<'div'>) {
  366. return useRender({
  367. defaultTagName: 'div',
  368. props: mergeProps<'div'>(
  369. {
  370. className: cn(
  371. 'text-sidebar-foreground/70 ring-sidebar-ring h-8 rounded-md px-2 text-xs font-medium transition-[margin,opacity] duration-200 ease-linear group-data-[collapsible=icon]:-mt-8 group-data-[collapsible=icon]:opacity-0 focus-visible:ring-2 [&>svg]:size-4 flex shrink-0 items-center outline-hidden [&>svg]:shrink-0',
  372. className,
  373. ),
  374. },
  375. props,
  376. ),
  377. render,
  378. state: {
  379. slot: 'sidebar-group-label',
  380. sidebar: 'group-label',
  381. },
  382. });
  383. }
  384. function SidebarGroupAction({
  385. className,
  386. render,
  387. ...props
  388. }: useRender.ComponentProps<'button'> & React.ComponentProps<'button'>) {
  389. return useRender({
  390. defaultTagName: 'button',
  391. props: mergeProps<'button'>(
  392. {
  393. className: cn(
  394. 'text-sidebar-foreground ring-sidebar-ring hover:bg-sidebar-accent hover:text-sidebar-accent-foreground absolute top-3.5 right-3 w-5 rounded-md p-0 focus-visible:ring-2 [&>svg]:size-4 flex aspect-square items-center justify-center outline-hidden transition-transform group-data-[collapsible=icon]:hidden after:absolute after:-inset-2 md:after:hidden [&>svg]:shrink-0',
  395. className,
  396. ),
  397. },
  398. props,
  399. ),
  400. render,
  401. state: {
  402. slot: 'sidebar-group-action',
  403. sidebar: 'group-action',
  404. },
  405. });
  406. }
  407. function SidebarGroupContent({
  408. className,
  409. ...props
  410. }: React.ComponentProps<'div'>) {
  411. return (
  412. <div
  413. data-slot="sidebar-group-content"
  414. data-sidebar="group-content"
  415. className={cn('text-sm w-full', className)}
  416. {...props}
  417. />
  418. );
  419. }
  420. function SidebarMenu({ className, ...props }: React.ComponentProps<'ul'>) {
  421. return (
  422. <ul
  423. data-slot="sidebar-menu"
  424. data-sidebar="menu"
  425. className={cn('gap-0 flex w-full min-w-0 flex-col', className)}
  426. {...props}
  427. />
  428. );
  429. }
  430. function SidebarMenuItem({ className, ...props }: React.ComponentProps<'li'>) {
  431. return (
  432. <li
  433. data-slot="sidebar-menu-item"
  434. data-sidebar="menu-item"
  435. className={cn('group/menu-item relative', className)}
  436. {...props}
  437. />
  438. );
  439. }
  440. const sidebarMenuButtonVariants = cva(
  441. 'ring-sidebar-ring hover:bg-sidebar-accent hover:text-sidebar-accent-foreground active:bg-sidebar-accent active:text-sidebar-accent-foreground data-active:bg-sidebar-accent data-active:text-sidebar-accent-foreground data-open:hover:bg-sidebar-accent data-open:hover:text-sidebar-accent-foreground gap-2 rounded-md p-2 text-left text-sm transition-[width,height,padding] group-has-data-[sidebar=menu-action]/menu-item:pr-8 group-data-[collapsible=icon]:size-8! group-data-[collapsible=icon]:p-2! focus-visible:ring-2 data-active:font-medium peer/menu-button group/menu-button flex w-full items-center overflow-hidden outline-hidden disabled:pointer-events-none disabled:opacity-50 aria-disabled:pointer-events-none aria-disabled:opacity-50 [&_svg]:size-4 [&_svg]:shrink-0 [&>span:last-child]:truncate',
  442. {
  443. variants: {
  444. variant: {
  445. default: 'hover:bg-sidebar-accent hover:text-sidebar-accent-foreground',
  446. outline:
  447. 'bg-background hover:bg-sidebar-accent hover:text-sidebar-accent-foreground shadow-[0_0_0_1px_var(--sidebar-border)] hover:shadow-[0_0_0_1px_var(--sidebar-accent)]',
  448. },
  449. size: {
  450. default: 'h-8 text-sm',
  451. sm: 'h-7 text-xs',
  452. lg: 'h-12 text-sm group-data-[collapsible=icon]:p-0!',
  453. },
  454. },
  455. defaultVariants: {
  456. variant: 'default',
  457. size: 'default',
  458. },
  459. },
  460. );
  461. function SidebarMenuButton({
  462. render,
  463. isActive = false,
  464. variant = 'default',
  465. size = 'default',
  466. tooltip,
  467. className,
  468. ...props
  469. }: useRender.ComponentProps<'button'> &
  470. React.ComponentProps<'button'> & {
  471. isActive?: boolean;
  472. tooltip?: string | React.ComponentProps<typeof TooltipContent>;
  473. } & VariantProps<typeof sidebarMenuButtonVariants>) {
  474. const { isMobile, state } = useSidebar();
  475. const comp = useRender({
  476. defaultTagName: 'button',
  477. props: mergeProps<'button'>(
  478. {
  479. className: cn(sidebarMenuButtonVariants({ variant, size }), className),
  480. },
  481. props,
  482. ),
  483. render: !tooltip ? render : <TooltipTrigger render={render} />,
  484. state: {
  485. slot: 'sidebar-menu-button',
  486. sidebar: 'menu-button',
  487. size,
  488. active: isActive,
  489. },
  490. });
  491. if (!tooltip) {
  492. return comp;
  493. }
  494. if (typeof tooltip === 'string') {
  495. tooltip = {
  496. children: tooltip,
  497. };
  498. }
  499. return (
  500. <Tooltip>
  501. {comp}
  502. <TooltipContent
  503. side="right"
  504. align="center"
  505. hidden={state !== 'collapsed' || isMobile}
  506. {...tooltip}
  507. />
  508. </Tooltip>
  509. );
  510. }
  511. function SidebarMenuAction({
  512. className,
  513. render,
  514. showOnHover = false,
  515. ...props
  516. }: useRender.ComponentProps<'button'> &
  517. React.ComponentProps<'button'> & {
  518. showOnHover?: boolean;
  519. }) {
  520. return useRender({
  521. defaultTagName: 'button',
  522. props: mergeProps<'button'>(
  523. {
  524. className: cn(
  525. 'text-sidebar-foreground ring-sidebar-ring hover:bg-sidebar-accent hover:text-sidebar-accent-foreground peer-hover/menu-button:text-sidebar-accent-foreground absolute top-1.5 right-1 aspect-square w-5 rounded-md p-0 peer-data-[size=default]/menu-button:top-1.5 peer-data-[size=lg]/menu-button:top-2.5 peer-data-[size=sm]/menu-button:top-1 focus-visible:ring-2 [&>svg]:size-4 flex items-center justify-center outline-hidden transition-transform group-data-[collapsible=icon]:hidden after:absolute after:-inset-2 md:after:hidden [&>svg]:shrink-0',
  526. showOnHover &&
  527. 'group-focus-within/menu-item:opacity-100 group-hover/menu-item:opacity-100 peer-data-active/menu-button:text-sidebar-accent-foreground aria-expanded:opacity-100 md:opacity-0',
  528. className,
  529. ),
  530. },
  531. props,
  532. ),
  533. render,
  534. state: {
  535. slot: 'sidebar-menu-action',
  536. sidebar: 'menu-action',
  537. },
  538. });
  539. }
  540. function SidebarMenuBadge({
  541. className,
  542. ...props
  543. }: React.ComponentProps<'div'>) {
  544. return (
  545. <div
  546. data-slot="sidebar-menu-badge"
  547. data-sidebar="menu-badge"
  548. className={cn(
  549. 'text-sidebar-foreground peer-hover/menu-button:text-sidebar-accent-foreground peer-data-active/menu-button:text-sidebar-accent-foreground pointer-events-none absolute right-1 h-5 min-w-5 rounded-md px-1 text-xs font-medium peer-data-[size=default]/menu-button:top-1.5 peer-data-[size=lg]/menu-button:top-2.5 peer-data-[size=sm]/menu-button:top-1 flex items-center justify-center tabular-nums select-none group-data-[collapsible=icon]:hidden',
  550. className,
  551. )}
  552. {...props}
  553. />
  554. );
  555. }
  556. function SidebarMenuSkeleton({
  557. className,
  558. showIcon = false,
  559. ...props
  560. }: React.ComponentProps<'div'> & {
  561. showIcon?: boolean;
  562. }) {
  563. // Random width between 50 to 90%.
  564. const [width] = React.useState(() => {
  565. return `${Math.floor(Math.random() * 40) + 50}%`;
  566. });
  567. return (
  568. <div
  569. data-slot="sidebar-menu-skeleton"
  570. data-sidebar="menu-skeleton"
  571. className={cn('h-8 gap-2 rounded-md px-2 flex items-center', className)}
  572. {...props}
  573. >
  574. {showIcon && (
  575. <Skeleton
  576. className="size-4 rounded-md"
  577. data-sidebar="menu-skeleton-icon"
  578. />
  579. )}
  580. <Skeleton
  581. className="h-4 max-w-(--skeleton-width) flex-1"
  582. data-sidebar="menu-skeleton-text"
  583. style={
  584. {
  585. '--skeleton-width': width,
  586. } as React.CSSProperties
  587. }
  588. />
  589. </div>
  590. );
  591. }
  592. function SidebarMenuSub({ className, ...props }: React.ComponentProps<'ul'>) {
  593. return (
  594. <ul
  595. data-slot="sidebar-menu-sub"
  596. data-sidebar="menu-sub"
  597. className={cn(
  598. 'border-sidebar-border mx-3.5 translate-x-px gap-1 border-l px-2.5 py-0.5 group-data-[collapsible=icon]:hidden flex min-w-0 flex-col',
  599. className,
  600. )}
  601. {...props}
  602. />
  603. );
  604. }
  605. function SidebarMenuSubItem({
  606. className,
  607. ...props
  608. }: React.ComponentProps<'li'>) {
  609. return (
  610. <li
  611. data-slot="sidebar-menu-sub-item"
  612. data-sidebar="menu-sub-item"
  613. className={cn('group/menu-sub-item relative', className)}
  614. {...props}
  615. />
  616. );
  617. }
  618. function SidebarMenuSubButton({
  619. render,
  620. size = 'md',
  621. isActive = false,
  622. className,
  623. ...props
  624. }: useRender.ComponentProps<'a'> &
  625. React.ComponentProps<'a'> & {
  626. size?: 'sm' | 'md';
  627. isActive?: boolean;
  628. }) {
  629. return useRender({
  630. defaultTagName: 'a',
  631. props: mergeProps<'a'>(
  632. {
  633. className: cn(
  634. 'text-sidebar-foreground ring-sidebar-ring hover:bg-sidebar-accent hover:text-sidebar-accent-foreground active:bg-sidebar-accent active:text-sidebar-accent-foreground [&>svg]:text-sidebar-accent-foreground data-active:bg-sidebar-accent data-active:text-sidebar-accent-foreground h-7 gap-2 rounded-md px-2 focus-visible:ring-2 data-[size=md]:text-sm data-[size=sm]:text-xs [&>svg]:size-4 flex min-w-0 -translate-x-px items-center overflow-hidden outline-hidden group-data-[collapsible=icon]:hidden disabled:pointer-events-none disabled:opacity-50 aria-disabled:pointer-events-none aria-disabled:opacity-50 [&>span:last-child]:truncate [&>svg]:shrink-0',
  635. className,
  636. ),
  637. },
  638. props,
  639. ),
  640. render,
  641. state: {
  642. slot: 'sidebar-menu-sub-button',
  643. sidebar: 'menu-sub-button',
  644. size,
  645. active: isActive,
  646. },
  647. });
  648. }
  649. export {
  650. Sidebar,
  651. SidebarContent,
  652. SidebarFooter,
  653. SidebarGroup,
  654. SidebarGroupAction,
  655. SidebarGroupContent,
  656. SidebarGroupLabel,
  657. SidebarHeader,
  658. SidebarInput,
  659. SidebarInset,
  660. SidebarMenu,
  661. SidebarMenuAction,
  662. SidebarMenuBadge,
  663. SidebarMenuButton,
  664. SidebarMenuItem,
  665. SidebarMenuSkeleton,
  666. SidebarMenuSub,
  667. SidebarMenuSubButton,
  668. SidebarMenuSubItem,
  669. SidebarProvider,
  670. SidebarRail,
  671. SidebarSeparator,
  672. SidebarTrigger,
  673. useSidebar,
  674. };