Skip to content

Commit

Permalink
Try moving blockProps to button element
Browse files Browse the repository at this point in the history
This took quite a bit of refactoring. I'm concerned about the impacts of the CSS changes.
  • Loading branch information
jeryj committed Aug 29, 2024
1 parent 8a0e8bc commit bb6c8be
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 23 deletions.
51 changes: 31 additions & 20 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,13 +16,14 @@ 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,
} from '@wordpress/components';
import { useMergeRefs } from '@wordpress/compose';
import { __ } from '@wordpress/i18n';
import { keyboardReturn } from '@wordpress/icons';

Expand Down Expand Up @@ -108,12 +109,17 @@ 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',
'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 @@ -126,12 +132,16 @@ const SocialLinkEdit = ( {
// the social name, even when users enter and save an empty string or only
// 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 @@ -165,13 +175,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
2 changes: 1 addition & 1 deletion packages/block-library/src/social-link/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ function render_block_core_social_link( $attributes, $content, $block ) {
$icon = block_core_social_link_get_icon( $service );
$wrapper_attributes = get_block_wrapper_attributes(
array(
'class' => 'wp-social-link wp-social-link-' . $service . block_core_social_link_get_color_classes( $block->context ),
'class' => 'wp-social-link wp-social-link__list-item wp-social-link-' . $service . block_core_social_link_get_color_classes( $block->context ),
'style' => block_core_social_link_get_color_styles( $block->context ),
)
);
Expand Down
4 changes: 2 additions & 2 deletions packages/block-library/src/social-links/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
}
}

.wp-block-social-link {
.wp-social-link__list-item {
display: block;
border-radius: 9999px; // This makes it pill-shaped instead of oval, in cases where the image fed is not perfectly sized.
transition: transform 0.1s ease;
Expand All @@ -89,7 +89,7 @@
}

// This needs specificity because themes usually override it with things like .widget-area a.
.wp-block-social-links .wp-block-social-link.wp-social-link {
.wp-block-social-links .wp-social-link__list-item.wp-social-link {
display: inline-block;
margin: 0;
padding: 0;
Expand Down

0 comments on commit bb6c8be

Please sign in to comment.