Skip to content
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
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
14 changes: 10 additions & 4 deletions packages/block-library/src/social-link/edit.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ import { compose } from '@wordpress/compose';
import { __, sprintf } from '@wordpress/i18n';
import { link, Icon } from '@wordpress/icons';
import { withSelect } from '@wordpress/data';
import { store as blocksStore } from '@wordpress/blocks';

/**
* Internal dependencies
*/
import { getIconBySite, getNameBySite } from './social-list';
import { getSocialService } from './social-list';
import styles from './editor.scss';

const DEFAULT_ACTIVE_ICON_STYLES = {
Expand Down Expand Up @@ -54,6 +56,7 @@ const SocialLinkEdit = ( {
isSelected,
onFocus,
name,
activeVariation,
} ) => {
const { url, service = name } = attributes;
const [ isLinkSheetVisible, setIsLinkSheetVisible ] = useState( false );
Expand All @@ -64,8 +67,7 @@ const SocialLinkEdit = ( {
DEFAULT_ACTIVE_ICON_STYLES;
const animatedValue = useRef( new Animated.Value( 0 ) ).current;

const IconComponent = getIconBySite( service )();
const socialLinkName = getNameBySite( service );
const { icon, label: socialLinkName } = getSocialService( activeVariation );

// When new social icon is added link sheet is opened automatically.
useEffect( () => {
Expand Down Expand Up @@ -190,7 +192,7 @@ const SocialLinkEdit = ( {
>
<Icon
animated
icon={ IconComponent }
icon={ icon() }
style={ { color: activeIcon.color } }
/>
</Animated.View>
Expand All @@ -202,12 +204,16 @@ const SocialLinkEdit = ( {
export default compose( [
withSelect( ( select, { clientId } ) => {
const { getBlock } = select( blockEditorStore );
const { getActiveBlockVariation } = select( blocksStore );

const block = getBlock( clientId );
const name = block?.name.substring( 17 );

return {
name,
activeVariation: block
? getActiveBlockVariation( block.name, block.attributes )
: undefined,
};
} ),
] )( SocialLinkEdit );
13 changes: 13 additions & 0 deletions packages/block-library/src/social-link/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,19 @@ function block_core_social_link_services( $service = '', $field = '' ) {
),
);

/**
* Filter the list of available social service.
*
* This can be used to change icons or add custom icons (additionally to variations in the editor).
* Icons should be directly renderable - therefore SVGs work best.
*
* @since 6.9.0
*
* @param array $services_data The list of services. Each item is an array containing a 'name' and 'icon' key.
* @return array The list of social services.
*/
$services_data = apply_filters( 'block_core_social_link_get_services', $services_data );

if ( ! empty( $service )
&& ! empty( $field )
&& isset( $services_data[ $service ] )
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' ),
};
}
Loading