Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Social: Allow custom link icons using block variations
  • Loading branch information
Mamaduka committed Jun 11, 2025
commit 888d015bf6e179f0caadb47e5ff15b86053c7433
23 changes: 17 additions & 6 deletions packages/block-library/src/social-link/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ import clsx from 'clsx';
* WordPress dependencies
*/
import { DELETE, BACKSPACE, ENTER } from '@wordpress/keycodes';
import { useDispatch } from '@wordpress/data';

import { useDispatch, useSelect } from '@wordpress/data';
import {
BlockControls,
InspectorControls,
Expand All @@ -20,6 +19,7 @@ import {
} from '@wordpress/block-editor';
import { useState, useRef } from '@wordpress/element';
import {
Icon,
Button,
Dropdown,
TextControl,
Expand All @@ -31,11 +31,12 @@ import {
import { useMergeRefs } from '@wordpress/compose';
import { __ } from '@wordpress/i18n';
import { keyboardReturn } from '@wordpress/icons';
import { store as blocksStore } from '@wordpress/blocks';

/**
* Internal dependencies
*/
import { getIconBySite, getNameBySite } from './social-list';
import { getSocialService } from './social-list';
import { useToolsPanelDropdownMenuProps } from '../utils/hooks';

const SocialLinkURLPopover = ( {
Expand Down Expand Up @@ -108,6 +109,7 @@ const SocialLinkEdit = ( {
isSelected,
setAttributes,
clientId,
name,
} ) => {
const { url, service, label = '', rel } = attributes;
const dropdownMenuProps = useToolsPanelDropdownMenuProps();
Expand Down Expand Up @@ -138,8 +140,17 @@ const SocialLinkEdit = ( {
const [ popoverAnchor, setPopoverAnchor ] = useState( null );
const isContentOnlyMode = useBlockEditingMode() === 'contentOnly';

const IconComponent = getIconBySite( service );
const socialLinkName = getNameBySite( service );
const { activeVariation } = useSelect(
( select ) => {
const { getActiveBlockVariation } = select( blocksStore );
return {
activeVariation: getActiveBlockVariation( name, attributes ),
};
},
[ name, attributes ]
);

const { icon, label: socialLinkName } = getSocialService( activeVariation );
// The initial label (ie. the link text) is an empty string.
// We want to prevent empty links so that the link text always fallbacks to
// the social name, even when users enter and save an empty string or only
Expand Down Expand Up @@ -258,7 +269,7 @@ const SocialLinkEdit = ( {
*/
/* eslint-disable jsx-a11y/no-redundant-roles */ }
<button aria-haspopup="dialog" { ...blockProps } role="button">
<IconComponent />
<Icon icon={ icon } />
<span
className={ clsx( 'wp-block-social-link-label', {
'screen-reader-text': ! showLabels,
Expand Down
35 changes: 15 additions & 20 deletions packages/block-library/src/social-link/social-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,24 @@ import { __ } from '@wordpress/i18n';
/**
* Internal dependencies
*/
import variations from './variations';
import { ChainIcon } from './icons';

/**
* Retrieves the social service's icon component.
* Retrieves the social service's icon component and label.
*
* @param {string} name key for a social service (lowercase slug)
*
* @return {Component} Icon component for social service.
* @param {Object} variation The object of the social service variation
* @return {Object} An object containing the Icon component for social service and label.
*/
export const getIconBySite = ( name ) => {
const variation = variations.find( ( v ) => v.name === name );
return variation ? variation.icon : ChainIcon;
};
export function getSocialService( variation ) {
if ( ! variation?.name ) {
return {
icon: ChainIcon,
label: __( 'Social Icon' ),
};
}

/**
* Retrieves the display name for the social service.
*
* @param {string} name key for a social service (lowercase slug)
*
* @return {string} Display name for social service
*/
export const getNameBySite = ( name ) => {
const variation = variations.find( ( v ) => v.name === name );
return variation ? variation.title : __( 'Social Icon' );
};
return {
icon: variation?.icon ?? ChainIcon,
label: variation?.title ?? __( 'Social Icon' ),
};
}