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

Site logo: Add link toggle option #31162

Merged
merged 10 commits into from
May 6, 2021
8 changes: 8 additions & 0 deletions packages/block-library/src/site-logo/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@
},
"width": {
"type": "number"
},
"isLink": {
"type": "boolean",
"default": true
},
"linkTarget": {
"type": "string",
"default": "_self"
}
},
"supports": {
Expand Down
84 changes: 53 additions & 31 deletions packages/block-library/src/site-logo/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
RangeControl,
ResizableBox,
Spinner,
ToggleControl,
} from '@wordpress/components';
import { useViewportMatch } from '@wordpress/compose';
import {
Expand Down Expand Up @@ -46,7 +47,7 @@ const ACCEPT_MEDIA_STRING = 'image/*';

const SiteLogo = ( {
alt,
attributes: { align, width, height },
attributes: { align, width, height, isLink, linkTarget },
containerRef,
isSelected,
setAttributes,
Expand All @@ -59,7 +60,7 @@ const SiteLogo = ( {
const isResizable = ! isWideAligned && isLargeViewport;
const [ { naturalWidth, naturalHeight }, setNaturalSize ] = useState( {} );
const { toggleSelection } = useDispatch( blockEditorStore );
const classes = classnames( {
const classes = classnames( 'custom-logo-link', {
'is-transient': isBlobURL( logoUrl ),
} );
const { maxWidth, title } = useSelect( ( select ) => {
Expand All @@ -83,35 +84,38 @@ const SiteLogo = ( {
}

const img = (
// Disable reason: Image itself is not meant to be interactive, but
// should direct focus to block.
/* eslint-disable jsx-a11y/no-noninteractive-element-interactions, jsx-a11y/click-events-have-key-events */
<a
href={ siteUrl }
className={ classes }
rel="home"
title={ title }
onClick={ ( event ) => event.preventDefault() }
>
<span className="custom-logo-link">
<img
className="custom-logo"
src={ logoUrl }
alt={ alt }
onLoad={ ( event ) => {
setNaturalSize(
pick( event.target, [
'naturalWidth',
'naturalHeight',
] )
);
} }
/>
</span>
</a>
/* eslint-enable jsx-a11y/no-noninteractive-element-interactions, jsx-a11y/click-events-have-key-events */
<img
className="custom-logo"
src={ logoUrl }
alt={ alt }
onLoad={ ( event ) => {
setNaturalSize(
pick( event.target, [ 'naturalWidth', 'naturalHeight' ] )
);
} }
/>
);

let imgWrapper = img;

// Disable reason: Image itself is not meant to be interactive, but
// should direct focus to block.
if ( isLink ) {
imgWrapper = (
/* eslint-disable jsx-a11y/no-noninteractive-element-interactions, jsx-a11y/click-events-have-key-events */
<a
href={ siteUrl }
className={ classes }
rel="home"
title={ title }
onClick={ ( event ) => event.preventDefault() }
>
{ img }
</a>
/* eslint-enable jsx-a11y/no-noninteractive-element-interactions, jsx-a11y/click-events-have-key-events */
);
}

let imageWidthWithinContainer;

if ( clientWidth && naturalWidth && naturalHeight ) {
Expand All @@ -120,7 +124,7 @@ const SiteLogo = ( {
}

if ( ! isResizable || ! imageWidthWithinContainer ) {
return <div style={ { width, height } }>{ img }</div>;
return <div style={ { width, height } }>{ imgWrapper }</div>;
}

const currentWidth = width || imageWidthWithinContainer;
Expand Down Expand Up @@ -192,6 +196,24 @@ const SiteLogo = ( {
value={ width || '' }
disabled={ ! isResizable }
/>
<ToggleControl
label={ __( 'Make image a link to home' ) }
carolinan marked this conversation as resolved.
Show resolved Hide resolved
carolinan marked this conversation as resolved.
Show resolved Hide resolved
carolinan marked this conversation as resolved.
Show resolved Hide resolved
onChange={ () => setAttributes( { isLink: ! isLink } ) }
checked={ isLink }
/>
{ isLink && (
<>
<ToggleControl
label={ __( 'Open in new tab' ) }
onChange={ ( value ) =>
setAttributes( {
linkTarget: value ? '_blank' : '_self',
} )
}
checked={ linkTarget === '_blank' }
/>
</>
) }
</PanelBody>
</InspectorControls>
<ResizableBox
Expand All @@ -217,7 +239,7 @@ const SiteLogo = ( {
} );
} }
>
{ img }
{ imgWrapper }
</ResizableBox>
</>
);
Expand Down
17 changes: 16 additions & 1 deletion packages/block-library/src/site-logo/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,22 @@ function render_block_core_site_logo( $attributes ) {

add_filter( 'wp_get_attachment_image_src', $adjust_width_height_filter );
$custom_logo = get_custom_logo();
$classnames = array();

if ( ! $attributes['isLink'] ) {
/* Remove the link */
carolinan marked this conversation as resolved.
Show resolved Hide resolved
$custom_logo = preg_replace( '#<a.*?>(.*?)</a>#i', '\1', $custom_logo );
}

if ( $attributes['isLink'] && '_blank' === $attributes['linkTarget'] ) {
/*
* Add the link target after the rel="home".
* Add an aria-label for informing that the page opens in a new tab.
*/
aristath marked this conversation as resolved.
Show resolved Hide resolved
carolinan marked this conversation as resolved.
Show resolved Hide resolved
$aria_label = 'aria-label="' . esc_attr__( '(Home link, opens in a new tab)' ) . '"';
$custom_logo = str_replace( 'rel="home"', 'rel="home" target="' . $attributes['linkTarget'] . '"' . $aria_label, $custom_logo );
}

$classnames = array();
if ( ! empty( $attributes['className'] ) ) {
$classnames[] = $attributes['className'];
}
Expand Down
5 changes: 4 additions & 1 deletion packages/e2e-tests/fixtures/blocks/core__site-logo.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
"clientId": "_clientId_0",
"name": "core/site-logo",
"isValid": true,
"attributes": {},
"attributes": {
"isLink": true,
"linkTarget": "_self"
},
"innerBlocks": [],
"originalContent": ""
}
Expand Down