forked from WordPress/gutenberg
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Explore applying colorways to block instances including UI updates
- Loading branch information
1 parent
fa55feb
commit bb8003e
Showing
10 changed files
with
562 additions
and
12 deletions.
There are no files selected for viewing
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
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
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
197 changes: 197 additions & 0 deletions
197
packages/block-editor/src/components/global-styles/colorway-dropdown.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,197 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import classnames from 'classnames'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { | ||
BaseControl, | ||
Button, | ||
ColorIndicator, | ||
Dropdown, | ||
Flex, | ||
FlexItem, | ||
Icon, | ||
MenuGroup, | ||
MenuItem, | ||
__experimentalHStack as HStack, | ||
__experimentalToolsPanelItem as ToolsPanelItem, | ||
__experimentalVStack as VStack, | ||
__experimentalZStack as ZStack, | ||
} from '@wordpress/components'; | ||
import { __ } from '@wordpress/i18n'; | ||
import { check } from '@wordpress/icons'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { getValueFromVariable } from './utils'; | ||
|
||
const checkIcon = <Icon icon={ check } size={ 24 } />; | ||
const noop = () => undefined; | ||
|
||
// TODO: Work out what styling of options happens. The element colour sets | ||
// issue shows two options that appear to have background colors but they | ||
// don't match the swatches and the dark one has light text. | ||
function getColorwayPaletteStyles( colorway ) { | ||
if ( ! colorway?.styles?.color ) { | ||
return; | ||
} | ||
|
||
const styles = {}; | ||
const { background, gradient, text } = colorway.styles.color; | ||
|
||
if ( background ) { | ||
styles.backgroundColor = getValueFromVariable( | ||
colorway, | ||
'', | ||
background | ||
); | ||
} | ||
|
||
if ( gradient ) { | ||
styles.background = getValueFromVariable( colorway, '', gradient ); | ||
} | ||
|
||
if ( text ) { | ||
styles.color = getValueFromVariable( colorway, '', text ); | ||
} | ||
|
||
return styles; | ||
} | ||
|
||
// TODO: Confirm which colors need indicators. Not clear from issue. | ||
// | ||
function ColorwayIndicator( { colorway } ) { | ||
const { background, gradient, text } = colorway?.styles?.color || {}; | ||
const link = colorway?.styles?.elements?.link?.color?.text; | ||
const heading = colorway?.styles?.elements?.heading?.color?.text; | ||
|
||
const indicators = [ | ||
getValueFromVariable( colorway, '', background || gradient ), | ||
getValueFromVariable( colorway, '', text ), | ||
getValueFromVariable( colorway, '', link ), | ||
getValueFromVariable( colorway, '', heading ), | ||
]; | ||
|
||
const label = colorway?.title || __( 'Default' ); | ||
|
||
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 className="" title={ label }> | ||
{ label } | ||
</FlexItem> | ||
</HStack> | ||
); | ||
} | ||
|
||
function ColorwayDropdownToggle( { onToggle, isOpen, value } ) { | ||
const toggleProps = { | ||
onClick: onToggle, | ||
className: classnames( | ||
'block-editor-tools-panel-colorway__dropdown-toggle', | ||
{ | ||
'is-open': isOpen, | ||
} | ||
), | ||
'aria-expanded': isOpen, | ||
'aria-label': __( 'Colorways options' ), | ||
style: getColorwayPaletteStyles( value ), | ||
}; | ||
|
||
return ( | ||
<Button { ...toggleProps }> | ||
<ColorwayIndicator colorway={ value } /> | ||
</Button> | ||
); | ||
} | ||
|
||
function ColorwayDropdownContent( { colorways, activeColorway, onSelect } ) { | ||
return ( | ||
<MenuGroup | ||
className="block-editor-tools-panel-colorway__dropdown-group" | ||
label={ __( 'Colorways' ) } | ||
> | ||
{ colorways.map( ( colorway, index ) => { | ||
const paletteStyles = getColorwayPaletteStyles( colorway ); | ||
const isSelected = activeColorway?.title === colorway.title; | ||
|
||
return ( | ||
<MenuItem | ||
key={ index } | ||
isSelected={ isSelected } | ||
onClick={ () => onSelect( colorway ) } | ||
role="menuitemradio" | ||
style={ paletteStyles } | ||
suffix={ isSelected ? checkIcon : undefined } | ||
> | ||
<ColorwayIndicator colorway={ colorway } /> | ||
</MenuItem> | ||
); | ||
} ) } | ||
</MenuGroup> | ||
); | ||
} | ||
|
||
export default function ColorwayDropdown( { | ||
className, | ||
colorways = [], | ||
value, | ||
label, | ||
onDeselect = noop, | ||
onSelect = noop, | ||
panelId, | ||
...props | ||
} ) { | ||
if ( ! colorways.length ) { | ||
return null; | ||
} | ||
|
||
const classes = classnames( | ||
className, | ||
'block-editor-tools-panel-colorway__dropdown' | ||
); | ||
|
||
return ( | ||
<ToolsPanelItem | ||
className="block-editor-tools-panel-colorway" | ||
hasValue={ () => !! value } | ||
label={ label } | ||
onDeselect={ onDeselect } | ||
isShownByDefault={ true } | ||
panelId={ panelId } | ||
> | ||
<VStack spacing={ 0 }> | ||
<BaseControl.VisualLabel>{ label }</BaseControl.VisualLabel> | ||
<Dropdown | ||
{ ...props } | ||
label={ label } | ||
className={ classes } | ||
renderToggle={ ( toggleProps ) => ( | ||
<ColorwayDropdownToggle | ||
{ ...toggleProps } | ||
value={ value } | ||
/> | ||
) } | ||
renderContent={ ( contentProps ) => ( | ||
<ColorwayDropdownContent | ||
{ ...contentProps } | ||
activeColorway={ value } | ||
colorways={ colorways } | ||
onSelect={ onSelect } | ||
/> | ||
) } | ||
/> | ||
</VStack> | ||
</ToolsPanelItem> | ||
); | ||
} |
Oops, something went wrong.