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 3 commits 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
87 changes: 67 additions & 20 deletions packages/block-editor/src/hooks/contrast-checker.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,40 +13,87 @@ function getComputedStyle( node ) {
return node.ownerDocument.defaultView.getComputedStyle( node );
}

function getBlockColors( 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 window.MutationObserver( ( mutations ) => {
const hasStyleChanges = mutations.some(
( mutation ) =>
mutation.attributeName === 'style' ||
mutation.attributeName === 'class'
);

if ( hasStyleChanges ) {
callback();
}
} );

observer.observe( node, {
attributeFilter: [ 'style', 'class' ],
} );

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 );
function updateContrastChecker() {
const colors = getBlockColors( blockEl );
setDetectedColor( colors.textColor );
setDetectedBackgroundColor( colors.backgroundColor );
if ( colors.linkColor ) {
setDetectedLinkColor( colors.linkColor );
}
}

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;
}
// Check colors on mount.
updateContrastChecker();

const observer = createStyleObserver( blockEl, updateContrastChecker );

setDetectedBackgroundColor( backgroundColor );
return () => observer.disconnect();
}, [ blockEl ] );

return (
Expand Down
Loading