# @embedpdf/react — ./toolbar BarChild [TypeAliasDeclaration] (from packages/core/ui/src/schema.ts): export type BarChild = BarItem | CustomItem | string; BarGroup [InterfaceDeclaration] (from packages/core/ui/src/schema.ts): export interface BarGroup{ readonly id: string; readonly labelKey?: string; readonly role?: 'buttons' | 'tabs'; readonly shed?: boolean; readonly collapse?: 'menu' | 'select'; readonly importance?: Importance; readonly dependent?: boolean; readonly items: readonly BarChild[]; } BarItem [InterfaceDeclaration] (from packages/core/ui/src/schema.ts): export interface BarItem{ readonly command: string; readonly variants?: readonly Variant[]; readonly importance?: Importance; readonly emphasis?: Emphasis; } BarSchema [InterfaceDeclaration] (from packages/core/ui/src/schema.ts): export interface BarSchema{ readonly id: string; readonly sections: BarSections; } BarSections [InterfaceDeclaration] (from packages/core/ui/src/schema.ts): export interface BarSections{ readonly start?: readonly BarGroup[]; readonly center?: readonly BarGroup[]; readonly end?: readonly BarGroup[]; } CollapsedGroupView [TypeAliasDeclaration]: export type CollapsedGroupView = CollapsedGroupViewOf; CustomItem [InterfaceDeclaration] (from packages/core/ui/src/schema.ts): export interface CustomItem{ readonly slot: string; readonly variants?: readonly string[]; readonly terminal?: string; readonly importance?: Importance; readonly grow?: boolean; } CustomSlotContext [InterfaceDeclaration] (from packages/core/ui/src/toolbar-layout.ts): export interface CustomSlotContext{ layer: 'live' | 'measure'; measure: (length: number) => void; } DEFAULT_IMPORTANCE [VariableDeclaration] (from packages/core/ui/src/schema.ts): DEFAULT_IMPORTANCE: Importance = 3 GroupDisclosureView [TypeAliasDeclaration]: export type GroupDisclosureView = GroupDisclosureViewOf; Importance [TypeAliasDeclaration] (from packages/core/ui/src/schema.ts): export type Importance = 1 | 2 | 3 | 4 | 5; OverflowMenuView [TypeAliasDeclaration]: export type OverflowMenuView = OverflowMenuViewOf; OverflowRow [TypeAliasDeclaration] (from packages/core/ui/src/projection.ts): export type OverflowRow = |{ readonly kind: 'command'; readonly command: string} |{ readonly kind: 'submenu'; readonly command: string; readonly menu: string} ; OverflowSection [InterfaceDeclaration] (from packages/core/ui/src/projection.ts): export interface OverflowSection{ readonly labelKey?: string; readonly role?: 'radio'; readonly rows: readonly OverflowRow[]; } PINNED [VariableDeclaration] (from packages/core/ui/src/schema.ts): PINNED: Importance = 5 StripView [TypeAliasDeclaration]: export type StripView = StripViewOf; StripViewGroup [TypeAliasDeclaration]: export type StripViewGroup = StripViewGroupOf; Toolbar [FunctionDeclaration]: export function Toolbar({ bar, orientation = 'horizontal', gap = 8, separatorWidth = 1, 'aria-label': ariaLabel, className, style, renderCommand = defaultRenderCommand, renderCustom, renderCollapsed, renderGroupTrigger, renderSeparator, renderOverflowTrigger = defaultRenderOverflowTrigger, renderOverflowMenu, } : ToolbarProps){ const kernel = useKernel(); const commands = useCapability(CommandsToken); const documentId = useDocumentId(); const vertical = orientation === 'vertical'; const [, force] = useReducer((x: number) => x + 1, 0); useEffect(() => kernel.subscribe(force), [kernel]); const containerRef = useRef(null); const [containerLength, setContainerLength] = useState(0); useLayoutEffect( () => containerRef.current ? observeContentLength(containerRef.current, orientation, setContainerLength) : undefined, [orientation], ); const [lengths] = useState(createToolbarLengths); const [, bumpMeasureVersion] = useReducer((version: number) => version + 1, 0); const onLength = useMemo( () => (key: string, length: number) =>{ if (lengths.report(orientation, key, length)) bumpMeasureVersion(); } , [lengths, orientation], ); const roving = useRef(null); useLayoutEffect(() =>{ const toolbar = containerRef.current; if (!toolbar) return; const focus = attachToolbarFocus(toolbar, orientation); roving.current = focus; return () =>{ focus.detach(); roving.current = null; } ; } , [orientation]); useLayoutEffect(() => roving.current?.refresh()); const resolveCmd = (id: string): ResolvedCommand | null => commands.resolveCommand(id, documentId ?? undefined); const executeCmd = (id: string) => void commands.execute(id, { documentId: documentId ?? undefined} ); const normalized = useMemo(() => normalizeBar(bar), [bar]); const layout = layoutToolbar({ bar: normalized, resolve: resolveCmd, unregistered: unregisteredCommand, execute: executeCmd, menuTarget: (id) => commands.getMenuTarget(id), metrics: lengths.metrics(orientation, gap, separatorWidth), measureKey: toolbarMeasureKey, containerLength, orientation, } ); const [overflowOpen, setOverflowOpen] = useState(false); useEffect(() =>{ if (!layout.hasOverflow) setOverflowOpen(false); } , [layout.hasOverflow]); const isExternalSlot = (part: ToolbarPart): boolean => part.kind === 'custom' && !renderCustom?.[part.name]; const renderPart = ( part: ToolbarPart, layer: 'live' | 'measure', ): React.ReactNode =>{ switch (part.kind){ case 'command': return renderCommand(part.command, part.variant, part.run); case 'custom':{ const terminal = () => part.terminal ? renderCommand(part.terminal, 'icon', part.runTerminal) : null; const registered = renderCustom?.[part.name]; if (registered){ const custom = registered(part.variant, { layer, measure: (length) => onLength(part.measureKey, length), } ); return custom === undefined ? terminal() : custom; } if (layer === 'measure') return null; return ( { terminal()} ); } case 'collapsed':{ const drawn = renderCollapsed?.(part.view); return drawn === undefined ? ( ) : ( drawn ); } case 'disclosure':{ const drawn = renderGroupTrigger?.(part.view); return drawn === undefined ? ( ) : ( drawn ); } } } ; const renderLiveSection = (section: LiveSection): React.ReactNode => section.groups.map((group, i) => ( { i > 0 && (renderSeparator ? renderSeparator() : )} { group.parts.map((part) => ( { renderPart(part, 'live')} ))} )); const sectionStyle = (section: LiveSection): React.CSSProperties => ({ display: 'flex', flexDirection: vertical ? 'column' : 'row', alignItems: 'center', gap, flex: section.grows ? '1 0 auto' : '0 0 auto', ...(section.spacing === 'balance' ? vertical ?{ marginTop: 'auto', marginBottom: 'auto'} :{ marginLeft: 'auto', marginRight: 'auto'} : section.spacing === 'gap' ? vertical ?{ marginTop: gap} :{ marginInlineStart: gap} : null), } ); const overflowView: OverflowMenuView ={ sections: layout.overflow, isOpen: overflowOpen, close: () => setOverflowOpen(false), resolve: resolveCmd, execute: executeCmd, } ; return (
{ layout.sections.map((section) => (
{ renderLiveSection(section)} { section.name === 'end' && layout.hasOverflow && ( { renderOverflowTrigger(overflowOpen, () => setOverflowOpen((previous) => !previous))} { renderOverflowMenu ? ( renderOverflowMenu(overflowView) ) : ( )} )}
))} { }
{ layout.measured .filter((part) => !isExternalSlot(part)) .map((part) =>{ const drawn = renderPart(part, 'measure'); if (part.kind === 'custom' && drawn === null) return null; return ( { drawn} ); } )} { renderOverflowTrigger(false, () =>{ } )}
); } ToolbarProps [InterfaceDeclaration]: export interface ToolbarProps{ bar: BarSchema; orientation?: 'horizontal' | 'vertical'; gap?: number; separatorWidth?: number; 'aria-label'?: string; className?: string; style?: React.CSSProperties; renderCommand?: (cmd: ResolvedCommand, variant: string, run: () => void) => React.ReactNode; renderCustom?: Record React.ReactNode>; renderCollapsed?: (view: CollapsedGroupView) => React.ReactNode; renderGroupTrigger?: (view: GroupDisclosureView) => React.ReactNode; renderSeparator?: () => React.ReactNode; renderOverflowTrigger?: (isOpen: boolean, toggle: () => void) => React.ReactNode; renderOverflowMenu?: (view: OverflowMenuView) => React.ReactNode; } Variant [TypeAliasDeclaration] (from packages/core/ui/src/schema.ts): export type Variant = 'icon+label' | 'icon' | 'label'; custom [FunctionDeclaration] (from packages/core/ui/src/schema.ts): export function custom( slot: string, terminal?: string, options?: Omit, ): CustomItem{ return terminal === undefined ?{ slot, ...options} :{ slot, terminal, ...options} ; } group [FunctionDeclaration] (from packages/core/ui/src/schema.ts): export function group( id: string, items: readonly BarChild[], options?: Omit, ): BarGroup{ return{ id, ...options, items} ; } item [FunctionDeclaration] (from packages/core/ui/src/schema.ts): export function item(command: string, options?: Omit): BarItem{ return{ command, ...options} ; } useStripView [FunctionDeclaration]: export function useStripView(bar: BarSchema | undefined): StripView | null{ const commands = useCapability(CommandsToken); const documentId = useDocumentId(); const normalized = useMemo(() => (bar ? normalizeBar(bar) : null), [bar]); const groups = useKernelValue( () => normalized ? stripGroupsOf(normalized, (id) => commands.resolveCommand(id, documentId ?? undefined)) : NO_STRIP_GROUPS, stripGroupsEqual, ); return useMemo( () => groups.length === 0 ? null :{ groups, execute: (id: string) => void commands.execute(id, { documentId: documentId ?? undefined} ), } , [groups, commands, documentId], ); }