@wordpress/block-editor
Version:
8 lines (7 loc) • 6.17 kB
Source Map (JSON)
{
"version": 3,
"sources": ["../../src/hooks/contrast-checker.js"],
"sourcesContent": ["/**\n * WordPress dependencies\n */\nimport { useLayoutEffect, useReducer } from '@wordpress/element';\nimport { useSelect } from '@wordpress/data';\nimport { store as blocksStore } from '@wordpress/blocks';\nimport { getBlockSelector } from '@wordpress/global-styles-engine';\n\n/**\n * Internal dependencies\n */\nimport ContrastChecker from '../components/contrast-checker';\nimport { useBlockElement } from '../components/block-list/use-block-props/use-block-refs';\n\nfunction getComputedValue( node, property ) {\n\treturn node.ownerDocument.defaultView\n\t\t.getComputedStyle( node )\n\t\t.getPropertyValue( property );\n}\n\nfunction getBlockElementColors( blockEl, blockType ) {\n\tif ( ! blockEl || ! blockType ) {\n\t\treturn {};\n\t}\n\n\t// Get color-specific selectors.\n\tconst textSelector = getBlockSelector( blockType, 'color.text', {\n\t\tfallback: true,\n\t} );\n\tconst backgroundSelector = getBlockSelector(\n\t\tblockType,\n\t\t'color.background',\n\t\t{ fallback: true }\n\t);\n\n\t// Find target elements - querySelector handles all the complexity\n\tconst textElement = blockEl.querySelector( textSelector ) || blockEl;\n\tconst backgroundElement =\n\t\tblockEl.querySelector( backgroundSelector ) || blockEl;\n\tconst linkElement = blockEl.querySelector( 'a' );\n\n\t// Get computed colors from the appropriate elements\n\tconst textColor = getComputedValue( textElement, 'color' );\n\tconst linkColor =\n\t\tlinkElement && linkElement.textContent\n\t\t\t? getComputedValue( linkElement, 'color' )\n\t\t\t: undefined;\n\n\tlet backgroundColorNode = backgroundElement;\n\tlet backgroundColor = getComputedValue(\n\t\tbackgroundColorNode,\n\t\t'background-color'\n\t);\n\twhile (\n\t\tbackgroundColor === 'rgba(0, 0, 0, 0)' &&\n\t\tbackgroundColorNode.parentNode &&\n\t\tbackgroundColorNode.parentNode.nodeType ===\n\t\t\tbackgroundColorNode.parentNode.ELEMENT_NODE\n\t) {\n\t\tbackgroundColorNode = backgroundColorNode.parentNode;\n\t\tbackgroundColor = getComputedValue(\n\t\t\tbackgroundColorNode,\n\t\t\t'background-color'\n\t\t);\n\t}\n\n\treturn {\n\t\ttextColor,\n\t\tbackgroundColor,\n\t\tlinkColor,\n\t};\n}\n\nfunction reducer( prevColors, newColors ) {\n\tconst hasChanged = Object.keys( newColors ).some(\n\t\t( key ) => prevColors[ key ] !== newColors[ key ]\n\t);\n\n\t// Do not re-render if the colors have not changed.\n\treturn hasChanged ? newColors : prevColors;\n}\n\nexport default function BlockColorContrastChecker( { clientId, name } ) {\n\tconst blockEl = useBlockElement( clientId );\n\tconst [ colors, setColors ] = useReducer( reducer, {} );\n\n\tconst blockType = useSelect(\n\t\t( select ) => {\n\t\t\treturn name\n\t\t\t\t? select( blocksStore ).getBlockType( name )\n\t\t\t\t: undefined;\n\t\t},\n\t\t[ name ]\n\t);\n\n\t// There are so many things that can change the color of a block\n\t// So we perform this check on every render.\n\tuseLayoutEffect( () => {\n\t\tif ( ! blockEl || ! blockType ) {\n\t\t\treturn;\n\t\t}\n\n\t\t// Combine `useLayoutEffect` and two rAF calls to ensure that values are read\n\t\t// after the current paint but before the next paint.\n\t\twindow.requestAnimationFrame( () =>\n\t\t\twindow.requestAnimationFrame( () =>\n\t\t\t\tsetColors( getBlockElementColors( blockEl, blockType ) )\n\t\t\t)\n\t\t);\n\t} );\n\n\t// Runs in its own effect with dependencies so the observer is only\n\t// recreated when the block element or block type changes.\n\tuseLayoutEffect( () => {\n\t\tif ( ! blockEl || ! blockType ) {\n\t\t\treturn;\n\t\t}\n\n\t\tconst observer = new window.MutationObserver( () => {\n\t\t\tsetColors( getBlockElementColors( blockEl, blockType ) );\n\t\t} );\n\n\t\tobserver.observe( blockEl, {\n\t\t\tattributes: true,\n\t\t\tattributeFilter: [ 'class', 'style' ],\n\t\t} );\n\n\t\treturn () => {\n\t\t\tobserver.disconnect();\n\t\t};\n\t}, [ blockEl, blockType ] );\n\n\treturn (\n\t\t<ContrastChecker\n\t\t\tbackgroundCol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