-
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
Fix: Setting a contrast color, triggers a contrast warning. #14963
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 |
---|---|---|
|
@@ -8,8 +8,9 @@ import { every, isEqual } from 'lodash'; | |
*/ | ||
import { Component } from '@wordpress/element'; | ||
import { createHigherOrderComponent } from '@wordpress/compose'; | ||
import isShallowEqual from '@wordpress/is-shallow-equal'; | ||
|
||
export default ( mapNodeToProps ) => createHigherOrderComponent( | ||
export default ( mapNodeToProps, computeWhenChange ) => createHigherOrderComponent( | ||
( WrappedComponent ) => { | ||
return class extends Component { | ||
constructor() { | ||
|
@@ -19,6 +20,7 @@ export default ( mapNodeToProps ) => createHigherOrderComponent( | |
fallbackStyles: undefined, | ||
grabStylesCompleted: false, | ||
}; | ||
this.shouldRecomputeValues = this.shouldRecomputeValues().bind( this ); | ||
|
||
this.bindRef = this.bindRef.bind( this ); | ||
} | ||
|
@@ -38,9 +40,24 @@ export default ( mapNodeToProps ) => createHigherOrderComponent( | |
this.grabFallbackStyles(); | ||
} | ||
|
||
shouldRecomputeValues() { | ||
let lastRecomputeValues = []; | ||
return () => { | ||
if ( ! computeWhenChange ) { | ||
return ! this.state.grabStylesCompleted; | ||
} | ||
const newRecomputeValues = computeWhenChange( this.props ); | ||
if ( isShallowEqual( lastRecomputeValues, newRecomputeValues ) ) { | ||
return false; | ||
} | ||
lastRecomputeValues = newRecomputeValues; | ||
return true; | ||
}; | ||
} | ||
|
||
grabFallbackStyles() { | ||
const { grabStylesCompleted, fallbackStyles } = this.state; | ||
if ( this.nodeRef && ! grabStylesCompleted ) { | ||
const { fallbackStyles } = this.state; | ||
if ( this.nodeRef && this.shouldRecomputeValues() ) { | ||
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. Is It looks like it would solve the issue if we would avoid the local state in the first place. Otherwise, we need to find a way to reset it when the styles change outside, e.g. you apply a class name to the block or change its style variant. which is not taken into account in the proposed solution. |
||
const newFallbackStyles = mapNodeToProps( this.nodeRef, this.props ); | ||
if ( ! isEqual( newFallbackStyles, fallbackStyles ) ) { | ||
this.setState( { | ||
|
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.
It is a public API change so we would have to document it and ensure it is backward compatible. It increases the complexity to use this HOC as well.