/**
* The frame every panel opens in: its header (the title, or what the panel
* puts there, and a close button) and one of three ways to open, the panel's
* `present` for the viewer's size, or else the default for it
* (`panel-layout.ts`):
*
* - dock: beside the pages, which make room for it. Its inner edge drags it
* wider or narrower, by pointer or with the keys on the edge, within the
* panel's `width` limits, unless it isn't `resizable`. The viewer
* remembers that width per panel while it runs, and tells the app
* (`onPanelResize`). It is part of the page: Tab reaches it in order, and
* it never takes focus. A panel that can't close (`closable: false`) has no
* close button here.
* - overlay: over the edge of the pages, which stay where they are and dim.
* A click on them closes it; the bars around the pages keep working. Its
* inner edge drags as a docked panel's does, to the same width.
* - sheet: up from the bottom of the viewer, over everything (`sheet.tsx`):
* as wide as the viewer on a phone, and as wide as the panel, in the
* middle, on a wider viewer. It has no edge.
*
* An overlay or a sheet takes focus when it opens, closes on Escape and gives
* focus back to the button that opened it. A sheet keeps Tab inside it; from
* an overlay Tab moves on to the rest of the viewer. A panel that holds a
* list closes from either when one of its items is picked
* ({@link useCloseOnPick}). The frame is named after its title for assistive
* technology: a region when docked, a dialog otherwise (modal as a sheet).
*
* A panel with nothing to list opens as a bar instead ({@link PanelBar}).
*/
import type { PanelPresentation, PanelPresentationBySize, PanelWidth } from '@embedpdf/core-ui';
import { useT } from '@embedpdf/react/i18n';
import { useKernel } from '@embedpdf/react/runtime';
import {
useId,
useLayoutEffect,
useReducer,
useRef,
type PointerEvent as ReactPointerEvent,
type ReactNode,
} from 'react';
import { useLayoutEnv, usePanelResize } from '../config-context';
import { Icon } from './icons';
import {
clampPanelWidth,
maxPanelWidthOf,
overlayWidthOf,
panelWidthsOf,
presentationOf,
sheetWidthOf,
widthAfterDrag,
widthAfterKey,
type PanelEdge as PanelEdgeOf,
type PanelSide,
type PanelWidths,
} from './panel-layout';
import { CloseOnPickProvider, SCRIM_CLASS, Sheet, useCloseOnPick } from './sheet';
import { WithTooltip } from './tooltip';
import { holdFocus } from './viewer-layer';
export { useCloseOnPick };
export interface PanelFrameProps {
/** The panel's id: the frame's `data-epdf-panel`, and what its dragged width is kept under. */
readonly id: string;
readonly side: PanelSide;
/** How it opens, at every size or per size. Default: dock, overlay, or sheet on a phone. */
readonly present?: PanelPresentation | PanelPresentationBySize;
/** Its width in px, or where it starts and how far it drags. Default 288, from 200 to 640. */
readonly width?: number | PanelWidth;
/** Whether its edge drags, docked or as an overlay. Default true. */
readonly resizable?: boolean;
/** Whether it can close where it docks: without, it has no close button there. Default true. */
readonly closable?: boolean;
/** Its title: in its header, and the name assistive technology gives the frame. */
readonly title: string;
readonly onClose: () => void;
/** Drawn in the header in place of the title: the switcher between the panels on its side. */
readonly header?: ReactNode;
/** Drawn under the header, outside what scrolls: the bars the layout puts in the panel. */
readonly bars?: ReactNode;
/** It holds a list: in a sheet or an overlay, picking an item closes it ({@link useCloseOnPick}). */
readonly list?: boolean;
readonly children: ReactNode;
}
export function PanelFrame(props: PanelFrameProps) {
const { size } = useLayoutEnv();
const present = presentationOf(size, props.present);
if (present === 'sheet') return ;
if (present === 'overlay') return ;
return ;
}
// ── the header ───────────────────────────────────────────────────────────────
/** The header: the title, or what the panel puts there, and a close button with `onClose`. */
function PanelHeader({
title,
header,
onClose,
}: {
title: string;
header: ReactNode;
onClose?: () => void;
}) {
const t = useT();
return (
{header ??
{title}
}
{onClose && (
{(tooltip) => (
)}
)}
);
}
// ── widths ───────────────────────────────────────────────────────────────────
/** The width each panel was dragged to, per viewer (its kernel), for as long as the viewer runs. */
const draggedWidths = new WeakMap