-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Shadow: Add shadow presets and UI tools in global styles (#46502)
* add shadow ui controls in global styles hide/show default shadows based on the setting from theme.json add block style variations prefix rename border menu to border & shadow * update shadow presets * Make the shadow panel use small buttons * fix issues with global settings * clip shadow outside 6px boundary * update default shadow presets * update variation path * fix block variation styles for shadow --------- Co-authored-by: Ben Dwyer <ben@escruffian.com>
- Loading branch information
1 parent
18e6dc3
commit d9da125
Showing
9 changed files
with
253 additions
and
6 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
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
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
174 changes: 174 additions & 0 deletions
174
packages/edit-site/src/components/global-styles/shadow-panel.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,174 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import classnames from 'classnames'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { | ||
__experimentalToolsPanel as ToolsPanel, | ||
__experimentalToolsPanelItem as ToolsPanelItem, | ||
__experimentalItemGroup as ItemGroup, | ||
__experimentalHStack as HStack, | ||
__experimentalVStack as VStack, | ||
__experimentalGrid as Grid, | ||
__experimentalHeading as Heading, | ||
FlexItem, | ||
Dropdown, | ||
__experimentalDropdownContentWrapper as DropdownContentWrapper, | ||
Button, | ||
} from '@wordpress/components'; | ||
import { __ } from '@wordpress/i18n'; | ||
import { shadow as shadowIcon, Icon, check } from '@wordpress/icons'; | ||
import { useCallback } from '@wordpress/element'; | ||
import { experiments as blockEditorExperiments } from '@wordpress/block-editor'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { getSupportedGlobalStylesPanels } from './hooks'; | ||
import { IconWithCurrentColor } from './icon-with-current-color'; | ||
import { unlock } from '../../experiments'; | ||
|
||
const { useGlobalSetting, useGlobalStyle } = unlock( blockEditorExperiments ); | ||
|
||
export function useHasShadowControl( name ) { | ||
const supports = getSupportedGlobalStylesPanels( name ); | ||
return supports.includes( 'shadow' ); | ||
} | ||
|
||
export default function ShadowPanel( { name, variation = '' } ) { | ||
const prefix = variation ? `variations.${ variation }.` : ''; | ||
const [ shadow, setShadow ] = useGlobalStyle( `${ prefix }shadow`, name ); | ||
const [ userShadow ] = useGlobalStyle( `${ prefix }shadow`, name, 'user' ); | ||
const hasShadow = () => !! userShadow; | ||
|
||
const resetShadow = () => setShadow( undefined ); | ||
const resetAll = useCallback( | ||
() => resetShadow( undefined ), | ||
[ resetShadow ] | ||
); | ||
|
||
return ( | ||
<ToolsPanel label={ __( 'Shadow' ) } resetAll={ resetAll }> | ||
<ToolsPanelItem | ||
label={ __( 'Shadow' ) } | ||
hasValue={ hasShadow } | ||
onDeselect={ resetShadow } | ||
isShownByDefault | ||
> | ||
<ItemGroup isBordered isSeparated> | ||
<ShadowPopover | ||
shadow={ shadow } | ||
onShadowChange={ setShadow } | ||
/> | ||
</ItemGroup> | ||
</ToolsPanelItem> | ||
</ToolsPanel> | ||
); | ||
} | ||
|
||
const ShadowPopover = ( { shadow, onShadowChange } ) => { | ||
const popoverProps = { | ||
placement: 'left-start', | ||
offset: 36, | ||
shift: true, | ||
}; | ||
|
||
return ( | ||
<Dropdown | ||
popoverProps={ popoverProps } | ||
className="edit-site-global-styles__shadow-dropdown" | ||
renderToggle={ renderShadowToggle() } | ||
renderContent={ () => ( | ||
<DropdownContentWrapper paddingSize="medium"> | ||
<ShadowPopoverContainer | ||
shadow={ shadow } | ||
onShadowChange={ onShadowChange } | ||
/> | ||
</DropdownContentWrapper> | ||
) } | ||
/> | ||
); | ||
}; | ||
|
||
function renderShadowToggle() { | ||
return ( { onToggle, isOpen } ) => { | ||
const toggleProps = { | ||
onClick: onToggle, | ||
className: classnames( { 'is-open': isOpen } ), | ||
'aria-expanded': isOpen, | ||
}; | ||
|
||
return ( | ||
<Button { ...toggleProps }> | ||
<HStack justify="flex-start"> | ||
<IconWithCurrentColor icon={ shadowIcon } size={ 24 } /> | ||
<FlexItem className="edit-site-global-styles__shadow-label"> | ||
{ __( 'Shadow' ) } | ||
</FlexItem> | ||
</HStack> | ||
</Button> | ||
); | ||
}; | ||
} | ||
|
||
function ShadowPopoverContainer( { shadow, onShadowChange } ) { | ||
const [ defaultShadows ] = useGlobalSetting( 'shadow.presets.default' ); | ||
const [ themeShadows ] = useGlobalSetting( 'shadow.presets.theme' ); | ||
const [ defaultPresetsEnabled ] = useGlobalSetting( | ||
'shadow.defaultPresets' | ||
); | ||
|
||
const shadows = [ | ||
...( defaultPresetsEnabled ? defaultShadows : [] ), | ||
...( themeShadows || [] ), | ||
]; | ||
|
||
return ( | ||
<div className="edit-site-global-styles__shadow-panel"> | ||
<VStack spacing={ 4 }> | ||
<Heading level={ 5 }>{ __( 'Shadows' ) }</Heading> | ||
<ShadowPresets | ||
presets={ shadows } | ||
activeShadow={ shadow } | ||
onSelect={ onShadowChange } | ||
/> | ||
</VStack> | ||
</div> | ||
); | ||
} | ||
|
||
function ShadowPresets( { presets, activeShadow, onSelect } ) { | ||
return ! presets ? null : ( | ||
<Grid columns={ 6 } gap={ 0 } align="center" justify="center"> | ||
{ presets.map( ( { name, shadow }, i ) => ( | ||
<ShadowIndicator | ||
key={ i } | ||
label={ name } | ||
isActive={ shadow === activeShadow } | ||
onSelect={ () => | ||
onSelect( shadow === activeShadow ? undefined : shadow ) | ||
} | ||
shadow={ shadow } | ||
/> | ||
) ) } | ||
</Grid> | ||
); | ||
} | ||
|
||
function ShadowIndicator( { label, isActive, onSelect, shadow } ) { | ||
return ( | ||
<div className="edit-site-global-styles__shadow-indicator-wrapper"> | ||
<Button | ||
className="edit-site-global-styles__shadow-indicator" | ||
onClick={ onSelect } | ||
aria-label={ label } | ||
style={ { boxShadow: shadow } } | ||
> | ||
{ isActive && <Icon icon={ check } /> } | ||
</Button> | ||
</div> | ||
); | ||
} |
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
d9da125
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Flaky tests detected in d9da125.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.
🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/4061767979
📝 Reported issues:
specs/editor/various/block-deletion.test.js
specs/editor/various/publish-button.test.js