-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #616 from WordPress/add/button-block
Blocks: add "Button"
- Loading branch information
Showing
4 changed files
with
175 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ) { | ||
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 ] | ||
}, | ||
|
||
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" | ||
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> | ||
); | ||
} | ||
} ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,3 +6,4 @@ import './text'; | |
import './list'; | ||
import './quote'; | ||
import './separator'; | ||
import './button'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters