v1.1.1

Draggable, resizable panes for the web.

Turn any <div> into a window you can move and resize with a mouse, finger, or pen. No dependencies, about 3.5 KB.

$ npm install panes-ui

Give it a try

No panes open

Drag a titlebar to move, any edge or corner to resize. Clicking a pane raises it. Esc closes open panes.

Why panes-ui

Install

With a bundler

sh
npm install panes-ui
ts
import { Pane } from "panes-ui";
import "panes-ui/style.css"; // default look — optional

From a CDN (no build step)

The <script> build exposes a global named Panes. Pin a version in production.

html
<link rel="stylesheet" href="https://unpkg.com/panes-ui@1/dist/pane.css" />
<script src="https://unpkg.com/panes-ui@1"></script>
<script>
  Panes.open("#my-pane", { title: "Hello" });
</script>

Quickstart

1. Markup

Any element works. The library looks for three optional children by class name.

html
<div id="my-pane" style="width:360px;height:240px">
  <div class="pane-title">Hello</div>              <!-- drag handle -->
  <button class="pane-close" aria-label="Close">&times;</button>
  <div class="pane-content">Drag my titlebar.</div> <!-- scrollable body -->
</div>

2. Open it

ts
const pane = new Pane("#my-pane", {
  title: "Hello",
  minWidth: 200,
  onResize: (p, w, h) => console.log(w, h),
});
pane.open();

// later
pane.close();
pane.destroy(); // remove listeners when you're done

API

new Pane(target, options?, manager?)

target is an element, an element id, or a CSS selector. Every option is optional.

OptionDefaultDescription
title—String or element placed in the titlebar (inserted as text, never HTML).
movabletrueDrag by the titlebar.
resizabletrueResize from any edge or corner.
minWidth / minHeight120 / 80Minimum size in px.
constraintrueKeep the pane inside the viewport.
centertrueCenter the pane on its first open().
closeOnEscapetrueClose when Esc is pressed.
titleSelector".pane-title"Selector for the drag handle.
contentSelector".pane-content"Selector for the body.
closeSelector".pane-close"Selector for the close button.
onOpen / onClose—(pane) => void, fired on state changes only.
onMove—(pane, left, top) => void
onResize—(pane, width, height) => void
onFocus—(pane) => void, fired when the pane is raised.

Methods

Methods that change state return the pane, so calls can be chained.

MethodDescription
open() / close() / toggle()Show or hide. The first open() centers the pane.
isOpen()Whether the pane is visible.
focus()Raise above the other panes.
center()Re-center in the viewport.
setPosition(left, top)Move (respects constrain).
setSize(width, height)Resize (respects the minimums).
getRect(){ left, top, width, height } in viewport coordinates.
destroy()Remove all listeners and unregister. Safe to call twice.

Other exports

open(target, options?)Get-or-create a pane on an element and open it. Options apply on the first call only.
managerThe default PaneManager. manager.list() returns every registered pane.
PaneManagerConstruct your own for an isolated stacking context.
versionThe library version string.

Full details, including styling hooks and the DOM contract, are in the README.

Frameworks

Create the pane after the element is mounted and call destroy() when it unmounts. Panes need the DOM, so construct them in browser-only code.

React
tsx
import { useEffect, useRef } from "react";
import { Pane } from "panes-ui";
import "panes-ui/style.css";

export function Report({ open }: { open: boolean }) {
  const ref = useRef<HTMLDivElement>(null);
  const pane = useRef<Pane | null>(null);

  useEffect(() => {
    pane.current = new Pane(ref.current!, { title: "Report" });
    return () => pane.current?.destroy();
  }, []);

  useEffect(() => {
    open ? pane.current?.open() : pane.current?.close();
  }, [open]);

  return (
    <div ref={ref} style={{ width: 420, height: 300 }}>
      <div className="pane-title" />
      <button className="pane-close" aria-label="Close">×</button>
      <div className="pane-content">…</div>
    </div>
  );
}
Vue 3
vue
<script setup lang="ts">
import { onMounted, onBeforeUnmount, ref } from "vue";
import { Pane } from "panes-ui";
import "panes-ui/style.css";

const root = ref<HTMLDivElement>();
let pane: Pane | null = null;

onMounted(() => { pane = new Pane(root.value!, { title: "Report" }).open(); });
onBeforeUnmount(() => pane?.destroy());
</script>

<template>
  <div ref="root" style="width:420px;height:300px">
    <div class="pane-title" />
    <button class="pane-close" aria-label="Close">×</button>
    <div class="pane-content"><slot /></div>
  </div>
</template>
Svelte
svelte
<script lang="ts">
  import { onMount } from "svelte";
  import { Pane } from "panes-ui";
  import "panes-ui/style.css";

  let el: HTMLDivElement;

  onMount(() => {
    const pane = new Pane(el, { title: "Report" }).open();
    return () => pane.destroy();
  });
</script>

<div bind:this={el} style="width:420px;height:300px">
  <div class="pane-title"></div>
  <button class="pane-close" aria-label="Close">×</button>
  <div class="pane-content"><slot /></div>
</div>
Next.js / SSR

Importing panes-ui on the server is safe; only constructing a Pane needs a browser. In the App Router, put the component in a file marked "use client" and create the pane inside useEffect, as in the React example.

Recipes

Remember position and size

ts
const KEY = "pane:inspector";
const saved = JSON.parse(localStorage.getItem(KEY) ?? "null");
const save = (p: Pane) => localStorage.setItem(KEY, JSON.stringify(p.getRect()));

const pane = new Pane("#inspector", {
  center: !saved,          // only center when there's nothing to restore
  onMove: save,
  onResize: save,
}).open();               // open first: a hidden pane measures 0×0
if (saved) pane.setSize(saved.width, saved.height).setPosition(saved.left, saved.top);

Close only the top pane on Escape

By default, Esc closes every open pane that has closeOnEscape enabled. To close only the front-most one, turn it off and handle the key yourself:

ts
import { Pane, manager } from "panes-ui";

const a = new Pane("#a", { closeOnEscape: false });
const b = new Pane("#b", { closeOnEscape: false });

document.addEventListener("keydown", (e) => {
  if (e.key !== "Escape") return;
  const top = manager.list()
    .filter((p) => p.isOpen())
    .sort((x, y) => +y.el.style.zIndex - +x.el.style.zIndex)[0];
  top?.close();
});

A fixed, non-movable panel

ts
new Pane("#sidebar", { movable: false, center: false }).open(); // still resizable

Use your own markup classes

ts
new Pane("#win", {
  titleSelector: ".win-header",
  contentSelector: ".win-body",
  closeSelector: "[data-close]",
});

A true modal

Panes are non-blocking (aria-modal="false"). For a blocking dialog, put a backdrop behind the pane, set aria-modal="true" on the element yourself (the library won't overwrite it), and trap focus inside, or use the native <dialog> element.

Support the project

panes-ui is free, MIT-licensed, and maintained in spare time. If it saves you time, sponsoring helps keep it maintained and dependency-free.

Not in a position to sponsor? A star on GitHub, a bug report, or a docs fix helps too.