Give it a try
Drag a titlebar to move, any edge or corner to resize. Clicking a pane raises it. Esc closes open panes.
Why panes-ui
- Zero dependencies~3.5 KB min+gzip. Nothing to audit but this package.
- Mouse, touch & penOne Pointer Events code path, with pointer capture so drags don't drop.
- Smooth under loadMoves are coalesced with
requestAnimationFrame. - Bring your own markupWorks on any element; you keep full control of HTML and CSS.
- Framework-agnosticPlain HTML, React, Vue, Svelte, Solid, Angular — anything with a DOM.
- Accessible defaults
role="dialog",aria-labelledby, Escape to close — never overriding yours. - Stays on screenPositions and resizes are clamped to the viewport, and re-clamped when it changes.
- TypedWritten in TypeScript; ESM, CJS and a
<script>build ship with.d.ts.
Install
With a bundler
npm install panes-uiimport { Pane } from "panes-ui";
import "panes-ui/style.css"; // default look — optionalFrom a CDN (no build step)
The <script> build exposes a global named Panes. Pin a version in production.
<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.
<div id="my-pane" style="width:360px;height:240px">
<div class="pane-title">Hello</div> <!-- drag handle -->
<button class="pane-close" aria-label="Close">×</button>
<div class="pane-content">Drag my titlebar.</div> <!-- scrollable body -->
</div>2. Open it
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 doneAPI
new Pane(target, options?, manager?)
target is an element, an element id, or a CSS selector. Every option is optional.
| Option | Default | Description |
|---|---|---|
title | — | String or element placed in the titlebar (inserted as text, never HTML). |
movable | true | Drag by the titlebar. |
resizable | true | Resize from any edge or corner. |
minWidth / minHeight | 120 / 80 | Minimum size in px. |
constrain | true | Keep the pane inside the viewport. |
center | true | Center the pane on its first open(). |
closeOnEscape | true | Close 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.
| Method | Description |
|---|---|
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. |
manager | The default PaneManager. manager.list() returns every registered pane. |
PaneManager | Construct your own for an isolated stacking context. |
version | The 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
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
<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
<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
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:
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
new Pane("#sidebar", { movable: false, center: false }).open(); // still resizableUse your own markup classes
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.