-
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.
Border Block Support: Add utilities to get border classes and styles (#…
…31264) * Add utilities to get border support classes and styles This follows the same pattern as the utilities added for the colors block support. Its intended use is in retrieving border related styles and classes to apply to inner block elements. * Export border support utilities for native
- Loading branch information
1 parent
716febb
commit e66e6d9
Showing
4 changed files
with
77 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import classnames from 'classnames'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { getInlineStyles } from './style'; | ||
import { | ||
getColorClassName, | ||
getColorObjectByAttributeValues, | ||
} from '../components/colors'; | ||
import useEditorFeature from '../components/use-editor-feature'; | ||
|
||
// This utility is intended to assist where the serialization of the border | ||
// block support is being skipped for a block but the border related CSS classes | ||
// & styles still need to be generated so they can be applied to inner elements. | ||
|
||
const EMPTY_ARRAY = []; | ||
|
||
/** | ||
* Provides the CSS class names and inline styles for a block's border support | ||
* attributes. | ||
* | ||
* @param {Object} attributes Block attributes. | ||
* @param {string} attributes.borderColor Selected named border color. | ||
* @param {Object} attributes.style Block's styles attribute. | ||
* | ||
* @return {Object} Border block support derived CSS classes & styles. | ||
*/ | ||
export function getBorderClassesAndStyles( { borderColor, style } ) { | ||
const borderStyles = style?.border || {}; | ||
const borderClass = getColorClassName( 'border-color', borderColor ); | ||
|
||
const className = classnames( { | ||
[ borderClass ]: !! borderClass, | ||
'has-border-color': borderColor || style?.border?.color, | ||
} ); | ||
|
||
return { | ||
className: className || undefined, | ||
style: getInlineStyles( { border: borderStyles } ), | ||
}; | ||
} | ||
|
||
/** | ||
* Derives the border related props for a block from its border block support | ||
* attributes. | ||
* | ||
* Inline styles are forced for named colors to ensure these selections are | ||
* reflected when themes do not load their color stylesheets in the editor. | ||
* | ||
* @param {Object} attributes Block attributes. | ||
* @return {Object} ClassName & style props from border block support. | ||
*/ | ||
export function useBorderProps( attributes ) { | ||
const colors = useEditorFeature( 'color.palette' ) || EMPTY_ARRAY; | ||
const borderProps = getBorderClassesAndStyles( attributes ); | ||
|
||
// Force inline style to apply border color when themes do not load their | ||
// color stylesheets in the editor. | ||
if ( attributes.borderColor ) { | ||
const borderColorObject = getColorObjectByAttributeValues( | ||
colors, | ||
attributes.borderColor | ||
); | ||
|
||
borderProps.style.borderColor = borderColorObject.color; | ||
} | ||
|
||
return borderProps; | ||
} |
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