diff --git a/docs/designers-developers/developers/themes/theme-json.md b/docs/designers-developers/developers/themes/theme-json.md index 223da07cb7e0c3..093f11e4851901 100644 --- a/docs/designers-developers/developers/themes/theme-json.md +++ b/docs/designers-developers/developers/themes/theme-json.md @@ -348,6 +348,7 @@ These are the current color properties supported by blocks: | Post Title | Yes | Yes | - | Yes | | Site Tagline | Yes | Yes | - | Yes | | Site Title | Yes | Yes | - | Yes | +| Social Links | Yes | - | - | Yes | | Template Part | Yes | Yes | Yes | Yes | [1] The heading block represents 6 distinct HTML elements: H1-H6. It comes with selectors to target each individual element (ex: core/heading/h1 for H1, etc). diff --git a/packages/block-library/src/social-links/block.json b/packages/block-library/src/social-links/block.json index 2bc44dd79f36e7..766ab4d52795b3 100644 --- a/packages/block-library/src/social-links/block.json +++ b/packages/block-library/src/social-links/block.json @@ -3,6 +3,24 @@ "name": "core/social-links", "category": "widgets", "attributes": { + "iconColor": { + "type": "string" + }, + "customIconColor": { + "type": "string" + }, + "iconColorValue": { + "type": "string" + }, + "iconBackgroundColor": { + "type": "string" + }, + "customIconBackgroundColor": { + "type": "string" + }, + "iconBackgroundColorValue": { + "type": "string" + }, "openInNewTab": { "type": "boolean", "default": false diff --git a/packages/block-library/src/social-links/edit.js b/packages/block-library/src/social-links/edit.js index b7d78bcf87821e..e0e62f1bd56c75 100644 --- a/packages/block-library/src/social-links/edit.js +++ b/packages/block-library/src/social-links/edit.js @@ -7,13 +7,16 @@ import classNames from 'classnames'; * WordPress dependencies */ -import { Fragment } from '@wordpress/element'; +import { Fragment, useEffect } from '@wordpress/element'; import { BlockControls, __experimentalUseInnerBlocksProps as useInnerBlocksProps, useBlockProps, InspectorControls, + ContrastChecker, + PanelColorSettings, + withColors, } from '@wordpress/block-editor'; import { DropdownMenu, @@ -38,10 +41,34 @@ const sizeOptions = [ export function SocialLinksEdit( props ) { const { - attributes: { size, openInNewTab }, + attributes, + iconBackgroundColor, + iconColor, setAttributes, + setIconBackgroundColor, + setIconColor, } = props; + const { + iconBackgroundColorValue, + iconColorValue, + openInNewTab, + size, + } = attributes; + + // Remove icon background color if logos only style selected. + const logosOnly = + attributes.className?.indexOf( 'is-style-logos-only' ) >= 0; + useEffect( () => { + if ( logosOnly ) { + setAttributes( { + iconBackgroundColor: undefined, + customIconBackgroundColor: undefined, + iconBackgroundColorValue: undefined, + } ); + } + }, [ logosOnly, setAttributes ] ); + const SocialPlaceholder = (
@@ -53,8 +80,21 @@ export function SocialLinksEdit( props ) {
); - const className = classNames( size ); - const blockProps = useBlockProps( { className } ); + // Fallback color values are used maintain selections in case switching + // themes and named colors in palette do not match. + const className = classNames( size, { + 'has-icon-color': iconColor.color || iconColorValue, + 'has-icon-background-color': + iconBackgroundColor.color || iconBackgroundColorValue, + } ); + + const style = { + '--wp--social-links--icon-color': iconColor.color || iconColorValue, + '--wp--social-links--icon-background-color': + iconBackgroundColor.color || iconBackgroundColorValue, + }; + + const blockProps = useBlockProps( { className, style } ); const innerBlocksProps = useInnerBlocksProps( blockProps, { allowedBlocks: ALLOWED_BLOCKS, orientation: 'horizontal', @@ -127,10 +167,55 @@ export function SocialLinksEdit( props ) { } /> + { + setIconColor( colorValue ); + // Set explicit color value used to add CSS variable in save.js + setAttributes( { iconColorValue: colorValue } ); + }, + label: __( 'Icon color' ), + }, + ! logosOnly && { + // Use custom attribute as fallback to prevent loss of named color selection when + // switching themes to a new theme that does not have a matching named color. + value: + iconBackgroundColor.color || + iconBackgroundColorValue, + onChange: ( colorValue ) => { + setIconBackgroundColor( colorValue ); + // Set explicit color value used to add CSS variable in save.js + setAttributes( { + iconBackgroundColorValue: colorValue, + } ); + }, + label: __( 'Icon background color' ), + }, + ] } + /> + { ! logosOnly && ( + + ) }