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 links: add background color block supports #43293

Merged
merged 4 commits into from
Aug 24, 2022
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
2 changes: 1 addition & 1 deletion docs/reference-guides/core-blocks.md
Original file line number Diff line number Diff line change
Expand Up @@ -761,7 +761,7 @@ Display icons linking to your social media profiles or sites. ([Source](https://

- **Name:** core/social-links
- **Category:** widgets
- **Supports:** align (center, left, right), anchor, spacing (blockGap, margin, units)
- **Supports:** align (center, left, right), anchor, color (background, gradients, ~~enableContrastChecker~~, ~~text~~), spacing (blockGap, margin, units)
- **Attributes:** customIconBackgroundColor, customIconColor, iconBackgroundColor, iconBackgroundColorValue, iconColor, iconColorValue, openInNewTab, showLabels, size

## Spacer
Expand Down
9 changes: 9 additions & 0 deletions packages/block-library/src/social-links/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,15 @@
"type": "flex"
}
},
"color": {
"enableContrastChecker": false,
"background": true,
"gradients": true,
"text": false,
"__experimentalDefaultControls": {
"background": false
Copy link
Member Author

Choose a reason for hiding this comment

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

Not entirely sure why I had to explicitly define text and __experimentalDefaultControls.background as false, but a text control appeared and the background color control is default without them.

}
},
"spacing": {
"blockGap": [ "horizontal", "vertical" ],
"margin": [ "top", "bottom" ],
Expand Down
67 changes: 47 additions & 20 deletions packages/block-library/src/social-links/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@ import classNames from 'classnames';
* WordPress dependencies
*/
import { getBlockSupport } from '@wordpress/blocks';
import { Fragment, useEffect, useRef } from '@wordpress/element';
import { useEffect, useRef } from '@wordpress/element';
import {
BlockControls,
useInnerBlocksProps,
useBlockProps,
InspectorControls,
ContrastChecker,
PanelColorSettings,
withColors,
__experimentalColorGradientSettingsDropdown as ColorGradientSettingsDropdown,
__experimentalUseMultipleOriginColorsAndGradients as useMultipleOriginColorsAndGradients,
} from '@wordpress/block-editor';
import {
MenuGroup,
Expand Down Expand Up @@ -46,6 +47,7 @@ const getDefaultBlockLayout = ( blockTypeOrName ) => {

export function SocialLinksEdit( props ) {
const {
clientId,
name,
attributes,
iconBackgroundColor,
Expand Down Expand Up @@ -137,6 +139,10 @@ export function SocialLinksEdit( props ) {
setAttributes( { iconColorValue: colorValue } );
},
label: __( 'Icon color' ),
resetAllFilter: () => {
setIconColor( undefined );
setAttributes( { iconColorValue: undefined } );
},
},
];

Expand All @@ -152,11 +158,17 @@ export function SocialLinksEdit( props ) {
} );
},
label: __( 'Icon background' ),
resetAllFilter: () => {
setIconBackgroundColor( undefined );
setAttributes( { iconBackgroundColorValue: undefined } );
},
} );
}

const colorGradientSettings = useMultipleOriginColorsAndGradients();

return (
<Fragment>
<>
<BlockControls group="other">
<ToolbarDropdownMenu
label={ __( 'Size' ) }
Expand Down Expand Up @@ -211,26 +223,41 @@ export function SocialLinksEdit( props ) {
}
/>
</PanelBody>
<PanelColorSettings
__experimentalHasMultipleOrigins
__experimentalIsRenderedInSidebar
title={ __( 'Color' ) }
colorSettings={ colorSettings }
enableAlpha
>
{ ! logosOnly && (
<ContrastChecker
{ ...{
textColor: iconColorValue,
backgroundColor: iconBackgroundColorValue,
} }
isLargeText={ false }
</InspectorControls>
<InspectorControls __experimentalGroup="color">
{ colorSettings.map(
( { onChange, label, value, resetAllFilter } ) => (
<ColorGradientSettingsDropdown
key={ `social-links-color-${ label }` }
__experimentalHasMultipleOrigins
__experimentalIsRenderedInSidebar
settings={ [
{
colorValue: value,
label,
onColorChange: onChange,
isShownByDefault: true,
resetAllFilter,
enableAlpha: true,
},
] }
panelId={ clientId }
{ ...colorGradientSettings }
/>
) }
</PanelColorSettings>
)
) }
{ ! logosOnly && (
<ContrastChecker
{ ...{
textColor: iconColorValue,
backgroundColor: iconBackgroundColorValue,
} }
isLargeText={ false }
/>
) }
</InspectorControls>
<ul { ...innerBlocksProps } />
</Fragment>
</>
);
}

Expand Down