Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: Dan Rukas <dan.rukas@gmail.com>
99 lines
2.4 KiB
TypeScript
99 lines
2.4 KiB
TypeScript
import React from 'react';
|
|
import { useToolbar } from '@ohif/core';
|
|
|
|
/**
|
|
* Props for the Toolbar component that renders a collection of toolbar buttons and/or button sections.
|
|
*
|
|
* @interface ToolbarProps
|
|
*/
|
|
interface ToolbarProps {
|
|
/**
|
|
* The section of buttons to display in the toolbar.
|
|
* Common values include 'primary', 'secondary', 'tertiary', etc.
|
|
* Defaults to 'primary' if not specified.
|
|
*
|
|
* @default 'primary'
|
|
*/
|
|
buttonSection?: string;
|
|
|
|
/**
|
|
* The unique identifier of the viewport this toolbar is associated with.
|
|
*/
|
|
viewportId?: string;
|
|
|
|
/**
|
|
* The numeric position or location of the toolbar.
|
|
* Used for ordering and layout purposes in the UI.
|
|
*/
|
|
location?: number;
|
|
}
|
|
|
|
export function Toolbar({ buttonSection = 'primary', viewportId, location }: ToolbarProps) {
|
|
const {
|
|
toolbarButtons,
|
|
onInteraction,
|
|
isItemOpen,
|
|
isItemLocked,
|
|
openItem,
|
|
closeItem,
|
|
toggleLock,
|
|
} = useToolbar({
|
|
buttonSection,
|
|
});
|
|
|
|
if (!toolbarButtons.length) {
|
|
return null;
|
|
}
|
|
|
|
return (
|
|
<>
|
|
{toolbarButtons?.map(toolDef => {
|
|
if (!toolDef) {
|
|
return null;
|
|
}
|
|
|
|
const { id, Component, componentProps } = toolDef;
|
|
|
|
// Enhanced props with state and actions - respecting viewport specificity
|
|
const enhancedProps = {
|
|
...componentProps,
|
|
isOpen: isItemOpen(id, viewportId),
|
|
isLocked: isItemLocked(id, viewportId),
|
|
onOpen: () => openItem(id, viewportId),
|
|
onClose: () => closeItem(id, viewportId),
|
|
onToggleLock: () => toggleLock(id, viewportId),
|
|
viewportId,
|
|
};
|
|
|
|
const tool = (
|
|
<Component
|
|
key={id}
|
|
id={id}
|
|
location={location}
|
|
onInteraction={args => {
|
|
onInteraction({
|
|
...args,
|
|
itemId: id,
|
|
viewportId,
|
|
});
|
|
}}
|
|
{...enhancedProps}
|
|
/>
|
|
);
|
|
|
|
return (
|
|
<div
|
|
key={id}
|
|
// This wrapper div exists solely for React's key prop requirement during reconciliation.
|
|
// We use display:contents to make it transparent to the layout engine (children appear
|
|
// as direct children of the parent) while keeping it in the DOM for React's virtual DOM.
|
|
className="contents"
|
|
>
|
|
{tool}
|
|
</div>
|
|
);
|
|
})}
|
|
</>
|
|
);
|
|
}
|