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

ContrastChecker: Add font size logic inside the component; Add additional test cases; #8059

Merged
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
2 changes: 1 addition & 1 deletion core-blocks/paragraph/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -248,10 +248,10 @@ class ParagraphBlock extends Component {
textColor={ textColor.value }
backgroundColor={ backgroundColor.value }
{ ...{
fontSize,
fallbackBackgroundColor,
fallbackTextColor,
} }
isLargeText={ fontSize >= 18 }
/>
</InspectorControls>
<RichText
Expand Down
11 changes: 9 additions & 2 deletions editor/components/contrast-checker/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,14 @@ import { Notice } from '@wordpress/components';
*/
import './style.scss';

function ContrastChecker( { backgroundColor, textColor, isLargeText, fallbackBackgroundColor, fallbackTextColor } ) {
function ContrastChecker( {
backgroundColor,
fallbackBackgroundColor,
fallbackTextColor,
fontSize,
isLargeText,
textColor,
} ) {
if ( ! ( backgroundColor || fallbackBackgroundColor ) || ! ( textColor || fallbackTextColor ) ) {
return null;
}
Expand All @@ -25,7 +32,7 @@ function ContrastChecker( { backgroundColor, textColor, isLargeText, fallbackBac
if ( hasTransparency || tinycolor.isReadable(
tinyBackgroundColor,
tinyTextColor,
{ level: 'AA', size: ( isLargeText ? 'large' : 'small' ) }
{ level: 'AA', size: ( isLargeText || ( isLargeText !== false && fontSize >= 18 ) ? 'large' : 'small' ) }
) ) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,79 @@ exports[`ContrastChecker should render messages when the textColor is valid, but
</div>
</ContrastChecker>
`;

exports[`ContrastChecker should take into consideration the font size passed 1`] = `
<ContrastChecker
backgroundColor="#C44B4B"
fontSize={17}
textColor="#000000"
>
<div
className="editor-contrast-checker"
>
<Notice
isDismissible={false}
status="warning"
>
<div
className="notice notice-alt notice-warning"
>
<p>
This color combination may be hard for people to read. Try using a brighter background color and/or a darker text color.
</p>
</div>
</Notice>
</div>
</ContrastChecker>
`;

exports[`ContrastChecker should take into consideration wherever text is large or not 1`] = `
<ContrastChecker
backgroundColor="#C44B4B"
isLargeText={false}
textColor="#000000"
>
<div
className="editor-contrast-checker"
>
<Notice
isDismissible={false}
status="warning"
>
<div
className="notice notice-alt notice-warning"
>
<p>
This color combination may be hard for people to read. Try using a brighter background color and/or a darker text color.
</p>
</div>
</Notice>
</div>
</ContrastChecker>
`;

exports[`ContrastChecker should use isLargeText to make decisions if both isLargeText and fontSize props are passed 1`] = `
<ContrastChecker
backgroundColor="#C44B4B"
fontSize={18}
isLargeText={false}
textColor="#000000"
>
<div
className="editor-contrast-checker"
>
<Notice
isDismissible={false}
status="warning"
>
<div
className="notice notice-alt notice-warning"
>
<p>
This color combination may be hard for people to read. Try using a brighter background color and/or a darker text color.
</p>
</div>
</Notice>
</div>
</ContrastChecker>
`;
68 changes: 68 additions & 0 deletions editor/components/contrast-checker/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,74 @@ describe( 'ContrastChecker', () => {
expect( componentWrapper ).toMatchSnapshot();
} );

test( 'should take into consideration wherever text is large or not', () => {
const componentWrapperSmallText = mount(
<ContrastChecker
backgroundColor="#C44B4B"
textColor="#000000"
isLargeText={ false }
/>
);

expect( componentWrapperSmallText ).toMatchSnapshot();

const componentWrapperLargeText = mount(
<ContrastChecker
backgroundColor="#C44B4B"
textColor="#000000"
isLargeText={ true }
/>
);

expect( componentWrapperLargeText.html() ).toBeNull();
} );

test( 'should take into consideration the font size passed', () => {
const componentWrapperSmallFontSize = mount(
<ContrastChecker
backgroundColor="#C44B4B"
textColor="#000000"
fontSize={ 17 }
/>
);

expect( componentWrapperSmallFontSize ).toMatchSnapshot();

const componentWrapperLargeText = mount(
<ContrastChecker
backgroundColor="#C44B4B"
textColor="#000000"
fontSize={ 18 }
/>
);

expect( componentWrapperLargeText.html() ).toBeNull();
} );

test( 'should use isLargeText to make decisions if both isLargeText and fontSize props are passed', () => {
const componentWrapper = mount(
<ContrastChecker
backgroundColor="#C44B4B"
textColor="#000000"
fontSize={ 17 }
isLargeText={ true }
/>
);

expect( componentWrapper.html() ).toBeNull();

const componentWrapperNoLargeText = mount(
<ContrastChecker
backgroundColor="#C44B4B"
textColor="#000000"
fontSize={ 18 }
isLargeText={ false }
/>
);

expect( componentWrapperNoLargeText ).toMatchSnapshot();
} );

test( 'should render null when the colors meet AA WCAG guidelines, with only fallback colors.', () => {
const componentWrapper = mount(
<ContrastChecker
Expand Down