/** * External dependencies */ // eslint-disable-next-line no-restricted-imports import type { Ref } from 'react'; /** * GeChiUI dependencies */ import { check, reset, moreVertical, plus } from '@gechiui/icons'; import { __, _x, sprintf } from '@gechiui/i18n'; /** * Internal dependencies */ import DropdownMenu from '../../dropdown-menu'; import MenuGroup from '../../menu-group'; import MenuItem from '../../menu-item'; import { HStack } from '../../h-stack'; import { Heading } from '../../heading'; import { useToolsPanelHeader } from './hook'; import { contextConnect, GeChiUIComponentProps } from '../../ui/context'; import type { ToolsPanelControlsGroupProps, ToolsPanelHeaderProps, } from '../types'; const noop = () => {}; const DefaultControlsGroup = ( { items, onClose, toggleItem, }: ToolsPanelControlsGroupProps ) => { if ( ! items.length ) { return null; } return ( { items.map( ( [ label, hasValue ] ) => { const icon = hasValue ? reset : check; const itemLabel = hasValue ? sprintf( // translators: %s: The name of the control being reset e.g. "Padding". __( 'Reset %s' ), label ) : undefined; return ( { toggleItem( label ); onClose(); } } role="menuitemcheckbox" > { label } ); } ) } ); }; const OptionalControlsGroup = ( { items, onClose, toggleItem, }: ToolsPanelControlsGroupProps ) => { if ( ! items.length ) { return null; } return ( { items.map( ( [ label, isSelected ] ) => { const itemLabel = isSelected ? sprintf( // translators: %s: The name of the control being hidden and reset e.g. "Padding". __( 'Hide and reset %s' ), label ) : sprintf( // translators: %s: The name of the control to display e.g. "Padding". __( 'Show %s' ), label ); return ( { toggleItem( label ); onClose(); } } role="menuitemcheckbox" > { label } ); } ) } ); }; const ToolsPanelHeader = ( props: GeChiUIComponentProps< ToolsPanelHeaderProps, 'h2' >, forwardedRef: Ref< any > ) => { const { areAllOptionalControlsHidden, dropdownMenuClassName, hasMenuItems, headingClassName, label: labelText, menuItems, resetAll, toggleItem, ...headerProps } = useToolsPanelHeader( props ); if ( ! labelText ) { return null; } const defaultItems = Object.entries( menuItems?.default || {} ); const optionalItems = Object.entries( menuItems?.optional || {} ); const dropDownMenuIcon = areAllOptionalControlsHidden ? plus : moreVertical; const dropDownMenuLabelText = areAllOptionalControlsHidden ? _x( 'View and add options', 'Button label to reveal tool panel options' ) : _x( 'View options', 'Button label to reveal tool panel options' ); return ( { labelText } { hasMenuItems && ( { ( { onClose = noop } ) => ( <> { resetAll(); onClose(); } } > { __( 'Reset all' ) } ) } ) } ); }; const ConnectedToolsPanelHeader = contextConnect( ToolsPanelHeader, 'ToolsPanelHeader' ); export default ConnectedToolsPanelHeader;