From 1dfae203dccadd48b45f974a6b598ee87a669ca6 Mon Sep 17 00:00:00 2001 From: Ben Dwyer Date: Thu, 22 Jul 2021 10:03:18 +0100 Subject: [PATCH] Add an 'is-light' class to the cover block when it's saved with a light background --- packages/block-library/src/cover/block.json | 4 +++ packages/block-library/src/cover/edit.js | 39 +++++++++++++-------- packages/block-library/src/cover/save.js | 2 ++ packages/block-library/src/cover/style.scss | 5 +-- 4 files changed, 33 insertions(+), 17 deletions(-) diff --git a/packages/block-library/src/cover/block.json b/packages/block-library/src/cover/block.json index 3b3ad5d804417e..24a2d5aedae4c3 100644 --- a/packages/block-library/src/cover/block.json +++ b/packages/block-library/src/cover/block.json @@ -51,6 +51,10 @@ }, "contentPosition": { "type": "string" + }, + "isDark": { + "type": "boolean", + "default": true } }, "supports": { diff --git a/packages/block-library/src/cover/edit.js b/packages/block-library/src/cover/edit.js index 971eb0bdd94804..626161c892e0b1 100644 --- a/packages/block-library/src/cover/edit.js +++ b/packages/block-library/src/cover/edit.js @@ -209,18 +209,18 @@ function ResizableCover( { * color are set, dimRatio is used to decide what is used * for background darkness checking purposes. * @param {?string} overlayColor String containing the overlay color value if one exists. + * The default is black to match the CSS. * @param {?Object} elementRef If a media background is set, elementRef should contain a reference to a * dom element that renders that media. - * - * @return {boolean} True if the cover background is considered "dark" and false otherwise. + * @param {?Function} setAttributes Function to set attributes on the block. */ function useCoverIsDark( url, dimRatio = 50, overlayColor = '#000000', - elementRef + elementRef, + setAttributes ) { - const [ isDark, setIsDark ] = useState( false ); useEffect( () => { // If opacity is lower than 50 the dominant color is the image or video color, // so use that color for the dark mode computation. @@ -228,30 +228,37 @@ function useCoverIsDark( retrieveFastAverageColor().getColorAsync( elementRef.current, ( color ) => { - setIsDark( color.isDark ); + setAttributes( { + isDark: color.isDark, + } ); } ); } - }, [ url, url && dimRatio <= 50 && elementRef.current, setIsDark ] ); + }, [ url, url && dimRatio <= 50 && elementRef.current, setAttributes ] ); useEffect( () => { // If opacity is greater than 50 the dominant color is the overlay color, // so use that color for the dark mode computation. if ( dimRatio > 50 || ! url ) { if ( ! overlayColor ) { // If no overlay color exists the overlay color is black (isDark ) - setIsDark( true ); + setAttributes( { + isDark: true, + } ); return; } - setIsDark( tinycolor( overlayColor ).isDark() ); + setAttributes( { + isDark: tinycolor( overlayColor ).isDark(), + } ); } - }, [ overlayColor, dimRatio > 50 || ! url, setIsDark ] ); + }, [ overlayColor, dimRatio > 50 || ! url, setAttributes ] ); useEffect( () => { if ( ! url && ! overlayColor ) { // Reset isDark - setIsDark( false ); + setAttributes( { + isDark: false, + } ); } - }, [ ! url && ! overlayColor, setIsDark ] ); - return isDark; + }, [ ! url && ! overlayColor, setAttributes ] ); } function mediaPosition( { x, y } ) { @@ -324,6 +331,7 @@ function CoverEdit( { minHeightUnit, style: styleAttribute, url, + isDark, } = attributes; const { gradientClass, @@ -380,11 +388,12 @@ function CoverEdit( { }; const isDarkElement = useRef(); - const isDark = useCoverIsDark( + useCoverIsDark( url, dimRatio, overlayColor.color, - isDarkElement + isDarkElement, + setAttributes ); const isImageBackground = IMAGE_BACKGROUND_TYPE === backgroundType; @@ -609,7 +618,7 @@ function CoverEdit( { const classes = classnames( dimRatioToClass( dimRatio ), { - 'is-dark-theme': isDark, + 'is-light': ! isDark, 'has-background-dim': dimRatio !== 0, 'is-transient': isUploadingMedia, 'has-parallax': hasParallax, diff --git a/packages/block-library/src/cover/save.js b/packages/block-library/src/cover/save.js index d9f1bb0def0323..60022dea459e10 100644 --- a/packages/block-library/src/cover/save.js +++ b/packages/block-library/src/cover/save.js @@ -41,6 +41,7 @@ export default function save( { attributes } ) { id, minHeight: minHeightProp, minHeightUnit, + isDark, } = attributes; const overlayColorClass = getColorClassName( 'background-color', @@ -75,6 +76,7 @@ export default function save( { attributes } ) { dimRatioToClass( dimRatio ), overlayColorClass, { + 'is-light': ! isDark, 'has-background-dim': dimRatio !== 0, 'has-parallax': hasParallax, 'is-repeated': isRepeated, diff --git a/packages/block-library/src/cover/style.scss b/packages/block-library/src/cover/style.scss index af8ec7f3901a61..b9a794fd807158 100644 --- a/packages/block-library/src/cover/style.scss +++ b/packages/block-library/src/cover/style.scss @@ -102,14 +102,15 @@ } .wp-block-cover__inner-container { + color: $white; width: 100%; z-index: z-index(".wp-block-cover__inner-container"); } - &.is-dark-theme { + &.is-light { .wp-block-cover__inner-container { - color: $white; + color: $black; } }