AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

sidebar.tsx 21KB

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