-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
41243b8
commit ff5c1db
Showing
1 changed file
with
80 additions
and
0 deletions.
There are no files selected for viewing
80 changes: 80 additions & 0 deletions
80
packages/block-editor/src/components/block-styles/block-styles-control.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
); | ||
} |