Skip to content

Commit

Permalink
Add support for text alignment
Browse files Browse the repository at this point in the history
  • Loading branch information
porada committed May 19, 2021
1 parent 8dad8c8 commit dcd1dbb
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 12 deletions.
3 changes: 3 additions & 0 deletions packages/block-library/src/pullquote/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
"default": "",
"__experimentalRole": "content"
},
"textAlign": {
"type": "string"
},
"mainColor": {
"type": "string"
},
Expand Down
23 changes: 17 additions & 6 deletions packages/block-library/src/pullquote/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import { Platform, useEffect, useRef } from '@wordpress/element';
import {
RichText,
ContrastChecker,
AlignmentToolbar,
BlockControls,
InspectorControls,
withColors,
PanelColorSettings,
Expand All @@ -33,7 +35,7 @@ import { SOLID_COLOR_CLASS } from './shared';
function PullQuoteEdit( {
colorUtils,
textColor,
attributes: { value, citation },
attributes: { value, citation, textAlign },
setAttributes,
setTextColor,
setMainColor,
Expand Down Expand Up @@ -108,12 +110,13 @@ function PullQuoteEdit( {
style={ {
color: textColor.color,
} }
className={
textColor.color &&
classnames( 'has-text-color', {
className={ classnames( {
[ `has-text-align-${ textAlign }` ]: textAlign,
...( textColor.color && {
'has-text-color': true,
[ textColor.class ]: textColor.class,
} )
}
} ),
} ) }
>
<RichText
identifier="value"
Expand Down Expand Up @@ -157,6 +160,14 @@ function PullQuoteEdit( {
) }
</BlockQuote>
</Figure>
<BlockControls>
<AlignmentToolbar
value={ textAlign }
onChange={ ( nextTextAlign ) =>
setAttributes( { textAlign: nextTextAlign } )
}
/>
</BlockControls>
{ Platform.OS === 'web' && (
<InspectorControls>
<PanelColorSettings
Expand Down
12 changes: 8 additions & 4 deletions packages/block-library/src/pullquote/save.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,12 @@ export default function save( { attributes } ) {
customTextColor,
value,
citation,
textAlign,
className,
} = attributes;

const isSolidColorStyle = includes( className, SOLID_COLOR_CLASS );
const isTextColorSet = Boolean( textColor || customTextColor );

let figureClasses, figureStyles;

Expand All @@ -56,11 +58,13 @@ export default function save( { attributes } ) {
}

const blockquoteTextColorClass = getColorClassName( 'color', textColor );
const blockquoteClasses =
( textColor || customTextColor ) &&
classnames( 'has-text-color', {
const blockquoteClasses = classnames( {
[ `has-text-align-${ textAlign }` ]: textAlign,
...( isTextColorSet && {
'has-text-color': true,
[ blockquoteTextColorClass ]: blockquoteTextColorClass,
} );
} ),
} );

const blockquoteStyles = blockquoteTextColorClass
? undefined
Expand Down
22 changes: 20 additions & 2 deletions packages/block-library/src/pullquote/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
footer {
position: relative;
}

.has-text-color a {
color: inherit;
}
Expand All @@ -30,15 +31,32 @@
background: none;
}

/*
* 1. Defining `text-align` on the parent would get overwritten by the theme’s
* defintion of the default style.
* 2. When no text alignment is set, the solid color style aligns text to the
* side, not center.
* 3. Twenty Twenty-One has a hardcoded `text-align` on `blockquote::before`.
* The only way to make the decorative quotation mark follow the same
* alignment as its parent (both in the editor and on the front-end) is
* by resetting the property with `!important`.
*/
.wp-block-pullquote.is-style-solid-color {
border: none;

blockquote {
margin-left: auto;
margin-right: auto;
text-align: left;

max-width: 60%;

&:not([class*="has-text-align-"]) { /* 1 */
text-align: left; /* 2 */
}

&::before {
text-align: inherit !important; /* 3 */
}

p {
margin-top: 0;
margin-bottom: 0;
Expand Down

0 comments on commit dcd1dbb

Please sign in to comment.