add contrast badges - #1384
Conversation
|
There was a problem hiding this comment.
Pull request overview
This PR enhances the Storybook ColorScale component to display contrast ratios between each swatch color and several key theme foreground/background tokens, adding a compact badge row UI and updated layout styling.
Changes:
- Added contrast ratio calculation utilities and a probe-based approach to resolve CSS variable colors at runtime.
- Rendered contrast “badges” (with icons + formatted ratios) and updated the swatch content structure.
- Refreshed
ColorScalestyles to a flex-column layout and introduced tooltip styling for badges.
Show a summary per file
| File | Description |
|---|---|
| docs/storybook/stories/StorybookComponents/ColorScale/ColorScale.tsx | Adds contrast computation, CSS-var probes, and renders badge UI + hex display. |
| docs/storybook/stories/StorybookComponents/ColorScale/ColorScale.css | Updates layout styling and adds badge/tooltip presentation styles. |
Copilot's findings
Tip
Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comments suppressed due to low confidence (2)
docs/storybook/stories/StorybookComponents/ColorScale/ColorScale.tsx:109
- Switching from
bordertooutlinemay cause the optional border to ignore the element’s border-radius (outlines don’t reliably follow rounding), which can visually regress the rounded corners set in.ColorScale--block:first/last-of-type. If rounding is desired, consider keeping a real border or using an inset box-shadow for the 1px stroke.
style={{
backgroundColor: `var(--${color})`,
outline: border ? '1px solid var(--borderColor-default)' : undefined,
}}
docs/storybook/stories/StorybookComponents/ColorScale/ColorScale.tsx:152
- Badges rely on a hover-only tooltip (
data-tooltip) and aren’t keyboard focusable, so the tooltip content isn’t accessible to keyboard/screen-reader users. Consider adding an accessible name (e.g.,aria-label) and making the badge focusable (or use atitleattribute) so the extra context is available without hover.
<span key={key} className="cs-badge" data-tooltip={tooltip}>
{icon === 'aa' ? (
<span className="cs-aa" style={{color: `var(${cssVar})`}}>
Aa
</span>
) : icon === 'outline-square' ? (
<span className="cs-square cs-square--outline" style={{borderColor: `var(${cssVar})`}} />
) : (
<span className="cs-square cs-square--filled" style={{backgroundColor: `var(${cssVar})`}} />
)}
<span className="cs-num" style={{color: numColor}}>
{fmt(ratio)}
</span>
</span>
- Files reviewed: 2/2 changed files
- Comments generated: 2
| .cs-badge[data-tooltip]:hover::after { | ||
| content: attr(data-tooltip); | ||
| position: absolute; | ||
| bottom: calc(100% + 6px); | ||
| left: 50%; | ||
| transform: translateX(-50%); | ||
| white-space: nowrap; | ||
| background: var(--bgColor-emphasis, #1f2328); | ||
| color: var(--fgColor-onEmphasis, #ffffff); | ||
| font-size: 11px; | ||
| font-family: sans-serif; | ||
| padding: 3px 7px; | ||
| border-radius: 4px; | ||
| pointer-events: none; | ||
| z-index: 100; | ||
| } |
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Agent-Logs-Url: https://github.com/primer/primitives/sessions/bd328b1a-e866-40ce-b71c-259b661ba1fb Co-authored-by: lukasoppermann <813754+lukasoppermann@users.noreply.github.com>
This pull request significantly improves the
ColorScalecomponent in Storybook by enhancing its accessibility and usability. The changes add dynamic contrast ratio calculations for various foreground and background color combinations, introduce a new visual badge system for displaying these ratios, and update the component's styling for better clarity and structure.