|
| 1 | +import { IconLayoutSidebar } from "@tabler/icons-react"; |
| 2 | + |
| 3 | +import { Button } from "@/components/ui/button"; |
| 4 | +import { useOptionalSidebar } from "@/components/ui/sidebar"; |
| 5 | +import { |
| 6 | + Tooltip, |
| 7 | + TooltipContent, |
| 8 | + TooltipTrigger, |
| 9 | +} from "@/components/ui/tooltip"; |
| 10 | +import { cn } from "@/lib/utils"; |
| 11 | + |
| 12 | +/** |
| 13 | + * ⌘ on Apple platforms, Ctrl everywhere else. The primitive's listener accepts either modifier, so |
| 14 | + * this only decides which of the two to name. |
| 15 | + */ |
| 16 | +const SHORTCUT_LABEL = /Mac|iPhone|iPad|iPod/.test(navigator.userAgent) |
| 17 | + ? "⌘B" |
| 18 | + : "Ctrl+B"; |
| 19 | + |
| 20 | +/** |
| 21 | + * The control that opens and closes the shell's sidebar. |
| 22 | + * |
| 23 | + * WHAT THIS IS FIXING. The sidebar could always collapse — the primitive has had the state, the |
| 24 | + * width transition and a ⌘B shortcut since it was vendored in. Nothing ever rendered a trigger for |
| 25 | + * it. The only affordance was `SidebarRail`, a 16px transparent strip carrying `tabIndex={-1}`, so |
| 26 | + * the eye could not find it and the keyboard could not reach it; and under 768px, where the sidebar |
| 27 | + * becomes a Sheet that starts closed, there was no way to open the roster at all. |
| 28 | + * |
| 29 | + * It is therefore drawn in the chrome each screen already has, and in BOTH states, rather than |
| 30 | + * inside the sidebar it hides — a trigger that disappears along with the sidebar cannot bring it |
| 31 | + * back. |
| 32 | + * |
| 33 | + * Built from `Button` rather than the primitive's `SidebarTrigger` because that component hardcodes |
| 34 | + * its own children after the prop spread, including an `sr-only` "Toggle Sidebar" that would |
| 35 | + * contradict the label below. The state still belongs to the primitive: `toggleSidebar` is its hook. |
| 36 | + */ |
| 37 | +export function SidebarToggle({ className }: { className?: string }) { |
| 38 | + const sidebar = useOptionalSidebar(); |
| 39 | + // No sidebar in scope, so nothing to toggle and nothing to draw. |
| 40 | + if (!sidebar) return null; |
| 41 | + const { isMobile, open, openMobile, toggleSidebar } = sidebar; |
| 42 | + /* |
| 43 | + * The label says what the click will do, not what is on screen. Below 768px the sidebar is an |
| 44 | + * overlay Sheet with its own open state, and `open` describes the desktop pane — reading it there |
| 45 | + * would name the wrong action. |
| 46 | + */ |
| 47 | + const label = (isMobile ? openMobile : open) |
| 48 | + ? "Hide sidebar" |
| 49 | + : "Show sidebar"; |
| 50 | + |
| 51 | + return ( |
| 52 | + <Tooltip> |
| 53 | + <TooltipTrigger |
| 54 | + render={ |
| 55 | + <Button |
| 56 | + aria-label={label} |
| 57 | + className={cn("text-muted-foreground", className)} |
| 58 | + onClick={toggleSidebar} |
| 59 | + size="icon" |
| 60 | + variant="ghost" |
| 61 | + > |
| 62 | + <IconLayoutSidebar className="size-4.5" /> |
| 63 | + </Button> |
| 64 | + } |
| 65 | + /> |
| 66 | + {/* An accelerator nobody is told about is not a feature. */} |
| 67 | + <TooltipContent side="bottom"> |
| 68 | + {label} |
| 69 | + <span className="text-background/60">{SHORTCUT_LABEL}</span> |
| 70 | + </TooltipContent> |
| 71 | + </Tooltip> |
| 72 | + ); |
| 73 | +} |
| 74 | + |
| 75 | +/** |
| 76 | + * The toggle on a screen that draws no header of its own. |
| 77 | + * |
| 78 | + * Three `_app` screens open straight into their content, and the toggle still has to land in the |
| 79 | + * same 48px band it occupies everywhere else — a control that moves between screens is a control |
| 80 | + * somebody has to look for each time. No bottom border: a divider under an otherwise empty bar is a |
| 81 | + * line with nothing to divide. |
| 82 | + */ |
| 83 | +export function SidebarToggleBar() { |
| 84 | + return ( |
| 85 | + <div className="h-12 shrink-0 flex items-center px-3"> |
| 86 | + <SidebarToggle /> |
| 87 | + </div> |
| 88 | + ); |
| 89 | +} |
0 commit comments