Navigation

v2.1.0
Overlays

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>
Drag a header, or the corner at its bottom end. Press inside the panel behind to bring it to the front. Tab to a header and use the arrow keys, Shift for longer steps, Alt to resize; Escape closes the panel.

#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.

app.js
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-x and --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-width and --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 input while the panel moves or resizes, and change when a drag ends, a key moved it or it was folded. Both bubble, so a field in the body says them too: compare event.target with the root.
OptionWhat it sets
stepHow 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>
Open the panel: focus moves to its header. Close it with Escape and focus returns to the button.

#Keyboard

KeysAction
Enter, SpaceOn a button with commandfor and command="toggle-popover": open the panel and move focus to its handle; again, close it
ArrowLeft, ArrowRight, ArrowUp, ArrowDownOn the handle: move the panel a step, 16px, that way; it stops at the edges of the viewport
Shift+Arrow keysOn the handle: move the panel four steps
Alt+ArrowRight, Alt+ArrowLeftOn 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+ArrowUpOn the handle: make the panel taller or shorter by a step, four with Shift; a folded panel keeps its height
Enter, SpaceOn the minimize button: fold the panel to its header, or unfold it
EscapeWith 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+TabMove 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 with aria-labelledby. It is not modal: there is no aria-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.

ui/panel.tsx
'use client'import { useCallback, useEffect, useId, useReducer, useRef, useState, type ComponentProps, type CSSProperties, type ReactNode } from 'react'import { panel, type PanelValue } from '@master/ui/behaviors/panel.js'import { useBehavior } from './use-behavior'export type { PanelValue }export interface PanelProps extends Omit<ComponentProps<'div'>, 'title' | 'defaultValue' | 'popover' | 'role' | 'ref'> {  /** The title in the header, which names the panel. */  title: ReactNode  /** The name of the handle: what it moves and how, such as "Move the Layers panel with the arrow keys". */  handleLabel: string  /** Whether the panel is open, when this component is told. Leave it out and the trigger opens it. */  open?: boolean  onOpenChange?: (open: boolean) => void  /** Where the panel is, how large and whether it is folded. Keep it to put the panel back where it was. */  value?: PanelValue  defaultValue?: PanelValue  /** Called as the panel moves, resizes or folds. */  onValueChange?: (value: PanelValue) => void  /** Called once a drag ends, a key moved it or it was folded: the moment to keep the value. */  onValueCommit?: (value: PanelValue) => void  /** The button that opens the panel: spread what it is given on a button of your own. */  trigger?: (attributes: { commandfor: string, command: 'toggle-popover' }) => ReactNode  /** More controls in the header, before the minimize and close buttons. */  actions?: ReactNode  minimizeLabel?: string  closeLabel?: string  /** Leave out the corner that resizes it. */  resizable?: boolean  /** How far an arrow key moves or resizes the panel, in pixels. */  step?: number}const measures = ['x', 'y', 'width', 'height'] as const/** * A floating panel. The browser opens it from the trigger and puts it in the top layer; the behavior moves, * resizes, folds and stacks it. This component opens and closes it from `open`, and passes its place in and out. */export function Panel({ title, handleLabel, open, onOpenChange, value, defaultValue, onValueChange, onValueCommit, trigger, actions, minimizeLabel = 'Minimize', closeLabel = 'Close', resizable = true, step = 16, className, id, style, children, ...root }: PanelProps) {  const generated = useId()  const name = id ?? generated  const [first] = useState(value ?? defaultValue)  const latest = useRef(first)  const ref = useBehavior(panel, { step }, {    value,    defaultValue,    onValueChange: (next: PanelValue) => { latest.current = next; onValueChange?.(next) },    // The panel says change once a move ends. A field in its body says change too.    on: { change: (event) => { if (event.target === event.currentTarget) onValueCommit?.(latest.current!) } },  })  const element = useRef<HTMLDivElement | null>(null)  const heard = useRef(onOpenChange)  /** What the owner knows: the last state it set or was told. */  const known = useRef(false)  const [tick, again] = useReducer((count: number) => count + 1, 0)  useEffect(() => { heard.current = onOpenChange })  useEffect(() => {    const popover = element.current    if (!popover || open === undefined) return    const shown = popover.matches(':popover-open')    known.current = open    if (open && !shown) popover.showPopover()    else if (!open && shown) popover.hidePopover()  }, [open, tick])  useEffect(() => {    const popover = element.current    if (!popover) return    const report = () => {      const shown = popover.matches(':popover-open')      if (shown !== known.current) heard.current?.(known.current = shown)      // An `open` this component is told wins: if its owner did not take the change, the panel goes back.      again()    }    popover.addEventListener('toggle', report)    return () => popover.removeEventListener('toggle', report)  }, [])  // The first place and size are drawn into the first render; after that the behavior writes them.  const place = Object.fromEntries(measures.filter(measure => typeof first?.[measure] === 'number').map(measure => [`--panel-${measure}`, `${first![measure]}px`]))  const attach = useCallback((node: HTMLDivElement | null) => {    element.current = node    return ref(node)  }, [ref])  return (    <>      {trigger?.({ commandfor: name, command: 'toggle-popover' })}      <div id={name} popover="manual" role="dialog" aria-labelledby={`${name}-title`} className={className ? `panel ${className}` : 'panel'} style={{ ...place, ...style } as CSSProperties} ref={attach} {...root}>        <div className="panel-header">          <button type="button" className="panel-handle" aria-label={handleLabel} />          <h2 className="panel-title" id={`${name}-title`}>{title}</h2>          <div className="panel-actions">            {actions}            <button type="button" className="btn btn-quiet btn-sm btn-square panel-minimize" aria-pressed={first?.minimized ?? false} aria-label={minimizeLabel}>              <svg className="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" strokeWidth="1" strokeLinecap="round" strokeLinejoin="round" aria-hidden="true"><path d="M3 8L13 8" /></svg>            </button>            <button type="button" className="btn btn-quiet btn-sm btn-square" {...{ commandfor: name, command: 'hide-popover' }} aria-label={closeLabel}>              <svg className="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" strokeWidth="1" strokeLinecap="round" strokeLinejoin="round" aria-hidden="true"><path d="M4 4L12 12" /><path d="M4 12L12 4" /></svg>            </button>          </div>        </div>        <div className="panel-body">{children}</div>        {resizable && <span className="panel-resize" aria-hidden="true" />}      </div>    </>  )}
ui/Panel.svelte
<script lang="ts" module>  import type { Snippet } from 'svelte'  import type { HTMLAttributes } from 'svelte/elements'  import type { PanelValue } from '@master/ui/behaviors/panel.js'  export type { PanelValue }  export interface PanelProps extends Omit<HTMLAttributes<HTMLDivElement>, 'title' | 'popover' | 'role'> {    /** The title in the header, which names the panel. */    title: string    /** The name of the handle: what it moves and how, such as "Move the Layers panel with the arrow keys". */    handleLabel: string    /** Whether the panel is open. Bind it, or leave it out and the trigger opens it. */    open?: boolean    onOpenChange?: (open: boolean) => void    /** Where the panel is, how large and whether it is folded. Bind it to put the panel back where it was. */    value?: PanelValue    /** Called as the panel moves, resizes or folds. */    onValueChange?: (value: PanelValue) => void    /** Called once a drag ends, a key moved it or it was folded: the moment to keep the value. */    onValueCommit?: (value: PanelValue) => void    /** The button that opens the panel: spread what it is given on a button of your own. */    trigger?: Snippet<[{ commandfor: string, command: 'toggle-popover' }]>    /** More controls in the header, before the minimize and close buttons. */    actions?: Snippet    minimizeLabel?: string    closeLabel?: string    /** Leave out the corner that resizes it. */    resizable?: boolean    /** How far an arrow key moves or resizes the panel, in pixels. */    step?: number  }</script><script lang="ts">  import { untrack } from 'svelte'  import type { Attachment } from 'svelte/attachments'  import { panel } from '@master/ui/behaviors/panel.js'  import { behavior } from './behavior.svelte.js'  let { title, handleLabel, open = $bindable(), onOpenChange, value = $bindable(), onValueChange, onValueCommit, trigger, actions, minimizeLabel = 'Minimize', closeLabel = 'Close', resizable = true, step = 16, class: className, id, children, ...root }: PanelProps = $props()  const generated = $props.id()  const name = $derived(id ?? generated)  const first = untrack(() => value)  // Svelte writes a style again whenever its owner changes it, so it writes the place the behavior holds.  let held = $state(first)  const px = (measure: number | null | undefined) => typeof measure === 'number' ? `${measure}px` : undefined  const attach = behavior(panel, () => ({ step }), {    value: () => value,    change: (next) => { value = held = next; onValueChange?.(next) },    settled: (kept) => { held = kept },    // The panel says change once a move ends. A field in its body says change too.    on: { change: (event) => { if (event.target === event.currentTarget && held) onValueCommit?.(held) } },  })  /** What the owner knows: the last state it set or was told. */  let known = false  const presence: Attachment<HTMLDivElement> = (popover) => {    const report = () => {      const shown = popover.matches(':popover-open')      if (shown === known) return      open = known = shown      onOpenChange?.(known)    }    popover.addEventListener('toggle', report)    $effect(() => {      if (open === undefined) return      const shown = popover.matches(':popover-open')      known = open      if (open && !shown) popover.showPopover()      else if (!open && shown) popover.hidePopover()    })    return () => popover.removeEventListener('toggle', report)  }</script><!--  @component  A floating panel. The browser opens it from the trigger and puts it in the top layer; the behavior moves,  resizes, folds and stacks it. This component opens and closes it from `open`, and passes its place in and out.-->{@render trigger?.({ commandfor: name, command: 'toggle-popover' })}<div id={name} popover="manual" role="dialog" aria-labelledby="{name}-title" class={['panel', className]} style:--panel-x={px(held?.x)} style:--panel-y={px(held?.y)} style:--panel-width={px(held?.width)} style:--panel-height={px(held?.height)} {@attach attach} {@attach presence} {...root}>  <div class="panel-header">    <button type="button" class="panel-handle" aria-label={handleLabel}></button>    <h2 class="panel-title" id="{name}-title">{title}</h2>    <div class="panel-actions">      {@render actions?.()}      <button type="button" class="btn btn-quiet btn-sm btn-square panel-minimize" aria-pressed={first?.minimized ?? false} aria-label={minimizeLabel}>        <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={name} command="hide-popover" aria-label={closeLabel}>        <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">{@render children?.()}</div>  {#if resizable}    <span class="panel-resize" aria-hidden="true"></span>  {/if}</div>
ui/Panel.vue
<script setup lang="ts">import { computed, nextTick, onMounted, useId, useTemplateRef, watch } from 'vue'import { panel, type PanelValue } from '@master/ui/behaviors/panel.js'import { useBehavior } from './use-behavior'defineOptions({ inheritAttrs: false })const { id, title, handleLabel, minimizeLabel = 'Minimize', closeLabel = 'Close', resizable = true, step = 16 } = defineProps<{  id?: string  /** The title in the header, which names the panel. */  title: string  /** The name of the handle: what it moves and how, such as "Move the Layers panel with the arrow keys". */  handleLabel: string  minimizeLabel?: string  closeLabel?: string  /** Leave out the corner that resizes it. */  resizable?: boolean  /** How far an arrow key moves or resizes the panel, in pixels. */  step?: number}>()/** Where the panel is, how large and whether it is folded: `v-model`. Keep it to put the panel back where it was. */const model = defineModel<PanelValue>()/** Whether the panel is open: `v-model:open`. Leave it out and the trigger opens it. */const open = defineModel<boolean>('open')const emit = defineEmits<{  /** The value, once a drag ends, a key moved it or it was folded: the moment to keep it. */  valueCommit: [value: PanelValue]}>()defineSlots<{  /** The button that opens the panel: bind what it is given on a button of your own. */  trigger?: (attributes: { commandfor: string, command: 'toggle-popover' }) => unknown  /** More controls in the header, before the minimize and close buttons. */  actions?: () => unknown  default?: () => unknown}>()/* * A floating panel. The browser opens it from the trigger and puts it in the top layer; the behavior moves, * resizes, folds and stacks it. This component opens and closes it from `open`, and passes its place in and out. */const generated = useId()const name = computed(() => id ?? generated)const first = model.valueconst root = useTemplateRef<HTMLDivElement>('root')const current = useBehavior(root, panel, () => ({ step }), model, {  // The panel says change once a move ends. A field in its body says change too.  change: (event) => {    const held = current()    if (event.target === root.value && held) emit('valueCommit', held)  },})// Vue writes a style again each time it renders, so it writes the place the behavior holds, not the first one.const place = () => {  const held = current() ?? first  const px = (measure: number | null | undefined) => typeof measure === 'number' ? `${measure}px` : undefined  return { '--panel-x': px(held?.x), '--panel-y': px(held?.y), '--panel-width': px(held?.width), '--panel-height': px(held?.height) }}/** What the owner knows: the last state it set or was told. */let known = falseconst sync = () => {  const popover = root.value  if (!popover || open.value === undefined) return  const shown = popover.matches(':popover-open')  known = open.value  if (open.value && !shown) popover.showPopover()  else if (!open.value && shown) popover.hidePopover()}const report = () => {  const popover = root.value  if (!popover) return  const shown = popover.matches(':popover-open')  if (shown !== known) open.value = known = shown  // An `open` this component is told wins: if its owner did not take the change, the panel goes back.  void nextTick(sync)}onMounted(sync)watch(open, sync, { flush: 'post' })</script><template>  <slot name="trigger" v-bind="{ commandfor: name, command: 'toggle-popover' as const }" />  <div :id="name" popover="manual" role="dialog" :aria-labelledby="`${name}-title`" class="panel" :style="place()" ref="root" v-bind="$attrs" @toggle="report">    <div class="panel-header">      <button type="button" class="panel-handle" :aria-label="handleLabel" />      <h2 class="panel-title" :id="`${name}-title`">{{ title }}</h2>      <div class="panel-actions">        <slot name="actions" />        <button type="button" class="btn btn-quiet btn-sm btn-square panel-minimize" :aria-pressed="first?.minimized ?? false" :aria-label="minimizeLabel">          <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="name" command="hide-popover" :aria-label="closeLabel">          <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"><slot /></div>    <span v-if="resizable" class="panel-resize" aria-hidden="true" />  </div></template>

#Classes

ClassDescription
panelThe 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-headerThe 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-handleA 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-titleThe 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-actionsThe controls at the end of the header, over the handle: small quiet square buttons
panel-minimizeThe button that folds the panel to its header. Pressed, it sits in a well, and the body and the corner are hidden
panel-bodyWhat 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-resizeThe 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

PropertyDefaultDescription
--panel-x100%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-y0pxThe 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-width20remThe width; the script writes it in pixels. It is kept between --panel-min-width and the viewport's width
--panel-heightautoThe 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-width12remThe narrowest a resize makes it
--panel-gutter.5remThe least room kept between the panel and the edges of the viewport
--panel-padding1remPadding around the body
--panel-radiuscalc(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

#Source

@master/ui/components/panel.css
/* * Panel: a floating surface the reader moves, resizes, folds to its header and closes, while the page under it * stays usable: layers beside a canvas, a color picker, an inspector, a chat. It is a popover="manual" in the * top layer, so it floats over everything without a z-index; panels stack in the order they were shown, and the * behavior brings the one the reader reaches into to the front. * * Material: the floating sheet of a popover, with the floating shadow, and the overlay shadow while it is held, * so a panel being dragged rises a step. The header is a bar of the same sheet over a hairline; its whole face is * one button, the handle, so a press anywhere on the bar that is not a control picks the panel up, and focus on * the handle rings the panel the keys move. * * Geometry: the header holds small (32px) buttons 6px from its edges, so the panel's corner is a small control's * corner plus that inset, 14px. On touch a button's 44px target reaches those 6px, to the header's edge. The panel is 20rem wide unless told otherwise, never narrower than 12rem. * * Placement is the stylesheet's. A script writes what only an event knows, the place and the size asked for * (--panel-x, --panel-y, --panel-width, --panel-height); the stylesheet clamps them to the viewport, the box a * fixed element's percentages are read from, so a panel is never wider or taller than the window, the whole of * its width stays in it and its header can never leave it. The window narrowing pushes a panel in; widening it * again lets the panel go back to the place it was given. Without a place, a panel opens at the top end corner. */@layer components.ui {  .panel {    --panel-x: 100%;    --panel-y: 0px;    --panel-width: 20rem;    --panel-height: auto;    --panel-min-width: 12rem;    --panel-gutter: .5rem;    --panel-inset: .375rem;    /* The inset is added after the corner scale, so it is passed divided by it. */    --panel-radius: calc(var(--radius-control-sm) + var(--panel-inset) / var(--corner-scale));    --panel-padding: 1rem;    /* The header's height: a small button and the inset around it. The part of the panel that never leaves the viewport. */    --panel-bar: calc(2rem + 2 * var(--panel-inset));    /* The width it is drawn at: the width asked for, no less than the least, no more than the viewport gives. */    --panel-drawn-width: min(max(var(--panel-min-width), var(--panel-width)), 100% - 2 * var(--panel-gutter));    --surface-color: var(--color-surface-floating);    position: fixed;    inset: auto;    top: clamp(var(--panel-gutter), var(--panel-y), 100% - var(--panel-bar) - var(--panel-gutter));    left: clamp(var(--panel-gutter), var(--panel-x), 100% - var(--panel-drawn-width) - var(--panel-gutter));    flex-direction: column;    width: var(--panel-drawn-width);    height: var(--panel-height);    min-height: var(--panel-bar);    max-height: calc(100% - 2 * var(--panel-gutter));    margin: 0;    padding: 0;    border: 0;    border-radius: calc(var(--panel-radius) * var(--corner-scale));    corner-shape: var(--corner-shape);    /* The header's tone and the body's scroll are finished by the panel's corner. */    overflow: clip;    background: var(--color-surface-floating);    color: var(--color-text-body);    box-shadow: var(--shadow-floating);    font-size: .875rem;    line-height: 1.5;    transform-origin: top;    @apply --pop;    transition-property: opacity, scale, box-shadow, overlay, display;    /* With no place given, a panel from right to left opens at the top left. */    &:dir(rtl) {      --panel-x: 0px;    }    &:popover-open {      display: flex;    }    /* Held by its handle or its corner: it rises to the overlay shadow, and the drag selects nothing. */    &:has(.panel-handle:active:not(:disabled, [aria-disabled="true"]), .panel-resize:active:not([aria-disabled="true"])) {      box-shadow: var(--shadow-overlay);      user-select: none;      -webkit-user-select: none;    }    /* The keys move the whole panel, so focus on its handle rings the whole panel. */    &:has(.panel-handle:focus-visible) {      @apply --focus-ring;    }    /*     * Folded to its header: only as tall as the header, with no body and no corner to resize from. The body is     * hidden from sight, focus and assistive technology even where a utility gives it a display of its own.     */    &:has(.panel-minimize[aria-pressed="true"]) {      height: var(--panel-bar);      .panel-body,      .panel-resize {        display: none;        visibility: hidden;      }    }    @media (forced-colors: active) {      outline: 1px solid CanvasText;      outline-offset: -1px;      &:has(.panel-handle:focus-visible) {        outline: 2px solid Highlight;        outline-offset: 2px;      }    }  }  /* The header: the title at the start, the actions at the end, and the handle under both. */  .panel-header {    position: relative;    display: flex;    flex: none;    align-items: center;    gap: .25rem;    min-height: var(--panel-bar);    padding: var(--panel-inset);    padding-inline-start: .875rem;  }  /*   * The handle: a button the size of the header, under the title and the actions, so the bar is what the hand   * grabs. It draws nothing of its own; its focus is drawn on the panel.   */  .panel-handle {    position: absolute;    inset: 0;    margin: 0;    padding: 0;    border: 0;    border-radius: 0;    outline: none;    background: transparent;    color: inherit;    appearance: none;    cursor: grab;    touch-action: none;    user-select: none;    -webkit-user-select: none;    -webkit-tap-highlight-color: transparent;    &:active:not(:disabled, [aria-disabled="true"]) {      cursor: grabbing;    }    /* A handle the page turned off carries nothing, so it does not offer the hand. */    &:is(:disabled, [aria-disabled="true"]) {      cursor: default;    }  }  /* The title takes no pointer, so a press on it reaches the handle under it. */  .panel-title {    flex: 1 1 auto;    min-width: 0;    margin: 0;    overflow: hidden;    color: var(--color-text-strong);    font: var(--font-weight-control) .875rem/1.25 var(--font-family-sans);    font-feature-settings: inherit;    text-overflow: ellipsis;    white-space: nowrap;    pointer-events: none;  }  /* The controls at the end of the header, over the handle. */  .panel-actions {    position: relative;    display: inline-flex;    flex: none;    align-items: center;    gap: 2px;    margin-inline-start: auto;  }  /* The two stand 2px apart: on touch each target grows above and below only, so Close does not answer for Minimize. */  @media (pointer: coarse) {    .panel-actions .btn::after {      @apply --touch-target-block;    }  }  /* The minimize button is a quiet toggle: pressed, it sits in a well, and the panel is folded to its header. */  .panel-minimize {    flex: none;  }  /* What the panel holds, under a hairline. A long body scrolls inside the panel. */  .panel-body {    flex: 1 1 auto;    min-height: 0;    padding: var(--panel-padding);    border-block-start: 1px solid var(--color-line-hair);    overflow: auto;    overscroll-behavior: contain;    scrollbar-width: thin;    scrollbar-color: var(--color-line-strong) transparent;  }  /*   * The corner a pointer resizes from, at the bottom end: two short diagonal lines in the ink of a firm line,   * on a 16px target, 44px on touch screens. The keys resize from the handle.   */  .panel-resize {    position: absolute;    inset-block-end: 0;    inset-inline-end: 0;    width: 1rem;    height: 1rem;    color: var(--color-line-mark);    cursor: nwse-resize;    touch-action: none;    user-select: none;    -webkit-user-select: none;    -webkit-tap-highlight-color: transparent;    transition: color var(--duration-quick) var(--easing-glide);    &::before {      content: '';      position: absolute;      inset-block-end: .3125rem;      inset-inline-end: .3125rem;      width: .5rem;      height: .5rem;      background: linear-gradient(135deg, transparent 38%, currentColor 38% 50%, transparent 50% 70%, currentColor 70% 82%, transparent 82%);    }    &:dir(rtl) {      cursor: nesw-resize;      &::before {        scale: -1 1;      }    }    @media (hover: hover) {      &:hover {        color: var(--color-text-muted);      }    }    &:active {      color: var(--color-text-strong);    }    @media (pointer: coarse) {      width: 2.75rem;      height: 2.75rem;      color: var(--color-text-muted);    }    /* A corner the page turned off resizes nothing: it keeps its place in disabled ink and offers no resize. */    &[aria-disabled="true"] {      color: var(--color-text-disabled);      cursor: default;    }    /* The two lines are a gradient, which forced colors would drop: kept out of them, in the system's text color. */    @media (forced-colors: active) {      forced-color-adjust: none;      color: CanvasText;    }  }}