Repository navigation
Theme: Document accessibility responsibilities #79943
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
141 changes: 141 additions & 0 deletions
141
packages/theme/src/test/semantic-color-contrast.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,141 @@ | ||
| import { renderHook } from '@testing-library/react'; | ||
| import { getContrast } from '../color-ramps/lib/color-utils'; | ||
| import { useThemeProviderStyles } from '../use-theme-provider-styles'; | ||
|
|
||
| const MINIMUM_TEXT_CONTRAST = 4.5; | ||
| const CUSTOM_PRIMARY = '#0057b8'; | ||
| const CUSTOM_BACKGROUND = '#f6f3ef'; | ||
|
|
||
| const THEME_PROVIDER_STYLE_CASES = [ | ||
| { | ||
| name: 'default seed colors', | ||
| settings: undefined, | ||
| }, | ||
| { | ||
| name: 'custom seed colors', | ||
| settings: { | ||
| color: { | ||
| primary: CUSTOM_PRIMARY, | ||
| background: CUSTOM_BACKGROUND, | ||
| }, | ||
| }, | ||
| }, | ||
| ] as const; | ||
|
|
||
| const CONTRAST_PAIRS = [ | ||
| { | ||
| background: '--wpds-color-background-surface-neutral', | ||
| foreground: '--wpds-color-foreground-content-neutral', | ||
| }, | ||
| { | ||
| background: '--wpds-color-background-surface-neutral-strong', | ||
| foreground: '--wpds-color-foreground-content-neutral', | ||
| }, | ||
| { | ||
| background: '--wpds-color-background-surface-neutral-weak', | ||
| foreground: '--wpds-color-foreground-content-neutral', | ||
| }, | ||
| { | ||
| background: '--wpds-color-background-surface-neutral', | ||
| foreground: '--wpds-color-foreground-content-neutral-weak', | ||
| }, | ||
| { | ||
| background: '--wpds-color-background-surface-info', | ||
| foreground: '--wpds-color-foreground-content-info', | ||
| }, | ||
| { | ||
| background: '--wpds-color-background-surface-info-weak', | ||
| foreground: '--wpds-color-foreground-content-info-weak', | ||
| }, | ||
| { | ||
| background: '--wpds-color-background-surface-success', | ||
| foreground: '--wpds-color-foreground-content-success', | ||
| }, | ||
| { | ||
| background: '--wpds-color-background-surface-success-weak', | ||
| foreground: '--wpds-color-foreground-content-success-weak', | ||
| }, | ||
| { | ||
| background: '--wpds-color-background-surface-warning', | ||
| foreground: '--wpds-color-foreground-content-warning', | ||
| }, | ||
| { | ||
| background: '--wpds-color-background-surface-warning-weak', | ||
| foreground: '--wpds-color-foreground-content-warning-weak', | ||
| }, | ||
| { | ||
| background: '--wpds-color-background-surface-caution', | ||
| foreground: '--wpds-color-foreground-content-caution', | ||
| }, | ||
| { | ||
| background: '--wpds-color-background-surface-caution-weak', | ||
| foreground: '--wpds-color-foreground-content-caution-weak', | ||
| }, | ||
| { | ||
| background: '--wpds-color-background-surface-error', | ||
| foreground: '--wpds-color-foreground-content-error', | ||
| }, | ||
| { | ||
| background: '--wpds-color-background-surface-error-weak', | ||
| foreground: '--wpds-color-foreground-content-error-weak', | ||
| }, | ||
| { | ||
| background: '--wpds-color-background-interactive-brand-strong', | ||
| foreground: '--wpds-color-foreground-interactive-brand-strong', | ||
| }, | ||
| { | ||
| background: '--wpds-color-background-interactive-brand-strong-active', | ||
| foreground: '--wpds-color-foreground-interactive-brand-strong-active', | ||
| }, | ||
| { | ||
| background: '--wpds-color-background-interactive-error-strong', | ||
| foreground: '--wpds-color-foreground-interactive-error-strong', | ||
| }, | ||
| { | ||
| background: '--wpds-color-background-interactive-error-strong-active', | ||
| foreground: '--wpds-color-foreground-interactive-error-strong-active', | ||
| }, | ||
| { | ||
| background: '--wpds-color-background-interactive-neutral-strong', | ||
| foreground: '--wpds-color-foreground-interactive-neutral-strong', | ||
| }, | ||
| { | ||
| background: '--wpds-color-background-interactive-neutral-strong-active', | ||
| foreground: '--wpds-color-foreground-interactive-neutral-strong-active', | ||
| }, | ||
| ] as const; | ||
|
|
||
| function readToken( | ||
| styles: Record< string, string | number | undefined >, | ||
| token: string | ||
| ) { | ||
| const value = styles[ token ]; | ||
| if ( typeof value !== 'string' || value === '' ) { | ||
| throw new Error( `Missing semantic color token: ${ token }` ); | ||
| } | ||
| return value; | ||
| } | ||
|
|
||
| describe( 'semantic color contrast', () => { | ||
| it.each( THEME_PROVIDER_STYLE_CASES )( | ||
| 'keeps critical foreground/background pairs above WCAG AA text contrast with $name', | ||
| ( { settings } ) => { | ||
| const { result } = renderHook( () => | ||
| useThemeProviderStyles( settings ) | ||
| ); | ||
| const styles = result.current.themeProviderStyles as Record< | ||
| string, | ||
| string | number | undefined | ||
| >; | ||
|
|
||
| CONTRAST_PAIRS.forEach( ( { foreground, background } ) => { | ||
| const foregroundValue = readToken( styles, foreground ); | ||
| const backgroundValue = readToken( styles, background ); | ||
|
|
||
| expect( | ||
| getContrast( foregroundValue, backgroundValue ) | ||
| ).toBeGreaterThanOrEqual( MINIMUM_TEXT_CONTRAST ); | ||
| } ); | ||
| } | ||
| ); | ||
| } ); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
WDYT about adding a code example here to educate folks better? Not everyone knows how to do this or cares to seek it, but if we just show it, it might become a habit.
or
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Example added