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

Home Link: Remove label attribute synchronization #67151

Merged
merged 2 commits into from
Nov 20, 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
72 changes: 27 additions & 45 deletions packages/block-library/src/home-link/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,10 @@ import clsx from 'clsx';
/**
* WordPress dependencies
*/
import {
RichText,
useBlockProps,
store as blockEditorStore,
} from '@wordpress/block-editor';
import { RichText, useBlockProps } from '@wordpress/block-editor';
import { __ } from '@wordpress/i18n';
import { useSelect, useDispatch } from '@wordpress/data';
import { useSelect } from '@wordpress/data';
import { store as coreStore } from '@wordpress/core-data';
import { useEffect } from '@wordpress/element';

const preventDefault = ( event ) => event.preventDefault();

Expand All @@ -24,8 +19,6 @@ export default function HomeEdit( { attributes, setAttributes, context } ) {
return select( coreStore ).getEntityRecord( 'root', '__unstableBase' )
?.home;
}, [] );
const { __unstableMarkNextChangeAsNotPersistent } =
useDispatch( blockEditorStore );

const { textColor, backgroundColor, style } = context;
const blockProps = useBlockProps( {
Expand All @@ -41,42 +34,31 @@ export default function HomeEdit( { attributes, setAttributes, context } ) {
},
} );

const { label } = attributes;

useEffect( () => {
if ( label === undefined ) {
__unstableMarkNextChangeAsNotPersistent();
setAttributes( { label: __( 'Home' ) } );
}
}, [ label ] );

return (
<>
<div { ...blockProps }>
<a
className="wp-block-home-link__content wp-block-navigation-item__content"
href={ homeUrl }
onClick={ preventDefault }
>
<RichText
identifier="label"
className="wp-block-home-link__label"
value={ label }
onChange={ ( labelValue ) => {
setAttributes( { label: labelValue } );
} }
aria-label={ __( 'Home link text' ) }
placeholder={ __( 'Add home link' ) }
withoutInteractiveFormatting
allowedFormats={ [
'core/bold',
'core/italic',
'core/image',
'core/strikethrough',
] }
/>
</a>
</div>
</>
<div { ...blockProps }>
<a
className="wp-block-home-link__content wp-block-navigation-item__content"
href={ homeUrl }
onClick={ preventDefault }
>
<RichText
identifier="label"
className="wp-block-home-link__label"
value={ attributes.label ?? __( 'Home' ) }
onChange={ ( labelValue ) => {
setAttributes( { label: labelValue } );
} }
aria-label={ __( 'Home link text' ) }
placeholder={ __( 'Add home link' ) }
withoutInteractiveFormatting
allowedFormats={ [
'core/bold',
'core/italic',
'core/image',
'core/strikethrough',
] }
/>
</a>
</div>
);
}
3 changes: 0 additions & 3 deletions packages/block-library/src/home-link/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,6 @@ function block_core_home_link_build_li_wrapper_attributes( $context ) {
*/
function render_block_core_home_link( $attributes, $content, $block ) {
if ( empty( $attributes['label'] ) ) {
// Using a fallback for the label attribute allows rendering the block even if no attributes have been set,
// e.g. when using the block as a hooked block.
// Note that the fallback value needs to be kept in sync with the one set in `edit.js` (upon first loading the block in the editor).
$attributes['label'] = __( 'Home' );
}
$aria_current = '';
Expand Down
Loading