Skip to content

Commit

Permalink
Add Contrast Checking
Browse files Browse the repository at this point in the history
  • Loading branch information
George Hotelling committed Apr 27, 2021
1 parent b615955 commit 81f006c
Showing 1 changed file with 82 additions and 2 deletions.
84 changes: 82 additions & 2 deletions packages/block-library/src/navigation/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import classnames from 'classnames';
/**
* WordPress dependencies
*/
import { useState } from '@wordpress/element';
import { useState, useEffect, Platform } from '@wordpress/element';
import {
InnerBlocks,
__experimentalUseInnerBlocksProps as useInnerBlocksProps,
Expand All @@ -17,6 +17,7 @@ import {
store as blockEditorStore,
withColors,
PanelColorSettings,
ContrastChecker,
} from '@wordpress/block-editor';
import { useDispatch, withSelect, withDispatch } from '@wordpress/data';
import { PanelBody, ToggleControl, ToolbarGroup } from '@wordpress/components';
Expand Down Expand Up @@ -44,6 +45,38 @@ const LAYOUT = {
alignments: [],
};

function getBlockDOMNode( clientId, doc ) {
return doc.getElementById( 'block-' + clientId );
}

function getComputedStyle( node ) {
return node.ownerDocument.defaultView.getComputedStyle( node );
}

function detectColors( clientId, setColor, setBackground ) {
const colorsDetectionElement = getBlockDOMNode( clientId, document );
if ( ! colorsDetectionElement ) {
return;
}
setColor( getComputedStyle( colorsDetectionElement ).color );

let backgroundColorNode = colorsDetectionElement;
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;
}

setBackground( backgroundColor );
}

function Navigation( {
selectedBlockHasDescendants,
attributes,
Expand All @@ -62,6 +95,7 @@ function Navigation( {
setOverlayBackgroundColor,
overlayTextColor,
setOverlayTextColor,
subMenuClientId,
hasSubmenuIndicatorSetting = true,
hasItemJustificationControls = true,
} ) {
Expand Down Expand Up @@ -118,6 +152,31 @@ function Navigation( {
}
);

// Turn on contrast checker for web only since it's not supported on mobile yet.
const enableContrastChecking = Platform.OS === 'web';

const [ detectedBackgroundColor, setDetectedBackgroundColor ] = useState();
const [ detectedColor, setDetectedColor ] = useState();
const [
detectedOverlayBackgroundColor,
setDetectedOverlayBackgroundColor,
] = useState();
const [ detectedOverlayColor, setDetectedOverlayColor ] = useState();

useEffect( () => {
if ( ! enableContrastChecking ) {
return;
}
detectColors( clientId, setDetectedColor, setDetectedBackgroundColor );
if ( subMenuClientId ) {
detectColors(
subMenuClientId,
setDetectedOverlayColor,
setDetectedOverlayBackgroundColor
);
}
} );

if ( isPlaceholderShown ) {
return (
<div { ...blockProps }>
Expand Down Expand Up @@ -174,6 +233,7 @@ function Navigation( {
) }
<PanelColorSettings
title={ __( 'Color' ) }
initialOpen={ false }
colorSettings={ [
{
value: textColor.color,
Expand All @@ -196,7 +256,22 @@ function Navigation( {
label: __( 'Overlay background' ),
},
] }
/>
>
{ enableContrastChecking && (
<>
<ContrastChecker
backgroundColor={ detectedBackgroundColor }
textColor={ detectedColor }
/>
<ContrastChecker
backgroundColor={
detectedOverlayBackgroundColor
}
textColor={ detectedOverlayColor }
/>
</>
) }
</PanelColorSettings>
</InspectorControls>
<nav { ...blockProps }>
<ul { ...innerBlocksProps } />
Expand All @@ -221,9 +296,14 @@ export default compose( [
const selectedBlockHasDescendants = !! getClientIdsOfDescendants( [
selectedBlockId,
] )?.length;

const subMenuClientId = innerBlocks.find(
( b ) => b.innerBlocks.length
)?.innerBlocks[ 0 ]?.clientId;
return {
isImmediateParentOfSelectedBlock,
selectedBlockHasDescendants,
subMenuClientId,
hasExistingNavItems: !! innerBlocks.length,
};
} ),
Expand Down

0 comments on commit 81f006c

Please sign in to comment.