Skip to content

Improve focus/selection contrast and support forced colors mode - #4243

Closed
amanmahajan7 wants to merge 9 commits into
mainfrom
a11y-contrast-forced-colors
Closed

amanmahajan7 wants to merge 9 commits into
mainfrom
a11y-contrast-forced-colors

Conversation

@amanmahajan7

@amanmahajan7 amanmahajan7 commented Oct 26, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Fixes contrast failures in the default theme's focus/selection colors, and adds forced colors mode (Windows contrast themes) support so row selection, focus and the fill handle stay visible.

Visual regression tests

test/visual/focusAndSelection.test.tsx adds screenshots of what this PR fixes:

  • Scenarios: the active cell (with the fill handle) and the focused checkbox, each on a selected row and on an unselected row.
  • Modes: light, dark and forced colors.
  • Forced colors setup: a new emulateMedia browser command (Playwright's page.emulateMedia) turns it on. WebKit is skipped in that mode: it matches (forced-colors: active) when emulated but doesn't force any colors.

To show the before/after diff, the fixes are temporarily reverted (86c3810):

  1. Generate baseline screenshots with the current main styles.
  2. Re-apply the fixes.
  3. Regenerate the screenshots; the screenshot diff in this PR then shows exactly what changed.

Why forced colors support matters

Forced colors mode is an OS-level accessibility setting: mainly Windows Contrast themes (formerly "High Contrast"), and also Firefox's color override. The user chooses a small palette, and the browser applies it to every page: author colors are replaced, and box-shadow, background-image and gradients are removed. People with low vision, light sensitivity, migraines or some cognitive disabilities rely on it, often all day and for every app, including the data-heavy apps where this grid is embedded.

Apps can't opt out on the user's behalf, so a component that shows state only through background colors loses that state completely. Before this PR, that's what happened to the grid:

  • Selected rows looked the same as unselected rows. Users could select rows and then had no way to see which ones were selected. That fails WCAG 1.4.1 (Use of Color) and 4.1.2 in practice: the state still reaches screen readers through aria-selected, but not sighted users.
  • The fill handle is an 8px square drawn only with background-color, so it disappeared entirely.
  • The column drag-over target was shown only through a background tint, so drag-and-drop gave no feedback.

Data grids are one of the hardest UIs to use in high contrast mode, because they depend on many state colors (active cell, selection, hover, drop targets). Because the grid is a shared component, getting this right once fixes every downstream app. Fixing it inside apps would mean every consumer reverse-engineering the grid's internals.

Changes

Contrast

Token Before After
--rdg-selection-color hsl(207, 75%, 66%) light-dark(hsl(207deg 100% 40%), hsl(207deg 100% 70%))
--rdg-checkbox-focus-color hsl(207deg 100% 69%) light-dark(hsl(207deg 100% 40%), hsl(207deg 100% 70%))
--rdg-row-selected-background-color (dark) hsl(207deg 76% 42%) hsl(207deg 76% 32%)
--rdg-row-selected-hover-background-color (dark) hsl(207deg 76% 38%) hsl(207deg 76% 28%)
  • Active cell / row focus outline (WCAG 1.4.11 / 2.4.13 need 3:1): the light theme was 2.37:1 against the background and 1.96:1 against a selected row. It's now at least 3.56:1 against every row and header background in both themes. The fill handle and the text editor's focus border use the same token, so they improve too.
  • Checkbox focus ring: it was 2.14:1 on white and now uses the same colors. It stays inset, since the active cell outline already acts as the compliant focus indicator.
  • Dark-mode selected rows: #ddd text was 3.65:1, below WCAG 1.4.3's 4.5:1. It's now 5.49:1, and 6.5:1 on hover.

Forced colors mode

Inside @media (forced-colors: active), the grid maps its tokens to system colors, which follow the user's chosen palette:

Element System colors
Focus outlines (active cell/row), checkbox focus ring, fill handle CanvasText
Selected rows SelectedItem background, SelectedItemText text
Column drag-over target dashed Highlight outline
  • I tried Highlight / HighlightText for selected rows and reverted it (5b00213, reverted in 502cba0). Chromium paints a Canvas-colored readability backplate behind text in forced colors mode, and HighlightText text on that backplate was invisible: white on white in the default emulated palette. SelectedItem / SelectedItemText renders correctly.
  • This follows the Modern Web Guidance color guidance ("Define system color fallbacks for color tokens using @media (forced-colors: active)"), and doesn't use forced-color-adjust: none.

Try it: open the DevTools command menu (⌘⇧P) → "Emulate CSS forced-colors: active", or turn on a Windows Contrast theme.

Notes for reviewers

  • Visual change: the light-theme focus ring is noticeably darker than the old pastel blue.
  • Trade-off in dark mode: selected rows contrast less with unselected rows, 2.16:1 vs 3.24:1 before. That's the cost of making their text readable.
  • Custom tokens: grids that override these CSS variables in their own (unlayered) styles also override the forced-colors values.
  • Visual regression screenshots need updating in CI.
  • README.md's CSS variables section is updated.

Testing

  • vp check, vp run typecheck and vp pack pass.

  • A temporary Chromium browser test, with forced colors emulated over CDP, confirmed:

    • selected rows compute to SelectedItem / SelectedItemText;
    • screenshots show readable text on selected rows and a visible focus ring.

    I removed the test afterwards.

  • vp test locally: 7 failures (sorting, keyboard navigation, WebKit modal editors). The same 7 also fail on main on this machine, so they're unrelated to this PR.

  • Not tested with a real Windows contrast theme or in Firefox/WebKit forced colors.

🤖 Generated with Claude Code

amanmahajan7 and others added 2 commits October 25, 2026 18:03
- Darken `--rdg-selection-color` and `--rdg-checkbox-focus-color` in light mode
  so focus indicators meet 3:1 against every row/header background
- Darken the dark-mode selected row backgrounds so text meets 4.5:1
- Draw the checkbox focus ring outside the checkbox
- In forced colors mode, use system colors for selection, focus, and
  the drag handle, and outline header cells during column drag-and-drop

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The active cell outline already provides a compliant focus indicator,
so the checkbox ring does not need to sit outside the checkbox.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
amanmahajan7 and others added 2 commits October 25, 2026 21:01
Highlight/HighlightText is the long-established, widely supported pair for
selection. Draw focus outlines in HighlightText on selected rows, since
CanvasText is not guaranteed to contrast with Highlight.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…s mode"

HighlightText text is painted over Chromium's Canvas-colored readability
backplate, which made selected-row text invisible (white on white).
SelectedItem/SelectedItemText renders correctly.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@amanmahajan7
amanmahajan7 marked this pull request as draft October 26, 2026 02:07
amanmahajan7 and others added 2 commits October 25, 2026 21:07
…shots

Restores src/ and README.md to main so the new visual tests can record
the current behavior before the fixes are re-applied.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Covers the active cell (with the fill handle) and the focused checkbox, on
selected and unselected rows, in light mode, dark mode, and forced colors
mode. Adds an `emulateMedia` browser command to emulate forced colors.
WebKit is skipped in forced colors mode, as it matches the media query
when emulated but does not force any colors.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
amanmahajan7 and others added 2 commits October 26, 2026 02:12
Reverts 86c3810, which temporarily reverted the fixes to capture
baseline screenshots.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@amanmahajan7

Copy link
Copy Markdown
Collaborator Author

Closing in favor of #4244, which covers forced colors mode only and builds the screenshots step by step. The contrast changes from this PR are dropped for now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant