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

Added contrast verification to paragraph colors #3483

Merged
merged 5 commits into from
Nov 24, 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
8 changes: 4 additions & 4 deletions blocks/contrast-checker/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ import { Notice } from '@wordpress/components';
*/
import './style.scss';

function ContrastChecker( { backgroundColor, textColor, isLargeText } ) {
if ( ! backgroundColor || ! textColor ) {
function ContrastChecker( { backgroundColor, textColor, isLargeText, fallbackBackgroundColor, fallbackTextColor } ) {
if ( ! ( backgroundColor || fallbackBackgroundColor ) || ! ( textColor || fallbackTextColor ) ) {
return null;
}
const tinyBackgroundColor = tinycolor( backgroundColor );
const tinyTextColor = tinycolor( textColor );
const tinyBackgroundColor = tinycolor( backgroundColor || fallbackBackgroundColor );
const tinyTextColor = tinycolor( textColor || fallbackTextColor );
if ( tinycolor.isReadable(
tinyBackgroundColor,
tinyTextColor,
Expand Down
63 changes: 19 additions & 44 deletions blocks/library/button/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*/
import { __ } from '@wordpress/i18n';
import { Component } from '@wordpress/element';
import { Dashicon, IconButton, PanelColor } from '@wordpress/components';
import { Dashicon, IconButton, PanelColor, withFallbackStyles } from '@wordpress/components';

/**
* Internal dependencies
Expand All @@ -23,29 +23,23 @@ import BlockDescription from '../../block-description';

const { getComputedStyle } = window;

const ContrastCheckerWithFallbackStyles = withFallbackStyles( ( node, ownProps ) => {
const { textColor, backgroundColor } = ownProps;
//avoid the use of querySelector if textColor color is known and verify if node is available.
const textNode = ! textColor && node ? node.querySelector( '[contenteditable="true"]' ) : null;
return {
fallbackBackgroundColor: backgroundColor || ! node ? undefined : getComputedStyle( node ).backgroundColor,
fallbackTextColor: textColor || ! textNode ? undefined : getComputedStyle( textNode ).color,
};
} )( ContrastChecker );

class ButtonBlock extends Component {
constructor() {
super( ...arguments );

this.containers = {};
this.fallbackColors = {};

this.state = {
fallbackBackgroundColor: undefined,
fallbackTextColor: undefined,
};

this.nodeRef = null;
this.bindRef = this.bindRef.bind( this );
this.updateAlignment = this.updateAlignment.bind( this );
this.toggleClear = this.toggleClear.bind( this );
this.bindRef = this.bindRef.bind( this );
}

componentDidMount() {
this.grabColors();
}

componentDidUpdate() {
this.grabColors();
}

updateAlignment( nextAlign ) {
Expand All @@ -61,23 +55,7 @@ class ButtonBlock extends Component {
if ( ! node ) {
return;
}

this.containers.background = node;
this.containers.text = node.querySelector( '[contenteditable="true"]' );
}

grabColors() {
const { background, text } = this.containers;
const { textColor, color } = this.props.attributes;
const { fallbackTextColor, fallbackBackgroundColor } = this.state;

if ( ! color && ! fallbackBackgroundColor && background ) {
this.setState( { fallbackBackgroundColor: getComputedStyle( background ).backgroundColor } );
}

if ( ! textColor && ! fallbackTextColor && text ) {
this.setState( { fallbackTextColor: getComputedStyle( text ).color } );
}
this.nodeRef = node;
}

render() {
Expand All @@ -99,10 +77,6 @@ class ButtonBlock extends Component {
clear,
} = attributes;

const {
fallbackBackgroundColor,
fallbackTextColor,
} = this.state;
return [
focus && (
<BlockControls key="controls">
Expand Down Expand Up @@ -146,11 +120,12 @@ class ButtonBlock extends Component {
onChange={ ( colorValue ) => setAttributes( { textColor: colorValue } ) }
/>
</PanelColor>
<ContrastChecker
textColor={ textColor || fallbackTextColor }
backgroundColor={ color || fallbackBackgroundColor }
{ this.nodeRef && <ContrastCheckerWithFallbackStyles
node={ this.nodeRef }
textColor={ textColor }
backgroundColor={ color }
isLargeText={ true }
/>
/> }
</InspectorControls>
}
</span>,
Expand Down
3 changes: 3 additions & 0 deletions blocks/library/paragraph/editor.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.editor-block-list__block:not( .is-multi-selected ) .wp-block-paragraph {
background: white;
}
Loading