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

[RNMobile] Fix background and text colour on pullquote block #34451

Merged
merged 16 commits into from
Oct 13, 2021
Merged
Show file tree
Hide file tree
Changes from 13 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
14 changes: 12 additions & 2 deletions packages/block-library/src/pullquote/blockquote.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,25 @@ import { Children, cloneElement } from '@wordpress/element';
import styles from './blockquote.scss';

export const BlockQuote = ( props ) => {
const citationStyle = { ...styles.citation };
const quoteStyle = { ...styles.quote };

if ( props.textColor ) {
quoteStyle.color = props.textColor;
quoteStyle.placeholderColor = props.textColor;
citationStyle.color = props.textColor;
citationStyle.placeholderColor = props.textColor;
}

const newChildren = Children.map( props.children, ( child ) => {
if ( child && child.props.identifier === 'value' ) {
return cloneElement( child, {
style: styles.quote,
style: quoteStyle,
} );
}
if ( child && child.props.identifier === 'citation' ) {
return cloneElement( child, {
style: styles.citation,
style: citationStyle,
} );
}
return child;
Expand Down
129 changes: 129 additions & 0 deletions packages/block-library/src/pullquote/edit.native.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import {
AlignmentControl,
BlockControls,
RichText,
useBlockProps,
getColorObjectByAttributeValues,
__experimentalGetColorClassesAndStyles as getColorClassesAndStyles,
} from '@wordpress/block-editor';
import { createBlock } from '@wordpress/blocks';

/**
* Internal dependencies
*/
import { Figure } from './figure';
import { BlockQuote } from './blockquote';

const getBackgroundColor = ( { attributes, colors, style } ) => {
const { backgroundColor } = attributes;

const colorProps = getColorClassesAndStyles( attributes );
const colorObject = getColorObjectByAttributeValues(
colors,
backgroundColor
);

return (
colorObject?.color ||
colorProps.style?.backgroundColor ||
colorProps.style?.background ||
style?.backgroundColor
);
};

const getTextColor = ( { attributes, colors, style } ) => {
const colorProps = getColorClassesAndStyles( attributes );
const colorObject = getColorObjectByAttributeValues(
colors,
attributes.textColor
);
return (
colorObject?.color ||
colorProps.style?.color ||
style?.color ||
style?.baseColors?.color?.text
);
};
const getBorderColor = ( props ) => {
const { wrapperProps } = props;
const defaultColor = getTextColor( props );

return wrapperProps?.style?.borderColor || defaultColor;
};
/**
* Internal dependencies
*/

function PullQuoteEdit( props ) {
const { attributes, setAttributes, isSelected, insertBlocksAfter } = props;
const { textAlign, citation, value } = attributes;

const blockProps = useBlockProps( {
backgroundColor: getBackgroundColor( props ),
borderColor: getBorderColor( props ),
} );

const shouldShowCitation = ! RichText.isEmpty( citation ) || isSelected;

return (
<>
<BlockControls group="block">
<AlignmentControl
value={ textAlign }
onChange={ ( nextAlign ) => {
setAttributes( { textAlign: nextAlign } );
} }
/>
</BlockControls>
<Figure { ...blockProps }>
<BlockQuote textColor={ getTextColor( props ) }>
<RichText
identifier="value"
multiline
value={ value }
onChange={ ( nextValue ) =>
setAttributes( {
value: nextValue,
} )
}
aria-label={ __( 'Pullquote text' ) }
placeholder={
// translators: placeholder text used for the quote
__( 'Add quote' )
}
textAlign={ textAlign ?? 'center' }
/>
{ shouldShowCitation && (
<RichText
identifier="citation"
value={ citation }
aria-label={ __( 'Pullquote citation text' ) }
placeholder={
// translators: placeholder text used for the citation
__( 'Add citation' )
}
onChange={ ( nextCitation ) =>
setAttributes( {
citation: nextCitation,
} )
}
__unstableMobileNoFocusOnMount
textAlign={ textAlign ?? 'center' }
__unstableOnSplitAtEnd={ () =>
insertBlocksAfter(
createBlock( 'core/paragraph' )
)
}
/>
) }
</BlockQuote>
</Figure>
</>
);
}

export default PullQuoteEdit;
24 changes: 17 additions & 7 deletions packages/block-library/src/pullquote/figure.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,29 @@ import { View } from 'react-native';
/**
* WordPress dependencies
*/
import { withPreferredColorScheme } from '@wordpress/compose';
import { usePreferredColorSchemeStyle } from '@wordpress/compose';
/**
* Internal dependencies
*/
import styles from './figure.scss';

export const Figure = withPreferredColorScheme( ( props ) => {
const { children, getStylesFromColorScheme } = props;

const wpPullquoteFigure = getStylesFromColorScheme(
export const Figure = ( { children, backgroundColor, borderColor } ) => {
const wpPullquoteFigure = usePreferredColorSchemeStyle(
styles.light,
styles.dark
);

return <View style={ wpPullquoteFigure }>{ children }</View>;
} );
const customStyles = {};
if ( borderColor ) {
customStyles.borderTopColor = borderColor;
customStyles.borderBottomColor = borderColor;
}

if ( backgroundColor ) {
customStyles.backgroundColor = backgroundColor;
}

return (
<View style={ [ wpPullquoteFigure, customStyles ] }>{ children }</View>
);
};