SplitView
Resizable two-pane layout with pointer drag, keyboard nudging, and persistable size.
Live demo
01Sidebar + content (horizontal)
Drag the gutter or focus it (Tab) and use the arrow keys. The primary pane remembers its size through `bind:primarySize`.
- π index.svelte
- π +layout.svelte
- π components
- π routes
- π README.md
// 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'));
280px Collapsed: false Vertical β header + scrolling body
Stacked orientation. Same drag and keyboard mechanics, just rotated. Useful for docked toolbars above scrolling content.
Toolbar, breadcrumb, anything you want pinned to the top.
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.
180pxInteractive playground
Drive the same SplitView from sliders to see how min/max bounds feel.
Resize with the sliders above or grab the gutter.
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.
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'));
420px Implementation
02<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
03What 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:
- Touch and pen just work. Pointer events unify mouse + touch + pen under one ID β no separate
touchmovehandler. - 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.
- No leak.
releasePointerCapture(or the implicit release onpointerup/pointercancel) is a clean teardown β nowindow.removeEventListenerto 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 2frbut 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:
setPointerCapturekeeps moves flowing untilpointerup/pointercancel, even when the cursor leaves the window. - Touch on a non-passive listener:
touch-action: noneon the splitter prevents the browser from scrolling the page while the user drags. - Mouse middle-click drag: ignored (
event.button !== 0short-circuits) β middle-click is reserved for opening links in new tabs by convention. InfinitymaxPrimary: serialised as noaria-valuemaxon the separator (the attribute is omitted when the bound would beInfinity) 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 fileThe 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| Prop | Type | Default | Description |
|---|---|---|---|
orientation | 'horizontal' | 'vertical' | 'horizontal' | Side-by-side or stacked panes. |
primarySize | number | 280 | Bindable pixel size of the primary pane along its layout axis. |
minPrimary | number | 80 | Hard lower bound applied after every change (drag, keyboard, prop). |
maxPrimary | number | Infinity | Hard upper bound. Use a finite value to leave room for the secondary pane. |
collapsed | boolean | false | Bindable. When true, primary collapses to its minimum. |
ariaLabel | string | 'Resize splitter' | Accessible name for the separator. |
primary | Snippet | required | Primary pane content. |
secondary | Snippet | required | Secondary pane content. |
class | string | '' | Extra CSS class on the root. |