Panel
A floating surface the reader moves, resizes, folds to its header and closes.
On this page
A panel is a floating surface that stays open beside the work while the page under it stays usable: the layers of a canvas, a color picker, an inspector, a chat. The reader drags it by its header, resizes it from its corner, folds it to its header and closes it. It is a popover="manual" with role="dialog", so the browser opens it from a button and lifts it into the top layer, over everything on the page, with no z-index to keep in order. The panel behavior does the rest: moving, resizing, folding, Escape, focus and which panel is in front.
Panel
<button type="button" class="btn btn-sm" commandfor="layers" command="toggle-popover"> Layers</button><button type="button" class="btn btn-sm" commandfor="comments" command="toggle-popover"> Comments</button><div id="layers" popover="manual" role="dialog" class="panel" aria-labelledby="layers-title" style="--panel-x: 24px; --panel-y: 24px; --panel-width: 232px"> <div class="panel-header"> <button type="button" class="panel-handle" aria-label="Move the Layers panel with the arrow keys"></button> <h2 class="panel-title" id="layers-title">Layers</h2> <div class="panel-actions"> <button type="button" class="btn btn-quiet btn-sm btn-square panel-minimize" aria-pressed="false" aria-label="Minimize"> <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"> <path d="M3 8L13 8" /> </svg> </button> <button type="button" class="btn btn-quiet btn-sm btn-square" commandfor="layers" command="hide-popover" aria-label="Close"> <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"> <path d="M4 4L12 12" /><path d="M4 12L12 4" /> </svg> </button> </div> </div> <div class="panel-body"> <ol class="list"> <li class="list-row"> <span class="list-body"><span class="list-title">Header</span></span> </li> <li class="list-row"> <span class="list-body"><span class="list-title">Hero image</span></span> </li> <li class="list-row"> <span class="list-body"><span class="list-title">Pricing table</span></span> </li> <li class="list-row"> <span class="list-body"><span class="list-title">Footer</span></span> </li> </ol> </div> <span class="panel-resize" aria-hidden="true"></span></div><div id="comments" popover="manual" role="dialog" class="panel" aria-labelledby="comments-title" style="--panel-x: 296px; --panel-y: 64px; --panel-width: 272px"> <div class="panel-header"> <button type="button" class="panel-handle" aria-label="Move the Comments panel with the arrow keys"></button> <h2 class="panel-title" id="comments-title">Comments</h2> <div class="panel-actions"> <button type="button" class="btn btn-quiet btn-sm btn-square panel-minimize" aria-pressed="false" aria-label="Minimize"> <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"> <path d="M3 8L13 8" /> </svg> </button> <button type="button" class="btn btn-quiet btn-sm btn-square" commandfor="comments" command="hide-popover" aria-label="Close"> <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"> <path d="M4 4L12 12" /><path d="M4 12L12 4" /> </svg> </button> </div> </div> <div class="panel-body"> <div class="flex flex-col gap-sm"> <p class="m(0)"> <span class="fg-text-strong">Mira</span> The hero image crops the product on narrow screens. </p> <p class="m(0)"> <span class="fg-text-strong">Jun</span> Fixed in the next build: it keeps the middle third. </p> <input class="input input-sm" type="text" placeholder="Reply" aria-label="Reply"> </div> </div> <span class="panel-resize" aria-hidden="true"></span></div><script>document.getElementById('layers').showPopover()document.getElementById('comments').showPopover()</script><script type="module"> import { attach } from '@master/ui/behaviors' attach()</script>#What the stylesheet draws
- The sheet. The floating face of a popover, with the floating shadow. While it is held by its header or its corner it rises to the overlay shadow, and the drag selects no text. Its corner is a small button's corner plus the 6px the header keeps around it, 14px.
- The header is a bar of the same sheet over a hairline, with the title at the start and the actions at the end. Under both lies the handle, a button the size of the bar, so a press anywhere on the header that is not a control picks the panel up, with an open hand and then a closed one. The title takes no pointer, so a press on it reaches the handle.
- Focus on the handle rings the whole panel, since what the keys move is the whole panel.
- Folded, when the minimize button is pressed, the panel is only its header: the body and the corner are hidden, and the button sits in a well.
- The body scrolls inside the panel when it is longer than the room it has, without moving the page under it.
- The corner at the bottom end, two short diagonal lines, is a 16px target, 44px on a touch screen; the header's buttons keep their size there and their targets reach to its edge.
#Where it stands
The place and the size are custom properties: --panel-x and --panel-y for the top left corner, in pixels from the viewport's top left corner, and --panel-width and --panel-height. The behavior writes them from the pointer and the keys, because only an event knows them; a page may write them too, as the preview does to start each panel somewhere.
The stylesheet does the placing and the limits with clamp(), against the viewport a fixed element's percentages are read from, which leaves out a scrollbar:
- The panel is never wider than the viewport, never narrower than
--panel-min-width(12rem), and the whole of its width stays inside. - Its header never leaves the viewport, so a panel can always be found and dragged back; it is never taller than the viewport, and a body that does not fit scrolls.
- A narrower window pushes a panel in. A wider one lets it go back to the place it was given, since the place it holds is not changed by the clamp.
- Given no place, a panel opens at the top end corner, 8px (
--panel-gutter) inside it.
At the end of each move and resize the behavior reads where the stylesheet drew the panel and keeps that, so the value an application saves is the place the reader saw.
#Several panels
Panels stand in the top layer in the order they were shown: the last one shown is in front. A press or focus inside a panel behind another brings it to the front: the behavior shows it again, which puts it last in the top layer. It hides and shows it in one task, before the browser draws, so nothing is seen to leave, no entrance plays, and the browser reports one toggle from open to open. Focus stays where the reader put it.
#The behavior
panel takes the root, the element with the panel class, and returns the function that detaches it. Its value is where the panel is, how large and whether it is folded: { x, y, width, height, minimized }, the four numbers in pixels or null where the stylesheet's default holds. Keep it, and set it back when the page loads.
import { panel } from '@master/ui/behaviors/panel.js'const root = document.querySelector('#layers')const layers = panel(root)const saved = localStorage.getItem('layers-panel')if (saved) layers.value = JSON.parse(saved)root.addEventListener('change', (event) => { if (event.target === root) localStorage.setItem('layers-panel', JSON.stringify(layers.value))})- Moving. A drag of the handle writes
--panel-xand--panel-y; the point the panel was picked up by stays under the pointer. The arrow keys on the handle move it 16px, four times as far with Shift. - Resizing. A drag of the corner writes
--panel-widthand--panel-height; Alt with the arrow keys on the handle does the same, a step at a time. From right to left the corner is at the bottom left, so the panel widens to the left and Alt+ArrowLeft makes it wider. - Folding. A click on the minimize button turns its
aria-pressed. - Closing. Escape with focus in the panel closes it, unless a control inside handled the key, such as a menu that closes first. The close button needs no script: it is
command="hide-popover". - Focus. A panel opened from a control moves focus to its handle and gives it back to that control when it closes with focus inside. A panel a page opens as it loads leaves focus where it is.
- Events. The root dispatches
inputwhile the panel moves or resizes, andchangewhen a drag ends, a key moved it or it was folded. Both bubble, so a field in the body says them too: compareevent.targetwith the root.
| Option | What it sets |
|---|---|
step | How far an arrow key moves or resizes the panel, in pixels. 16 by default |
Without the behavior a panel still opens from its button over the page, in the top layer, and closes from its close button; it stays where its custom properties put it.
#Opened from a button
A button with commandfor and command="toggle-popover" opens and closes the panel; popovertarget does the same. The panel is next in the tab order after its button, and focus goes to its handle when it opens, so the arrow keys can move it at once. Close it, with its button, its close button or Escape, and focus returns to the button.
Opened from a button
<button type="button" class="btn" commandfor="inspector" command="toggle-popover">Inspect</button><div id="inspector" popover="manual" role="dialog" class="panel" aria-labelledby="inspector-title" style="--panel-x: 24px; --panel-y: 24px; --panel-width: 264px"> <div class="panel-header"> <button type="button" class="panel-handle" aria-label="Move the Inspector panel with the arrow keys"></button> <h2 class="panel-title" id="inspector-title">Inspector</h2> <div class="panel-actions"> <button type="button" class="btn btn-quiet btn-sm btn-square panel-minimize" aria-pressed="false" aria-label="Minimize"> <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"> <path d="M3 8L13 8" /> </svg> </button> <button type="button" class="btn btn-quiet btn-sm btn-square" commandfor="inspector" command="hide-popover" aria-label="Close"> <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"> <path d="M4 4L12 12" /><path d="M4 12L12 4" /> </svg> </button> </div> </div> <div class="panel-body"> <dl class="data-list"> <div class="data-list-item"> <dt class="data-list-term">Element</dt> <dd class="data-list-value">Hero image</dd> </div> <div class="data-list-item"> <dt class="data-list-term">Size</dt> <dd class="data-list-value">1280 × 720</dd> </div> <div class="data-list-item"> <dt class="data-list-term">Format</dt> <dd class="data-list-value">AVIF</dd> </div> </dl> </div> <span class="panel-resize" aria-hidden="true"></span></div><script type="module"> import { attach } from '@master/ui/behaviors' attach()</script>import { useEffect, useRef } from 'react'import { attach } from '@master/ui/behaviors'import MinusIcon from '@master/icons-react/native/16/soft/minus'import XIcon from '@master/icons-react/native/16/soft/x'export function Example() { const root = useRef<HTMLDivElement>(null) useEffect(() => attach(root.current!), []) return ( <div ref={root}> <button type="button" className="btn" {...{ commandfor: 'inspector', command: 'toggle-popover' }}> Inspect </button> <div id="inspector" popover="manual" role="dialog" className="panel" aria-labelledby="inspector-title" style={{ '--panel-x': '24px', '--panel-y': '24px', '--panel-width': '264px' } as React.CSSProperties}> <div className="panel-header"> <button type="button" className="panel-handle" aria-label="Move the Inspector panel with the arrow keys"></button> <h2 className="panel-title" id="inspector-title">Inspector</h2> <div className="panel-actions"> <button type="button" className="btn btn-quiet btn-sm btn-square panel-minimize" aria-pressed="false" aria-label="Minimize"> <MinusIcon className="icon" /> </button> <button type="button" className="btn btn-quiet btn-sm btn-square" {...{ commandfor: 'inspector', command: 'hide-popover' }} aria-label="Close"> <XIcon className="icon" /> </button> </div> </div> <div className="panel-body"> <dl className="data-list"> <div className="data-list-item"> <dt className="data-list-term">Element</dt> <dd className="data-list-value">Hero image</dd> </div> <div className="data-list-item"> <dt className="data-list-term">Size</dt> <dd className="data-list-value">1280 × 720</dd> </div> <div className="data-list-item"> <dt className="data-list-term">Format</dt> <dd className="data-list-value">AVIF</dd> </div> </dl> </div> <span className="panel-resize" aria-hidden="true"></span> </div> </div> )}<script> import { attach } from '@master/ui/behaviors' import MinusIcon from '@master/icons-svelte/native/16/soft/minus' import XIcon from '@master/icons-svelte/native/16/soft/x'</script><div {@attach attach}> <button type="button" class="btn" commandfor="inspector" command="toggle-popover">Inspect</button> <div id="inspector" popover="manual" role="dialog" class="panel" aria-labelledby="inspector-title" style="--panel-x: 24px; --panel-y: 24px; --panel-width: 264px"> <div class="panel-header"> <button type="button" class="panel-handle" aria-label="Move the Inspector panel with the arrow keys"></button> <h2 class="panel-title" id="inspector-title">Inspector</h2> <div class="panel-actions"> <button type="button" class="btn btn-quiet btn-sm btn-square panel-minimize" aria-pressed="false" aria-label="Minimize"> <MinusIcon class="icon" /> </button> <button type="button" class="btn btn-quiet btn-sm btn-square" commandfor="inspector" command="hide-popover" aria-label="Close"> <XIcon class="icon" /> </button> </div> </div> <div class="panel-body"> <dl class="data-list"> <div class="data-list-item"> <dt class="data-list-term">Element</dt> <dd class="data-list-value">Hero image</dd> </div> <div class="data-list-item"> <dt class="data-list-term">Size</dt> <dd class="data-list-value">1280 × 720</dd> </div> <div class="data-list-item"> <dt class="data-list-term">Format</dt> <dd class="data-list-value">AVIF</dd> </div> </dl> </div> <span class="panel-resize" aria-hidden="true"></span> </div></div><script setup>import { onMounted, onUnmounted, ref } from 'vue'import { attach } from '@master/ui/behaviors'import MinusIcon from '@master/icons-vue/native/16/soft/minus'import XIcon from '@master/icons-vue/native/16/soft/x'const root = ref(null)let detachonMounted(() => { detach = attach(root.value) })onUnmounted(() => detach?.())</script><template> <div ref="root"> <button type="button" class="btn" commandfor="inspector" command="toggle-popover"> Inspect </button> <div id="inspector" popover="manual" role="dialog" class="panel" aria-labelledby="inspector-title" style="--panel-x: 24px; --panel-y: 24px; --panel-width: 264px"> <div class="panel-header"> <button type="button" class="panel-handle" aria-label="Move the Inspector panel with the arrow keys"></button> <h2 class="panel-title" id="inspector-title">Inspector</h2> <div class="panel-actions"> <button type="button" class="btn btn-quiet btn-sm btn-square panel-minimize" aria-pressed="false" aria-label="Minimize"> <MinusIcon class="icon" /> </button> <button type="button" class="btn btn-quiet btn-sm btn-square" commandfor="inspector" command="hide-popover" aria-label="Close"> <XIcon class="icon" /> </button> </div> </div> <div class="panel-body"> <dl class="data-list"> <div class="data-list-item"> <dt class="data-list-term">Element</dt> <dd class="data-list-value">Hero image</dd> </div> <div class="data-list-item"> <dt class="data-list-term">Size</dt> <dd class="data-list-value">1280 × 720</dd> </div> <div class="data-list-item"> <dt class="data-list-term">Format</dt> <dd class="data-list-value">AVIF</dd> </div> </dl> </div> <span class="panel-resize" aria-hidden="true"></span> </div> </div></template>#Keyboard
| Keys | Action |
|---|---|
| Enter, Space | On a button with commandfor and command="toggle-popover": open the panel and move focus to its handle; again, close it |
| ArrowLeft, ArrowRight, ArrowUp, ArrowDown | On the handle: move the panel a step, 16px, that way; it stops at the edges of the viewport |
| Shift+Arrow keys | On the handle: move the panel four steps |
| Alt+ArrowRight, Alt+ArrowLeft | On the handle: make the panel wider or narrower by a step, four with Shift; the other way around from right to left, where the corner it resizes from is at the left |
| Alt+ArrowDown, Alt+ArrowUp | On the handle: make the panel taller or shorter by a step, four with Shift; a folded panel keeps its height |
| Enter, Space | On the minimize button: fold the panel to its header, or unfold it |
| Escape | With focus in the panel: close it and return focus to the control that opened it. A control inside that handles Escape, such as an open menu, keeps it |
| Tab, Shift+Tab | Move between the handle, the actions and the body, and on to the page: the panel is not modal |
#Accessibility
- The panel is a
role="dialog"named by its title witharia-labelledby. It is not modal: there is noaria-modal, the page stays usable and Tab leaves the panel. Use a dialog when the task must be finished first. - The handle is a real button, so it is announced and reached like one. Name it with what it moves and how: "Move the Layers panel with the arrow keys". The corner is hidden from assistive technology, since the keys resize from the handle.
- Every drag has a key: the arrows move, Alt with the arrows resizes. Where a reader who uses a pointer cannot drag, offer a single press too: a menu in the header whose items set the value, such as "Dock left", is enough.
- The minimize button keeps its name and says its state with
aria-pressed. - On a touch screen the header keeps its height: its buttons' targets reach 44px, to the header's edge, and the corner is a 44px target.
- In forced colors the panel is outlined in the system's text color, and focus on the handle in its highlight.
#React, Svelte and Vue
The panel component renders the header, its handle and its buttons, opens and closes the panel from open, and passes the value in and out: onValueChange as it moves and onValueCommit when a move ends, the moment to save it. The trigger and the body stay the page's own markup. Copy it into the project with npx master-ui add panel; React, Svelte and Vue says how the components are built and used.
#Classes
| Class | Description |
|---|---|
panel | The floating sheet: a popover="manual" with role="dialog", named by its title with aria-labelledby. It stands where --panel-x and --panel-y say, kept inside the viewport, 20rem wide unless told otherwise, at the top end corner when given no place |
panel-header | The bar at the top: the title at the start, the actions at the end and the handle under both. It is the part that never leaves the viewport |
panel-handle | A button the size of the header, first in it, that carries the panel: a drag anywhere on the bar that is not a control moves it, and the arrow keys move it while it has focus. Name it with what it moves and how, such as "Move the Layers panel with the arrow keys". Its focus rings the panel |
panel-title | The title, on one line; point the panel's aria-labelledby at it. It takes no pointer, so a press on it reaches the handle |
panel-actions | The controls at the end of the header, over the handle: small quiet square buttons |
panel-minimize | The button that folds the panel to its header. Pressed, it sits in a well, and the body and the corner are hidden |
panel-body | What the panel holds, under a hairline, with --panel-padding around it. A long body scrolls inside the panel. Lay out what it holds in an element inside it: the panel hides the body when it is folded |
panel-resize | The corner a pointer resizes the panel from, at the bottom end: a 16px target, 44px on touch screens. The keys resize from the handle, so hide it from assistive technology |
#Custom properties
| Property | Default | Description |
|---|---|---|
--panel-x | 100% | The left edge, from the viewport's left edge; the script writes it in pixels. It is kept so the whole width stays in the viewport, at the right edge by default and at the left from right to left |
--panel-y | 0px | The top edge, from the viewport's top edge; the script writes it in pixels. It is kept so the header stays in the viewport |
--panel-width | 20rem | The width; the script writes it in pixels. It is kept between --panel-min-width and the viewport's width |
--panel-height | auto | The height, as tall as the content by default; the script writes it in pixels. It is never more than the viewport's height |
--panel-min-width | 12rem | The narrowest a resize makes it |
--panel-gutter | .5rem | The least room kept between the panel and the edges of the viewport |
--panel-padding | 1rem | Padding around the body |
--panel-radius | calc(var(--radius-control-sm) + var(--panel-inset) / var(--corner-scale)) | The corner: a small button's corner plus the 6px the header keeps around it |
#Related
- DialogA native modal on an overlay sheet, opened and closed without script.
- PopoverA floating sheet anchored to the button that opens it.
- SplitterTwo panes and the handle between them that sets how they share the space.
- ToolbarA small raised sheet that holds a row of quiet controls.