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

Add aria-expanded to social link url popover trigger #64949

Open
wants to merge 5 commits into
base: trunk
Choose a base branch
from
Open
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
55 changes: 38 additions & 17 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,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 @@ -127,14 +136,21 @@ 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 );
}
},
} );

const isURLPopoverOpen = isSelected && showURLPopover;

return (
<>
<InspectorControls>
Expand Down Expand Up @@ -165,12 +181,17 @@ const SocialLinkEdit = ( {
onChange={ ( value ) => setAttributes( { rel: value } ) }
/>
</InspectorControls>
<li { ...blockProps }>
<li
className={ wrapperClasses }
style={ {
color: iconColorValue,
backgroundColor: iconBackgroundColorValue,
} }
>
<button
className="wp-block-social-link-anchor"
ref={ setPopoverAnchor }
onClick={ () => setPopover( true ) }
aria-haspopup="dialog"
aria-expanded={ isURLPopoverOpen }
{ ...blockProps }
>
<IconComponent />
<span
Expand All @@ -181,7 +202,7 @@ const SocialLinkEdit = ( {
{ socialLinkText }
</span>
</button>
{ isSelected && showURLPopover && (
{ isURLPopoverOpen && (
<SocialLinkURLPopover
url={ url }
setAttributes={ setAttributes }
Expand Down
11 changes: 4 additions & 7 deletions packages/block-library/src/social-link/editor.scss
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,10 @@
// This rule is duplicated from the style.scss and needs to be the same as there.
padding: 0.25em;

// Focus styles replicate the `@wordpress/components` button component.
&:focus:not(:disabled) {
border-radius: 2px;
box-shadow: 0 0 0 var(--wp-admin-border-width-focus) var(--wp-admin-theme-color);

// 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;
}
}

Expand Down
Loading