-
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
Block Editor: Fix block color contrast checker #68799
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,60 +1,94 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useState, useEffect } from '@wordpress/element'; | ||
import { useLayoutEffect, useReducer } from '@wordpress/element'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import ContrastChecker from '../components/contrast-checker'; | ||
import { useBlockElement } from '../components/block-list/use-block-props/use-block-refs'; | ||
|
||
function getComputedStyle( node ) { | ||
return node.ownerDocument.defaultView.getComputedStyle( node ); | ||
function getComputedValue( node, property ) { | ||
return node.ownerDocument.defaultView | ||
.getComputedStyle( node ) | ||
.getPropertyValue( property ); | ||
} | ||
|
||
function getBlockElementColors( blockEl ) { | ||
if ( ! blockEl ) { | ||
return {}; | ||
} | ||
|
||
const firstLinkElement = blockEl.querySelector( 'a' ); | ||
const linkColor = !! firstLinkElement?.innerText | ||
? getComputedValue( firstLinkElement, 'color' ) | ||
: undefined; | ||
|
||
const textColor = getComputedValue( blockEl, 'color' ); | ||
|
||
let backgroundColorNode = blockEl; | ||
let backgroundColor = getComputedValue( | ||
backgroundColorNode, | ||
'background-color' | ||
); | ||
while ( | ||
backgroundColor === 'rgba(0, 0, 0, 0)' && | ||
backgroundColorNode.parentNode && | ||
backgroundColorNode.parentNode.nodeType === | ||
backgroundColorNode.parentNode.ELEMENT_NODE | ||
) { | ||
backgroundColorNode = backgroundColorNode.parentNode; | ||
backgroundColor = getComputedValue( | ||
backgroundColorNode, | ||
'background-color' | ||
); | ||
} | ||
|
||
return { | ||
textColor, | ||
backgroundColor, | ||
linkColor, | ||
}; | ||
} | ||
|
||
function reducer( prevColors, newColors ) { | ||
const hasChanged = Object.keys( newColors ).some( | ||
( key ) => prevColors[ key ] !== newColors[ key ] | ||
); | ||
|
||
// Do not re-render if the colors have not changed. | ||
return hasChanged ? newColors : prevColors; | ||
} | ||
|
||
export default function BlockColorContrastChecker( { clientId } ) { | ||
const [ detectedBackgroundColor, setDetectedBackgroundColor ] = useState(); | ||
const [ detectedColor, setDetectedColor ] = useState(); | ||
const [ detectedLinkColor, setDetectedLinkColor ] = useState(); | ||
const blockEl = useBlockElement( clientId ); | ||
const [ colors, setColors ] = useReducer( reducer, {} ); | ||
|
||
// There are so many things that can change the color of a block | ||
// So we perform this check on every render. | ||
useEffect( () => { | ||
useLayoutEffect( () => { | ||
if ( ! blockEl ) { | ||
return; | ||
} | ||
setDetectedColor( getComputedStyle( blockEl ).color ); | ||
|
||
const firstLinkElement = blockEl.querySelector( 'a' ); | ||
if ( firstLinkElement && !! firstLinkElement.innerText ) { | ||
setDetectedLinkColor( getComputedStyle( firstLinkElement ).color ); | ||
} | ||
|
||
let backgroundColorNode = blockEl; | ||
let backgroundColor = | ||
getComputedStyle( backgroundColorNode ).backgroundColor; | ||
while ( | ||
backgroundColor === 'rgba(0, 0, 0, 0)' && | ||
backgroundColorNode.parentNode && | ||
backgroundColorNode.parentNode.nodeType === | ||
backgroundColorNode.parentNode.ELEMENT_NODE | ||
) { | ||
backgroundColorNode = backgroundColorNode.parentNode; | ||
backgroundColor = | ||
getComputedStyle( backgroundColorNode ).backgroundColor; | ||
function updateColors() { | ||
setColors( getBlockElementColors( blockEl ) ); | ||
} | ||
|
||
setDetectedBackgroundColor( backgroundColor ); | ||
}, [ blockEl ] ); | ||
// Combine `useLayoutEffect` and two rAF calls to ensure that values are read | ||
// after the current paint but before the next paint. | ||
window.requestAnimationFrame( () => | ||
window.requestAnimationFrame( updateColors ) | ||
); | ||
} ); | ||
|
||
return ( | ||
<ContrastChecker | ||
backgroundColor={ detectedBackgroundColor } | ||
textColor={ detectedColor } | ||
backgroundColor={ colors.backgroundColor } | ||
textColor={ colors.textColor } | ||
linkColor={ colors.linkColor } | ||
enableAlphaChecker | ||
linkColor={ detectedLinkColor } | ||
/> | ||
); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This is the actual fix. The rest is just moving code around for readability.
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 seems to me that the actual fix is removing the
[ blockEl ]
dependency array from the effect. Then it runs on every render, not only onblockEl
change (the block's DOM element hardly ever changes), and catches all the color updates.Running after the current paint but before the next paint is exactly what
useEffect
does. It runs when the rendered DOM is already painted. You shouldn't need to simulate it with layout effect + rAF.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.
@jsnajdr, the component had an issue before adding
blockEl
as a dependency. It didn't always work, and the computed properties fromblockEl
were stale.I would like to avoid it as well, but this the only solution that works.
Quoting from: #59227.
This is probably a better summary for React 18 - facebook/react#24409 (comment).