← Components

Containment

Resizable

Panels separated by handles you drag or move with the arrow keys. The handle widens on hover and squishes in the drag direction.

Examples and variants

Horizontal

Side-by-side panels.

Sources
Editor. Drag the handle, or focus it and use the arrow keys.
Example
function ResizableHorizontal() {
  return (
    <ResizablePanelGroup defaultSizes={[40, 60]} className="h-48 w-full max-w-xl rounded-(--shape-lg) bg-surface">
      <ResizablePanel index={0} minSize={20} className="p-4 text-sm">
        Sources
      </ResizablePanel>
      <ResizableHandle index={0} />
      <ResizablePanel index={1} minSize={30} className="p-4 text-sm">
        Editor. Drag the handle, or focus it and use the arrow keys.
      </ResizablePanel>
    </ResizablePanelGroup>
  )
}

Vertical

Stacked panels.

Preview
Console
Example
function ResizableVertical() {
  return (
    <ResizablePanelGroup direction="vertical" defaultSizes={[60, 40]} className="h-64 w-full max-w-sm rounded-(--shape-lg) bg-surface">
      <ResizablePanel index={0} minSize={25} className="p-4 text-sm">
        Preview
      </ResizablePanel>
      <ResizableHandle index={0} />
      <ResizablePanel index={1} minSize={20} className="p-4 text-sm">
        Console
      </ResizablePanel>
    </ResizablePanelGroup>
  )
}

Spec drawing

Measured from the live component at default density. Each code maps to the token that sets it; change the token and the drawing follows.

Install

With the CLI

The shadcn CLI only delivers files: it copies our code from the Isotopi registry into your project and installs the packages it uses. Add the foundation once, then the component.

Terminal
npx shadcn@latest add https://isotopi.com/r/minimal.json
npx shadcn@latest add https://isotopi.com/r/resizable.json

By hand

No CLI needed. Add the foundation once (see Getting started), then follow these steps. They include every component this one builds on.

  1. Install the packages it uses.

    Terminal
    npm install cn
  2. Copy this file to the same paths in your project. Imports use the @/ alias for your project root.

    • components/ui/resizable.tsxresizable
      components/ui/resizable.tsx
      "use client"
      
      import * as React from "react"
      import { cn } from "cn"
      
      /*
        Resizable: panels separated by handles you drag (or move with the arrow
        keys). The handle grows on hover and squishes slightly in the drag
        direction while moving. Sizes are percentages of the group.
      */
      type Group = {
        direction: "horizontal" | "vertical"
        sizes: number[]
        register: (index: number, config: { min: number; max: number }) => void
        resize: (handle: number, deltaPercent: number) => void
        groupRef: React.RefObject<HTMLDivElement | null>
      }
      const GroupContext = React.createContext<Group | null>(null)
      
      function useGroup() {
        const group = React.useContext(GroupContext)
        if (!group) throw new Error("Resizable parts must be inside <ResizablePanelGroup>")
        return group
      }
      
      function ResizablePanelGroup({
        direction = "horizontal",
        defaultSizes,
        onSizesChange,
        className,
        children,
        ...props
      }: Omit<React.ComponentProps<"div">, "dir"> & {
        direction?: "horizontal" | "vertical"
        /** Percent per panel, e.g. [30, 70]. */
        defaultSizes: number[]
        onSizesChange?: (sizes: number[]) => void
      }) {
        const [sizes, setSizes] = React.useState(defaultSizes)
        const limits = React.useRef<{ min: number; max: number }[]>([])
        const groupRef = React.useRef<HTMLDivElement>(null)
      
        const register = React.useCallback((index: number, config: { min: number; max: number }) => {
          limits.current[index] = config
        }, [])
      
        const resize = React.useCallback(
          (handle: number, delta: number) => {
            setSizes((current) => {
              const next = [...current]
              const a = limits.current[handle] ?? { min: 0, max: 100 }
              const b = limits.current[handle + 1] ?? { min: 0, max: 100 }
              let d = delta
              d = Math.max(d, a.min - next[handle], next[handle + 1] - b.max)
              d = Math.min(d, a.max - next[handle], next[handle + 1] - b.min)
              next[handle] += d
              next[handle + 1] -= d
              onSizesChange?.(next)
              return next
            })
          },
          [onSizesChange]
        )
      
        return (
          <GroupContext.Provider value={{ direction, sizes, register, resize, groupRef }}>
            <div
              ref={groupRef}
              data-slot="resizable-group"
              data-direction={direction}
              className={cn("flex size-full overflow-hidden", direction === "vertical" && "flex-col", className)}
              {...props}
            >
              {children}
            </div>
          </GroupContext.Provider>
        )
      }
      
      function ResizablePanel({
        index,
        minSize = 10,
        maxSize = 90,
        className,
        ...props
      }: React.ComponentProps<"div"> & { index: number; minSize?: number; maxSize?: number }) {
        const { sizes, register } = useGroup()
        React.useEffect(() => register(index, { min: minSize, max: maxSize }), [index, minSize, maxSize, register])
        return (
          <div
            data-slot="resizable-panel"
            className={cn("min-h-0 min-w-0 overflow-auto", className)}
            style={{ flexBasis: `${sizes[index]}%`, flexGrow: 0, flexShrink: 0 }}
            {...props}
          />
        )
      }
      
      /** Sits between panel `index` and `index + 1`. */
      function ResizableHandle({ index, label = "Resize panels", className }: { index: number; label?: string; className?: string }) {
        const { direction, sizes, resize, groupRef } = useGroup()
        const [dragging, setDragging] = React.useState<0 | 1 | -1>(0)
        const last = React.useRef(0)
        const horizontal = direction === "horizontal"
      
        function onPointerDown(event: React.PointerEvent<HTMLDivElement>) {
          event.currentTarget.setPointerCapture(event.pointerId)
          last.current = horizontal ? event.clientX : event.clientY
          setDragging(1)
        }
      
        function onPointerMove(event: React.PointerEvent<HTMLDivElement>) {
          if (!dragging || !groupRef.current) return
          const position = horizontal ? event.clientX : event.clientY
          const total = horizontal ? groupRef.current.offsetWidth : groupRef.current.offsetHeight
          const delta = position - last.current
          last.current = position
          if (delta === 0) return
          setDragging(delta > 0 ? 1 : -1)
          resize(index, (delta / total) * 100)
        }
      
        return (
          <div
            role="separator"
            tabIndex={0}
            aria-label={label}
            aria-orientation={horizontal ? "vertical" : "horizontal"}
            aria-valuenow={Math.round(sizes[index])}
            aria-valuemin={0}
            aria-valuemax={100}
            data-slot="resizable-handle"
            data-dragging={dragging ? (dragging > 0 ? "forward" : "back") : undefined}
            data-direction={direction}
            onPointerDown={onPointerDown}
            onPointerMove={onPointerMove}
            onPointerUp={() => setDragging(0)}
            onPointerCancel={() => setDragging(0)}
            onKeyDown={(event) => {
              const step = event.shiftKey ? 10 : 2
              const back = horizontal ? "ArrowLeft" : "ArrowUp"
              const forward = horizontal ? "ArrowRight" : "ArrowDown"
              if (event.key === back) resize(index, -step)
              else if (event.key === forward) resize(index, step)
              else return
              event.preventDefault()
            }}
            className={cn(
              "resizable-handle group/handle relative flex shrink-0 touch-none items-center justify-center outline-none",
              horizontal ? "w-(--resizable-hit-area) cursor-col-resize" : "h-(--resizable-hit-area) cursor-row-resize",
              className
            )}
          >
            <span
              aria-hidden
              className={cn(
                "resizable-grip rounded-(--shape-full) bg-(--resizable-handle) group-focus-visible/handle:bg-(--resizable-handle-active) group-data-[dragging]/handle:bg-(--resizable-handle-active)",
                horizontal ? "h-(--size-control-md) w-(--resizable-handle-width)" : "h-(--resizable-handle-width) w-(--size-control-md)"
              )}
            />
          </div>
        )
      }
      
      export { ResizablePanelGroup, ResizablePanel, ResizableHandle }
      

Imports

What the examples on this page import:

Imports
import { FolderIcon, HomeIcon, InboxIcon, SettingsIcon } from "lucide-react"
import { NavigationItem } from "@/components/ui/navigation"
import { ResizableHandle, ResizablePanel, ResizablePanelGroup } from "@/components/ui/resizable"
import { Sidebar, SidebarHeader, SidebarInset, SidebarLabel, SidebarNav, SidebarProvider, SidebarTrigger } from "@/components/ui/sidebar"

Anatomy

  1. 1Group · Lays panels out; sizes are percentages.
  2. 2Panel · minSize / maxSize in percent.
  3. 3Handle · --resizable-hit-area; grip --resizable-handle-width, --resizable-handle; active --resizable-handle-active.

Props

PropTypeDefaultDescription
ResizablePanelGroup direction"horizontal" | "vertical""horizontal"Layout.
defaultSizesnumber[]—Percent per panel.
onSizesChange(sizes: number[]) => void—Persist sizes.
ResizablePanel index, minSize, maxSizenumber—Panel position and limits.
ResizableHandle indexnumber—Sits after panel index.

Tokens

Every visual spec is a component token in styles/tokens/components.css, pointing at a semantic token. Change a token to restyle the component; themes change the semantic layer.

TokenPoints toLightDark
--resizable-handle-widthvar(--space-1)4pxsame
--resizable-handle-width-hovervar(--space-2)8pxsame
--resizable-handlevar(--stroke-color-default)oklch(0.22 0.01 260 / 18%)oklch(1 0 0 / 12%)
--resizable-handle-activevar(--stroke-color-focus)oklch(0.6 0.17 248)oklch(0.72 0.14 245)
--resizable-hit-areavar(--space-3)12pxsame

Motion

TriggerWhat happensTokens
Hover / dragGrip widens; while dragging it squishes in the drag direction.--duration-fast, --ease-bounce

Calm keeps the motion without overshoot. Still (and reduced motion) keeps a designed frame with no movement.

Accessibility

  • Handles are separators with aria-valuenow and keyboard focus.
  • Arrow keys resize by 2%, Shift+arrow by 10%.

Usage

Do

  • Use for editors and split views.

Don’t

  • Don't use for simple page layouts.

Code

components/ui/resizable.tsx
"use client"

import * as React from "react"
import { cn } from "cn"

/*
  Resizable: panels separated by handles you drag (or move with the arrow
  keys). The handle grows on hover and squishes slightly in the drag
  direction while moving. Sizes are percentages of the group.
*/
type Group = {
  direction: "horizontal" | "vertical"
  sizes: number[]
  register: (index: number, config: { min: number; max: number }) => void
  resize: (handle: number, deltaPercent: number) => void
  groupRef: React.RefObject<HTMLDivElement | null>
}
const GroupContext = React.createContext<Group | null>(null)

function useGroup() {
  const group = React.useContext(GroupContext)
  if (!group) throw new Error("Resizable parts must be inside <ResizablePanelGroup>")
  return group
}

function ResizablePanelGroup({
  direction = "horizontal",
  defaultSizes,
  onSizesChange,
  className,
  children,
  ...props
}: Omit<React.ComponentProps<"div">, "dir"> & {
  direction?: "horizontal" | "vertical"
  /** Percent per panel, e.g. [30, 70]. */
  defaultSizes: number[]
  onSizesChange?: (sizes: number[]) => void
}) {
  const [sizes, setSizes] = React.useState(defaultSizes)
  const limits = React.useRef<{ min: number; max: number }[]>([])
  const groupRef = React.useRef<HTMLDivElement>(null)

  const register = React.useCallback((index: number, config: { min: number; max: number }) => {
    limits.current[index] = config
  }, [])

  const resize = React.useCallback(
    (handle: number, delta: number) => {
      setSizes((current) => {
        const next = [...current]
        const a = limits.current[handle] ?? { min: 0, max: 100 }
        const b = limits.current[handle + 1] ?? { min: 0, max: 100 }
        let d = delta
        d = Math.max(d, a.min - next[handle], next[handle + 1] - b.max)
        d = Math.min(d, a.max - next[handle], next[handle + 1] - b.min)
        next[handle] += d
        next[handle + 1] -= d
        onSizesChange?.(next)
        return next
      })
    },
    [onSizesChange]
  )

  return (
    <GroupContext.Provider value={{ direction, sizes, register, resize, groupRef }}>
      <div
        ref={groupRef}
        data-slot="resizable-group"
        data-direction={direction}
        className={cn("flex size-full overflow-hidden", direction === "vertical" && "flex-col", className)}
        {...props}
      >
        {children}
      </div>
    </GroupContext.Provider>
  )
}

function ResizablePanel({
  index,
  minSize = 10,
  maxSize = 90,
  className,
  ...props
}: React.ComponentProps<"div"> & { index: number; minSize?: number; maxSize?: number }) {
  const { sizes, register } = useGroup()
  React.useEffect(() => register(index, { min: minSize, max: maxSize }), [index, minSize, maxSize, register])
  return (
    <div
      data-slot="resizable-panel"
      className={cn("min-h-0 min-w-0 overflow-auto", className)}
      style={{ flexBasis: `${sizes[index]}%`, flexGrow: 0, flexShrink: 0 }}
      {...props}
    />
  )
}

/** Sits between panel `index` and `index + 1`. */
function ResizableHandle({ index, label = "Resize panels", className }: { index: number; label?: string; className?: string }) {
  const { direction, sizes, resize, groupRef } = useGroup()
  const [dragging, setDragging] = React.useState<0 | 1 | -1>(0)
  const last = React.useRef(0)
  const horizontal = direction === "horizontal"

  function onPointerDown(event: React.PointerEvent<HTMLDivElement>) {
    event.currentTarget.setPointerCapture(event.pointerId)
    last.current = horizontal ? event.clientX : event.clientY
    setDragging(1)
  }

  function onPointerMove(event: React.PointerEvent<HTMLDivElement>) {
    if (!dragging || !groupRef.current) return
    const position = horizontal ? event.clientX : event.clientY
    const total = horizontal ? groupRef.current.offsetWidth : groupRef.current.offsetHeight
    const delta = position - last.current
    last.current = position
    if (delta === 0) return
    setDragging(delta > 0 ? 1 : -1)
    resize(index, (delta / total) * 100)
  }

  return (
    <div
      role="separator"
      tabIndex={0}
      aria-label={label}
      aria-orientation={horizontal ? "vertical" : "horizontal"}
      aria-valuenow={Math.round(sizes[index])}
      aria-valuemin={0}
      aria-valuemax={100}
      data-slot="resizable-handle"
      data-dragging={dragging ? (dragging > 0 ? "forward" : "back") : undefined}
      data-direction={direction}
      onPointerDown={onPointerDown}
      onPointerMove={onPointerMove}
      onPointerUp={() => setDragging(0)}
      onPointerCancel={() => setDragging(0)}
      onKeyDown={(event) => {
        const step = event.shiftKey ? 10 : 2
        const back = horizontal ? "ArrowLeft" : "ArrowUp"
        const forward = horizontal ? "ArrowRight" : "ArrowDown"
        if (event.key === back) resize(index, -step)
        else if (event.key === forward) resize(index, step)
        else return
        event.preventDefault()
      }}
      className={cn(
        "resizable-handle group/handle relative flex shrink-0 touch-none items-center justify-center outline-none",
        horizontal ? "w-(--resizable-hit-area) cursor-col-resize" : "h-(--resizable-hit-area) cursor-row-resize",
        className
      )}
    >
      <span
        aria-hidden
        className={cn(
          "resizable-grip rounded-(--shape-full) bg-(--resizable-handle) group-focus-visible/handle:bg-(--resizable-handle-active) group-data-[dragging]/handle:bg-(--resizable-handle-active)",
          horizontal ? "h-(--size-control-md) w-(--resizable-handle-width)" : "h-(--resizable-handle-width) w-(--size-control-md)"
        )}
      />
    </div>
  )
}

export { ResizablePanelGroup, ResizablePanel, ResizableHandle }