Skip to content

Commit

Permalink
Move social icons blockProps from li to button
Browse files Browse the repository at this point in the history
By moving the blockProps for the social icon from the <li> to the <button>, we get the appropriate behavior of being able to select and create social icons with a clean keyboard behavior.
  • Loading branch information
jeryj authored and getdave committed Sep 24, 2024
1 parent 4a91d32 commit 97eb961
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 19 deletions.
53 changes: 34 additions & 19 deletions packages/block-library/src/social-link/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import clsx from 'clsx';
/**
* WordPress dependencies
*/
import { DELETE, BACKSPACE } from '@wordpress/keycodes';
import { DELETE, BACKSPACE, ENTER } from '@wordpress/keycodes';
import { useDispatch } from '@wordpress/data';

import {
Expand All @@ -16,14 +16,15 @@ import {
useBlockProps,
store as blockEditorStore,
} from '@wordpress/block-editor';
import { useState } from '@wordpress/element';
import { useState, useRef } from '@wordpress/element';
import {
Button,
PanelBody,
PanelRow,
TextControl,
__experimentalInputControlSuffixWrapper as InputControlSuffixWrapper,
} from '@wordpress/components';
import { useMergeRefs } from '@wordpress/compose';
import { __ } from '@wordpress/i18n';
import { keyboardReturn } from '@wordpress/icons';

Expand Down Expand Up @@ -112,12 +113,20 @@ const SocialLinkEdit = ( {
iconBackgroundColorValue,
} = context;
const [ showURLPopover, setPopover ] = useState( false );
const classes = clsx( 'wp-social-link', 'wp-social-link-' + service, {
'wp-social-link__is-incomplete': ! url,
[ `has-${ iconColor }-color` ]: iconColor,
[ `has-${ iconBackgroundColor }-background-color` ]:
iconBackgroundColor,
} );
const wrapperClasses = clsx(
'wp-social-link',
// Manually adding this class for backwards compatibility of CSS when moving the
// blockProps from the li to the button: https://github.com/WordPress/gutenberg/pull/64883
'wp-block-social-link',
'wp-social-link__list-item',
'wp-social-link-' + service,
{
'wp-social-link__is-incomplete': ! url,
[ `has-${ iconColor }-color` ]: iconColor,
[ `has-${ iconBackgroundColor }-background-color` ]:
iconBackgroundColor,
}
);

// Use internal state instead of a ref to make sure that the component
// re-renders when the popover's anchor updates.
Expand All @@ -131,11 +140,16 @@ const SocialLinkEdit = ( {
// spaces. The PHP render callback fallbacks to the social name as well.
const socialLinkText = label.trim() === '' ? socialLinkName : label;

const ref = useRef();
const blockProps = useBlockProps( {
className: classes,
style: {
color: iconColorValue,
backgroundColor: iconBackgroundColorValue,
className: 'wp-block-social-link-anchor',
ref: useMergeRefs( [ setPopoverAnchor, ref ] ),
onClick: () => setPopover( true ),
onKeyDown: ( event ) => {
if ( event.keyCode === ENTER ) {
event.preventDefault();
setPopover( true );
}
},
} );

Expand Down Expand Up @@ -169,13 +183,14 @@ const SocialLinkEdit = ( {
onChange={ ( value ) => setAttributes( { rel: value } ) }
/>
</InspectorControls>
<li { ...blockProps }>
<button
className="wp-block-social-link-anchor"
ref={ setPopoverAnchor }
onClick={ () => setPopover( true ) }
aria-haspopup="dialog"
>
<li
className={ wrapperClasses }
style={ {
color: iconColorValue,
backgroundColor: iconBackgroundColorValue,
} }
>
<button aria-haspopup="dialog" { ...blockProps }>
<IconComponent />
<span
className={ clsx( 'wp-block-social-link-label', {
Expand Down
6 changes: 6 additions & 0 deletions packages/block-library/src/social-link/editor.scss
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@
// Windows High Contrast mode will show this outline, but not the box-shadow.
outline: 3px solid transparent;
}

// Override the shared `.wp-block-social-link` class used on both the li and button
// due to backwards compatibility from moving the blockProps from the li to the button.
&:hover {
transform: none;
}
}

:root :where(.wp-block-social-links.is-style-pill-shape .wp-social-link button) {
Expand Down

0 comments on commit 97eb961

Please sign in to comment.