Splitter Examples
The examples below assume that you have imported the component and set up the theme.
Basic
Let users resize two side-by-side areas by dragging the line between them. sv-splitter-panel holds each area, size sets its start size, and min stops the drag.
Focus the handle and press the arrow keys, Home, or End, or double-click it to go back to size.
Sidebar
Main
<div class="splitter-basic"> <sv-splitter> <sv-splitter-panel aria-label="Sidebar" min="15%" size="30%"> <p>Sidebar</p> </sv-splitter-panel> <sv-splitter-panel aria-label="Main" min="15%"> <p>Main</p> </sv-splitter-panel> </sv-splitter></div>
<style> .splitter-basic { display: grid;
sv-splitter { block-size: 16rem; background-color: var(--sv-gray-7); border-radius: var(--sv-bdr-rad-md); }
p { display: grid; place-items: center; block-size: 100%; margin: 0; }
sv-splitter-panel::part(content) { scrollbar-width: thin; } }</style>Sizes and Limits
Keep a sidebar at a fixed width while the other panels scale with the page. A length in size stays the same when the splitter resizes, and a percentage scales. A panel with no size shares the rest of 100, and min and max limit each panel.
Resize the frame to see which panels change. When the next panel reaches its limit, a drag pushes the panels further along, and with no-push, it changes only the two panels next to the handle.
size="12rem" Keeps its width.
size="60%" Scales with the splitter.
no size Shares the rest of 100.
<div class="splitter-sizes"> <sv-splitter> <sv-splitter-panel aria-label="Navigation" max="18rem" min="4rem" size="12rem"> <p> <code>size="12rem"</code> Keeps its width. </p> </sv-splitter-panel> <sv-splitter-panel aria-label="Content" min="3rem" size="60%"> <p> <code>size="60%"</code> Scales with the splitter. </p> </sv-splitter-panel> <sv-splitter-panel aria-label="Details" min="3rem"> <p> <code>no size</code> Shares the rest of 100. </p> </sv-splitter-panel> </sv-splitter></div>
<style> .splitter-sizes { sv-splitter { block-size: 14rem; background-color: var(--sv-gray-7); border-radius: var(--sv-bdr-rad-md); }
p { display: grid; gap: 0.5em; place-content: center; block-size: 100%; padding: 0.5em; margin: 0; font-size: 0.85em; text-align: center; overflow-wrap: anywhere; }
sv-splitter-panel::part(content) { scrollbar-width: thin; } }</style>Collapsible Panels
Let users collapse a side panel with a drag, a key, or a button of your own. collapsible lets a drag and the keys collapse the panel, and toggle() collapses it from the buttons.
To collapse the first panel, drag its handle past half of min, or focus the handle and press Enter.
With collapsed-size, part of the panel stays visible while it is collapsed.
- index.html
- styles.css
- main.ts
Preview
Details
<div class="splitter-collapsible"> <div class="actions"> <button type="button" class="btn" data-panel="Files">Toggle files</button> <button type="button" class="btn" data-panel="Details">Toggle details</button> </div>
<sv-splitter> <sv-splitter-panel aria-label="Files" collapsible min="3.5rem" size="13rem"> <ul> <li>index.html</li> <li>styles.css</li> <li>main.ts</li> </ul> </sv-splitter-panel> <sv-splitter-panel aria-label="Preview" min="3.5rem"> <p>Preview</p> </sv-splitter-panel> <sv-splitter-panel aria-label="Details" collapsible min="3.5rem" size="11rem"> <p>Details</p> </sv-splitter-panel> </sv-splitter></div>
<script type="application/x-typescript" module> import type { SplitterPanel } from "@staticview/ui/splitter";
const example = document.querySelector(".splitter-collapsible")!;
for (const button of example.querySelectorAll<HTMLButtonElement>("button")) { const panel = example.querySelector<SplitterPanel>(`[aria-label="${button.dataset.panel}"]`)!;
button.addEventListener("click", () => panel.toggle()); }</script>
<style> .splitter-collapsible { display: grid; gap: 1em;
.actions { display: flex; flex-wrap: wrap; gap: 0.5em; }
sv-splitter { block-size: 16rem; background-color: var(--sv-gray-7); border-radius: var(--sv-bdr-rad-md); }
ul { padding: 1em; margin: 0; list-style: none; }
p { display: grid; place-items: center; block-size: 100%; margin: 0; }
sv-splitter-panel::part(content) { overflow-x: hidden; scrollbar-width: thin; } }</style>Snap Positions
Stop the handle at set positions while the user drags it. snap lists the positions from the start edge, and snap-threshold sets how close the handle must come.
Only a drag snaps, and the keys move the handle past the positions.
Before
After
<div class="splitter-snap"> <sv-splitter snap="25% 50% 75%" snap-threshold="16"> <sv-splitter-panel aria-label="Before" size="35%"> <p>Before</p> </sv-splitter-panel> <sv-splitter-panel aria-label="After"> <p>After</p> </sv-splitter-panel> </sv-splitter>
<div class="ruler" aria-hidden="true"> <span>25%</span> <span>50%</span> <span>75%</span> </div></div>
<style> .splitter-snap { sv-splitter { block-size: 12rem; background-color: var(--sv-gray-7); border-radius: var(--sv-bdr-rad-md); }
p { display: grid; place-items: center; block-size: 100%; margin: 0; }
.ruler { display: grid; grid-template-columns: repeat(4, 1fr); justify-items: end; margin-block-start: 0.25em; font-size: 0.75em; color: var(--sv-gray-3); }
span { translate: 50%; }
sv-splitter-panel::part(content) { scrollbar-width: thin; } }</style>Nested Splitters
Resize a grid of four areas, and let users drag the point where the handles cross. A vertical sv-splitter inside a panel stacks its panels, and one press where perpendicular handles meet drags them together.
Move the pointer over the point in the center to see the move cursor.
Top left
Bottom left
Top right
Bottom right
<div class="splitter-nested"> <sv-splitter> <sv-splitter-panel aria-label="Left column" min="4rem"> <sv-splitter vertical> <sv-splitter-panel aria-label="Top left" min="3rem"> <p>Top left</p> </sv-splitter-panel> <sv-splitter-panel aria-label="Bottom left" min="3rem"> <p>Bottom left</p> </sv-splitter-panel> </sv-splitter> </sv-splitter-panel> <sv-splitter-panel aria-label="Right column" min="4rem"> <sv-splitter vertical> <sv-splitter-panel aria-label="Top right" min="3rem"> <p>Top right</p> </sv-splitter-panel> <sv-splitter-panel aria-label="Bottom right" min="3rem"> <p>Bottom right</p> </sv-splitter-panel> </sv-splitter> </sv-splitter-panel> </sv-splitter></div>
<style> .splitter-nested { > sv-splitter { block-size: 18rem; background-color: var(--sv-gray-7); border-radius: var(--sv-bdr-rad-md); }
p { display: grid; place-items: center; block-size: 100%; margin: 0; }
sv-splitter-panel::part(content) { scrollbar-width: thin; } }</style>Save the Layout
Keep the layout of the user across page loads, and let the user go back to the default. persist-key saves the sizes and the collapsed states in localStorage, and reset() goes back to the size and collapsed attributes.
Drag the handle, then reload the page.
Sidebar
Main
<div class="splitter-persist"> <sv-splitter persist-key="docs-splitter-persist"> <sv-splitter-panel aria-label="Sidebar" collapsible min="4rem" size="30%"> <p>Sidebar</p> </sv-splitter-panel> <sv-splitter-panel aria-label="Main" min="4rem"> <p>Main</p> </sv-splitter-panel> </sv-splitter>
<button type="button" class="btn">Reset the layout</button></div>
<script type="application/x-typescript" module> import type { Splitter } from "@staticview/ui/splitter";
const splitter = document.querySelector<Splitter>(".splitter-persist sv-splitter")!; const resetButton = document.querySelector(".splitter-persist button")!;
resetButton.addEventListener("click", () => splitter.reset());</script>
<style> .splitter-persist { display: grid; gap: 1em;
sv-splitter { block-size: 12rem; background-color: var(--sv-gray-7); border-radius: var(--sv-bdr-rad-md); }
.btn { justify-self: start; }
p { display: grid; place-items: center; block-size: 100%; margin: 0; }
sv-splitter-panel::part(content) { scrollbar-width: thin; } }</style>Custom Handle
Replace the grip with a round thumb of your own on the line. The handle slot of a panel holds the icon, --sv-splitter-handle-thickness and --sv-splitter-ln-clr set the line, and ::part(handle) colors the icon.
Before
After
<div class="splitter-custom-handle"> <sv-splitter> <sv-splitter-panel aria-label="Before" min="15%"> <p>Before</p>
<!-- Arrows --> <svg aria-hidden="true" slot="handle" viewBox="0 0 24 24"> <path d="m9 7-5 5 5 5M15 7l5 5-5 5" /> </svg> </sv-splitter-panel> <sv-splitter-panel aria-label="After" min="15%"> <p>After</p> </sv-splitter-panel> </sv-splitter></div>
<style> .splitter-custom-handle { sv-splitter { --sv-splitter-handle-thickness: 4px; --sv-splitter-hit-sz: 2rem; --sv-splitter-ln-clr: var(--sv-gray-5);
block-size: 16rem; background-color: var(--sv-gray-7); border-radius: var(--sv-bdr-rad-md); }
sv-splitter-panel::part(handle) { color: var(--sv-gray-2); }
sv-splitter-panel::part(handle):hover { color: var(--sv-accent-content); }
svg { position: relative; box-sizing: border-box; inline-size: 2rem; block-size: 2rem; padding: 0.35rem; background-color: var(--sv-gray-6); border: var(--sv-bdr-sz-sm) solid var(--sv-gray-4); border-radius: 50%; fill: none; stroke: currentcolor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
sv-splitter[vertical] svg { rotate: 90deg; }
p { display: grid; place-items: center; block-size: 100%; margin: 0; }
sv-splitter-panel::part(content) { scrollbar-width: thin; } }</style>