splitter

<tosi-splitter> goes between two items of a flex container and lets the reader divide the space they share: drag it, or focus it and use the arrow keys. It works in a row or a column, reads which from the container, and changes its cursor and orientation to match.

<div class="split-row">
  <section>Left</section>
  <tosi-splitter aria-label="Resize the left pane"></tosi-splitter>
  <section>Right</section>
</div>
<div class="split-column">
  <section>Top</section>
  <tosi-splitter></tosi-splitter>
  <section>Bottom</section>
</div>
.preview {
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding: 10px;
}
.split-row, .split-column {
  display: flex;
  flex: 1 1 0;
  min-height: 0;
  box-shadow: inset 0 0 0 1px #8884;
}
.split-column {
  flex-direction: column;
}
.preview section {
  flex: 1 1 0;
  min-width: 0;
  min-height: 0;
  padding: 10px;
  overflow: auto;
}
const { TosiSplitter } = tosijsui
const splitter = preview.querySelector('.split-row tosi-splitter')
test('a splitter in a row is a vertical separator', () => {
  expect(splitter instanceof TosiSplitter).toBe(true)
  expect(splitter.getAttribute('role')).toBe('separator')
  expect(splitter.getAttribute('aria-orientation')).toBe('vertical')
  expect(
    preview.querySelector('.split-column tosi-splitter').getAttribute('aria-orientation')
  ).toBe('horizontal')
})
test('the arrow keys move it, and it reports a value', () => {
  const left = splitter.previousElementSibling
  const before = left.getBoundingClientRect().width
  splitter.dispatchEvent(new KeyboardEvent('keydown', { key: 'ArrowRight', bubbles: true }))
  const after = left.getBoundingClientRect().width
  expect(Math.round(after - before)).toBe(splitter.step)
  expect(splitter.value > 0.5).toBe(true)
})

Behaviour

Give it an aria-label that says what it resizes; it is "Resize" otherwise.

The neighbours are its previous and next element siblings, so it must sit directly between them. If they hold content that cannot shrink, give them min-width: 0 (or min-height: 0 in a column): a flex item's automatic minimum is its content's, and the browser will not go below it whatever the splitter asks.

Styling

variable default
--tosi-splitter-size 6px the width of the handle (its height, in a column)
--tosi-splitter-color #8884 the line drawn down its middle
--tosi-splitter-active-color the accent the line while hovered, focused or dragged