Back to Snippets

Resizable Split Panes JS

A code panel on the left and a preview on the right, separated by a thin divider you can drag. The panes stay above a minimum width, double clicking the divider snaps back to an even split, and with the divider focused the arrow keys nudge it in small steps.

The drag uses pointer events with setPointerCapture on the divider, so the divider keeps receiving moves even when the cursor races ahead of it or leaves the window. On each move the script converts the pointer position into a percentage of the container, clamps it between the two limits, and writes it to a custom property that the grid template reads. While dragging, a class on the container disables text selection so the drag stays smooth.

The divider has role separator with aria-valuenow updated on every move, and because it is focusable the keyboard path needs nothing beyond the keydown handler.

0
#javascript
#css
#split pane
#resizable
#drag
#layout
#editor
#ui
#accessibility