Skip to content

Commit

Permalink
Add new block styles control
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronrobertshaw committed Feb 21, 2024
1 parent 41243b8 commit ff5c1db
Showing 1 changed file with 80 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/**
* WordPress dependencies
*/
import {
ColorIndicator,
Flex,
FlexItem,
privateApis as componentsPrivateApis,
__experimentalHStack as HStack,
__experimentalZStack as ZStack,
} from '@wordpress/components';
import { __ } from '@wordpress/i18n';

/**
* Internal dependencies
*/
import { unlock } from '../../lock-unlock';
import { getDefaultStyle } from './utils';

const { CustomSelect, CustomSelectItem } = unlock( componentsPrivateApis );

function BlockStyleItem( { blockStyle: { styles, label } } ) {
const indicators = [ styles?.color?.background, styles?.color?.text ];

return (
<HStack justify="flex-start">
<ZStack isLayered={ false } offset={ -8 }>
{ indicators.map( ( indicator, index ) => (
<Flex key={ index } expanded={ false }>
<ColorIndicator colorValue={ indicator } />
</Flex>
) ) }
</ZStack>
<FlexItem title={ label }>{ label }</FlexItem>
</HStack>
);
}

export default function BlockStylesControl( props ) {
const { value, blockStyles, onChange, onHover } = props;
const defaultStyle = getDefaultStyle( blockStyles );

const renderSelectedBlockStyle = ( currentStyle ) => {
const currentBlockStyle = blockStyles.find(
( style ) => style.name === currentStyle
);

if ( ! currentBlockStyle ) {
return null;
}

return <BlockStyleItem blockStyle={ currentBlockStyle } />;
};

return (
<CustomSelect
className="block-editor-block-styles__button"
defaultValue={ defaultStyle?.name }
label={ __( 'Select a block style' ) }
onChange={ onChange }
renderSelectedValue={ renderSelectedBlockStyle }
value={ value?.name }
hideLabelFromVision
popoverProps={ { className: 'block-editor-block-styles__popover' } }
>
{ blockStyles.map( ( blockStyle, index ) => (
<CustomSelectItem
key={ index }
value={ blockStyle.name }
onMouseEnter={ () => onHover( blockStyle ) }
onMouseLeave={ () => onHover( null ) }
onFocus={ () => onHover( blockStyle ) }
onBlur={ () => onHover( null ) }
>
<BlockStyleItem blockStyle={ blockStyle } />
</CustomSelectItem>
) ) }
</CustomSelect>
);
}

0 comments on commit ff5c1db

Please sign in to comment.