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

Improve Social Icons setup and appending #64877

Merged
merged 8 commits into from
Aug 29, 2024
Merged
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
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,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a focus loss when pressing escape from the appender popover.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. I'll take a look.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't seem to replicate this...how do you do it?

How do you track focus? I'm using document.activeElement.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Navigate to the appender
  • Press Enter
  • Focus is within the popover
  • Press Escape
  • Focus is...? It should be visible and on the appender again since that's what opened the popover.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh man. I missed the word "appender". My bad. I was looking at the URL popover 🤦

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I pushed a commit which aims to restore focus to the appender once the quick inserter is closed. LMK what you think 🙏

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed! Thank you!

} );

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