Skip to content
Application

Sidebar

Collapsible nav with sections, active indicator, optional brand and footer slots.

MotionlayoutId active pill on spring.snappy

Install

Install Kineto and follow the theme setup once in your app.

bash
npm install kineto
tsx
import { Sidebar } from "kineto/sidebar";

Preview

tsx
"use client";

import * as React from "react";
import { motion, useReducedMotion } from "motion/react";
import { type LucideIcon } from "lucide-react";

import { cn } from "@/lib/cn";
import { spring } from "@/lib/motion-presets";

function useControllableState<T>({
  prop,
  defaultProp,
  onChange,
}: {
  prop: T | undefined;
  defaultProp: T;
  onChange?: (value: T) => void;
}): [T, (next: T) => void] {
  const [uncontrolled, setUncontrolled] = React.useState(defaultProp);
  const isControlled = prop !== undefined;
  const value = isControlled ? (prop as T) : uncontrolled;
  const setValue = React.useCallback(
    (next: T) => {
      if (!isControlled) setUncontrolled(next);
      onChange?.(next);
    },
    [isControlled, onChange]
  );
  return [value, setValue];
}

export interface SidebarItem {
  label: string;
  icon: LucideIcon;
  value: string;
  badge?: React.ReactNode;
}

export interface SidebarSection {
  title?: string;
  items: SidebarItem[];
}

export interface SidebarProps extends React.HTMLAttributes<HTMLElement> {
  sections: SidebarSection[];
  activeValue?: string;
  defaultActiveValue?: string;
  onActiveChange?: (value: string) => void;
  brand?: React.ReactNode;
  footer?: React.ReactNode;
}

export const Sidebar = React.forwardRef<HTMLElement, SidebarProps>(
  (
    {
      className,
      sections,
      activeValue,
      defaultActiveValue,
      onActiveChange,
      brand,
      footer,
      ...props
    },
    ref
  ) => {
    const firstValue = sections[0]?.items[0]?.value ?? "";
    const [active, setActive] = useControllableState({
      prop: activeValue,
      defaultProp: defaultActiveValue ?? firstValue,
      onChange: onActiveChange,
    });
    const layoutId = React.useId();
    const reduced = useReducedMotion();

    return (
      <aside
        ref={ref}
        className={cn(
          "flex h-full w-60 flex-col gap-4 border-r border-border bg-card p-3",
          className
        )}
        {...props}
      >
        {brand ? <div className="px-2 py-2">{brand}</div> : null}

        <nav className="flex flex-1 flex-col gap-5">
          {sections.map((section, si) => (
            <div key={si} className="flex flex-col gap-1">
              {section.title ? (
                <p className="px-3 pb-1 text-xs font-medium uppercase tracking-wider text-muted-foreground">
                  {section.title}
                </p>
              ) : null}
              {section.items.map((item) => {
                const Icon = item.icon;
                const isActive = item.value === active;
                return (
                  <button
                    key={item.value}
                    type="button"
                    onClick={() => setActive(item.value)}
                    aria-current={isActive ? "page" : undefined}
                    className={cn(
                      "relative flex items-center gap-3 rounded-lg px-3 py-2 text-sm font-medium transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring",
                      isActive
                        ? "text-primary"
                        : "text-muted-foreground hover:bg-accent hover:text-foreground"
                    )}
                  >
                    {isActive ? (
                      <motion.span
                        layoutId={`${layoutId}-sidebar-active`}
                        transition={reduced ? { duration: 0 } : spring.snappy}
                        className="absolute inset-0 rounded-lg bg-primary/10"
                      />
                    ) : null}
                    <span className="relative z-10 flex flex-1 items-center gap-3">
                      <Icon className="size-4" />
                      {item.label}
                      {item.badge ? (
                        <span className="ml-auto">{item.badge}</span>
                      ) : null}
                    </span>
                  </button>
                );
              })}
            </div>
          ))}
        </nav>

        {footer ? <div className="mt-auto">{footer}</div> : null}
      </aside>
    );
  }
);
Sidebar.displayName = "Sidebar";

src/components/application/sidebar.tsx

Props

PropTypeDefaultDescription
sectionsSidebarSection[]—Grouped nav items (label, icon, value).
activeValue / defaultActiveValuestring—Controlled or uncontrolled active item.
onActiveChange(value: string) => void—Fired when the active item changes.
brand / footerReactNode—Optional header and footer slots.