// // Copyright 2026 DXOS.org // // @import-as-namespace import React, { type ButtonHTMLAttributes, type InputHTMLAttributes, type KeyboardEvent, type PointerEvent, useCallback, useContext, useEffect, useId, useRef, useState, } from 'react'; import { useTranslation } from 'react-i18next'; import { composeEventHandlers, useControllableState } from '@dxos/react-hooks'; import { AI_ACTION_ICON } from '@dxos/ui-types'; import { downloadBlob } from '@dxos/util'; import { translationKey } from '#translations'; import { composable } from '../../util/slots.ts'; import { Button, type ButtonContentProps, type ButtonVariantProps } from '../Button/Button.tsx'; import { RowContext } from '../Listbox/grid.ts'; import { Toggle } from '../Toggle/Toggle.tsx'; import type * as Tooltip from '../Tooltip/Tooltip.tsx'; /** * Every preset is a Button whose icon is fixed and whose label defaults from the `system-button.*` translations; * callers may still override `label`. */ type SystemButtonProps = Omit, 'children' | 'aria-label' | 'title'> & ButtonVariantProps & { label?: string; /** Only the icon, named by the label in a Tooltip (the default); `false` shows the label after the icon. */ iconOnly?: boolean; /** Icon-only: opt out of the label Tooltip. */ showTooltip?: boolean; /** Icon-only: the side the label Tooltip opens on. */ tooltipSide?: Tooltip.Side; }; type PresetContent = Pick & { icon: string; label: string; }; /** Button content for a preset: icon-only with its Tooltip unless `iconOnly` is false, when the label shows. */ const presetContent = ({ icon, label, iconOnly = true, showTooltip, tooltipSide, }: PresetContent): ButtonContentProps => iconOnly ? { iconOnly: true, icon, label, showTooltip, tooltipSide } : { icon, label }; /** Star and Bookmark: a Toggle (`aria-pressed`) whose icon and label follow the pressed state. */ export type SystemTogglePresetProps = SystemButtonProps & { pressed?: boolean; defaultPressed?: boolean; onPressedChange?: (pressed: boolean) => void; }; type ToggleSpec = { icon: string; activeIcon: string; labelKey: string; activeLabelKey: string; iconValence?: 'warning'; }; const createTogglePreset = ( displayName: string, { icon, activeIcon, labelKey, activeLabelKey, iconValence }: ToggleSpec, ) => { const Preset = composable( ( { label, iconOnly, showTooltip, tooltipSide, pressed: pressedProp, defaultPressed, onPressedChange, ...props }, forwardedRef, ) => { const { t } = useTranslation(translationKey); // Owned here, not left to the toggle machine, because the label (not only the icon) follows the pressed state. const [pressed = false, setPressed] = useControllableState({ prop: pressedProp, defaultProp: defaultPressed ?? false, onChange: onPressedChange, }); return ( ); }, ); Preset.displayName = displayName; return Preset; }; const createStaticPreset = ( displayName: string, icon: string, labelKey: string, defaults: Pick = {}, ) => { const Preset = composable( ({ label, iconOnly, showTooltip, tooltipSide, ...props }, forwardedRef) => { const { t } = useTranslation(translationKey); return (