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

Blocks: add "Button" #616

Merged
merged 3 commits into from
May 4, 2017
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
109 changes: 109 additions & 0 deletions blocks/library/button/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/**
* Internal dependencies
*/
import './style.scss';
import { registerBlock, query } from 'api';
import Editable from 'components/editable';
import IconButton from '../../../editor/components/icon-button';

const { attr, children } = query;

/**
* Returns an attribute setter with behavior that if the target value is
* already the assigned attribute value, it will be set to undefined.
*
* @param {string} align Alignment value
* @return {Function} Attribute setter
*/
function applyOrUnset( align ) {
Copy link
Member

Choose a reason for hiding this comment

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

Per @iseulde's comment at #401 (comment), maybe there's value in allowing the control to be associated with an attribute to toggle, even just as an optional convenience.

Also, this has since been renamed in the image block as toggleAlignment.

return ( attributes, setAttributes ) => {
const nextAlign = attributes.align === align ? undefined : align;
setAttributes( { align: nextAlign } );
};
}

registerBlock( 'core/button', {
title: wp.i18n.__( 'Button' ),

icon: 'marker',

category: 'common',

attributes: {
url: attr( 'a', 'href' ),
title: attr( 'a', 'title' ),
text: children( 'a' ),
align: ( node ) => ( node.className.match( /\balign(\S+)/ ) || [] )[ 1 ]
Copy link
Member Author

Choose a reason for hiding this comment

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

@aduth what's the latest on this?

Copy link
Member

Choose a reason for hiding this comment

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

@aduth what's the latest on this?

See #624

},

controls: [
{
icon: 'align-left',
title: wp.i18n.__( 'Align left' ),
isActive: ( { align } ) => 'left' === align,
onClick: applyOrUnset( 'left' )
},
{
icon: 'align-center',
title: wp.i18n.__( 'Align center' ),
isActive: ( { align } ) => 'center' === align,
onClick: applyOrUnset( 'center' )
},
{
icon: 'align-right',
title: wp.i18n.__( 'Align right' ),
isActive: ( { align } ) => 'right' === align,
onClick: applyOrUnset( 'right' )
}
],

getEditWrapperProps( attributes ) {
const { align } = attributes;
if ( 'left' === align || 'right' === align || 'center' === align ) {
return { 'data-align': align };
}
},

edit( { attributes, setAttributes, focus, setFocus } ) {
const { text, url, title } = attributes;

return (
<span className="blocks-button" title={ title }>
<Editable
tagName="span"
placeholder={ wp.i18n.__( 'Write some text…' ) }
value={ text }
onFocus={ setFocus }
onChange={ ( value ) => setAttributes( { text: value } ) }
/>
{ focus &&
<form
className="editable-format-toolbar__link-modal"
Copy link
Member Author

Choose a reason for hiding this comment

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

This should be a component like Editable so we don't rely on reusing classes.

Copy link
Member Author

Choose a reason for hiding this comment

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

It would also simplify the interface for developers.

onSubmit={ ( event ) => event.preventDefault() }>
<input
className="editable-format-toolbar__link-input"
type="url"
required
value={ url }
onChange={ ( event ) => setAttributes( { url: event.target.value } ) }
placeholder={ wp.i18n.__( 'Paste URL or type' ) }
/>
<IconButton icon="editor-break" type="submit" />
</form>
}
</span>
);
},

save( { attributes } ) {
const { url, text, title, align = 'none' } = attributes;

return (
<div className={ `align${ align }` }>
<a href={ url } title={ title }>
{ text }
</a>
</div>
);
}
} );
61 changes: 61 additions & 0 deletions blocks/library/button/style.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
.blocks-button {
font-family: $default-font;
display: inline-block;
text-decoration: none;
font-size: 17px;
line-height: 1.5;
margin: 0;
padding: 6px 14px;
cursor: default;
border-width: 1px;
border-style: solid;
border-radius: 2px;
white-space: nowrap;
box-sizing: border-box;
background: $blue-medium;
color: $white;
border-color: $blue-wordpress;
vertical-align: top;
position: relative;

&:hover {
color: $white;
}

.editable-format-toolbar__link-modal {
top: -1px;
left: calc( 100% + 12px );
}
}

.blocks-button__link {
position: absolute;
right: -32px;
top: 5px;
color: $dark-gray-500;
text-decoration: none;

&:hover {
color: $dark-gray-300;
}

.dashicon {
vertical-align: middle;
}
}

.editor-visual-editor__block[data-type="core/button"] {
&[data-align="center"] {
text-align: center;
}

&[data-align="right"] {
text-align: right;

.editable-format-toolbar__link-modal {
top: -1px;
left: auto;
right: calc( 100% + 12px );
}
}
}
1 change: 1 addition & 0 deletions blocks/library/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ import './text';
import './list';
import './quote';
import './separator';
import './button';
4 changes: 4 additions & 0 deletions post-content.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ window._wpGutenbergPost = {
'<h1>1.0 Is The Loneliest Number</h1>',
'<!-- /wp:core/heading -->',

'<!-- wp:core/button -->',
'<div class="aligncenter"><a href="https://github.com/WordPress/gutenberg"><span>Gutenberg on GitHub</span></a></div>',
'<!-- /wp:core/button -->',

'<!-- wp:core/text -->',
'<p>I imagine prior to the launch of the iPod, or the iPhone, there were teams saying the same thing: the copy + paste guys are <em>so close</em> to being ready and we know Walt Mossberg is going to ding us for this so let\'s just not ship to the manufacturers in China for just a few more weeks… The Apple teams were probably embarrassed. But <strong>if you\'re not embarrassed when you ship your first version you waited too long</strong>.</p>',
'<!-- /wp:core/text -->',
Expand Down