Repository navigation
fix(Code): inherited inline code in a MediaTheme surface takes the chip's own text color #14307
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
Open
cixzhang
wants to merge
6
commits into
main
Choose a base branch
from
fix/code-inherit-in-media-surface
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+310
−35
Open
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
eb5209c
test(Code): contrast of inline code on a media surface in every theme
cixzhang 1695029
fix(Code): inherited inline code on a media surface takes the chip's …
cixzhang b74008f
test(Code): check inline code in a Toast too
cixzhang c556ddd
test(Code): wait on the code element, not text a Toast's live region …
cixzhang 6862eac
docs(Code): bound the inherit note to a MediaTheme surface; name it i…
cixzhang 0d35d58
docs(Code): a media surface does not restyle the chip; record the inh…
cixzhang 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| --- | ||
| '@astryxdesign/core': patch | ||
| --- | ||
|
|
||
| [fix] `<Code color="inherit">` on an inverted surface, such as in a Toast, now reads in every theme. Inline code keeps the page's chip there, so inherited code text takes the chip's own text color instead of the surface's, which drew dark text on a dark chip in Gothic. | ||
|
|
||
| @cixzhang | ||
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
Large diffs are not rendered by default.
Oops, something went wrong.
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
179 changes: 179 additions & 0 deletions
179
packages/core/src/Code/__tests__/CodeInMediaSurface.a11y.chromium.spec.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,179 @@ | ||
| // Copyright (c) Meta Platforms, Inc. and affiliates. | ||
| // @pr-a11y scope ^packages/core/src/theme/ ^packages/themes/ | ||
| /** | ||
| * @file CodeInMediaSurface.a11y.chromium.spec.ts | ||
| * @input The built `Core/Code` InMediaSurface and InToast stories in every shipped theme and | ||
| * Core's base tokens, light and dark: default and `color="inherit"` inline | ||
| * code in Text on an inverted surface | ||
| * @output Chromium proof that each text part on the media surface, the code | ||
| * chips included, meets WCAG AA (4.5:1) against what it paints on | ||
| * @position Inline code keeps the page's chip in a media surface; inherited | ||
| * code text there takes the chip's own primary text. | ||
| * | ||
| * SYNC: Fixture lives in /apps/storybook/stories/Code.stories.tsx | ||
| * (InMediaSurface, InToast). | ||
| */ | ||
| import {expect, test, type Page} from '@astryxdesign/a11y-spec/playwright'; | ||
| import {holdMotionStill} from '@astryxdesign/a11y-spec/chromium'; | ||
| import { | ||
| DEFAULT_STORYBOOK_DIR, | ||
| serveStorybook, | ||
| type StaticServer, | ||
| } from '@astryxdesign/a11y-spec/storybook'; | ||
|
|
||
| let storybook: StaticServer; | ||
|
|
||
| test.beforeAll(async () => { | ||
| storybook = await serveStorybook( | ||
| process.env.ASTRYX_STORYBOOK_DIR ?? DEFAULT_STORYBOOK_DIR, | ||
| ); | ||
| }); | ||
|
|
||
| test.afterAll(async () => { | ||
| await storybook?.close(); | ||
| }); | ||
|
|
||
| // A plain media surface, and a Toast, whose card applies its side itself. | ||
| const STORIES = ['core-code--in-media-surface', 'core-code--in-toast']; | ||
| // 'none' is Core's base tokens with no theme. | ||
| const SHIPPED_THEMES = [ | ||
| 'none', | ||
| 'butter', | ||
| 'chocolate', | ||
| 'gothic', | ||
| 'matcha', | ||
| 'neutral', | ||
| 'stone', | ||
| 'y2k', | ||
| ]; | ||
| const MODES = ['light', 'dark']; | ||
|
|
||
| /** Each text part on a media surface that falls below 4.5:1. */ | ||
| async function lowContrastParts(page: Page): Promise<string[]> { | ||
| return page.evaluate(() => { | ||
| const canvas = document.createElement('canvas').getContext('2d', { | ||
| willReadFrequently: true, | ||
| }) as CanvasRenderingContext2D; | ||
| type Rgba = [number, number, number, number]; | ||
| const rgba = (color: string): Rgba => { | ||
| canvas.clearRect(0, 0, 1, 1); | ||
| canvas.fillStyle = color; | ||
| canvas.fillRect(0, 0, 1, 1); | ||
| const [r, g, b, a] = canvas.getImageData(0, 0, 1, 1).data; | ||
| return [r, g, b, a / 255]; | ||
| }; | ||
| const over = (top: Rgba, under: Rgba): Rgba => { | ||
| const a = top[3] + under[3] * (1 - top[3]); | ||
| const mix = (i: number) => | ||
| a === 0 | ||
| ? 0 | ||
| : (top[i] * top[3] + under[i] * under[3] * (1 - top[3])) / a; | ||
| return [mix(0), mix(1), mix(2), a]; | ||
| }; | ||
| const luminance = ([r, g, b]: Rgba) => { | ||
| const linear = (v: number) => { | ||
| const c = v / 255; | ||
| return c <= 0.04045 ? c / 12.92 : ((c + 0.055) / 1.055) ** 2.4; | ||
| }; | ||
| return 0.2126 * linear(r) + 0.7152 * linear(g) + 0.0722 * linear(b); | ||
| }; | ||
| const ratio = (a: Rgba, b: Rgba) => { | ||
| const [high, low] = [luminance(a), luminance(b)].sort((x, y) => y - x); | ||
| return (high + 0.05) / (low + 0.05); | ||
| }; | ||
| const backdropOf = (element: Element): Rgba => { | ||
| const chain: Element[] = []; | ||
| for ( | ||
| let node: Element | null = element; | ||
| node; | ||
| node = node.parentElement | ||
| ) { | ||
| chain.unshift(node); | ||
| } | ||
| let background: Rgba = [255, 255, 255, 1]; | ||
| for (const node of chain) { | ||
| background = over( | ||
| rgba(getComputedStyle(node).backgroundColor), | ||
| background, | ||
| ); | ||
| } | ||
| return background; | ||
| }; | ||
| const drawnColor = (element: Element, color: string, background: Rgba) => { | ||
| let opacity = 1; | ||
| for ( | ||
| let node: Element | null = element; | ||
| node; | ||
| node = node.parentElement | ||
| ) { | ||
| opacity *= Number(getComputedStyle(node).opacity); | ||
| } | ||
| const ink = rgba(color); | ||
| return over([ink[0], ink[1], ink[2], ink[3] * opacity], background); | ||
| }; | ||
|
|
||
| const failures: string[] = []; | ||
| for (const tooltip of document.querySelectorAll('[data-astryx-media]')) { | ||
| const walker = document.createTreeWalker(tooltip, NodeFilter.SHOW_TEXT); | ||
| for (let node = walker.nextNode(); node; node = walker.nextNode()) { | ||
| const text = node.textContent?.trim(); | ||
| const element = node.parentElement; | ||
| if (!text || element == null) { | ||
| continue; | ||
| } | ||
| const background = backdropOf(element); | ||
| const drawn = drawnColor( | ||
| element, | ||
| getComputedStyle(element).color, | ||
| background, | ||
| ); | ||
| const found = ratio(drawn, background); | ||
| if (found < 4.5) { | ||
| failures.push(`text "${text}": ${found.toFixed(2)}:1`); | ||
| } | ||
| } | ||
| for (const icon of tooltip.querySelectorAll('svg')) { | ||
| const background = backdropOf(icon); | ||
| const style = getComputedStyle(icon); | ||
| const paint = | ||
| style.fill !== 'none' && !style.fill.startsWith('url(') | ||
| ? style.fill | ||
| : style.stroke !== 'none' | ||
| ? style.stroke | ||
| : style.color; | ||
| const drawn = drawnColor(icon, paint, background); | ||
| const found = ratio(drawn, background); | ||
| if (found < 3) { | ||
| failures.push(`icon: ${found.toFixed(2)}:1`); | ||
| } | ||
| } | ||
| } | ||
| return failures; | ||
| }); | ||
| } | ||
|
|
||
| for (const story of STORIES) { | ||
| for (const theme of SHIPPED_THEMES) { | ||
| test(`inline code reaches 4.5:1 in ${story} in ${theme}, light and dark`, async ({ | ||
| page, | ||
| }) => { | ||
| const failures: string[] = []; | ||
| for (const mode of MODES) { | ||
| await page.goto( | ||
| `${storybook.origin}/iframe.html?id=${story}&viewMode=story&globals=astryxTheme:${theme};colorMode:${mode}`, | ||
| ); | ||
| await holdMotionStill(page); | ||
| // A Toast's live region repeats its text, so wait on the code itself. | ||
| await expect( | ||
| page.locator('code', {hasText: 'pnpm test'}), | ||
| ).toBeVisible(); | ||
| failures.push( | ||
| ...(await lowContrastParts(page)).map( | ||
| failure => `${mode}: ${failure}`, | ||
| ), | ||
| ); | ||
| } | ||
| expect(failures).toEqual([]); | ||
| }); | ||
| } | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| // Copyright (c) Meta Platforms, Inc. and affiliates. | ||
|
|
||
| /** | ||
| * @file mediaSurface.stylex.ts | ||
| * @input StyleX markers | ||
| * @output Exports mediaSurfaceMarker | ||
| * @position Internal marker MediaTheme's element carries, so a component | ||
| * inside a media surface can style itself for it with | ||
| * `stylex.when.ancestor('[data-astryx-media]', mediaSurfaceMarker)` | ||
| * | ||
| * SYNC: When modified, update: | ||
| * - /packages/core/src/theme/MediaTheme.tsx | ||
| * - /packages/core/src/Code/Code.tsx | ||
| */ | ||
|
|
||
| import * as stylex from '@stylexjs/stylex'; | ||
|
|
||
| export const mediaSurfaceMarker: ReturnType<typeof stylex.defineMarker> = | ||
| stylex.defineMarker(); |
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.
Uh oh!
There was an error while loading. Please reload this page.