Skip to content

Record your screen, ship a demo. Free and open-source, GPU-accelerated, no watermarks, no subscriptions. Windows, macOS, Linux. Actively maintained.

fix(toasts): keep the close button's border off when it is hovered

Nicolas Gonzalezec4c5a7f7f04
diff --git a/src/components/ui/sonner.test.tsx b/src/components/ui/sonner.test.tsx
index a34108fab..ae26c3a61 100644
--- a/src/components/ui/sonner.test.tsx
+++ b/src/components/ui/sonner.test.tsx
@@ -75,3 +75,35 @@ describe("close button placement contract", () => {
});
}
});
+
+// jsdom does not cascade, so it cannot show which rule wins. What it can pin is where the
+// colours live: sonner's dark theme sets the button's colour at (0,4,0), so a colour passed
+// as a Tailwind utility through `classNames` (0,3,0) silently never applies, and the one
+// place that does apply is the `html`-prefixed override in src/index.css.
+describe("close button colours", () => {
+ const root = path.resolve(__dirname, "..", "..", "..");
+ const appCss = readFileSync(path.join(root, "src/index.css"), "utf8");
+ const closeRule = (suffix: string) => {
+ const selector = `html [data-sonner-toaster] [data-sonner-toast][data-styled="true"] [data-close-button]${suffix} {`;
+ const start = appCss.indexOf(selector);
+ expect(start, selector).toBeGreaterThanOrEqual(0);
+ return appCss.slice(start, appCss.indexOf("}", start));
+ };
+
+ it("sets the resting and hover colour in the override that outranks sonner", () => {
+ expect(closeRule("")).toMatch(/^\s*color:/m);
+ expect(closeRule(":hover")).toMatch(/^\s*color:/m);
+ });
+
+ // Sonner's dark `:hover` rule outranks the base override and restores its border, so the
+ // "no border" decision has to be repeated on hover or a ring appears under the pointer.
+ it("keeps the border off on hover as well as at rest", () => {
+ expect(closeRule("")).toMatch(/border-color:\s*transparent/);
+ expect(closeRule(":hover")).toMatch(/border-color:\s*transparent/);
+ });
+
+ it("does not pass them through classNames, where they lose", () => {
+ const toaster = readFileSync(path.join(root, "src/components/ui/sonner.tsx"), "utf8");
+ expect(toaster).not.toMatch(/closeButton:\s*"/);
+ });
+});
diff --git a/src/index.css b/src/index.css
index 784b8a24f..2a92cfc26 100644
--- a/src/index.css
+++ b/src/index.css
@@ -388,7 +388,12 @@ html [data-sonner-toaster] [data-sonner-toast][data-styled="true"] [data-close-b
transition: background 0.12s ease-out, color 0.12s ease-out;
}
+/* `border-color` again, on purpose. The base rule's `transparent` loses on hover: sonner's
+ `:hover` rule sits at (0,5,0), above that rule's (0,4,1), and brings back
+ `--normal-border-hover`, a ring around a cross that has no border at rest. The `html`
+ keeps this one at (0,5,1). */
html [data-sonner-toaster] [data-sonner-toast][data-styled="true"] [data-close-button]:hover {
+ border-color: transparent;
background: color-mix(in oklab, var(--fg) 10%, transparent);
color: var(--fg);
}