Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion components/danger-button.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
* That token is a near-white on the light-theme red (4.6:1) and a near-black on
* the lightened dark-theme red (6.9:1), so the label clears WCAG AA in both
* themes. Override the fill or the label, never the two independently.
*
* The `dark:` fills repeat the solid fill because the `destructive` variant sets
* its own `dark:bg-destructive/20`, which would otherwise win in the dark theme
* and put the near-black label on a dark, mostly transparent fill.
*/
export const DANGER_BUTTON_CLASS =
"bg-destructive text-destructive-foreground hover:bg-destructive/95 focus-visible:border-destructive/40 focus-visible:ring-destructive/30"
"bg-destructive text-destructive-foreground hover:bg-destructive/95 focus-visible:border-destructive/40 focus-visible:ring-destructive/30 dark:bg-destructive dark:hover:bg-destructive/95"
26 changes: 26 additions & 0 deletions tests/lib/danger-button-dark.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import test from "node:test"
import assert from "node:assert/strict"
import fs from "node:fs"

import { DANGER_BUTTON_CLASS } from "../../components/danger-button"
import { cn } from "../../lib/utils"

// Confirmation buttons combine the soft destructive Button variant with DANGER_BUTTON_CLASS.
// The test runner cannot import .tsx, so read the variant classes from the source.
const buttonSource = fs.readFileSync("components/ui/button.tsx", "utf8")
const destructiveVariant = buttonSource.match(/destructive:\s*"([^"]+)"/)?.[1] ?? ""
const merged = cn(destructiveVariant, DANGER_BUTTON_CLASS).split(" ")

test("the soft destructive variant tints its fill in the dark theme", () => {
assert.ok(destructiveVariant.split(" ").includes("dark:bg-destructive/20"))
})

test("filled danger button keeps a solid fill in the dark theme", () => {
assert.ok(merged.includes("dark:bg-destructive"))
assert.ok(!merged.includes("dark:bg-destructive/20"))
})

test("filled danger button keeps a solid hover fill in the dark theme", () => {
assert.ok(merged.includes("dark:hover:bg-destructive/95"))
assert.ok(!merged.includes("dark:hover:bg-destructive/30"))
})