// // Copyright 2026 DXOS.org // // @import-as-namespace import { ark } from '@ark-ui/react/factory'; import { useMachine } from '@zag-js/react'; import React, { type AnchorHTMLAttributes, type HTMLAttributes, forwardRef, useContext, useId } from 'react'; import { recipes } from '../../recipes.ts'; import { type Size } from '../../sizes.ts'; import { composable, composableProps, slottable } from '../../util/slots.ts'; import * as ScrollArea from '../ScrollArea/ScrollArea.tsx'; import { Separator, type SeparatorProps } from '../Separator/Separator.tsx'; import * as ToggleGroup from '../ToggleGroup/ToggleGroup.tsx'; import { ToolbarContext, useToolbarItem } from './toolbar-context.ts'; import * as toolbar from './toolbar-machine.ts'; // // Root // type ToolbarRootProps = { size?: Size; orientation?: toolbar.Orientation; /** Arrow keys wrap from the last item to the first and back; on by default. */ loop?: boolean; /** Disables every control in the toolbar. */ disabled?: boolean; /** * Dims the controls without disabling them, for a toolbar whose host is not in play (an unattended plank): they stay * operable, so the first press both brings the host into play and acts. */ inactive?: boolean; }; /** * The toolbar element as a composable part, so the ScrollArea viewport slot merges onto it (a plain `ark.div` gets the * dev warning wrapper, which breaks the frame's child rules). */ const ToolbarElement = composable & { asChild?: boolean }>( (props, forwardedRef) => , ); /** * `role=toolbar` with the roving focus that role promises (decision 9); items that overflow scroll along its axis in a * thin ScrollArea whose bar shows on hover, the toolbar itself being the viewport. */ const ToolbarRoot = slottable( ( { children, asChild, size, orientation = 'horizontal', loop = true, disabled, inactive, ...props }, forwardedRef, ) => { const service = useMachine(toolbar.machine, { id: useId(), orientation, loop, disabled }); const api = toolbar.connect(service); const { className, ...rest } = composableProps(props, { classNames: recipes.toolbar() }); const rootProps = api.getRootProps(); return ( {children} ); }, ); ToolbarRoot.displayName = 'Toolbar.Root'; // // Separator // type ToolbarSeparatorProps = Omit & { /** `gap` is an empty spacer that grows, pushing the items after it to the toolbar's end. */ variant?: 'line' | 'gap'; }; /** * A rule across the toolbar's axis (vertical in a horizontal toolbar), or with `variant='gap'` a growing spacer; not an * item, so roving focus skips it. */ const ToolbarSeparator = composable(({ variant, ...props }, forwardedRef) => { const api = useContext(ToolbarContext); if (variant === 'gap') { const { className, ...rest } = composableProps(props, { classNames: recipes.toolbarGap() }); return
; } return ( ); }); ToolbarSeparator.displayName = 'Toolbar.Separator'; // // Text // type ToolbarTextProps = {}; /** A run of text that takes the free space and truncates; not an item, so roving focus skips it. */ const ToolbarText = slottable(({ children, asChild, ...props }, forwardedRef) => { const { className, ...rest } = composableProps(props, { classNames: recipes.toolbarText() }); return ( {children} ); }); ToolbarText.displayName = 'Toolbar.Text'; // // Link // type ToolbarLinkProps = AnchorHTMLAttributes; /** A link that is a toolbar item (roving focus); opens in a new tab by default, like the current `Link`. */ const ToolbarLink = composable( ({ children, target = '_blank', rel = 'noreferrer', onFocus, onClick, ...props }, forwardedRef) => { const { disabled, ...toolbarItem } = useToolbarItem() ?? { disabled: undefined, onFocus: undefined }; const { className, ...rest } = composableProps(props, { classNames: recipes.toolbarLink() }); return ( { onFocus?.(event); toolbarItem.onFocus?.(); }} onClick={(event) => { if (disabled) { event.preventDefault(); return; } onClick?.(event); }} data-scope='toolbar' data-part='link' className={className} ref={forwardedRef} > {children} ); }, ); ToolbarLink.displayName = 'Toolbar.Link'; // // ToggleGroup // type ToolbarToggleGroupProps = ToggleGroup.RootProps; /** A ToggleGroup whose items join the toolbar's roving focus, so the group adds no tab stop or arrow handling of its own. */ const ToolbarToggleGroup = forwardRef((props, forwardedRef) => ( )); ToolbarToggleGroup.displayName = 'Toolbar.ToggleGroup'; export type { ToolbarLinkProps as LinkProps, ToolbarRootProps as RootProps, ToolbarSeparatorProps as SeparatorProps, ToolbarTextProps as TextProps, ToolbarToggleGroupProps as ToggleGroupProps, }; export { ToolbarLink as Link, ToolbarRoot as Root, ToolbarSeparator as Separator, ToolbarText as Text, ToolbarToggleGroup as ToggleGroup, }; export * from './toolbar-context.ts';