Repository navigation
fix(Tooltip): Text content reads on the tooltip surface, through theme text roles for the inverted surface #12410
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
34
commits into
main
Choose a base branch
from
fix-tooltip-content-contrast
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.
+762
−46
Open
Changes from 3 commits
Commits
Show all changes
34 commits
Select commit
Hold shift + click to select a range
de9bf69
fix(Tooltip): content keeps contrast on the inverted surface
cixzhang 28ddf08
fix(Tooltip): keep the media-side helper inside useTooltip
cixzhang 63e5d50
fix(Tooltip): measure the content's media side inside the open layer;…
cixzhang 80b6792
fix(Tooltip): content reads the media tokens and keeps the surface te…
cixzhang 805e5f9
fix(Tooltip): string and number content renders without the media wra…
cixzhang f9d7d99
docs(Tooltip): the media wrapper comment says it renders while closed
cixzhang c9fae25
Merge remote-tracking branch 'refs/remotes/l4/main-new' into lane4/pr…
cixzhang 22494e5
perf(Tooltip): the media context adds about 0.4 to 1.3 KB to a page, …
cixzhang fc6ef3b
Merge main into #12410
cixzhang d28b067
Merge main into fix-tooltip-content-contrast
cixzhang 2a718c7
fix(Tooltip): the content's side follows the theme's surface in the p…
cixzhang 2212a34
fix(Tooltip): keep the side code local to Tooltip and out of the firs…
cixzhang d849f49
docs(Tooltip): record the content contrast gap before the side module…
cixzhang 305ce5e
fix(Tooltip): content takes the surface's text color until its side i…
cixzhang 499e914
docs(Tooltip): the pre-measurement content color is unmuted
cixzhang 0ff348c
chore(changeset): name the CLI for the Gothic and Matcha theme templates
cixzhang 4abee12
perf(Tooltip): import the media side statically; no loader and no aft…
cixzhang 6e51074
probe: surface-only content colors, no measurement (size probe, not f…
cixzhang bb8f887
wip: tooltip text derivation
cixzhang 5fd30c0
card-depth A: tooltip text tokens derived per theme; tooltip content …
cixzhang 45000d8
card-depth A v2: grammar names (--color-text-*-inverted); mapping on …
cixzhang 1e53364
card-depth A v3: render tooltip children directly; deps as on main (d…
cixzhang 5739253
fix(Tooltip): token-colored content takes the theme's text roles for …
cixzhang 65efcd0
Merge main into fix-tooltip-content-contrast
cixzhang a901598
docs(theme-tokens): INV15 records the text roles on the inverted surf…
cixzhang be8899d
fix(Tooltip): token docs list the inverted text roles; the derivation…
cixzhang 17f3965
chore(changeset): name the CLI, whose token docs list the inverted roles
cixzhang 9792af4
chore(Tooltip): narrow the changeset to the roles it moves; drop the …
cixzhang e9540c4
chore(theme): the derivation header names no record
cixzhang ad35457
fix(themes): Butter, Gothic, Matcha, Stone and Y2K set the inverted t…
cixzhang 19f5653
fix(theme): share the readable-ink and ratio helpers; regenerate the …
cixzhang 8718581
style(theme): format statusColor.ts
cixzhang d587f48
docs(Tooltip): bound the inverted text roles to primary, secondary an…
cixzhang b8d3143
docs(theme-tokens): INV15 scopes to tooltip content and to themes who…
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] Tooltip content is readable when it uses `Text` or other token-colored components, as the Tooltip docs show. The content now renders inside a `MediaTheme` on the side whose text reads best on the tooltip's inverted surface, as Toast's content does. Before, `<Text>` content took the page theme's primary text color, which is the color of the tooltip surface, so the tooltip looked empty in light and dark. Plain-text tooltips now use that side's primary text color, which can differ slightly from the page surface color they used before. | ||
|
|
||
| @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
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,88 @@ | ||
| // Copyright (c) Meta Platforms, Inc. and affiliates. | ||
|
|
||
| /** | ||
| * @file Tooltip.mediaTheme.test.tsx | ||
| * @input Tooltip with `Text` content under themes whose inverted surface is | ||
| * dark or light, in either color mode | ||
| * @output Proves that the supplied content renders inside a MediaTheme whose | ||
| * side reads on the tooltip's inverted surface, so token-colored content | ||
| * such as `Text` does not take the surface's own color | ||
| * @position Unit test for the tooltip content's media context | ||
| */ | ||
|
|
||
| import {describe, expect, it} from 'vitest'; | ||
| import {render, screen} from '@testing-library/react'; | ||
| import {Theme, defineTheme} from '../theme'; | ||
| import {Text} from '../Text/Text'; | ||
| import {MediaTheme} from '../theme/MediaTheme'; | ||
| import {Tooltip} from './Tooltip'; | ||
|
|
||
| /** A theme whose inverted surface is `surface` in both modes. */ | ||
| const withSurface = (surface: string) => ({ | ||
| ...defineTheme({ | ||
| name: `tooltip-surface-${surface.replace(/[^a-z0-9]+/gi, '-')}`, | ||
| tokens: { | ||
| '--color-background-inverted': [surface, surface], | ||
| '--color-on-dark': ['#ffffff', '#ffffff'], | ||
| '--color-on-light': ['#111111', '#111111'], | ||
| }, | ||
| }), | ||
| __built: true as const, | ||
| }); | ||
|
|
||
| const mediaOfContent = ( | ||
| theme: ReturnType<typeof withSurface>, | ||
| mode: 'light' | 'dark', | ||
| ) => { | ||
| render( | ||
| <Theme theme={theme} mode={mode}> | ||
| <Tooltip content={<Text type="body">Content text</Text>} isDefaultOpen> | ||
| <button type="button">Trigger</button> | ||
| </Tooltip> | ||
| </Theme>, | ||
| ); | ||
| return screen | ||
| .getByText('Content text') | ||
| .closest('[data-astryx-media]') | ||
| ?.getAttribute('data-astryx-media'); | ||
| }; | ||
|
|
||
| describe('Tooltip content media context', () => { | ||
| it('a dark inverted surface gives Text content light text in a light page', () => { | ||
| expect(mediaOfContent(withSurface('#0a1317'), 'light')).toBe('dark'); | ||
| }); | ||
|
|
||
| it('a dark inverted surface in a dark page still gives light text', () => { | ||
| expect(mediaOfContent(withSurface('#0a1317'), 'dark')).toBe('dark'); | ||
| }); | ||
|
|
||
| it('a light inverted surface gives Text content dark text', () => { | ||
| expect(mediaOfContent(withSurface('#f4f0e8'), 'dark')).toBe('light'); | ||
| }); | ||
|
|
||
| it('a surface the theme cannot resolve starts from the color mode', () => { | ||
| expect(mediaOfContent(withSurface('oklch(0.2 0.01 230)'), 'light')).toBe( | ||
| 'dark', | ||
| ); | ||
| }); | ||
|
|
||
| it('content that already holds a MediaTheme keeps its own and gets no second one', () => { | ||
| render( | ||
| <Theme theme={withSurface('#0a1317')} mode="light"> | ||
| <Tooltip | ||
| content={ | ||
| <MediaTheme mode="light"> | ||
| <Text type="body">Own context</Text> | ||
| </MediaTheme> | ||
| } | ||
| isDefaultOpen> | ||
| <button type="button">Trigger</button> | ||
| </Tooltip> | ||
| </Theme>, | ||
| ); | ||
| const text = screen.getByText('Own context'); | ||
| const media = text.closest('[data-astryx-media]'); | ||
| expect(media?.getAttribute('data-astryx-media')).toBe('light'); | ||
| expect(media?.parentElement?.closest('[data-astryx-media]')).toBeNull(); | ||
| }); | ||
| }); |
179 changes: 179 additions & 0 deletions
179
packages/core/src/Tooltip/__tests__/TooltipContentContrast.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 TooltipContentContrast.a11y.chromium.spec.ts | ||
| * @input The built `Core/Tooltip` RichContent story in every shipped theme, | ||
| * light and dark: a plain string, the docs' `Text` example, default and | ||
| * secondary `Text`, and an icon with text, each in an open tooltip | ||
| * @output Chromium proof that text in a tooltip meets WCAG AA (4.5:1) and an | ||
| * icon meets 3:1 against the tooltip's painted surface | ||
| * @position Content colors come from the media context around the content. | ||
| * Only a real engine resolves the theme tokens and the painted surface, so | ||
| * the measure runs in a browser. | ||
| * | ||
| * SYNC: Fixture lives in /apps/storybook/stories/Tooltip.stories.tsx | ||
| * (RichContent). | ||
| */ | ||
|
|
||
| import {expect, test, type Page} from '@astryxdesign/a11y-spec/playwright'; | ||
| 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(); | ||
| }); | ||
|
|
||
| const STORY = 'core-tooltip--rich-content'; | ||
| const SHIPPED_THEMES = [ | ||
| 'butter', | ||
| 'chocolate', | ||
| 'gothic', | ||
| 'matcha', | ||
| 'neutral', | ||
| 'stone', | ||
| 'y2k', | ||
| ]; | ||
| const MODES = ['light', 'dark']; | ||
|
|
||
| /** | ||
| * Each tooltip's text runs and icons that fall below their minimum: 4.5:1 for | ||
| * text, 3:1 for an icon. The measure uses the painted surface behind the | ||
| * content and the color each part draws with, opacity included. | ||
| */ | ||
| 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('[role="tooltip"]')) { | ||
| 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) { | ||
|
astracat-bot[bot] marked this conversation as resolved.
Resolved
|
||
| 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 theme of SHIPPED_THEMES) { | ||
| test(`tooltip text reaches 4.5:1 and icons 3:1 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}`, | ||
| ); | ||
|
astracat-bot[bot] marked this conversation as resolved.
Resolved
|
||
| await expect(page.getByRole('tooltip')).toHaveCount(5); | ||
| await expect(page.getByText('Body text')).toBeVisible(); | ||
| await page.evaluate(async () => { | ||
| await document.fonts.ready; | ||
| await new Promise(resolve => | ||
| requestAnimationFrame(() => requestAnimationFrame(resolve)), | ||
| ); | ||
| }); | ||
| failures.push( | ||
| ...(await lowContrastParts(page)).map(failure => `${mode}: ${failure}`), | ||
| ); | ||
| } | ||
| expect(failures).toEqual([]); | ||
| }); | ||
| } | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.