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

Try: Contrast checker - Add observer to provide a clear message for the current color selection. #67036

Open
wants to merge 1 commit into
base: trunk
Choose a base branch
from
Open
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
92 changes: 71 additions & 21 deletions packages/block-editor/src/hooks/contrast-checker.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,40 +13,90 @@ function getComputedStyle( node ) {
return node.ownerDocument.defaultView.getComputedStyle( node );
}

function detectColors( blockEl ) {
if ( ! blockEl ) {
return;
}

const firstLinkElement = blockEl.querySelector( 'a' );
const linkColor =
firstLinkElement && !! firstLinkElement.innerText
? getComputedStyle( firstLinkElement ).color
: null;

const textColor = getComputedStyle( blockEl ).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;
}

return {
textColor,
backgroundColor,
linkColor,
};
}

function createStyleObserver( node, callback ) {
// Watch for changes to style-related attributes
const observer = new MutationObserver( ( mutations ) => {
const hasStyleChanges = mutations.some(
( mutation ) => mutation.attributeName === 'style'
);

if ( hasStyleChanges ) {
const computedStyle =
node.ownerDocument.defaultView.getComputedStyle( node );
callback( computedStyle );
}
} );

// Observe style-related changes
observer.observe( node, {
attributeFilter: [ 'style' ],
} );

return observer;
}

export default function BlockColorContrastChecker( { clientId } ) {
const [ detectedBackgroundColor, setDetectedBackgroundColor ] = useState();
const [ detectedColor, setDetectedColor ] = useState();
const [ detectedLinkColor, setDetectedLinkColor ] = useState();
const blockEl = useBlockElement( clientId );

// There are so many things that can change the color of a block
// So we perform this check on every render.
useEffect( () => {
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;
let colors = detectColors( blockEl );
setDetectedColor( colors.textColor );
setDetectedBackgroundColor( colors.backgroundColor );
if ( colors.linkColor ) {
setDetectedLinkColor( colors.linkColor );
}

setDetectedBackgroundColor( backgroundColor );
const observer = createStyleObserver( blockEl, () => {
colors = detectColors( blockEl );
setDetectedColor( colors.textColor );
setDetectedBackgroundColor( colors.backgroundColor );
if ( colors.linkColor ) {
setDetectedLinkColor( colors.linkColor );
}
} );
// Cleanup
return () => observer.disconnect();
}, [ blockEl ] );

return (
Expand Down
Loading