//
// Copyright 2026 DXOS.org
//
// @import-as-namespace
import {
type FloatingPanelPoint,
FloatingPanel as FloatingPanelPrimitive,
type FloatingPanelSize,
type FloatingPanelStage,
} from '@ark-ui/react/floating-panel';
import { Portal } from '@ark-ui/react/portal';
import React, { type RefObject, forwardRef, useMemo } from 'react';
import { useTranslation } from 'react-i18next';
import { mx } from '@dxos/ui-theme';
import { type ThemedClassName } from '@dxos/ui-types';
import { translationKey } from '#translations';
import { recipes } from '../../recipes.ts';
import { type Size } from '../../sizes.ts';
import { Button, type ButtonProps } from '../Button/Button.tsx';
const RESIZE_AXES: readonly FloatingPanelPrimitive.ResizeTriggerAxis[] = ['n', 's', 'e', 'w', 'ne', 'nw', 'se', 'sw'];
//
// Root
//
type FloatingPanelRootProps = FloatingPanelPrimitive.RootProps;
/**
* Ark floating panel: a draggable, resizable window over the page. The machine owns the open state, position and size
* (as `--x`/`--y`/`--width`/`--height` on the positioner), the stack order of open panels and the minimized/maximized
* stages. Content mounts on open and unmounts on close unless the caller opts out.
*/
const FloatingPanelRoot = ({
lazyMount = true,
unmountOnExit = true,
translations: translationsProp,
...props
}: FloatingPanelRootProps) => {
const { t } = useTranslation(translationKey);
// zag names its own stage triggers, so the names come from the app's translations.
const translations = useMemo(
() => ({
minimize: t('floating-panel.minimize.label'),
maximize: t('floating-panel.maximize.label'),
restore: t('floating-panel.restore.label'),
...translationsProp,
}),
[t, translationsProp],
);
return (
);
};
FloatingPanelRoot.displayName = 'FloatingPanel.Root';
//
// Trigger
//
type FloatingPanelTriggerProps = FloatingPanelPrimitive.TriggerProps;
/** Use `asChild` to open the panel from a `Button`. */
const FloatingPanelTrigger = forwardRef((props, forwardedRef) => (
));
FloatingPanelTrigger.displayName = 'FloatingPanel.Trigger';
//
// Content
//
type FloatingPanelContentProps = ThemedClassName & {
/** The panel's size; it has no sized trigger row to inherit one from. */
size?: Size;
/** Portals into this element instead of the body. */
container?: RefObject;
};
/**
* zag writes `z-index: var(--z-index)` (its stack order among open panels, from 1) inline on the positioner, so the
* dialog band (50) is added inline too, where no class could reach.
*/
const POSITIONER_STYLE = { zIndex: 'calc(50 + var(--z-index, 1))' };
/**
* The window, a `raised` surface: portalled inside the machine's positioner, which carries its place and size, and
* edged with a resize handle on every side and corner (zag disables them unless `resizable`).
*/
const FloatingPanelContent = forwardRef(
({ classNames, size = 'md', container, children, ...props }, forwardedRef) => (
{children}
{RESIZE_AXES.map((axis) => (
))}
),
);
FloatingPanelContent.displayName = 'FloatingPanel.Content';
//
// Header
//
type FloatingPanelHeaderProps = ThemedClassName;
/** The block-tall title row: a DragTrigger holding the Title, then the Control. */
const FloatingPanelHeader = forwardRef(
({ classNames, ...props }, forwardedRef) => (
),
);
FloatingPanelHeader.displayName = 'FloatingPanel.Header';
//
// DragTrigger
//
type FloatingPanelDragTriggerProps = ThemedClassName;
/** The area a pointer drags the panel by, filling the header up to its Control. */
const FloatingPanelDragTrigger = forwardRef(
({ classNames, ...props }, forwardedRef) => (
),
);
FloatingPanelDragTrigger.displayName = 'FloatingPanel.DragTrigger';
//
// Title
//
type FloatingPanelTitleProps = ThemedClassName;
/** Names the window (the content's `aria-labelledby`), truncating. */
const FloatingPanelTitle = forwardRef(
({ classNames, ...props }, forwardedRef) => (
),
);
FloatingPanelTitle.displayName = 'FloatingPanel.Title';
//
// Control
//
type FloatingPanelControlProps = ThemedClassName;
/** The header's trailing run of buttons: stage triggers and the close. */
const FloatingPanelControl = forwardRef(
({ classNames, ...props }, forwardedRef) => (
),
);
FloatingPanelControl.displayName = 'FloatingPanel.Control';
//
// StageTrigger
//
const STAGE_KEYS: Record = {
minimized: 'minimize',
maximized: 'maximize',
default: 'restore',
};
const STAGE_ICONS: Record = {
minimized: 'ph--minus--regular',
maximized: 'ph--arrows-out-simple--regular',
default: 'ph--arrows-in-simple--regular',
};
type FloatingPanelStageTriggerProps = Omit & {
/** The stage the button moves the panel to; zag hides `default` until the panel is staged and the others while it is. */
stage: FloatingPanelStage;
icon?: string;
label?: string;
};
/** Minimize, maximize or restore, as a ghost icon-only Button named by the app's translations. */
const FloatingPanelStageTrigger = forwardRef(
({ stage, icon, label, ...props }, forwardedRef) => {
const { t } = useTranslation(translationKey);
return (
);
},
);
FloatingPanelStageTrigger.displayName = 'FloatingPanel.StageTrigger';
//
// CloseTrigger
//
type FloatingPanelCloseTriggerProps = Omit & {
icon?: string;
label?: string;
};
/** Closes the panel: a ghost icon-only Button. */
const FloatingPanelCloseTrigger = forwardRef(
({ icon, label, ...props }, forwardedRef) => {
const { t } = useTranslation(translationKey);
return (
);
},
);
FloatingPanelCloseTrigger.displayName = 'FloatingPanel.CloseTrigger';
//
// Body
//
type FloatingPanelBodyProps = ThemedClassName;
/** Takes the height the header leaves and clips; its content scrolls itself (e.g. in a `ScrollArea`). */
const FloatingPanelBody = forwardRef(
({ classNames, ...props }, forwardedRef) => (
),
);
FloatingPanelBody.displayName = 'FloatingPanel.Body';
export type {
FloatingPanelBodyProps as BodyProps,
FloatingPanelCloseTriggerProps as CloseTriggerProps,
FloatingPanelContentProps as ContentProps,
FloatingPanelControlProps as ControlProps,
FloatingPanelDragTriggerProps as DragTriggerProps,
FloatingPanelHeaderProps as HeaderProps,
FloatingPanelPoint as Point,
FloatingPanelRootProps as RootProps,
FloatingPanelSize as Size,
FloatingPanelStage as Stage,
FloatingPanelStageTriggerProps as StageTriggerProps,
FloatingPanelTitleProps as TitleProps,
FloatingPanelTriggerProps as TriggerProps,
};
export {
FloatingPanelBody as Body,
FloatingPanelCloseTrigger as CloseTrigger,
FloatingPanelContent as Content,
FloatingPanelControl as Control,
FloatingPanelDragTrigger as DragTrigger,
FloatingPanelHeader as Header,
FloatingPanelRoot as Root,
FloatingPanelStageTrigger as StageTrigger,
FloatingPanelTitle as Title,
FloatingPanelTrigger as Trigger,
};