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

Fix button block contrast checker #41294

Draft
wants to merge 3 commits into
base: trunk
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: bugfix

Button: Fix contrast checker when comparing a user specified color with a css provided color.

This file was deleted.

9 changes: 6 additions & 3 deletions projects/plugins/jetpack/extensions/blocks/button/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import { compose } from '@wordpress/compose';
import { __ } from '@wordpress/i18n';
import clsx from 'clsx';
import useWidth from '../../shared/use-width';
import applyFallbackStyles from './apply-fallback-styles';
import { IS_GRADIENT_AVAILABLE } from './constants';
import ButtonControls from './controls';
import useFallbackColors from './use-fallback-colors';
import usePassthroughAttributes from './use-passthrough-attributes';
import './editor.scss';

Expand Down Expand Up @@ -41,6 +41,8 @@ export function ButtonEdit( props ) {
className: clsx( 'wp-block-button', className ),
} );

const [ fallbackColors, textRef ] = useFallbackColors();

const buttonClasses = clsx( 'wp-block-button__link', {
'has-background': backgroundColor.color || gradientValue,
[ backgroundColor.class ]: ! gradientValue && backgroundColor.class,
Expand Down Expand Up @@ -71,6 +73,7 @@ export function ButtonEdit( props ) {
disableLineBreaks={ 'input' === element }
onChange={ value => setAttributes( { text: value } ) }
placeholder={ placeholder || __( 'Add text…', 'jetpack' ) }
ref={ textRef }
style={ buttonStyles }
value={ text }
withoutInteractiveFormatting
Expand All @@ -81,6 +84,7 @@ export function ButtonEdit( props ) {
gradientValue,
setGradient,
isGradientAvailable: IS_GRADIENT_AVAILABLE,
...fallbackColors,
...props,
} }
/>
Expand All @@ -90,6 +94,5 @@ export function ButtonEdit( props ) {
}

export default compose(
withColors( { backgroundColor: 'background-color' }, { textColor: 'color' } ),
applyFallbackStyles
withColors( { backgroundColor: 'background-color' }, { textColor: 'color' } )
)( ButtonEdit );
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { useRefEffect } from '@wordpress/compose';
import { useState } from '@wordpress/element';

export default function useFallbackColors() {
const [ fallbacks, setFallbacks ] = useState();

const ref = useRefEffect( node => {
const observer = new MutationObserver( () => {
const fallbackBackgroundColor = getComputedStyle( node ).backgroundColor;
const fallbackTextColor = getComputedStyle( node ).color;

// Don't update the fallback colors if they haven't changed.
if (
fallbackBackgroundColor === fallbacks?.fallbackBackgroundColor &&
fallbackTextColor === fallbacks?.fallbackTextColor
) {
return;
}

setFallbacks( {
fallbackBackgroundColor,
fallbackTextColor,
} );
} );

observer.observe( node, {
attributeFilter: [ 'style', 'class' ],
} );

return () => {
observer.disconnect();
};
}, [] );

return [ fallbacks, ref ];
}
Loading