Skip to content

Commit

Permalink
Improve Social Icons setup and appending (WordPress#64877)
Browse files Browse the repository at this point in the history
Co-authored-by: getdave <get_dave@git.wordpress.org>
Co-authored-by: jeryj <jeryj@git.wordpress.org>
Co-authored-by: Mamaduka <mamaduka@git.wordpress.org>
  • Loading branch information
4 people authored and bph committed Aug 31, 2024
1 parent 180af48 commit ddce303
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import clsx from 'clsx';
* WordPress dependencies
*/
import { Button, Tooltip, VisuallyHidden } from '@wordpress/components';
import { forwardRef } from '@wordpress/element';
import { forwardRef, useRef } from '@wordpress/element';
import { _x, sprintf } from '@wordpress/i18n';
import { Icon, plus } from '@wordpress/icons';
import deprecated from '@wordpress/deprecated';
Expand All @@ -16,17 +16,26 @@ import deprecated from '@wordpress/deprecated';
* Internal dependencies
*/
import Inserter from '../inserter';
import { useMergeRefs } from '@wordpress/compose';

function ButtonBlockAppender(
{ rootClientId, className, onFocus, tabIndex, onSelect },
ref
) {
const inserterButtonRef = useRef();

const mergedInserterButtonRef = useMergeRefs( [ inserterButtonRef, ref ] );
return (
<Inserter
position="bottom center"
rootClientId={ rootClientId }
__experimentalIsQuick
onSelectOrClose={ onSelect }
onSelectOrClose={ ( ...args ) => {
if ( onSelect && typeof onSelect === 'function' ) {
onSelect( ...args );
}
inserterButtonRef.current?.focus();
} }
renderToggle={ ( {
onToggle,
disabled,
Expand All @@ -53,7 +62,7 @@ function ButtonBlockAppender(
<Button
// TODO: Switch to `true` (40px size) if possible
__next40pxDefaultSize={ false }
ref={ ref }
ref={ mergedInserterButtonRef }
onFocus={ onFocus }
tabIndex={ tabIndex }
className={ clsx(
Expand Down
21 changes: 14 additions & 7 deletions packages/block-library/src/social-links/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ import {
InspectorControls,
ContrastChecker,
withColors,
InnerBlocks,
__experimentalColorGradientSettingsDropdown as ColorGradientSettingsDropdown,
__experimentalUseMultipleOriginColorsAndGradients as useMultipleOriginColorsAndGradients,
store as blockEditorStore,
} from '@wordpress/block-editor';
import {
MenuGroup,
Expand All @@ -26,6 +28,7 @@ import {
} from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import { check } from '@wordpress/icons';
import { useSelect } from '@wordpress/data';

const sizeOptions = [
{ name: __( 'Small' ), value: 'has-small-icon-size' },
Expand Down Expand Up @@ -55,6 +58,14 @@ export function SocialLinksEdit( props ) {
size,
} = attributes;

const hasSelectedChild = useSelect(
( select ) =>
select( blockEditorStore ).hasSelectedInnerBlock( clientId ),
[ clientId ]
);

const hasAnySelected = isSelected || hasSelectedChild;

const logosOnly = attributes.className?.includes( 'is-style-logos-only' );

// Remove icon background color when logos only style is selected or
Expand All @@ -75,6 +86,7 @@ export function SocialLinksEdit( props ) {
} else {
setAttributes( { ...backgroundBackupRef.current } );
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [ logosOnly ] );

const SocialPlaceholder = (
Expand All @@ -87,12 +99,6 @@ export function SocialLinksEdit( props ) {
</li>
);

const SelectedSocialPlaceholder = (
<li className="wp-block-social-links__social-prompt">
{ __( 'Click plus to add' ) }
</li>
);

// Fallback color values are used maintain selections in case switching
// themes and named colors in palette do not match.
const className = clsx( size, {
Expand All @@ -104,10 +110,11 @@ export function SocialLinksEdit( props ) {

const blockProps = useBlockProps( { className } );
const innerBlocksProps = useInnerBlocksProps( blockProps, {
placeholder: isSelected ? SelectedSocialPlaceholder : SocialPlaceholder,
placeholder: ! isSelected && SocialPlaceholder,
templateLock: false,
orientation: attributes.layout?.orientation ?? 'horizontal',
__experimentalAppenderTagName: 'li',
renderAppender: hasAnySelected && InnerBlocks.ButtonBlockAppender,
} );

const POPOVER_PROPS = {
Expand Down
29 changes: 24 additions & 5 deletions packages/block-library/src/social-links/editor.scss
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
// Editor specific styles for Social Links.
.wp-block-social-links {
div.block-editor-url-input {
display: inline-block;
margin-left: $grid-unit-10;
}
.wp-block-social-links div.block-editor-url-input {
display: inline-block;
margin-left: $grid-unit-10;
}


// Prevent toolbar from jumping when selecting / hovering a link.
.wp-social-link:hover {
transform: none;
Expand Down Expand Up @@ -98,3 +97,23 @@
.wp-social-link.wp-social-link__is-incomplete:focus {
opacity: 1;
}

.wp-block-social-links .block-list-appender {
position: static; // display inline.

.block-editor-button-block-appender.components-button.components-button {
padding: $grid-unit-10 - 2px;
}
}

.wp-block-social-links {
&.has-small-icon-size .block-editor-button-block-appender.components-button.components-button {
padding: 0;
}
&.has-large-icon-size .block-editor-button-block-appender.components-button.components-button {
padding: $grid-unit-20 - 2px;
}
&.has-huge-icon-size .block-editor-button-block-appender.components-button.components-button {
padding: $grid-unit-30 - 1px;
}
}

0 comments on commit ddce303

Please sign in to comment.