Skip to content

Commit 95f3250

Browse files
Fix: Setting a contrast color, triggers a contrast warning.
1 parent a90752e commit 95f3250

File tree

2 files changed

+37
-14
lines changed
  • packages
    • block-library/src/paragraph
    • components/src/higher-order/with-fallback-styles

2 files changed

+37
-14
lines changed

packages/block-library/src/paragraph/edit.js

+17-11
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,23 @@ const { getComputedStyle } = window;
3636

3737
const name = 'core/paragraph';
3838

39-
const applyFallbackStyles = withFallbackStyles( ( node, ownProps ) => {
40-
const { textColor, backgroundColor, fontSize, customFontSize } = ownProps.attributes;
41-
const editableNode = node.querySelector( '[contenteditable="true"]' );
42-
//verify if editableNode is available, before using getComputedStyle.
43-
const computedStyles = editableNode ? getComputedStyle( editableNode ) : null;
44-
return {
45-
fallbackBackgroundColor: backgroundColor || ! computedStyles ? undefined : computedStyles.backgroundColor,
46-
fallbackTextColor: textColor || ! computedStyles ? undefined : computedStyles.color,
47-
fallbackFontSize: fontSize || customFontSize || ! computedStyles ? undefined : parseInt( computedStyles.fontSize ) || undefined,
48-
};
49-
} );
39+
const applyFallbackStyles = withFallbackStyles(
40+
( node, ownProps ) => {
41+
const { textColor, backgroundColor, fontSize } = ownProps;
42+
const editableNode = node.querySelector( '[contenteditable="true"]' );
43+
//verify if editableNode is available, before using getComputedStyle.
44+
const computedStyles = editableNode ? getComputedStyle( editableNode ) : null;
45+
return {
46+
fallbackBackgroundColor: ( backgroundColor.color || ! computedStyles ) ? undefined : computedStyles.backgroundColor,
47+
fallbackTextColor: ( textColor.color || ! computedStyles ) ? undefined : computedStyles.color,
48+
fallbackFontSize: ( fontSize.size || ! computedStyles ) ? undefined : parseInt( computedStyles.fontSize ) || undefined,
49+
};
50+
},
51+
( ownProps ) => {
52+
const { textColor, backgroundColor, fontSize } = ownProps;
53+
return [ textColor.color, backgroundColor.color, fontSize.size ];
54+
}
55+
);
5056

5157
class ParagraphBlock extends Component {
5258
constructor() {

packages/components/src/higher-order/with-fallback-styles/index.js

+20-3
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ import { every, isEqual } from 'lodash';
88
*/
99
import { Component } from '@wordpress/element';
1010
import { createHigherOrderComponent } from '@wordpress/compose';
11+
import isShallowEqual from '@wordpress/is-shallow-equal';
1112

12-
export default ( mapNodeToProps ) => createHigherOrderComponent(
13+
export default ( mapNodeToProps, computeWhenChange ) => createHigherOrderComponent(
1314
( WrappedComponent ) => {
1415
return class extends Component {
1516
constructor() {
@@ -19,6 +20,7 @@ export default ( mapNodeToProps ) => createHigherOrderComponent(
1920
fallbackStyles: undefined,
2021
grabStylesCompleted: false,
2122
};
23+
this.shouldRecomputeValues = this.shouldRecomputeValues().bind( this );
2224

2325
this.bindRef = this.bindRef.bind( this );
2426
}
@@ -38,9 +40,24 @@ export default ( mapNodeToProps ) => createHigherOrderComponent(
3840
this.grabFallbackStyles();
3941
}
4042

43+
shouldRecomputeValues() {
44+
let lastRecomputeValues = [];
45+
return () => {
46+
if ( ! computeWhenChange ) {
47+
return ! this.state.grabStylesCompleted;
48+
}
49+
const newRecomputeValues = computeWhenChange( this.props );
50+
if ( isShallowEqual( lastRecomputeValues, newRecomputeValues ) ) {
51+
return false;
52+
}
53+
lastRecomputeValues = newRecomputeValues;
54+
return true;
55+
};
56+
}
57+
4158
grabFallbackStyles() {
42-
const { grabStylesCompleted, fallbackStyles } = this.state;
43-
if ( this.nodeRef && ! grabStylesCompleted ) {
59+
const { fallbackStyles } = this.state;
60+
if ( this.nodeRef && this.shouldRecomputeValues() ) {
4461
const newFallbackStyles = mapNodeToProps( this.nodeRef, this.props );
4562
if ( ! isEqual( newFallbackStyles, fallbackStyles ) ) {
4663
this.setState( {

0 commit comments

Comments
 (0)