SplitView

Resizable two-pane layout with pointer drag, keyboard nudging, and persistable size.

Live demo

01

Sidebar + content (horizontal)

Drag the gutter or focus it (Tab) and use the arrow keys. The primary pane remembers its size through `bind:primarySize`.

Files
  • πŸ“„ index.svelte
  • πŸ“„ +layout.svelte
  • πŸ“ components
  • πŸ“ routes
  • πŸ“„ README.md
Editor preview
// Resize me by dragging the gutter between the file tree
// and this editor. Try Tab β†’ focus the splitter β†’ arrow keys.
function greet(name) {
	return `Hello, ${name}!`;
}

console.log(greet('TFE'));
Active: 280px Collapsed: false

Vertical β€” header + scrolling body

Stacked orientation. Same drag and keyboard mechanics, just rotated. Useful for docked toolbars above scrolling content.

Header

Toolbar, breadcrumb, anything you want pinned to the top.

Scrolling body

This region grows to fill the remaining space. Add as much content as you like β€” the SplitView won't reflow because the gutter held a fixed pixel size.

Try dragging the horizontal splitter above: the header is the primary pane, this is the secondary pane, and the layout stays stable.

Header height: 180px

Interactive playground

Drive the same SplitView from sliders to see how min/max bounds feel.

Primary

Resize with the sliders above or grab the gutter.

Secondary

Fills the rest of the stage.

Editor + preview (horizontal, equal halves)

The classic two-pane editor / preview layout. Primary starts at 420px, secondary takes the rest.

Editor
Preview

Render of the editor contents.

// Resize me by dragging the gutter between the file tree
// and this editor. Try Tab β†’ focus the splitter β†’ arrow keys.
function greet(name) {
	return `Hello, ${name}!`;
}

console.log(greet('TFE'));
Editor: 420px

Implementation

02
SplitView.svelte
<script lang="ts">
  import SplitView from '$lib/components/SplitView.svelte';
​
  let sidebarWidth = $state(280);
</script>
​
<SplitView
  bind:primarySize={sidebarWidth}
  minPrimary={180}
  maxPrimary={520}
  ariaLabel="Resize file tree"
>
  {#snippet primary()}
    <nav>File tree, sidebar, filters β€” whatever fits.</nav>
  {/snippet}
  {#snippet secondary()}
    <main>Main content fills the rest.</main>
  {/snippet}
</SplitView>

SplitView renders two snippets around a focusable separator. The separator uses pointer-capture so a drag stays accurate even when the cursor leaves the window. Keyboard users Tab to the separator, arrow to nudge, Home/End to collapse or restore, and Enter or double-click to toggle β€” the same primarySize bindable backs both input paths so any consumer observing it sees a single source of truth.

Logic explainer

03

What Does It Do? (Plain English)

A two-pane layout separated by a draggable splitter. Drop anything into the primary and secondary snippets β€” a sidebar and a DataGrid, a Markdown editor and a preview, a list and a detail panel β€” and SplitView handles the dragging, keyboard nudging, minimum / maximum constraints, and the persisted size.

Think of it as the framework's answer to the IDE pattern: a side panel that you can grab, drag, and snap, with the keyboard doing the same thing for anyone who can't or doesn't want to grab the mouse. Horizontal orientation is the IDE/files layout; vertical orientation is the IDE/editor+preview layout.

How It Works (Pseudo-Code)

state:
  primarySize       = 280          // bindable, px
  collapsed         = false        // bindable
  isDragging        = false
  lastExpandedSize  = primarySize  // remembered for Home/End + double-click

derive effectiveSize:
  if collapsed: min(primarySize, minPrimary)
  else:         primarySize

events:
  on pointerdown on splitter (button === 0):
    isDragging   = true
    if collapsed: collapsed = false  // drag always starts by re-opening
    lastExpandedSize = primarySize
    splitter.setPointerCapture(pointerId)

  on pointermove while isDragging:
    raw = clientX - container.left    // (or clientY - container.top for vertical)
    primarySize = clamp(raw, minPrimary, maxPrimary)

  on pointerup / pointercancel:
    isDragging = false
    splitter.releasePointerCapture(pointerId)

  on ArrowLeft / ArrowUp  on splitter: primarySize = clamp(primarySize - 16, min, max)
  on ArrowRight / ArrowDown:           primarySize = clamp(primarySize + 16, min, max)
  on Home:                              collapsed = true (and remember last size)
  on End:                               collapsed = false, restore lastExpandedSize
  on Enter / Space:                     toggle collapsed
  on dblclick:                          toggle collapsed

  on minPrimary or maxPrimary change (effect):
    primarySize = clamp(primarySize, minPrimary, maxPrimary)

Pointer Capture, Not Global Mouse Listeners

The splitter uses setPointerCapture on itself rather than listening to mousemove on window. Three reasons:

  1. Touch and pen just work. Pointer events unify mouse + touch + pen under one ID β€” no separate touchmove handler.
  2. The drag follows the element, not the cursor. Capture routes all subsequent moves for that pointer ID to the splitter regardless of where they land (including outside the document). A user can drag fast, fly past the splitter, and the move still reaches us.
  3. No leak. releasePointerCapture (or the implicit release on pointerup / pointercancel) is a clean teardown β€” no window.removeEventListener to forget.

This is also why touch-action: none is set on the splitter β€” it tells the browser "I will handle horizontal/vertical gestures here, do not scroll the page."

Keyboard Nudging Mirrors the Drag

The arrow keys move the splitter 16px at a time, Home collapses, End restores the last size, and Enter / Space toggles collapse. The mirroring is intentional: the same DOM element (role="separator") is reachable by mouse and keyboard, and both paths update the same primarySize bindable, so anything observing it (CSS, a sibling component, a state store) sees a consistent value.

aria-valuenow, aria-valuemin, aria-valuemax are set on the splitter so screen readers announce "280 pixels, range 80 to infinity" β€” actual drag positions read back to AT users.

Reduced Motion Is Boring on Purpose

The drag itself is isDragging-gated: the splitter position follows the pointer with no transition. That's the right choice β€” a transition on drag would lag visibly behind the cursor, and that lag is motion sickness for some users. There are no other animations to gate.

The prefers-reduced-motion block in the stylesheet is therefore intentionally minimal: no media query, no keyframes. The component is reduced-motion-friendly by construction.

Distinct From

  • Drawer vs SplitView: Drawer is a modal overlay that slides in from an edge and traps focus. SplitView is an in-flow, non-modal layout with a draggable divider.
  • Splitter panels in CSS Grid: possible via grid-template-columns: 1fr auto 2fr but you give up pointer-drag, keyboard nudging, persisted size, min/max constraints, and collapse support. SplitView is the same idea with all of those first-class.
  • Window-manager resizers in OS shells: same primitive, but they live outside the page and can't be themed to match your app.

State Flow Diagram

                β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                β”‚   rendered         β”‚
                β”‚   (default size)   β”‚
                β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                         β”‚
        β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
        β–Ό                β–Ό                 β–Ό
   pointerdown       keydown          dblclick / Enter / Space
        β”‚                β”‚                 β”‚
        β–Ό                β–Ό                 β–Ό
   isDragging=true    arrow | Home |     toggle collapsed
        β”‚             End                β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
        β”‚                β”‚               β–Ό          β”‚
        β–Ό                β–Ό           β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”
   pointermove ──►  primarySize     β”‚collapsedβ”‚  β”‚expandedβ”‚
   pointerup           clamp         β”‚  true   β”‚  β”‚  false β”‚
                         β”‚           β””β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”¬β”€β”€β”€β”˜
                         β”‚                β”‚            β”‚
                         β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Props Reference

Prop Type Default Description
orientation 'horizontal' | 'vertical' 'horizontal' Side-by-side or stacked panes.
primarySize number (bindable, px) 280 Pixel size of the primary pane along its layout axis.
minPrimary number (px) 80 Hard lower bound on primarySize after clamp.
maxPrimary number (px) Infinity Hard upper bound. Use a finite value to leave room for the secondary pane.
collapsed boolean (bindable) false When true, primary collapses to its minimum and the divider reflects the collapsed state.
ariaLabel string 'Resize splitter' Accessible name for the separator.
primary Snippet required Primary pane content (rendered in the dimension-controlled region).
secondary Snippet required Secondary pane content (fills the remaining space).
class string '' Extra CSS class on the root, appended after layout class.

Edge Cases

  • Pane smaller than minPrimary: clamped on first render via the bounds effect, so the first paint shows a valid size.
  • Drag outside the document: setPointerCapture keeps moves flowing until pointerup / pointercancel, even when the cursor leaves the window.
  • Touch on a non-passive listener: touch-action: none on the splitter prevents the browser from scrolling the page while the user drags.
  • Mouse middle-click drag: ignored (event.button !== 0 short-circuits) β€” middle-click is reserved for opening links in new tabs by convention.
  • Infinity maxPrimary: serialised as no aria-valuemax on the separator (the attribute is omitted when the bound would be Infinity) to avoid meaningless "infinity" announcements.

Dependencies

Zero. Pure Svelte 5 runes + pointer events + scoped CSS.

File Structure

src/lib/components/
  SplitView.svelte                  # the component
  SplitView.test.ts                 # render, ARIA, keyboard, drag
  SplitViewTestHarness.test.svelte  # internal harness for the test
  SplitView.md                      # this file

The catalog entry and the /splitview demo route live next to the other components in src/lib/componentCatalog.ts and src/routes/splitview/+page.svelte.

API

04
PropTypeDefaultDescription
orientation'horizontal' | 'vertical''horizontal'Side-by-side or stacked panes.
primarySizenumber280Bindable pixel size of the primary pane along its layout axis.
minPrimarynumber80Hard lower bound applied after every change (drag, keyboard, prop).
maxPrimarynumberInfinityHard upper bound. Use a finite value to leave room for the secondary pane.
collapsedbooleanfalseBindable. When true, primary collapses to its minimum.
ariaLabelstring'Resize splitter'Accessible name for the separator.
primarySnippetrequiredPrimary pane content.
secondarySnippetrequiredSecondary pane content.
classstring''Extra CSS class on the root.