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

Improve color contrast handling #100

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@
"Gutenberg"
],
"dependencies": {
"@glidejs/glide": "^3.4.1",
"classnames": "^2.2.6",
"@glidejs/glide": "^3.4.1"
"fast-average-color": "^6.0.2"
},
"devDependencies": {
"@webdevstudios/css-coding-standards": "^1.0.1",
Expand Down
30 changes: 27 additions & 3 deletions src/blocks/carousel-slide/components/Settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ import {
InspectorControls,
PanelColorSettings,
} from '@wordpress/block-editor';
import { Platform } from '@wordpress/element';
import { Platform, useRef } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
import BackgroundSettingsPanel from '../../../utils/components/background-settings-panel';
import OverlayPanel from '../../../utils/components/overlay-panel';
import useMediaAverageColor from '../../../utils/hooks/use-media-average-color';

/**
* The Settings component displays settings for the Slide block via Inspector Controls.
Expand All @@ -32,9 +33,31 @@ export default function Settings( props ) {
setAttributes,
} = props;

// `ref` to background media element.
const mediaElementRef = useRef();

// Average color of background media element.
const { color: mediaColor } = useMediaAverageColor(
mediaElementRef?.current
);

// Whether media element is currently set as background.
const hasMediaBackground =
'image' === backgroundType || 'video' === backgroundType;

// Determine background color to contrast with `fontColor`.
let backgroundContrastColor = backgroundColor;

if ( hasMediaBackground && overlayOpacity && overlayOpacity > 50 ) {
// Use overlay color as background contrast if above 50% opacity.
backgroundContrastColor = overlayColor;
} else if ( hasMediaBackground ) {
// Use media average color if overlay is not set or opacity is <= 50%.
backgroundContrastColor = mediaColor?.hex
? mediaColor.hex
: backgroundContrastColor;
}

return (
<InspectorControls>
<PanelColorSettings
Expand All @@ -47,9 +70,9 @@ export default function Settings( props ) {
},
] }
>
{ 'web' === Platform.OS && 'color' === backgroundType && (
{ 'web' === Platform.OS && (
<ContrastChecker
backgroundColor={ backgroundColor }
backgroundColor={ backgroundContrastColor }
textColor={ fontColor }
/>
) }
Expand All @@ -73,6 +96,7 @@ export default function Settings( props ) {
backgroundVideo: value,
} )
}
ref={ mediaElementRef }
/>
{ hasMediaBackground && (
<OverlayPanel
Expand Down
19 changes: 11 additions & 8 deletions src/blocks/carousel-slide/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ function Edit( props ) {
attributes,
className,
setAttributes,
isSelected,
fontColor,
setFontColor,
backgroundColor,
Expand Down Expand Up @@ -50,14 +51,16 @@ function Edit( props ) {

return (
<>
<Settings
{ ...backgroundProps }
fontColor={ fontColor.color }
setFontColor={ setFontColor }
setBackgroundColor={ setBackgroundColor }
setOverlayColor={ setOverlayColor }
setAttributes={ setAttributes }
/>
{ isSelected && (
<Settings
{ ...backgroundProps }
fontColor={ fontColor.color }
setFontColor={ setFontColor }
setBackgroundColor={ setBackgroundColor }
setOverlayColor={ setOverlayColor }
setAttributes={ setAttributes }
/>
) }
<Slide { ...slideProps }>
<InnerBlocks
{ ...INNER_BLOCKS_PROPS }
Expand Down
10 changes: 9 additions & 1 deletion src/utils/components/background-settings-panel/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { PanelBody, SelectControl } from '@wordpress/components';
import { forwardRef, useRef } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
import ColorPaletteControl from '../color-palette-control';
import MediaControl from '../media-control';
Expand All @@ -10,9 +11,10 @@ import MediaControl from '../media-control';
* @since 2.1.0
*
* @param {Object} [props] Properties passed to the component.
* @param {Object} [ref] `ref` to inner media background component.
* @return {Element} Element to render.
*/
export default function BackgroundSettingsPanel( props ) {
function BackgroundSettingsPanel( props, ref ) {
const {
backgroundType,
setBackgroundType,
Expand All @@ -29,6 +31,8 @@ export default function BackgroundSettingsPanel( props ) {
setBackgroundVideo,
} = props;

const fallbackRef = useRef();

// Define background type options.
const options = [ { label: __( 'None', 'wdsblocks' ), value: 'none' } ];

Expand Down Expand Up @@ -76,6 +80,7 @@ export default function BackgroundSettingsPanel( props ) {
label={ __( 'Background image', 'wdsblocks' ) }
addLabel={ __( 'Add image', 'wdsblocks' ) }
removeLabel={ __( 'Remove image', 'wdsblocks' ) }
ref={ ref || fallbackRef }
/>
) }

Expand All @@ -87,8 +92,11 @@ export default function BackgroundSettingsPanel( props ) {
label={ __( 'Background video', 'wdsblocks' ) }
addLabel={ __( 'Add video', 'wdsblocks' ) }
removeLabel={ __( 'Remove video', 'wdsblocks' ) }
ref={ ref || fallbackRef }
/>
) }
</PanelBody>
);
}

export default forwardRef( BackgroundSettingsPanel );
22 changes: 18 additions & 4 deletions src/utils/components/media-control/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { MediaUpload, MediaUploadCheck } from '@wordpress/block-editor';
import { Button, ResponsiveWrapper } from '@wordpress/components';
import { forwardRef, useRef } from '@wordpress/element';

/**
* Display media preview according to type.
Expand All @@ -8,9 +9,10 @@ import { Button, ResponsiveWrapper } from '@wordpress/components';
* @since 2.1.0
*
* @param {Object} [props] Properties passed to the component.
* @param {Object} [ref] `ref` to media component.
* @return {Element} Media preview.
*/
function Preview( props ) {
const Preview = forwardRef( ( props, ref ) => {
const { type, media, label } = props;
const className = 'components-responsive-wrapper__content';

Expand All @@ -23,17 +25,23 @@ function Preview( props ) {
muted
loop
aria-hidden="true"
ref={ ref }
>
<source src={ media?.url } type={ media?.mime } />
</video>
);

default:
return (
<img className={ className } src={ media?.url } alt={ label } />
<img
className={ className }
src={ media?.url }
alt={ label }
ref={ ref }
/>
);
}
}
} );

/**
* The MediaControl component displays a control to select a media item and display a preview.
Expand All @@ -42,9 +50,10 @@ function Preview( props ) {
* @since 2.1.0
*
* @param {Object} [props] Properties passed to the component.
* @param {Object} [ref] `ref` to media component.
* @return {Element} Element to render.
*/
export default function MediaControl( props ) {
function MediaControl( props, ref ) {
const {
media,
setMedia,
Expand All @@ -55,6 +64,8 @@ export default function MediaControl( props ) {
removeLabel,
} = props;

const fallbackRef = useRef();

return (
<>
<div className="components-base-control">
Expand Down Expand Up @@ -86,6 +97,7 @@ export default function MediaControl( props ) {
media={ media }
label={ label }
type={ type }
ref={ ref || fallbackRef }
/>
</ResponsiveWrapper>
) }
Expand All @@ -108,3 +120,5 @@ export default function MediaControl( props ) {
</>
);
}

export default forwardRef( MediaControl );
43 changes: 43 additions & 0 deletions src/utils/hooks/use-media-average-color/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import FastAverageColor from 'fast-average-color';
import { useEffect, useState } from 'react';

/**
* Retrieve current or new instance of `FastAverageColor`.
*
* @author WebDevStudios
* @since 2.1.0
*
* @return {Object} Instance of `FastAverageColor`.
*/
function retrieveFastAverageColor() {
if ( ! retrieveFastAverageColor.fastAverageColor ) {
retrieveFastAverageColor.fastAverageColor = new FastAverageColor();
}

return retrieveFastAverageColor.fastAverageColor;
}

/**
* The useMediaAverageColor hook handles state for determining the average color of a given media element by URL.
*
* @author WebDevStudios
* @since 2.1.0
*
* @param {?Element} mediaElement Media element to process.
* @return {Object} Object consisting of state variable.
*/
export default function useMediaAverageColor( mediaElement ) {
const [ color, setColor ] = useState( null );

useEffect( () => {
if ( ! mediaElement ) {
setColor( null );
return;
}

// Attempt to retrieve average color.
setColor( retrieveFastAverageColor().getColor( mediaElement ) );
}, [ mediaElement ] );

return { color };
}