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

[Enhancement] Introduce Link Target in Button Block #10128

Merged
merged 12 commits into from
Jul 10, 2019
26 changes: 22 additions & 4 deletions packages/block-editor/src/components/url-input/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,17 +223,27 @@ class URLInput extends Component {
this.inputRef.current.focus();
}

static getDerivedStateFromProps( { showSuggestionsOverride }, { showSuggestions } ) {
return {
showSuggestions: showSuggestionsOverride !== undefined ? showSuggestionsOverride : showSuggestions,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's document this new prop in the component's README.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll address this in a follow-up PR 👍

};
}

render() {
const { value = '', autoFocus = true, instanceId, className } = this.props;
const { value = '', autoFocus = true, instanceId, className, id, isFullWidth, hasBorder } = this.props;
const { showSuggestions, suggestions, selectedSuggestion, loading } = this.state;

const suggestionsListboxId = `block-editor-url-input-suggestions-${ instanceId }`;
const suggestionOptionIdPrefix = `block-editor-url-input-suggestion-${ instanceId }`;

/* eslint-disable jsx-a11y/no-autofocus */
return (
<div className={ classnames( 'editor-url-input block-editor-url-input', className ) }>
<div className={ classnames( 'editor-url-input block-editor-url-input', className, {
'is-full-width': isFullWidth,
'has-border': hasBorder,
} ) }>
<input
id={ id }
autoFocus={ autoFocus }
type="text"
aria-label={ __( 'URL' ) }
Expand All @@ -254,9 +264,17 @@ class URLInput extends Component {
{ ( loading ) && <Spinner /> }

{ showSuggestions && !! suggestions.length &&
<Popover position="bottom" noArrow focusOnMount={ false }>
<Popover
position="bottom"
noArrow
focusOnMount={ false }
>
<div
className="editor-url-input__suggestions block-editor-url-input__suggestions"
className={ classnames(
'editor-url-input__suggestions',
'block-editor-url-input__suggestions',
`${ className }__suggestions`
) }
id={ suggestionsListboxId }
ref={ this.autocompleteRef }
role="listbox"
Expand Down
17 changes: 17 additions & 0 deletions packages/block-editor/src/components/url-input/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,23 @@ $input-size: 300px;
}
}

&.has-border input[type="text"] {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if this is the standard way to introduce the .has-button class. I could use @at-root to make it nested correctly, but I didn't notice it being used in Gutenberg.

Could you let me know if I have done this correctly or is there a more standard way to do this, preferably with one of your awesome examples?

Thank you so much ❤️

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me. 👍

border: 1px solid $dark-gray-500;
border-radius: 4px;
}

&.is-full-width {
width: 100%;

input[type="text"] {
width: 100%;
}

&__suggestions {
width: 100%;
}
}

.components-spinner {
position: absolute;
right: $input-padding;
Expand Down
12 changes: 12 additions & 0 deletions packages/block-library/src/button/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,18 @@
},
"customTextColor": {
"type": "string"
},
"linkTarget": {
"type": "string",
"source": "attribute",
"selector": "a",
"attribute": "target"
},
"rel": {
"type": "string",
"source": "attribute",
"selector": "a",
"attribute": "rel"
}
}
}
67 changes: 66 additions & 1 deletion packages/block-library/src/button/deprecated.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@
* External dependencies
*/
import { omit } from 'lodash';
import classnames from 'classnames';

/**
* WordPress dependencies
*/
import { RichText } from '@wordpress/block-editor';
import {
RichText,
getColorClassName,
} from '@wordpress/block-editor';

const colorsMigration = ( attributes ) => {
return omit( {
Expand Down Expand Up @@ -37,6 +41,67 @@ const blockAttributes = {
};

const deprecated = [
{
attributes: {
...blockAttributes,
align: {
type: 'string',
default: 'none',
},
backgroundColor: {
type: 'string',
},
textColor: {
type: 'string',
},
customBackgroundColor: {
type: 'string',
},
customTextColor: {
type: 'string',
},
},
save( { attributes } ) {
const {
url,
text,
title,
backgroundColor,
textColor,
customBackgroundColor,
customTextColor,
} = attributes;

const textClass = getColorClassName( 'color', textColor );
const backgroundClass = getColorClassName( 'background-color', backgroundColor );

const buttonClasses = classnames( 'wp-block-button__link', {
'has-text-color': textColor || customTextColor,
[ textClass ]: textClass,
'has-background': backgroundColor || customBackgroundColor,
[ backgroundClass ]: backgroundClass,
} );

const buttonStyle = {
backgroundColor: backgroundClass ? undefined : customBackgroundColor,
color: textClass ? undefined : customTextColor,
};

return (
<div>
<RichText.Content
tagName="a"
className={ buttonClasses }
href={ url }
title={ title }
style={ buttonStyle }
value={ text }
/>
</div>
);
},
migrate: colorsMigration,
},
{
attributes: {
...blockAttributes,
Expand Down
Loading