Skip to content

Resizable ​

Panels with a draggable divider that also resizes from the keyboard, horizontally or vertically.

Installation ​

bash
npx bootcn add resizable
bash
pnpm dlx bootcn add resizable
bash
yarn dlx bootcn add resizable
bash
bunx --bun bootcn add resizable

The CLI also installs the design tokens and these components: none. It prints the <link> and <script> tags to add to your layout.

Usage ​

Set the first panel's starting size with flex-basis (percent) and the same number in the handle's aria-valuenow; the panel after the handle fills the rest (bootcn-resizable__panel--fill). Save the size from the resize event if you want to restore it on the next visit.

JavaScript API ​

  • data-bootcn-resizable on the group (add bootcn-resizable--vertical to stack panels); groups can nest.
  • The handle (role="separator") controls the panel before it (aria-controls); aria-valuemin / aria-valuemax limit its size in percent, aria-valuenow holds the current size.
  • Event bootcn:resizable:resize (bubbles), detail.value in percent.
  • Bootcn.resizable.resize(handle, percent); init() / destroy().

Accessibility ​

  • The handle is a focusable separator with a name and min, max and current values, so screen readers announce the panel size.
  • Arrow keys resize by 5% (Left/Right, or Up/Down when vertical); Home and End jump to the limits.
  • Dragging uses pointer capture and works with mouse, pen and touch.

Dependencies ​

  • Bootstrap 5.3+ CSS.
  • themes/tokens/tokens.css.

Source ​

The exact files the CLI installs (resizable@1.0.0). View on GitHub · Report an issue

html
<div class="bootcn-resizable" data-bootcn-resizable style="height: 16rem; max-width: 32rem">
  <div class="bootcn-resizable__panel" id="bootcn-resizable-sidebar" style="flex-basis: 50%">
    <span>Sidebar</span>
  </div>
  <div
    class="bootcn-resizable__handle"
    role="separator"
    tabindex="0"
    aria-label="Resize sidebar"
    aria-orientation="vertical"
    aria-controls="bootcn-resizable-sidebar"
    aria-valuenow="50"
    aria-valuemin="20"
    aria-valuemax="80"
  >
    <span class="bootcn-resizable__grip" aria-hidden="true"></span>
  </div>
  <div class="bootcn-resizable__panel bootcn-resizable__panel--fill">
    <div class="bootcn-resizable bootcn-resizable--vertical" data-bootcn-resizable>
      <div class="bootcn-resizable__panel" id="bootcn-resizable-header" style="flex-basis: 30%">
        <span>Header</span>
      </div>
      <div
        class="bootcn-resizable__handle"
        role="separator"
        tabindex="0"
        aria-label="Resize header"
        aria-orientation="horizontal"
        aria-controls="bootcn-resizable-header"
        aria-valuenow="30"
        aria-valuemin="20"
        aria-valuemax="80"
      >
        <span class="bootcn-resizable__grip" aria-hidden="true"></span>
      </div>
      <div class="bootcn-resizable__panel bootcn-resizable__panel--fill">
        <span>Content</span>
      </div>
    </div>
  </div>
</div>
css
.bootcn-resizable {
  display: flex;
  width: 100%;
  height: 100%;
  overflow: hidden;
  border: var(--bootcn-border-width) solid var(--bootcn-border-color);
  border-radius: var(--bootcn-radius-lg);
}

.bootcn-resizable .bootcn-resizable {
  border: 0;
  border-radius: 0;
}

.bootcn-resizable--vertical {
  flex-direction: column;
}

.bootcn-resizable__panel {
  display: flex;
  flex: 0 0 auto;
  align-items: center;
  justify-content: center;
  min-width: 0;
  min-height: 0;
  overflow: auto;
  font-size: 0.875rem;
  font-weight: 600;
}

.bootcn-resizable__panel--fill {
  flex: 1 1 0;
}

.bootcn-resizable__handle {
  position: relative;
  display: flex;
  flex: none;
  align-items: center;
  justify-content: center;
  width: 1px;
  background-color: var(--bootcn-border-color);
  cursor: col-resize;
  touch-action: none;
}

.bootcn-resizable--vertical > .bootcn-resizable__handle {
  width: auto;
  height: 1px;
  cursor: row-resize;
}

/* A wider invisible hit area for the pointer. */
.bootcn-resizable__handle::before {
  content: '';
  position: absolute;
  inset: 0 -0.375rem;
}

.bootcn-resizable--vertical > .bootcn-resizable__handle::before {
  inset: -0.375rem 0;
}

.bootcn-resizable__grip {
  z-index: 1;
  width: 0.75rem;
  height: 1.25rem;
  border: var(--bootcn-border-width) solid var(--bootcn-border-color);
  border-radius: var(--bootcn-radius-sm);
  background-color: var(--bootcn-muted);
}

.bootcn-resizable--vertical > .bootcn-resizable__handle .bootcn-resizable__grip {
  width: 1.25rem;
  height: 0.75rem;
}

.bootcn-resizable__handle:focus-visible {
  outline: 0;
  background-color: var(--bootcn-ring);
}

.bootcn-resizable__handle:focus-visible .bootcn-resizable__grip {
  box-shadow: var(--bootcn-focus-ring);
}
js
(function () {
  'use strict';

  const ns = (window.Bootcn = window.Bootcn || {});
  if (ns.resizable) return; // already loaded: idempotent

  const HANDLE = '[data-bootcn-resizable] > .bootcn-resizable__handle';
  const STEP = 5;
  let drag = null;
  let listening = false;

  function limits(handle) {
    return {
      min: Number(handle.getAttribute('aria-valuemin') || 0),
      max: Number(handle.getAttribute('aria-valuemax') || 100),
    };
  }

  function vertical(handle) {
    return handle.parentElement.classList.contains('bootcn-resizable--vertical');
  }

  // Sets the controlled (first) panel to `percent` of the group, clamped.
  function resize(handle, percent) {
    const panel = document.getElementById(handle.getAttribute('aria-controls'));
    if (!panel) return;
    const { min, max } = limits(handle);
    const value = Math.round(Math.min(max, Math.max(min, percent)));
    panel.style.flexBasis = value + '%';
    handle.setAttribute('aria-valuenow', String(value));
    handle.dispatchEvent(
      new CustomEvent('bootcn:resizable:resize', { bubbles: true, detail: { value: value } }),
    );
  }

  function onKeydown(event) {
    const handle = event.target.closest && event.target.closest(HANDLE);
    if (!handle) return;
    const now = Number(handle.getAttribute('aria-valuenow') || 50);
    const { min, max } = limits(handle);
    const back = vertical(handle) ? 'ArrowUp' : 'ArrowLeft';
    const forward = vertical(handle) ? 'ArrowDown' : 'ArrowRight';
    let next = null;
    if (event.key === back) next = now - STEP;
    else if (event.key === forward) next = now + STEP;
    else if (event.key === 'Home') next = min;
    else if (event.key === 'End') next = max;
    if (next === null) return;
    event.preventDefault();
    resize(handle, next);
  }

  function onPointerDown(event) {
    const handle = event.target.closest && event.target.closest(HANDLE);
    if (!handle || event.button !== 0) return;
    event.preventDefault();
    handle.setPointerCapture(event.pointerId);
    handle.focus();
    drag = { handle: handle, rect: handle.parentElement.getBoundingClientRect() };
  }

  function onPointerMove(event) {
    if (!drag) return;
    const { handle, rect } = drag;
    const percent = vertical(handle)
      ? ((event.clientY - rect.top) / rect.height) * 100
      : ((event.clientX - rect.left) / rect.width) * 100;
    resize(handle, percent);
  }

  function onPointerUp() {
    drag = null;
  }

  function init() {
    if (listening) return;
    document.addEventListener('keydown', onKeydown);
    document.addEventListener('pointerdown', onPointerDown);
    document.addEventListener('pointermove', onPointerMove);
    document.addEventListener('pointerup', onPointerUp);
    document.addEventListener('pointercancel', onPointerUp);
    listening = true;
  }

  function destroy() {
    if (!listening) return;
    document.removeEventListener('keydown', onKeydown);
    document.removeEventListener('pointerdown', onPointerDown);
    document.removeEventListener('pointermove', onPointerMove);
    document.removeEventListener('pointerup', onPointerUp);
    document.removeEventListener('pointercancel', onPointerUp);
    listening = false;
    drag = null;
  }

  ns.resizable = { init: init, destroy: destroy, resize: resize };

  if (document.readyState === 'loading') {
    document.addEventListener('DOMContentLoaded', init);
  } else {
    init();
  }
})();

Released under the MIT License. Not affiliated with Bootstrap or shadcn/ui.