Skip to content

Commit

Permalink
add support for title rel and target
Browse files Browse the repository at this point in the history
  • Loading branch information
gwwar committed Apr 23, 2021
1 parent 1aa73ef commit 99e9de5
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 48 deletions.
7 changes: 6 additions & 1 deletion packages/block-library/src/home-link/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@
"opensInNewTab": {
"type": "boolean",
"default": false
},
"rel": {
"type": "string"
},
"title": {
"type": "string"
}
},
"usesContext": [
Expand All @@ -19,7 +25,6 @@
"customBackgroundColor",
"fontSize",
"customFontSize",
"showSubmenuIcon",
"style"
],
"supports": {
Expand Down
89 changes: 62 additions & 27 deletions packages/block-library/src/home-link/edit.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
/**
* WordPress dependencies
*/
import { RichText, useBlockProps } from '@wordpress/block-editor';
import {
RichText,
useBlockProps,
InspectorControls,
} from '@wordpress/block-editor';
import { __ } from '@wordpress/i18n';
import { useSelect } from '@wordpress/data';
import { store as coreStore } from '@wordpress/core-data';
import { useEffect } from '@wordpress/element';
import { PanelBody, TextControl, ToggleControl } from '@wordpress/components';

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

Expand All @@ -23,7 +28,7 @@ export default function HomeEdit( { attributes, setAttributes, clientId } ) {
[ clientId ]
);

const { label } = attributes;
const { label, opensInNewTab, rel, title } = attributes;

useEffect( () => {
if ( label === undefined ) {
Expand All @@ -32,30 +37,60 @@ export default function HomeEdit( { attributes, setAttributes, clientId } ) {
}, [ clientId, label ] );

return (
<li { ...blockProps }>
<a
className="wp-block-home-link__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>
</li>
<>
<InspectorControls>
<PanelBody title={ __( 'Home link settings' ) }>
<ToggleControl
label={ __( 'Open in new tab' ) }
checked={ opensInNewTab }
onChange={ () => {
setAttributes( { opensInNewTab: ! opensInNewTab } );
} }
help={ __( 'Opens the home link in a new tab' ) }
/>
<TextControl
value={ title || '' }
onChange={ ( titleValue ) => {
setAttributes( { title: titleValue } );
} }
label={ __( 'Link title' ) }
autoComplete="off"
/>
<TextControl
value={ rel || '' }
onChange={ ( relValue ) => {
setAttributes( { rel: relValue } );
} }
label={ __( 'Link rel' ) }
autoComplete="off"
/>
</PanelBody>
</InspectorControls>
<li { ...blockProps }>
<a
className="wp-block-home-link__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>
</li>
</>
);
}
61 changes: 41 additions & 20 deletions packages/block-library/src/home-link/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,49 @@ function render_block_core_home( $attributes ) {

$wrapper_attributes = get_block_wrapper_attributes( array( 'class' => implode( ' ', $classnames ) ) );

$label = wp_kses(
$attributes['label'],
array(
'code' => array(),
'em' => array(),
'img' => array(
'scale' => array(),
'class' => array(),
'style' => array(),
'src' => array(),
'alt' => array(),
),
's' => array(),
'span' => array(
'style' => array(),
),
'strong' => array(),
)
);
$html = '<li ' . $wrapper_attributes . '><a class="wp-block-home__content"';

// Start appending HTML attributes to anchor tag.
$html .= ' href="' . esc_url( home_url() ) . '"';

if ( isset( $attributes['opensInNewTab'] ) && true === $attributes['opensInNewTab'] ) {
$html .= ' target="_blank" ';
}

$html = sprintf( '<li %s><a classname="wp-block-home__content" href="%s">%s</a></li>', $wrapper_attributes, home_url(), $label );
if ( isset( $attributes['rel'] ) ) {
$html .= ' rel="' . esc_attr( $attributes['rel'] ) . '"';
}

if ( isset( $attributes['title'] ) ) {
$html .= ' title="' . esc_attr( $attributes['title'] ) . '"';
}

// End appending HTML attributes to anchor tag.
$html .= '>';

if ( isset( $attributes['label'] ) ) {
$html .= wp_kses(
$attributes['label'],
array(
'code' => array(),
'em' => array(),
'img' => array(
'scale' => array(),
'class' => array(),
'style' => array(),
'src' => array(),
'alt' => array(),
),
's' => array(),
'span' => array(
'style' => array(),
),
'strong' => array(),
)
);
}

$html .= '</a></li>';
return $html;
}

Expand Down
1 change: 1 addition & 0 deletions packages/block-library/src/home-link/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
}

&:not([style*="text-decoration"]) {
.wp-block-home-link,
.wp-block-home-link__content {
text-decoration: none;

Expand Down

0 comments on commit 99e9de5

Please sign in to comment.