ui
New useIconPresentation hook
This section details the introduction of the IconPresentationProvider and useIconPresentation hook, offering an optional way to manage icon size and container styling within the UI.
Key Changes:
- New Context and Hook: Introduction of
IconPresentationProvideranduseIconPresentationto provide a standardized way to control the size and potentially the container component/props for icons and related interactive elements (likeToolButton).
Migration Steps:
Using the IconPresentationProvider is entirely optional. If you do not wrap your components with this provider, components like ToolButton will continue to use their explicit size prop and default styling.
However, if you wish to centrally manage the presentation of icons within a specific part of your application's component tree, follow these steps:
-
Identify the component subtree: Determine which section of your UI you want to apply consistent icon styling to.
-
Wrap with
IconPresentationProvider: Wrap the root of that component subtree with theIconPresentationProvider. Pass the desiredsizeprop. You can also optionally provide a customIconContainercomponent andcontainerPropsif you want to change the wrapper around the icon itself (e.g., switching from aButtonto aToolButtonor applying specific styling).import { IconPresentationProvider, ToolButton } from '@ohif/ui-next';
function MyComponentTree() {
return (
// Icons and ToolButtons within this provider will inherit 'large' size
<IconPresentationProvider size="large">
{/* Any components inside that consume the context */}
<ToolButton id="myTool" icon="Circle" tooltip="Draw Circle" />
{/* ... other components ... */}
</IconPresentationProvider>
);
} -
Consume the context in components (if building custom components): If you are building a custom component that renders an icon and want it to respect the provider's settings, use the
useIconPresentationhook within that component. This hook provides the configured size, a calculated CSS class name (className), the specifiedIconContainercomponent, and itscontainerProps.import React from 'react';
import { useIconPresentation, Icons } from '@ohif/ui-next';
function MyCustomIconButton({ iconName, ...rest }) {
// This hook reads the nearest IconPresentationProvider context
const { className, IconContainer, containerProps } = useIconPresentation();
// Use the provided IconContainer and its props
return (
<IconContainer {...rest} {...containerProps}>
{/* Use the calculated className for the icon */}
<Icons.ByName name={iconName} className={className} />
</IconContainer>
);
}Note: Built-in components like
ToolButtonin@ohif/ui-nexthave been updated internally to consume this context automatically if a provider is available.