@behavioral-agentic-design-system/core 0.8.1 → 0.8.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +3 -2
- package/scripts/build-tokens.cjs +126 -14
- package/src/app/layout.tsx +1 -0
- package/src/app/preview/cards/page.tsx +1 -1
- package/src/app/preview/components/page.tsx +89 -4
- package/src/app/shell.tsx +92 -26
- package/src/components/Foundations/Motion.stories.tsx +98 -56
- package/src/components/brand-mark.tsx +7 -1
- package/src/components/customize/brand-mark-section.tsx +14 -5
- package/src/components/customize/letter-spacing.tsx +1 -1
- package/src/components/customize/save-bar.tsx +1 -1
- package/src/components/customize-tab.tsx +1 -1
- package/src/components/home-tab.tsx +48 -36
- package/src/components/layers-tab.tsx +26 -7
- package/src/components/ui/alert.tsx +26 -14
- package/src/components/ui/button.tsx +7 -7
- package/src/components/ui/scroll-area.tsx +13 -1
- package/src/components/ui/sheet.tsx +4 -2
- package/src/components/ui/sonner.tsx +36 -9
- package/src/density.css +74 -0
- package/src/lib/brand-mark-tone.ts +140 -0
- package/src/styles.css +312 -12
- package/src/tokens.json +30 -0
|
@@ -5,24 +5,36 @@ import { cn } from "../../lib/utils"
|
|
|
5
5
|
|
|
6
6
|
const alertVariants = cva(
|
|
7
7
|
// Icon is explicitly pinned to row 1 / col 1 so it sits alongside the title,
|
|
8
|
-
// not auto-placed somewhere ambiguous.
|
|
9
|
-
//
|
|
10
|
-
// leading-tight ≈ 22px line-box
|
|
11
|
-
"relative grid w-full grid-cols-[0_1fr] items-start gap-y-1 rounded-lg border px-6 py-5 text-sm has-[>svg]:grid-cols-[calc(var(--spacing)*
|
|
8
|
+
// not auto-placed somewhere ambiguous. size-5 (20px) balances the text-lg
|
|
9
|
+
// title; mt-0.5 nudges its top down ~2px so the icon's vertical center aligns
|
|
10
|
+
// with the title's cap height (text-lg leading-tight ≈ 22px line-box).
|
|
11
|
+
"relative grid w-full grid-cols-[0_1fr] items-start gap-y-1.5 rounded-lg border px-6 py-5 text-sm has-[>svg]:grid-cols-[calc(var(--spacing)*5)_1fr] has-[>svg]:gap-x-3.5 [&>svg]:size-5 [&>svg]:row-start-1 [&>svg]:col-start-1 [&>svg]:mt-0.5 [&>svg]:text-current",
|
|
12
12
|
{
|
|
13
13
|
variants: {
|
|
14
14
|
variant: {
|
|
15
|
-
// bg-
|
|
16
|
-
// a callout even
|
|
17
|
-
//
|
|
18
|
-
|
|
19
|
-
//
|
|
20
|
-
//
|
|
21
|
-
//
|
|
22
|
-
//
|
|
23
|
-
//
|
|
15
|
+
// Subtle primary tint (bg-primary/5) gives default alerts a surface
|
|
16
|
+
// bump so they read as a callout even nested in a Card, plus a 4px
|
|
17
|
+
// primary left-accent bar + primary icon — the same colored-icon +
|
|
18
|
+
// left-accent language the Sonner toasts use, so alerts and
|
|
19
|
+
// notifications match.
|
|
20
|
+
//
|
|
21
|
+
// NOTE: the accent + border colors use arbitrary `border-[var(--token)]`
|
|
22
|
+
// with `!`, NOT the named `border-primary` / `border-destructive`
|
|
23
|
+
// utilities. In this Tailwind v4 setup the named border-color utilities
|
|
24
|
+
// don't generate a rule (the class lands on the element but no CSS is
|
|
25
|
+
// emitted), so the color silently fell through to the `:root *`
|
|
26
|
+
// `border-border` base reset (the bug that made these accents render
|
|
27
|
+
// grey). Arbitrary values always generate; `!` wins over `:root *`.
|
|
28
|
+
default: "border-border border-l-4 border-l-[var(--primary)]! bg-primary/5 text-foreground [&>svg]:text-primary dark:bg-primary/10",
|
|
29
|
+
// Full red border (all four sides) so the error reads as an error at a
|
|
30
|
+
// glance, with the 4px left bar as the accent. bg-destructive/5 tint
|
|
31
|
+
// stays subtle; composite text contrast (destructive on destructive/5
|
|
32
|
+
// over card) stays above AA 4.5:1 on the default palette. The border is
|
|
33
|
+
// the destructive token, audited non-text AAA (`destructive accent on
|
|
34
|
+
// page`, 7.4:1 light / 8.2:1 dark). Forks with very saturated
|
|
35
|
+
// destructive tokens should re-check with `pnpm wcag:audit`.
|
|
24
36
|
destructive:
|
|
25
|
-
"border-
|
|
37
|
+
"border-[var(--destructive)]! border-l-4 bg-destructive/5 text-destructive *:data-[slot=alert-description]:text-destructive/90 [&>svg]:text-current dark:bg-destructive/10",
|
|
26
38
|
},
|
|
27
39
|
},
|
|
28
40
|
defaultVariants: {
|
|
@@ -5,7 +5,7 @@ import { Slot } from "@radix-ui/react-slot"
|
|
|
5
5
|
import { cn } from "../../lib/utils"
|
|
6
6
|
|
|
7
7
|
const buttonVariants = cva(
|
|
8
|
-
"inline-flex shrink-0 items-center justify-center gap-2 rounded-
|
|
8
|
+
"inline-flex shrink-0 items-center justify-center gap-2 rounded-md text-sm font-medium whitespace-nowrap transition-all outline-none focus-visible:border-ring focus-visible:ring-[3px] focus-visible:ring-ring/50 disabled:pointer-events-none disabled:opacity-50 aria-invalid:border-destructive aria-invalid:ring-destructive/20 dark:aria-invalid:ring-destructive/40 [&_svg]:pointer-events-none [&_svg]:shrink-0 [&_svg:not([class*='size-'])]:size-4",
|
|
9
9
|
{
|
|
10
10
|
variants: {
|
|
11
11
|
variant: {
|
|
@@ -23,13 +23,13 @@ const buttonVariants = cva(
|
|
|
23
23
|
},
|
|
24
24
|
size: {
|
|
25
25
|
default: "h-9 px-4 py-2",
|
|
26
|
-
xs: "h-6 gap-1
|
|
27
|
-
sm: "h-8 gap-1.5
|
|
28
|
-
lg: "h-10
|
|
29
|
-
xl: "h-11
|
|
30
|
-
"2xl": "h-12
|
|
26
|
+
xs: "h-6 gap-1 px-2 text-xs [&_svg:not([class*='size-'])]:size-3",
|
|
27
|
+
sm: "h-8 gap-1.5 px-3",
|
|
28
|
+
lg: "h-10 px-8",
|
|
29
|
+
xl: "h-11 px-10 text-base [&_svg:not([class*='size-'])]:size-5",
|
|
30
|
+
"2xl": "h-12 px-12 text-base font-semibold [&_svg:not([class*='size-'])]:size-5",
|
|
31
31
|
icon: "size-9",
|
|
32
|
-
"icon-xs": "size-6
|
|
32
|
+
"icon-xs": "size-6 [&_svg:not([class*='size-'])]:size-3",
|
|
33
33
|
"icon-sm": "size-8",
|
|
34
34
|
"icon-lg": "size-10",
|
|
35
35
|
"icon-xl": "size-11 [&_svg:not([class*='size-'])]:size-5",
|
|
@@ -23,7 +23,19 @@ function ScrollArea({
|
|
|
23
23
|
// scrollable-region-focusable). The focus-visible ring below already
|
|
24
24
|
// anticipates this — Radix's Viewport just isn't focusable on its own.
|
|
25
25
|
tabIndex={0}
|
|
26
|
-
|
|
26
|
+
// Radix's Viewport wraps `children` in a div it inline-styles as
|
|
27
|
+
// `display:table; min-width:100%`. `display:table` shrink-wraps to the
|
|
28
|
+
// widest unbreakable descendant (a long <pre> line, a whitespace-nowrap
|
|
29
|
+
// badge), so on a narrow viewport that wrapper balloons past the
|
|
30
|
+
// viewport width and the ScrollArea scrolls horizontally instead of the
|
|
31
|
+
// inner element's own `overflow-x-auto` kicking in — content gets
|
|
32
|
+
// clipped on mobile. Forcing the wrapper back to a normal block (still
|
|
33
|
+
// min-w-full for the vertical scrollbar math) makes it honor the
|
|
34
|
+
// viewport width so children wrap/scroll within their own box. This is
|
|
35
|
+
// the canonical fix for ScrollArea horizontal-overflow on mobile and is
|
|
36
|
+
// safe because no BADS surface relies on ScrollArea's own horizontal
|
|
37
|
+
// scroll (horizontal cases use a dedicated overflow-x-auto wrapper).
|
|
38
|
+
className="size-full rounded-[inherit] transition-[color,box-shadow] outline-none focus-visible:ring-[3px] focus-visible:ring-ring/50 focus-visible:outline-1 [&>div]:!block [&>div]:!min-w-0 [&>div]:w-full"
|
|
27
39
|
>
|
|
28
40
|
{children}
|
|
29
41
|
</ScrollAreaPrimitive.Viewport>
|
|
@@ -84,7 +84,7 @@ function SheetContent({
|
|
|
84
84
|
{showCloseButton && (
|
|
85
85
|
<SheetPrimitive.Close
|
|
86
86
|
data-slot="sheet-close"
|
|
87
|
-
className="absolute top-
|
|
87
|
+
className="absolute top-3 right-3 inline-flex size-9 items-center justify-center rounded-md text-foreground ring-offset-background transition-colors hover:bg-accent hover:text-accent-foreground focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus:outline-hidden disabled:pointer-events-none [&_svg]:pointer-events-none [&_svg]:shrink-0 [&_svg:not([class*='size-'])]:size-4"
|
|
88
88
|
style={{ strokeWidth: '1.5px' }}
|
|
89
89
|
>
|
|
90
90
|
<XIcon />
|
|
@@ -100,7 +100,9 @@ function SheetHeader({ className, ...props }: React.ComponentProps<"div">) {
|
|
|
100
100
|
return (
|
|
101
101
|
<div
|
|
102
102
|
data-slot="sheet-header"
|
|
103
|
-
|
|
103
|
+
// pr-12 reserves space for the absolutely-positioned close button (top-3
|
|
104
|
+
// right-3, size-9) so the title/description can never run underneath it.
|
|
105
|
+
className={cn("flex flex-col gap-1.5 p-4 pr-12", className)}
|
|
104
106
|
{...props}
|
|
105
107
|
/>
|
|
106
108
|
)
|
|
@@ -18,11 +18,11 @@ const Toaster = ({ ...props }: ToasterProps) => {
|
|
|
18
18
|
theme={theme as ToasterProps["theme"]}
|
|
19
19
|
className="toaster group"
|
|
20
20
|
icons={{
|
|
21
|
-
success: <CircleCheckIcon className="size-
|
|
22
|
-
info: <InfoIcon className="size-
|
|
23
|
-
warning: <TriangleAlertIcon className="size-
|
|
24
|
-
error: <OctagonXIcon className="size-
|
|
25
|
-
loading: <Loader2Icon className="size-
|
|
21
|
+
success: <CircleCheckIcon className="size-[50px] text-[var(--success)]" strokeWidth={1.5} />,
|
|
22
|
+
info: <InfoIcon className="size-[50px] text-[var(--info)]" strokeWidth={1.5} />,
|
|
23
|
+
warning: <TriangleAlertIcon className="size-[50px] text-[var(--warning)]" strokeWidth={1.5} />,
|
|
24
|
+
error: <OctagonXIcon className="size-[50px] text-[var(--destructive)]" strokeWidth={1.5} />,
|
|
25
|
+
loading: <Loader2Icon className="size-[50px] animate-spin" strokeWidth={1.5} />,
|
|
26
26
|
}}
|
|
27
27
|
style={
|
|
28
28
|
{
|
|
@@ -38,16 +38,43 @@ const Toaster = ({ ...props }: ToasterProps) => {
|
|
|
38
38
|
// regardless of where --border lands on the fork's palette. Sonner's
|
|
39
39
|
// default leans on --border, which can be invisible on tonal forks.
|
|
40
40
|
toast:
|
|
41
|
-
"group toast group-[.toaster]:bg-popover group-[.toaster]:text-popover-foreground group-[.toaster]:border group-[.toaster]:border-foreground/10 group-[.toaster]:shadow-lg",
|
|
41
|
+
"group toast group-[.toaster]:bg-popover group-[.toaster]:text-popover-foreground group-[.toaster]:border group-[.toaster]:border-foreground/10 group-[.toaster]:shadow-lg group-[.toaster]:items-start!",
|
|
42
|
+
// 50px status icon, top-aligned to the text. Sonner centers the icon
|
|
43
|
+
// row by default (align-items: center) and gives [data-icon] its own
|
|
44
|
+
// alignment + margin — `!` + self-start pins the icon to the top edge
|
|
45
|
+
// of the title instead of centering it against the full text block.
|
|
46
|
+
icon: "size-[50px]! shrink-0 self-start! mt-0.5 mr-3",
|
|
42
47
|
title: "text-base font-semibold",
|
|
43
|
-
// Sonner
|
|
44
|
-
//
|
|
48
|
+
// Sonner hard-codes the description color via a high-specificity
|
|
49
|
+
// selector (`[data-sonner-toast] [data-description]`, 0,3,0) that
|
|
50
|
+
// out-ranks a plain `text-muted-foreground` utility — so BADS' token
|
|
51
|
+
// silently lost and the description rendered at Sonner's fixed grey
|
|
52
|
+
// rather than ours (faint / off-brand depending on CSS load order).
|
|
53
|
+
// The `!` important modifier makes the AAA muted-foreground token win
|
|
54
|
+
// deterministically in both light and dark, putting the design system
|
|
55
|
+
// back in control of the description contrast.
|
|
45
56
|
description:
|
|
46
|
-
"text-sm text-muted-foreground leading-relaxed
|
|
57
|
+
"text-sm! text-muted-foreground! leading-relaxed",
|
|
47
58
|
actionButton:
|
|
48
59
|
"group-[.toast]:bg-primary group-[.toast]:text-primary-foreground",
|
|
49
60
|
cancelButton:
|
|
50
61
|
"group-[.toast]:bg-muted group-[.toast]:text-muted-foreground",
|
|
62
|
+
// Per-variant status color: a 4px left accent bar in the status
|
|
63
|
+
// token. The icon (set in `icons` above) carries the same color.
|
|
64
|
+
// Surface + text stay neutral (popover / popover-foreground) so body
|
|
65
|
+
// copy keeps its AAA contrast — only the non-text accent + icon are
|
|
66
|
+
// colored. Tokens are tuned ≥3:1 on popover in both themes
|
|
67
|
+
// (see wcag:audit "<status> accent on popover").
|
|
68
|
+
//
|
|
69
|
+
// `!` important: Sonner draws the toast border via a high-specificity
|
|
70
|
+
// `[data-sonner-toast]` rule that out-ranks a plain border utility
|
|
71
|
+
// (same reason the description color above needs `!`). Important makes
|
|
72
|
+
// the left edge win deterministically; the other three sides keep
|
|
73
|
+
// Sonner's neutral 1px border.
|
|
74
|
+
success: "border-l-4! border-l-[var(--success)]!",
|
|
75
|
+
warning: "border-l-4! border-l-[var(--warning)]!",
|
|
76
|
+
info: "border-l-4! border-l-[var(--info)]!",
|
|
77
|
+
error: "border-l-4! border-l-[var(--destructive)]!",
|
|
51
78
|
},
|
|
52
79
|
}}
|
|
53
80
|
{...props}
|
package/src/density.css
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
/* BADS density — opt-in.
|
|
2
|
+
*
|
|
3
|
+
* Set `data-density="compact"` or `data-density="comfortable"` on any
|
|
4
|
+
* container (a section, a card grid, the whole app) and the surfaces inside
|
|
5
|
+
* it tighten or loosen. Default (no attribute) is the canonical spacing.
|
|
6
|
+
*
|
|
7
|
+
* AAA SAFETY (load-bearing): density adjusts ONLY non-interactive rhythm —
|
|
8
|
+
* container padding, stack gaps, and data-table row height. It deliberately
|
|
9
|
+
* does NOT scale Button / Input / Select / Switch / Checkbox / Radio or any
|
|
10
|
+
* other hit area, because BADS mandates a 44×44 touch-target floor
|
|
11
|
+
* (WCAG 2.2 AAA, SC 2.5.5). shadcn's "Rhea" compact style shrinks control
|
|
12
|
+
* heights below that; BADS can't and won't. Compact here means a denser
|
|
13
|
+
* *layout*, not smaller targets.
|
|
14
|
+
*
|
|
15
|
+
* Mechanism: attribute selectors keyed on `data-slot` (BADS components stamp
|
|
16
|
+
* these). `[data-density=…] [data-slot=…]` is specificity (0,2,0), which beats
|
|
17
|
+
* Tailwind's (0,1,0) utilities, so these override the components' default
|
|
18
|
+
* padding without !important. The rules are inert until the attribute is set.
|
|
19
|
+
*
|
|
20
|
+
* Usage (consumer): `@import "@behavioral-agentic-design-system/core/density.css";`
|
|
21
|
+
* then `<div data-density="compact">…</div>`. Extend per-component by adding a
|
|
22
|
+
* `[data-density=compact] [data-slot=<your-slot>]` rule here.
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
[data-density="compact"] {
|
|
26
|
+
--density: compact;
|
|
27
|
+
}
|
|
28
|
+
[data-density="comfortable"] {
|
|
29
|
+
--density: comfortable;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/* ---- Card --------------------------------------------------------------- */
|
|
33
|
+
[data-density="compact"] [data-slot="card"] {
|
|
34
|
+
gap: 0.5rem;
|
|
35
|
+
}
|
|
36
|
+
[data-density="compact"] [data-slot="card-header"],
|
|
37
|
+
[data-density="compact"] [data-slot="card-content"],
|
|
38
|
+
[data-density="compact"] [data-slot="card-footer"] {
|
|
39
|
+
padding-left: 1rem;
|
|
40
|
+
padding-right: 1rem;
|
|
41
|
+
padding-top: 0.625rem;
|
|
42
|
+
padding-bottom: 0.625rem;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
[data-density="comfortable"] [data-slot="card"] {
|
|
46
|
+
gap: 1.25rem;
|
|
47
|
+
}
|
|
48
|
+
[data-density="comfortable"] [data-slot="card-header"],
|
|
49
|
+
[data-density="comfortable"] [data-slot="card-content"],
|
|
50
|
+
[data-density="comfortable"] [data-slot="card-footer"] {
|
|
51
|
+
padding-left: 2rem;
|
|
52
|
+
padding-right: 2rem;
|
|
53
|
+
padding-top: 1.5rem;
|
|
54
|
+
padding-bottom: 1.5rem;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/* ---- Data table (row rhythm only; cell *content* keeps its own size) ---- */
|
|
58
|
+
[data-density="compact"] [data-slot="table-cell"] {
|
|
59
|
+
padding-top: 0.25rem;
|
|
60
|
+
padding-bottom: 0.25rem;
|
|
61
|
+
}
|
|
62
|
+
[data-density="compact"] [data-slot="table-head"] {
|
|
63
|
+
height: 2rem;
|
|
64
|
+
}
|
|
65
|
+
[data-density="comfortable"] [data-slot="table-cell"] {
|
|
66
|
+
padding-top: 0.75rem;
|
|
67
|
+
padding-bottom: 0.75rem;
|
|
68
|
+
}
|
|
69
|
+
[data-density="comfortable"] [data-slot="table-head"] {
|
|
70
|
+
height: 3rem;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/* Reduced-motion / forced-colors need no special handling: this file only
|
|
74
|
+
* changes spacing, never color or motion. */
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Brand-mark tone classifier (client-safe — no @xmldom).
|
|
3
|
+
*
|
|
4
|
+
* Decides whether a brand mark would be hard to see against the current
|
|
5
|
+
* surface, so the renderer can auto-invert it in the offending theme. The
|
|
6
|
+
* goal: a black logo stays black in light mode but flips to white in dark
|
|
7
|
+
* mode (and vice-versa) without the designer hand-editing the SVG per theme.
|
|
8
|
+
*
|
|
9
|
+
* Rules:
|
|
10
|
+
* - A mark that uses `currentColor` is **adaptive** — the monotone path in
|
|
11
|
+
* svg-sanitize already rewrote it, so it follows the text color on its own.
|
|
12
|
+
* Never filter those (inverting would fight the currentColor adaptation).
|
|
13
|
+
* - A mark whose every resolvable color is dark is **dark** → invert it in
|
|
14
|
+
* dark mode. Every color light → **light** → invert it in light mode.
|
|
15
|
+
* - Anything with a mix of light and dark fixed colors is **mixed** → left
|
|
16
|
+
* exactly as authored, because inverting a multi-color logo wrecks the brand
|
|
17
|
+
* colors. Same for marks whose colors we can't resolve (oklch/hsl/exotic
|
|
18
|
+
* named) — we don't guess.
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
export type MarkTone = "adaptive" | "dark" | "light" | "mixed";
|
|
22
|
+
|
|
23
|
+
// Minimal named-color table — the ones that actually show up in logos. Unknown
|
|
24
|
+
// names resolve to null (unclassifiable) rather than a wrong guess.
|
|
25
|
+
const NAMED_HEX: Record<string, string> = {
|
|
26
|
+
black: "#000000",
|
|
27
|
+
white: "#ffffff",
|
|
28
|
+
gray: "#808080",
|
|
29
|
+
grey: "#808080",
|
|
30
|
+
silver: "#c0c0c0",
|
|
31
|
+
red: "#ff0000",
|
|
32
|
+
green: "#008000",
|
|
33
|
+
lime: "#00ff00",
|
|
34
|
+
blue: "#0000ff",
|
|
35
|
+
navy: "#000080",
|
|
36
|
+
yellow: "#ffff00",
|
|
37
|
+
orange: "#ffa500",
|
|
38
|
+
purple: "#800080",
|
|
39
|
+
teal: "#008080",
|
|
40
|
+
maroon: "#800000",
|
|
41
|
+
olive: "#808000",
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
function srgbToLinear(channel: number): number {
|
|
45
|
+
const s = channel / 255;
|
|
46
|
+
return s <= 0.03928 ? s / 12.92 : Math.pow((s + 0.055) / 1.055, 2.4);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function relativeLuminance(r: number, g: number, b: number): number {
|
|
50
|
+
return 0.2126 * srgbToLinear(r) + 0.7152 * srgbToLinear(g) + 0.0722 * srgbToLinear(b);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** Returns 0–1 relative luminance, or null if the color can't be resolved. */
|
|
54
|
+
function luminanceOf(raw: string): number | null {
|
|
55
|
+
let v = raw.trim().toLowerCase();
|
|
56
|
+
if (!v || v === "none" || v === "transparent" || v === "inherit" || v === "currentcolor") {
|
|
57
|
+
return null;
|
|
58
|
+
}
|
|
59
|
+
if (NAMED_HEX[v]) v = NAMED_HEX[v];
|
|
60
|
+
if (/^#[0-9a-f]{3}$/.test(v)) {
|
|
61
|
+
v = `#${v[1]}${v[1]}${v[2]}${v[2]}${v[3]}${v[3]}`;
|
|
62
|
+
}
|
|
63
|
+
if (/^#[0-9a-f]{6}$/.test(v)) {
|
|
64
|
+
const r = parseInt(v.slice(1, 3), 16);
|
|
65
|
+
const g = parseInt(v.slice(3, 5), 16);
|
|
66
|
+
const b = parseInt(v.slice(5, 7), 16);
|
|
67
|
+
return relativeLuminance(r, g, b);
|
|
68
|
+
}
|
|
69
|
+
const rgb = v.match(/^rgba?\(([^)]+)\)$/);
|
|
70
|
+
if (rgb) {
|
|
71
|
+
const parts = rgb[1]
|
|
72
|
+
.split(/[,\s/]+/)
|
|
73
|
+
.filter(Boolean)
|
|
74
|
+
.slice(0, 3)
|
|
75
|
+
.map(Number);
|
|
76
|
+
if (parts.length === 3 && parts.every((n) => !Number.isNaN(n))) {
|
|
77
|
+
return relativeLuminance(parts[0], parts[1], parts[2]);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
return null;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function colorTokens(svg: string): string[] {
|
|
84
|
+
const tokens: string[] = [];
|
|
85
|
+
let m: RegExpExecArray | null;
|
|
86
|
+
|
|
87
|
+
const attrRe = /(?:fill|stroke|stop-color)\s*=\s*["']([^"']+)["']/gi;
|
|
88
|
+
while ((m = attrRe.exec(svg))) tokens.push(m[1]);
|
|
89
|
+
|
|
90
|
+
const styleRe = /style\s*=\s*["']([^"']+)["']/gi;
|
|
91
|
+
while ((m = styleRe.exec(svg))) {
|
|
92
|
+
for (const decl of m[1].split(";")) {
|
|
93
|
+
const [prop, val] = decl.split(":").map((s) => s?.trim());
|
|
94
|
+
if (val && (prop?.toLowerCase() === "fill" || prop?.toLowerCase() === "stroke")) {
|
|
95
|
+
tokens.push(val);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
return tokens;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
// Luminance below this reads as "dark"; above LIGHT reads as "light". The gap
|
|
103
|
+
// in between is deliberately wide so mid-tone marks fall through to "mixed"
|
|
104
|
+
// and are left alone rather than aggressively inverted.
|
|
105
|
+
const DARK_MAX = 0.22;
|
|
106
|
+
const LIGHT_MIN = 0.6;
|
|
107
|
+
|
|
108
|
+
export function classifyMarkTone(svg: string | null): MarkTone {
|
|
109
|
+
if (!svg) return "adaptive";
|
|
110
|
+
const tokens = colorTokens(svg);
|
|
111
|
+
// Any currentColor usage means the mark already adapts to the theme.
|
|
112
|
+
if (tokens.some((t) => t.trim().toLowerCase() === "currentcolor")) return "adaptive";
|
|
113
|
+
|
|
114
|
+
const lums: number[] = [];
|
|
115
|
+
for (const t of tokens) {
|
|
116
|
+
const l = luminanceOf(t);
|
|
117
|
+
if (l !== null) lums.push(l);
|
|
118
|
+
}
|
|
119
|
+
if (lums.length === 0) return "adaptive"; // nothing fixed to worry about
|
|
120
|
+
|
|
121
|
+
if (lums.every((l) => l <= DARK_MAX)) return "dark";
|
|
122
|
+
if (lums.every((l) => l >= LIGHT_MIN)) return "light";
|
|
123
|
+
return "mixed";
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Tailwind class that inverts the mark only when it would be low-contrast.
|
|
128
|
+
*
|
|
129
|
+
* Pass `mode` for the Customize preview tiles, which fake light/dark via
|
|
130
|
+
* explicit backgrounds instead of the `.dark` class. Omit it on real surfaces
|
|
131
|
+
* — the app toggles `.dark` on <html>, so the `dark:` variants fire.
|
|
132
|
+
*/
|
|
133
|
+
export function markToneFilterClass(tone: MarkTone, mode?: "light" | "dark"): string {
|
|
134
|
+
if (tone === "adaptive" || tone === "mixed") return "";
|
|
135
|
+
if (mode) {
|
|
136
|
+
const invert = (tone === "dark" && mode === "dark") || (tone === "light" && mode === "light");
|
|
137
|
+
return invert ? "invert" : "";
|
|
138
|
+
}
|
|
139
|
+
return tone === "dark" ? "dark:invert" : "invert dark:invert-0";
|
|
140
|
+
}
|