Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Social Icons: Add icon & background color options #28084

Merged
merged 5 commits into from
Jan 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/designers-developers/developers/themes/theme-json.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👋 While this PR added support for icon color & background, it doesn't use the block support mechanism, hence these properties can't be targeted via theme.json. I've prepared a fix for this at #29294

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the fix!

| 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).
Expand Down
18 changes: 18 additions & 0 deletions packages/block-library/src/social-links/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
95 changes: 90 additions & 5 deletions packages/block-library/src/social-links/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 = (
<div className="wp-block-social-links__social-placeholder">
<div className="wp-social-link"></div>
Expand All @@ -53,8 +80,21 @@ export function SocialLinksEdit( props ) {
</div>
);

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',
Expand Down Expand Up @@ -127,10 +167,55 @@ export function SocialLinksEdit( props ) {
}
/>
</PanelBody>
<PanelColorSettings
title={ __( 'Color settings' ) }
colorSettings={ [
{
// 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: iconColor.color || iconColorValue,
onChange: ( colorValue ) => {
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 && (
<ContrastChecker
{ ...{
textColor: iconColorValue,
backgroundColor: iconBackgroundColorValue,
} }
isLargeText={ false }
/>
) }
</InspectorControls>
<ul { ...innerBlocksProps } />
</Fragment>
);
}

export default SocialLinksEdit;
const iconColorAttributes = {
iconColor: 'icon-color',
iconBackgroundColor: 'icon-background-color',
};

export default withColors( iconColorAttributes )( SocialLinksEdit );
14 changes: 11 additions & 3 deletions packages/block-library/src/social-links/save.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,21 @@ import { InnerBlocks, useBlockProps } from '@wordpress/block-editor';

export default function save( props ) {
const {
attributes: { size },
attributes: { iconBackgroundColorValue, iconColorValue, size },
} = props;

const className = classNames( size );
const className = classNames( size, {
'has-icon-color': iconColorValue,
'has-icon-background-color': iconBackgroundColorValue,
} );

const style = {
'--wp--social-links--icon-color': iconColorValue,
'--wp--social-links--icon-background-color': iconBackgroundColorValue,
};

return (
<ul { ...useBlockProps.save( { className } ) }>
<ul { ...useBlockProps.save( { className, style } ) }>
<InnerBlocks.Content />
</ul>
);
Expand Down
13 changes: 13 additions & 0 deletions packages/block-library/src/social-links/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,19 @@
&.alignright {
justify-content: flex-end;
}

// Ensure user color selections are applied to inner Social Link blocks.
// Double selectors to increase specificity to avoid themes overwriting user selection.
&.has-icon-color.has-icon-color {
> .wp-social-link {
color: var(--wp--social-links--icon-color);
}
}
&.has-icon-background-color.has-icon-background-color {
> .wp-social-link {
background-color: var(--wp--social-links--icon-background-color);
}
}
}

.wp-social-link {
Expand Down