UNPKG

@wordpress/block-editor

Version:
8 lines (7 loc) 3.95 kB
{ "version": 3, "sources": ["../../src/hooks/contrast-checker.js"], "sourcesContent": ["/**\n * WordPress dependencies\n */\nimport { useLayoutEffect, useReducer } from '@wordpress/element';\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 ) {\n\tif ( ! blockEl ) {\n\t\treturn {};\n\t}\n\n\tconst firstLinkElement = blockEl.querySelector( 'a' );\n\tconst linkColor = !! firstLinkElement?.innerText\n\t\t? getComputedValue( firstLinkElement, 'color' )\n\t\t: undefined;\n\n\tconst textColor = getComputedValue( blockEl, 'color' );\n\n\tlet backgroundColorNode = blockEl;\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 } ) {\n\tconst blockEl = useBlockElement( clientId );\n\tconst [ colors, setColors ] = useReducer( reducer, {} );\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 ) {\n\t\t\treturn;\n\t\t}\n\n\t\tfunction updateColors() {\n\t\t\tsetColors( getBlockElementColors( blockEl ) );\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( updateColors )\n\t\t);\n\t} );\n\n\treturn (\n\t\t<ContrastChecker\n\t\t\tbackgroundColor={ colors.backgroundColor }\n\t\t\ttextColor={ colors.textColor }\n\t\t\tlinkColor={ colors.linkColor }\n\t\t\tenableAlphaChecker\n\t\t/>\n\t);\n}\n"], "mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,qBAA4C;AAK5C,8BAA4B;AAC5B,4BAAgC;AA6E9B;AA3EF,SAAS,iBAAkB,MAAM,UAAW;AAC3C,SAAO,KAAK,cAAc,YACxB,iBAAkB,IAAK,EACvB,iBAAkB,QAAS;AAC9B;AAEA,SAAS,sBAAuB,SAAU;AACzC,MAAK,CAAE,SAAU;AAChB,WAAO,CAAC;AAAA,EACT;AAEA,QAAM,mBAAmB,QAAQ,cAAe,GAAI;AACpD,QAAM,YAAY,CAAC,CAAE,kBAAkB,YACpC,iBAAkB,kBAAkB,OAAQ,IAC5C;AAEH,QAAM,YAAY,iBAAkB,SAAS,OAAQ;AAErD,MAAI,sBAAsB;AAC1B,MAAI,kBAAkB;AAAA,IACrB;AAAA,IACA;AAAA,EACD;AACA,SACC,oBAAoB,sBACpB,oBAAoB,cACpB,oBAAoB,WAAW,aAC9B,oBAAoB,WAAW,cAC/B;AACD,0BAAsB,oBAAoB;AAC1C,sBAAkB;AAAA,MACjB;AAAA,MACA;AAAA,IACD;AAAA,EACD;AAEA,SAAO;AAAA,IACN;AAAA,IACA;AAAA,IACA;AAAA,EACD;AACD;AAEA,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