-
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.
Extract a BorderPanel component as a reusable component between Globa…
…l Styles and Block Inspector (#48636)
- Loading branch information
1 parent
03ec631
commit a08861e
Showing
12 changed files
with
526 additions
and
536 deletions.
There are no files selected for viewing
285 changes: 285 additions & 0 deletions
285
packages/block-editor/src/components/global-styles/border-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,285 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { | ||
__experimentalBorderBoxControl as BorderBoxControl, | ||
__experimentalHasSplitBorders as hasSplitBorders, | ||
__experimentalIsDefinedBorder as isDefinedBorder, | ||
__experimentalToolsPanel as ToolsPanel, | ||
__experimentalToolsPanelItem as ToolsPanelItem, | ||
} from '@wordpress/components'; | ||
import { useCallback, useMemo } from '@wordpress/element'; | ||
import { __ } from '@wordpress/i18n'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import BorderRadiusControl from '../border-radius-control'; | ||
import { useColorsPerOrigin } from './hooks'; | ||
import { getValueFromVariable } from './utils'; | ||
|
||
export function useHasBorderPanel( settings ) { | ||
const controls = [ | ||
useHasBorderColorControl( settings ), | ||
useHasBorderRadiusControl( settings ), | ||
useHasBorderStyleControl( settings ), | ||
useHasBorderWidthControl( settings ), | ||
]; | ||
|
||
return controls.some( Boolean ); | ||
} | ||
|
||
function useHasBorderColorControl( settings ) { | ||
return settings?.border?.color; | ||
} | ||
|
||
function useHasBorderRadiusControl( settings ) { | ||
return settings?.border?.radius; | ||
} | ||
|
||
function useHasBorderStyleControl( settings ) { | ||
return settings?.border?.style; | ||
} | ||
|
||
function useHasBorderWidthControl( settings ) { | ||
return settings?.border?.width; | ||
} | ||
|
||
function applyFallbackStyle( border ) { | ||
if ( ! border ) { | ||
return border; | ||
} | ||
|
||
if ( ! border.style && ( border.color || border.width ) ) { | ||
return { ...border, style: 'solid' }; | ||
} | ||
|
||
return border; | ||
} | ||
|
||
function applyAllFallbackStyles( border ) { | ||
if ( ! border ) { | ||
return border; | ||
} | ||
|
||
if ( hasSplitBorders( border ) ) { | ||
return { | ||
top: applyFallbackStyle( border.top ), | ||
right: applyFallbackStyle( border.right ), | ||
bottom: applyFallbackStyle( border.bottom ), | ||
left: applyFallbackStyle( border.left ), | ||
}; | ||
} | ||
|
||
return applyFallbackStyle( border ); | ||
} | ||
|
||
function BorderToolsPanel( { | ||
resetAllFilter, | ||
onChange, | ||
value, | ||
panelId, | ||
children, | ||
} ) { | ||
const resetAll = () => { | ||
const updatedValue = resetAllFilter( value ); | ||
onChange( updatedValue ); | ||
}; | ||
|
||
return ( | ||
<ToolsPanel | ||
label={ __( 'Border' ) } | ||
resetAll={ resetAll } | ||
panelId={ panelId } | ||
> | ||
{ children } | ||
</ToolsPanel> | ||
); | ||
} | ||
|
||
const DEFAULT_CONTROLS = { | ||
radius: true, | ||
color: true, | ||
width: true, | ||
}; | ||
|
||
export default function BorderPanel( { | ||
as: Wrapper = BorderToolsPanel, | ||
value, | ||
onChange, | ||
inheritedValue = value, | ||
settings, | ||
panelId, | ||
defaultControls = DEFAULT_CONTROLS, | ||
} ) { | ||
const colors = useColorsPerOrigin( settings ); | ||
const decodeValue = ( rawValue ) => | ||
getValueFromVariable( { settings }, '', rawValue ); | ||
const encodeColorValue = ( colorValue ) => { | ||
const allColors = colors.flatMap( | ||
( { colors: originColors } ) => originColors | ||
); | ||
const colorObject = allColors.find( | ||
( { color } ) => color === colorValue | ||
); | ||
return colorObject | ||
? 'var:preset|color|' + colorObject.slug | ||
: colorValue; | ||
}; | ||
const decodeColorValue = useCallback( | ||
( colorValue ) => { | ||
const allColors = colors.flatMap( | ||
( { colors: originColors } ) => originColors | ||
); | ||
const colorObject = allColors.find( | ||
( { slug } ) => colorValue === 'var:preset|color|' + slug | ||
); | ||
return colorObject ? colorObject.color : colorValue; | ||
}, | ||
[ colors ] | ||
); | ||
const border = useMemo( () => { | ||
if ( hasSplitBorders( inheritedValue?.border ) ) { | ||
const borderValue = { ...inheritedValue?.border }; | ||
[ 'top', 'right', 'bottom', 'left' ].forEach( ( side ) => { | ||
borderValue[ side ] = { | ||
...borderValue[ side ], | ||
color: decodeColorValue( borderValue[ side ]?.color ), | ||
}; | ||
} ); | ||
return borderValue; | ||
} | ||
return { | ||
...inheritedValue?.border, | ||
color: inheritedValue?.border?.color | ||
? decodeColorValue( inheritedValue?.border?.color ) | ||
: undefined, | ||
}; | ||
}, [ inheritedValue?.border, decodeColorValue ] ); | ||
const setBorder = ( newBorder ) => | ||
onChange( { ...value, border: newBorder } ); | ||
const showBorderColor = useHasBorderColorControl( settings ); | ||
const showBorderStyle = useHasBorderStyleControl( settings ); | ||
const showBorderWidth = useHasBorderWidthControl( settings ); | ||
|
||
// Border radius. | ||
const showBorderRadius = useHasBorderRadiusControl( settings ); | ||
const borderRadiusValues = decodeValue( border?.radius ); | ||
const setBorderRadius = ( newBorderRadius ) => | ||
setBorder( { ...border, radius: newBorderRadius } ); | ||
const hasBorderRadius = () => { | ||
const borderValues = value?.border?.radius; | ||
if ( typeof borderValues === 'object' ) { | ||
return Object.entries( borderValues ).some( Boolean ); | ||
} | ||
return !! borderValues; | ||
}; | ||
|
||
const resetBorder = () => { | ||
if ( hasBorderRadius() ) { | ||
return setBorder( { radius: value?.border?.radius } ); | ||
} | ||
|
||
setBorder( undefined ); | ||
}; | ||
|
||
const onBorderChange = ( newBorder ) => { | ||
// Ensure we have a visible border style when a border width or | ||
// color is being selected. | ||
const newBorderWithStyle = applyAllFallbackStyles( newBorder ); | ||
|
||
// As we can't conditionally generate styles based on if other | ||
// style properties have been set we need to force split border | ||
// definitions for user set border styles. Border radius is derived | ||
// from the same property i.e. `border.radius` if it is a string | ||
// that is used. The longhand border radii styles are only generated | ||
// if that property is an object. | ||
// | ||
// For borders (color, style, and width) those are all properties on | ||
// the `border` style property. This means if the theme.json defined | ||
// split borders and the user condenses them into a flat border or | ||
// vice-versa we'd get both sets of styles which would conflict. | ||
const updatedBorder = ! hasSplitBorders( newBorderWithStyle ) | ||
? { | ||
top: newBorderWithStyle, | ||
right: newBorderWithStyle, | ||
bottom: newBorderWithStyle, | ||
left: newBorderWithStyle, | ||
} | ||
: { | ||
color: null, | ||
style: null, | ||
width: null, | ||
...newBorderWithStyle, | ||
}; | ||
|
||
[ 'top', 'right', 'bottom', 'left' ].forEach( ( side ) => { | ||
updatedBorder[ side ] = { | ||
...updatedBorder[ side ], | ||
color: encodeColorValue( updatedBorder[ side ]?.color ), | ||
}; | ||
} ); | ||
|
||
// As radius is maintained separately to color, style, and width | ||
// maintain its value. Undefined values here will be cleaned when | ||
// global styles are saved. | ||
setBorder( { radius: border?.radius, ...updatedBorder } ); | ||
}; | ||
|
||
const resetAllFilter = useCallback( ( previousValue ) => { | ||
return { | ||
...previousValue, | ||
border: undefined, | ||
}; | ||
}, [] ); | ||
|
||
const showBorderByDefault = | ||
defaultControls?.color || defaultControls?.width; | ||
|
||
return ( | ||
<Wrapper | ||
resetAllFilter={ resetAllFilter } | ||
value={ value } | ||
onChange={ onChange } | ||
panelId={ panelId } | ||
> | ||
{ ( showBorderWidth || showBorderColor ) && ( | ||
<ToolsPanelItem | ||
hasValue={ () => isDefinedBorder( value?.border ) } | ||
label={ __( 'Border' ) } | ||
onDeselect={ () => resetBorder() } | ||
isShownByDefault={ showBorderByDefault } | ||
panelId={ panelId } | ||
> | ||
<BorderBoxControl | ||
colors={ colors } | ||
enableAlpha={ true } | ||
enableStyle={ showBorderStyle } | ||
onChange={ onBorderChange } | ||
popoverOffset={ 40 } | ||
popoverPlacement="left-start" | ||
value={ border } | ||
__experimentalIsRenderedInSidebar={ true } | ||
size={ '__unstable-large' } | ||
/> | ||
</ToolsPanelItem> | ||
) } | ||
{ showBorderRadius && ( | ||
<ToolsPanelItem | ||
hasValue={ hasBorderRadius } | ||
label={ __( 'Radius' ) } | ||
onDeselect={ () => setBorderRadius( undefined ) } | ||
isShownByDefault={ defaultControls.radius } | ||
panelId={ panelId } | ||
> | ||
<BorderRadiusControl | ||
values={ borderRadiusValues } | ||
onChange={ ( newValue ) => { | ||
setBorderRadius( newValue || undefined ); | ||
} } | ||
/> | ||
</ToolsPanelItem> | ||
) } | ||
</Wrapper> | ||
); | ||
} |
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
Oops, something went wrong.
a08861e
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 a08861e.
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/4311284947
📝 Reported issues:
specs/site-editor/global-styles-sidebar.test.js
specs/editor/various/block-hierarchy-navigation.test.js