Skip to content

Commit 20c15c2

Browse files
committed
preserve mention contrast across themes
Signed-off-by: tulsi <[email protected]>
1 parent a49bc5e commit 20c15c2

3 files changed

Lines changed: 137 additions & 0 deletions

File tree

‎desktop/src/shared/styles/globals/markdown.css‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,19 @@
302302
color: hsl(var(--primary) / 0.9);
303303
}
304304

305+
/* Mention backgrounds follow the active accent, while foreground text keeps
306+
normal-size labels at AA contrast across light, dark, and custom themes. */
307+
.message-markdown .mention-chip.inline-chip-icon-human,
308+
.message-markdown .mention-chip.inline-chip-icon-agent {
309+
color: hsl(var(--mention-foreground));
310+
}
311+
312+
.message-markdown .mention-chip-hover.inline-chip-icon-human:hover,
313+
.message-markdown .mention-chip-hover.inline-chip-icon-agent:hover {
314+
background: hsl(var(--primary) / 0.2);
315+
color: hsl(var(--mention-foreground));
316+
}
317+
305318
.message-markdown .mention-prefix-hidden {
306319
display: inline-block;
307320
width: 0;

‎desktop/src/shared/styles/globals/theme.css‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
--buzz-gradient-dark-top: #4a4616;
5858
--buzz-gradient-dark-bottom: #0a1423;
5959
--buzz-content-dark: 0 0% 10.1960784314%;
60+
--mention-foreground: var(--buzz-content-dark);
6061
/* Hosted-community onboarding intentionally keeps this light palette in
6162
both app themes; semantic tokens keep component code color-agnostic. */
6263
--buzz-hosted-community-surface-fg: 23 23 23;
@@ -117,6 +118,7 @@
117118
--sidebar-accent-foreground: 227 68.25% 87.65%;
118119
--sidebar-border: 231 15.61% 33.92%;
119120
--sidebar-ring: 231 15.61% 33.92%;
121+
--mention-foreground: 0 0% 96%;
120122
}
121123

122124
* {

‎desktop/tests/e2e/mentions.spec.ts‎

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { expect, test } from "@playwright/test";
22

3+
import { SYNTAX_THEMES } from "../../src/shared/theme/theme-loader";
34
import {
45
installMockBridge,
56
openChannelBrowser,
@@ -37,6 +38,85 @@ const DM_THREAD_AGENT_MENTION_ERROR_TEXT =
3738
const DM_THREAD_MEMBERS_LOADING_ERROR_TEXT =
3839
"Checking conversation members. Try again in a moment.";
3940

41+
async function readRenderedContrast(
42+
locator: import("@playwright/test").Locator,
43+
) {
44+
return locator.evaluate((element) => {
45+
type Rgba = { r: number; g: number; b: number; a: number };
46+
const parseColor = (value: string): Rgba => {
47+
if (value === "transparent") return { r: 0, g: 0, b: 0, a: 0 };
48+
const canvas = document.createElement("canvas");
49+
canvas.width = 1;
50+
canvas.height = 1;
51+
const context = canvas.getContext("2d");
52+
if (!context) return { r: 0, g: 0, b: 0, a: 0 };
53+
context.clearRect(0, 0, 1, 1);
54+
context.fillStyle = value;
55+
context.fillRect(0, 0, 1, 1);
56+
const [r, g, b, alpha] = context.getImageData(0, 0, 1, 1).data;
57+
return { r, g, b, a: alpha / 255 };
58+
};
59+
const composite = (foreground: Rgba, background: Rgba): Rgba => {
60+
const alpha = foreground.a + background.a * (1 - foreground.a);
61+
if (alpha === 0) return { r: 0, g: 0, b: 0, a: 0 };
62+
return {
63+
r:
64+
(foreground.r * foreground.a +
65+
background.r * background.a * (1 - foreground.a)) /
66+
alpha,
67+
g:
68+
(foreground.g * foreground.a +
69+
background.g * background.a * (1 - foreground.a)) /
70+
alpha,
71+
b:
72+
(foreground.b * foreground.a +
73+
background.b * background.a * (1 - foreground.a)) /
74+
alpha,
75+
a: alpha,
76+
};
77+
};
78+
const backgrounds: Rgba[] = [];
79+
for (let node: Element | null = element; node; node = node.parentElement) {
80+
backgrounds.push(parseColor(getComputedStyle(node).backgroundColor));
81+
}
82+
const background = backgrounds
83+
.reverse()
84+
.reduce((result, layer) => composite(layer, result), {
85+
r: 0,
86+
g: 0,
87+
b: 0,
88+
a: 0,
89+
});
90+
const foreground = composite(
91+
parseColor(getComputedStyle(element).color),
92+
background,
93+
);
94+
const luminance = (color: Rgba) =>
95+
[color.r, color.g, color.b]
96+
.map((channel) => {
97+
const value = channel / 255;
98+
return value <= 0.04045
99+
? value / 12.92
100+
: ((value + 0.055) / 1.055) ** 2.4;
101+
})
102+
.reduce(
103+
(sum, channel, index) =>
104+
sum + channel * [0.2126, 0.7152, 0.0722][index],
105+
0,
106+
);
107+
const foregroundLuminance = luminance(foreground);
108+
const backgroundLuminance = luminance(background);
109+
return {
110+
background: getComputedStyle(element).backgroundColor,
111+
backgroundAlpha: parseColor(getComputedStyle(element).backgroundColor).a,
112+
contrast:
113+
(Math.max(foregroundLuminance, backgroundLuminance) + 0.05) /
114+
(Math.min(foregroundLuminance, backgroundLuminance) + 0.05),
115+
foreground: getComputedStyle(element).color,
116+
};
117+
});
118+
}
119+
40120
/** Locator scoped to the mention autocomplete dropdown inside the composer. */
41121
function autocomplete(page: import("@playwright/test").Page) {
42122
return page
@@ -2734,6 +2814,48 @@ test("mention text is highlighted in sent messages", async ({ page }) => {
27342814
await expect(mentionChip).toHaveClass(/inline-chip-icon-human/);
27352815
});
27362816

2817+
for (const theme of SYNTAX_THEMES) {
2818+
test(`mention chip keeps AA contrast and theme accent in ${theme}`, async ({
2819+
page,
2820+
}) => {
2821+
await page.addInitScript((themeName) => {
2822+
window.localStorage.setItem("buzz-theme", themeName);
2823+
}, theme);
2824+
await page.goto("/");
2825+
await page.getByTestId("channel-general").click();
2826+
await expect(page.getByTestId("chat-title")).toHaveText("general");
2827+
await waitForMockLiveSubscription(page, "general");
2828+
2829+
await emitMockMessage(page, "general", `Ask @bob in ${theme}`, {
2830+
mentionPubkeys: [TEST_IDENTITIES.bob.pubkey],
2831+
});
2832+
await waitForTimelineSettled(page);
2833+
2834+
const mentionChip = page
2835+
.getByTestId("message-row")
2836+
.filter({ hasText: `Ask bob in ${theme}` })
2837+
.locator("[data-mention]", { hasText: "bob" });
2838+
await expect(mentionChip).toBeVisible();
2839+
2840+
const resting = await readRenderedContrast(mentionChip);
2841+
expect(resting.contrast).toBeGreaterThanOrEqual(4.5);
2842+
expect(resting.backgroundAlpha).toBeLessThan(1);
2843+
2844+
await mentionChip.hover();
2845+
await expect
2846+
.poll(() =>
2847+
mentionChip.evaluate(
2848+
(element) => getComputedStyle(element).backgroundColor,
2849+
),
2850+
)
2851+
.not.toBe(resting.background);
2852+
const hovered = await readRenderedContrast(mentionChip);
2853+
expect(hovered.foreground).toBe(resting.foreground);
2854+
expect(hovered.contrast).toBeGreaterThanOrEqual(4.5);
2855+
expect(hovered.backgroundAlpha).toBeLessThan(1);
2856+
});
2857+
}
2858+
27372859
test("clicking author name opens user profile panel", async ({ page }) => {
27382860
await page.goto("/");
27392861
await page.getByTestId("channel-general").click();

0 commit comments

Comments
 (0)