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
97 changes: 68 additions & 29 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 Down Expand Up @@ -82,36 +83,56 @@ const SiteLogo = ( {
toggleSelection( true );
}

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 */
let img = (
<span className="custom-logo-link">
carolinan marked this conversation as resolved.
Show resolved Hide resolved
<img
className="custom-logo"
src={ logoUrl }
alt={ alt }
onLoad={ ( event ) => {
setNaturalSize(
pick( event.target, [
'naturalWidth',
'naturalHeight',
] )
);
} }
/>
</span>
);

// 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 */
if ( isLink ) {
img = (
<a
href={ siteUrl }
className={ classes }
rel="home"
title={ title }
onClick={ ( event ) => event.preventDefault() }
>
<span className="custom-logo-link">
aristath marked this conversation as resolved.
Show resolved Hide resolved
<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 */

let imageWidthWithinContainer;

if ( clientWidth && naturalWidth && naturalHeight ) {
Expand Down Expand Up @@ -192,6 +213,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 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