Skip to content

Commit

Permalink
Social Link: Add contentOnly editing support (#66622)
Browse files Browse the repository at this point in the history
Co-authored-by: ndiego <[email protected]>
Co-authored-by: talldan <[email protected]>
  • Loading branch information
3 people authored and michalczaplinski committed Dec 5, 2024
1 parent b390de8 commit dfd28ec
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 2 deletions.
6 changes: 4 additions & 2 deletions packages/block-library/src/social-link/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@
"textdomain": "default",
"attributes": {
"url": {
"type": "string"
"type": "string",
"role": "content"
},
"service": {
"type": "string"
},
"label": {
"type": "string"
"type": "string",
"role": "content"
},
"rel": {
"type": "string"
Expand Down
40 changes: 40 additions & 0 deletions packages/block-library/src/social-link/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,22 @@ import { DELETE, BACKSPACE, ENTER } from '@wordpress/keycodes';
import { useDispatch } from '@wordpress/data';

import {
BlockControls,
InspectorControls,
URLPopover,
URLInput,
useBlockEditingMode,
useBlockProps,
store as blockEditorStore,
} from '@wordpress/block-editor';
import { useState, useRef } from '@wordpress/element';
import {
Button,
Dropdown,
PanelBody,
PanelRow,
TextControl,
ToolbarButton,
__experimentalInputControlSuffixWrapper as InputControlSuffixWrapper,
} from '@wordpress/components';
import { useMergeRefs } from '@wordpress/compose';
Expand Down Expand Up @@ -130,6 +134,7 @@ const SocialLinkEdit = ( {
// Use internal state instead of a ref to make sure that the component
// re-renders when the popover's anchor updates.
const [ popoverAnchor, setPopoverAnchor ] = useState( null );
const isContentOnlyMode = useBlockEditingMode() === 'contentOnly';

const IconComponent = getIconBySite( service );
const socialLinkName = getNameBySite( service );
Expand All @@ -154,6 +159,41 @@ const SocialLinkEdit = ( {

return (
<>
{ isContentOnlyMode && showLabels && (
// Add an extra control to modify the label attribute when content only mode is active.
// With content only mode active, the inspector is hidden, so users need another way
// to edit this attribute.
<BlockControls group="other">
<Dropdown
popoverProps={ { position: 'bottom right' } }
renderToggle={ ( { isOpen, onToggle } ) => (
<ToolbarButton
onClick={ onToggle }
aria-haspopup="true"
aria-expanded={ isOpen }
>
{ __( 'Text' ) }
</ToolbarButton>
) }
renderContent={ () => (
<TextControl
__next40pxDefaultSize
__nextHasNoMarginBottom
className="wp-block-social-link__toolbar_content_text"
label={ __( 'Text' ) }
help={ __(
'Provide a text label or use the default.'
) }
value={ label }
onChange={ ( value ) =>
setAttributes( { label: value } )
}
placeholder={ socialLinkName }
/>
) }
/>
</BlockControls>
) }
<InspectorControls>
<PanelBody title={ __( 'Settings' ) }>
<PanelRow>
Expand Down
5 changes: 5 additions & 0 deletions packages/block-library/src/social-link/editor.scss
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,8 @@
:root :where(.wp-block-social-links.is-style-logos-only .wp-social-link button) {
padding: 0;
}

.wp-block-social-link__toolbar_content_text {
// Corresponds to the size of the text control input in the block inspector.
width: 250px;
}

0 comments on commit dfd28ec

Please sign in to comment.