Keep Ariakit UI slot badges and avatars visible in forced colors
Context
The nav slot design work for #7579 and #7584 found that slot badges and avatars lose their surface in forced colors. The same problem exists on main, so #7584 did not cause it.
Problem
Forced colors replace the fill of a slot badge or avatar with the page color (Canvas). The slot has no border, so nothing marks its edge. A count such as "12" or "9" then reads as a stray number beside the label, and initials such as "AL" read as bare letters.
Measured on main at 87749801f with Playwright forcedColors: "active", light scheme:
| Element | Chromium 153 | Firefox 155 |
|---|---|---|
| 11 slot badges and avatars: button "Count badge", "Initial avatar" and "Label and description"; nav "Badges and avatars" (6 slots); tabs "Tab with a badge"; disclosure "Trailing badge" | Fill becomes Canvas, no border |
Fill becomes Canvas, no border |
Standalone Badge: badge "Default" and "Brand" |
1px border | 1px border |
| Button with a layer fill: button "Label and description" | 1px border | 1px border |
Slot badge with an explicit $border: button "Badge hue" |
1px border | 1px border |
The nav example shows the same result in the dark scheme, in Chromium and Firefox. A painted icon tile (button "Painted icon slot") also loses its fill and has no border, but its icon still shows.
Minimal reproduction, with forced colors active:
<Button>
<ButtonLabel>Inbox</ButtonLabel>
<ButtonSlot $kind="badge" $p="md">
12
</ButtonSlot>
</Button>
<Button>
<ButtonSlot $kind="avatar" $layer="brand">
WW
</ButtonSlot>
<ButtonLabel>Will Williams</ButtonLabel>
</Button>WebKit is not in the table. Playwright's emulation makes the forced-colors: active media query match in WebKit 26.6, but WebKit does not force the colors, so the fills stay. Safari has no forced colors mode. #7500 scoped its forced-colors tests to Chrome and Firefox for the same reason.
Cause
The control recipe adds a real border in forced colors when a control paints a layer and has no $border (control.ts#L86-L90):
// Forced colors remove a control's fill, which may be its only boundary.
if (variants.$border == null && hasLayerBackground(variants)) {
addClass("forced-colors:ak-frame-border");
}controlSlot extends frame, not control (control.ts#L100-L101), so it does not get this rule. Its defaults make a badge and an avatar paint a layer (control.ts#L240-L254), but its own refine adds no forced-colors border (control.ts#L310-L352).
The standalone Badge keeps its edge because it sets $border: true and $borderType: "inset" (badge.ts#L30-L31), and the inset type falls back to a real border in forced colors (frame.ts#L166-L169). #7500, which fixed #7476, added these fallbacks for frames, controls and badges, but not for slots.
Pre-existing on main
Yes. All measurements above are on main at 87749801f, with no change applied.
Why #7584 can finish without it
The cause is in controlSlot, which every slot shares: Button, Tabs, Disclosure, Nav, Combobox, Input, and the checkbox and radio cards. #7584 changes the nav slot API and sizes, not how a slot paints. The fix changes every component with a slot badge or avatar, and it needs its own forced-colors browser tests. So it is a separate change.
Suggested direction
Reuse the rule of control in the refine of controlSlot:
if (variants.$border == null && hasLayerBackground(variants)) {
addClass("forced-colors:ak-frame-border");
}I tested the class that this rule emits. I added forced-colors:ak-frame-border to each of the 12 slot badges and avatars in the examples above, in Chromium 153 and Firefox 155:
- With forced colors, each slot gets a 1px border. The border takes the text color: black in most controls, and the link color in nav links.
- The slot sizes do not change: 24x24, and 42x42 for the two-row avatar.
- Without forced colors, the class adds nothing (a 0px border).
With hasLayerBackground, a painted icon tile also gets the border, and a see-through icon slot does not. I did not test a painted icon tile with the class.
Workaround
Add the class to the slot yourself, for example <ButtonSlot $kind="badge" className="forced-colors:ak-frame-border">. This is the class that I tested above. An explicit $border also keeps a boundary in forced colors (button "Badge hue"), but it also draws an edge without forced colors.
Expected outcome
- Slot badges and avatars keep a visible boundary in forced colors, like the standalone
Badge. - The slot sizes and the rendering without forced colors do not change.
- A forced-colors browser test in Chrome and Firefox covers at least one slot badge and one slot avatar, like the tests that #7500 added.
- Worktree at
87749801f,pnpm dev-app, Playwright 1.63.0 with Chromium 153.0.8010.12, Firefox 155.0 and WebKit 26.6, 1280x900 viewport, device scale factor 2. - Each page was loaded in a new context with
forcedColors: "active"or"none", andcolorScheme: "light". The nav example was also loaded withcolorScheme: "dark". - For each slot and control: the computed background color, border width, border style and box shadow, and
matchMedia("(forced-colors: active)"). - The tested class was added to the elements in the page. No library file was changed.
Source: ariakit/ariakit