-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Surface alignment toolbar as block control #987
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { __ } from 'i18n'; | ||
import { Toolbar } from 'components'; | ||
|
||
const ALIGNMENT_CONTROLS = [ | ||
{ | ||
icon: 'editor-alignleft', | ||
title: __( 'Align left' ), | ||
align: 'left', | ||
}, | ||
{ | ||
icon: 'editor-aligncenter', | ||
title: __( 'Align center' ), | ||
align: 'center', | ||
}, | ||
{ | ||
icon: 'editor-alignright', | ||
title: __( 'Align right' ), | ||
align: 'right', | ||
}, | ||
]; | ||
|
||
export default function AlignmentToolbar( { value, onChange } ) { | ||
return ( | ||
<Toolbar | ||
controls={ ALIGNMENT_CONTROLS.map( ( control ) => { | ||
const { align } = control; | ||
const isActive = ( value === align ); | ||
|
||
return { | ||
...control, | ||
isActive, | ||
onClick: () => onChange( isActive ? null : align ), | ||
}; | ||
} ) } | ||
/> | ||
); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,8 @@ import { switchChildrenNodeName } from 'element'; | |
*/ | ||
import './style.scss'; | ||
import { registerBlockType, createBlock, query as hpq } from '../../api'; | ||
import AlignmentToolbar from '../../alignment-toolbar'; | ||
import BlockControls from '../../block-controls'; | ||
import Editable from '../../editable'; | ||
|
||
const { children, query } = hpq; | ||
|
@@ -111,11 +113,24 @@ registerBlockType( 'core/quote', { | |
}, | ||
|
||
edit( { attributes, setAttributes, focus, setFocus, mergeBlocks } ) { | ||
const { value, citation, style = 1 } = attributes; | ||
const { align, value, citation, style = 1 } = attributes; | ||
const focusedEditable = focus ? focus.editable || 'value' : null; | ||
|
||
return ( | ||
<blockquote className={ `blocks-quote blocks-quote-style-${ style }` }> | ||
return [ | ||
focus && ( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was thinking about the focus check, I guess we could avoid it by hiding the controls using CSS instead of removing them completely from the DOM. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've grown more okay with it. Seems reasonable to expect that the render logic should be responsible for deciding if controls should be shown depending on whether the block currently has focus. |
||
<BlockControls key="controls"> | ||
<AlignmentToolbar | ||
value={ align } | ||
onChange={ ( nextAlign ) => { | ||
setAttributes( { align: nextAlign } ); | ||
} } | ||
/> | ||
</BlockControls> | ||
), | ||
<blockquote | ||
key="quote" | ||
className={ `blocks-quote blocks-quote-style-${ style }` } | ||
> | ||
<Editable | ||
value={ value } | ||
onChange={ | ||
|
@@ -126,7 +141,7 @@ registerBlockType( 'core/quote', { | |
focus={ focusedEditable === 'value' ? focus : null } | ||
onFocus={ () => setFocus( { editable: 'value' } ) } | ||
onMerge={ mergeBlocks } | ||
showAlignments | ||
style={ { textAlign: align } } | ||
/> | ||
{ ( ( citation && citation.length > 0 ) || !! focus ) && ( | ||
<Editable | ||
|
@@ -143,17 +158,22 @@ registerBlockType( 'core/quote', { | |
inline | ||
/> | ||
) } | ||
</blockquote> | ||
); | ||
</blockquote>, | ||
]; | ||
}, | ||
|
||
save( { attributes } ) { | ||
const { value, citation, style = 1 } = attributes; | ||
const { align, value, citation, style = 1 } = attributes; | ||
|
||
return ( | ||
<blockquote className={ `blocks-quote-style-${ style }` }> | ||
{ value && value.map( ( paragraph, i ) => ( | ||
<p key={ i }>{ paragraph }</p> | ||
<p | ||
key={ i } | ||
style={ { textAlign: align ? align : null } } | ||
> | ||
{ paragraph } | ||
</p> | ||
) ) } | ||
{ citation && citation.length > 0 && ( | ||
<footer>{ citation }</footer> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
<!-- wp:core/text --> | ||
<!-- wp:core/text align="right" --> | ||
<p style="text-align:right;">... like this one, which is separate from the above and right aligned.</p> | ||
<!-- /wp:core/text --> |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
"uid": "_uid_0", | ||
"name": "core/text", | ||
"attributes": { | ||
"align": "right", | ||
"content": [ | ||
{ | ||
"type": "p", | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
<!-- wp:core/text --> | ||
<!-- wp:core/text align="right" --> | ||
<p style="text-align:right;">... like this one, which is separate from the above and right aligned.</p> | ||
<!-- /wp:core/text --> | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we probably should make
controls
optional and avoid including this if no controls are providedThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Technically it is optional, and
<Toolbar />
will not render itself when omitted:https://github.com/WordPress/gutenberg/blob/97ee1a3/components/toolbar/index.js#L13-L15
Example: Included Text and Quote block changes render
<BlockControls />
withoutcontrols
prop.