Skip to content

Commit

Permalink
Social Icons: Avoid loss of previously selected background color when…
Browse files Browse the repository at this point in the history
… switching back from "Logos Only" style (#39276)

* Background color issue fixed

* Add missing ref, micro-refactor and lint

* Add entry to changelog

Co-authored-by: Mitchell Austin <[email protected]>
  • Loading branch information
HILAYTRIVEDI and stokesman authored Mar 17, 2022
1 parent 3830802 commit 79cacc5
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
4 changes: 4 additions & 0 deletions packages/block-library/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

### Enhancement

- Social Icons: Avoid loss of previously selected background color when switching back from "Logos Only" style ([#39276](https://github.com/WordPress/gutenberg/pull/39276)).

## 7.1.0 (2022-03-11)

## 7.0.0 (2022-02-10)
Expand Down
20 changes: 15 additions & 5 deletions packages/block-library/src/social-links/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import classNames from 'classnames';
* WordPress dependencies
*/
import { getBlockSupport } from '@wordpress/blocks';
import { Fragment, useEffect } from '@wordpress/element';
import { Fragment, useEffect, useRef } from '@wordpress/element';
import {
BlockControls,
useInnerBlocksProps,
Expand Down Expand Up @@ -58,6 +58,7 @@ export function SocialLinksEdit( props ) {

const {
iconBackgroundColorValue,
customIconBackgroundColor,
iconColorValue,
openInNewTab,
showLabels,
Expand All @@ -66,18 +67,27 @@ export function SocialLinksEdit( props ) {
} = attributes;
const usedLayout = layout || getDefaultBlockLayout( name );

// Remove icon background color if logos only style selected.
const logosOnly =
attributes.className?.indexOf( 'is-style-logos-only' ) >= 0;
const logosOnly = attributes.className?.includes( 'is-style-logos-only' );

// Remove icon background color when logos only style is selected or
// restore it when any other style is selected.
const backgroundBackup = useRef( {} );
useEffect( () => {
if ( logosOnly ) {
backgroundBackup.current = {
iconBackgroundColor,
iconBackgroundColorValue,
customIconBackgroundColor,
};
setAttributes( {
iconBackgroundColor: undefined,
customIconBackgroundColor: undefined,
iconBackgroundColorValue: undefined,
} );
} else {
setAttributes( { ...backgroundBackup.current } );
}
}, [ logosOnly, setAttributes ] );
}, [ logosOnly ] );

const SocialPlaceholder = (
<li className="wp-block-social-links__social-placeholder">
Expand Down

0 comments on commit 79cacc5

Please sign in to comment.