Sidebar Toggle

Responsive toggle button for opening and closing the sidebar.

Signaler un bug

Preview

Switch between light and dark to inspect the embedded Storybook preview.

Installation

pnpm dlx shadcn@latest add https://ui.vllnt.ai/r/sidebar-toggle.json
bash

Storybook

Explorez les variantes, controles et verifications d'accessibilite dans le playground Storybook interactif.

Voir dans Storybook

Code

"use client";

import { Menu, X } from "lucide-react";

import { cn } from "../../lib/utils";
import { Button } from "../button";

export type SidebarToggleProps = {
  className?: string;
  /** Called when user clicks the toggle. */
  onToggle: () => void;
  /** Whether the sidebar is open. */
  open: boolean;
};

/** Responsive sidebar toggle button that shows Menu/X icons based on state. */
export function SidebarToggle({
  className,
  onToggle,
  open,
}: SidebarToggleProps) {
  return (
    <>
      {/* Mobile: shows X when open, Menu when closed */}
      <Button
        className={cn("lg:hidden", className)}
        onClick={onToggle}
        size="icon"
        variant="ghost"
      >
        {open ? <X className="size-5" /> : <Menu className="size-5" />}
      </Button>
      {/* Desktop: always shows Menu icon */}
      <Button
        className={cn("hidden lg:flex", className)}
        onClick={onToggle}
        size="icon"
        variant="ghost"
      >
        <Menu className="size-5" />
      </Button>
    </>
  );
}
typescript

Dependances

  • @vllnt/ui@^0.2.1
  • lucide-react