import * as React from "react"
import { IconCheck } from "@tabler/icons-react"
import { cn } from "@/registry/lib/utils"
/**
* `met`: a check that passes. `open`: one that does not yet. `optional`: an
* unscored nudge, drawn dashed so it reads as "you could also" and not as a
* check the reader is failing. There is deliberately no "failing" state: an
* amber or red mark on something a person is still in the middle of reads as
* an error where none was earned. A check that has really failed is an Alert.
*/
type ChecklistState = "met" | "open" | "optional"
const CHECKLIST_SIZE = {
sm: "text-1xs",
default: "text-xs",
lg: "text-sm",
} as const
type ChecklistSize = keyof typeof CHECKLIST_SIZE
// @use-when a read-only list of checks, each a mark and a label: met, not met
// yet, or optional. A readiness or quality panel, "what this still needs"
// before a step, password or domain requirements, a score's breakdown. Not
// Stepper, which is a sequence the user moves through, and not Checkbox, which
// is input the user sets.
function Checklist({
size = "default",
tone = "muted",
className,
style,
...props
}: React.ComponentProps<"ul"> & {
/** The label's type: `sm` 12px, `default` 13px, `lg` 14px. The mark stays
* 14px at every size, the kit's icon box, so a mark never outgrows the
* line it sits on. Pick the size of the text around the list. */
size?: ChecklistSize
/** `muted` for scored checks; `subtle`, a step quieter, for a list of
* optional nudges, so it reads as softer than the checks it sits beside. */
tone?: "muted" | "subtle"
}) {
return (
<ul
data-slot="checklist"
// `role="list"` because Safari drops list semantics from a `ul` whose
// markers are removed, and the count ("list, 4 items") is half of what
// a checklist tells a screen reader.
role="list"
className={cn(
"flex flex-col leading-normal",
CHECKLIST_SIZE[size],
tone === "muted" ? "text-muted-foreground" : "text-subtle-foreground",
className
)}
// `lines`: short lines that read as one unit, a step tighter than
// `cluster`, which is where they would stop being one list.
style={{ ...style, gap: "var(--spacing-rhythm-lines)" }}
{...props}
/>
)
}
const STATE_TEXT: Record<ChecklistState, string> = {
met: "Done: ",
open: "Not done: ",
optional: "Optional: ",
}
function ChecklistItem({
state,
className,
children,
...props
}: React.ComponentProps<"li"> & {
state: ChecklistState
}) {
return (
<li
data-slot="checklist-item"
data-state={state}
className={cn("flex items-start gap-2", className)}
{...props}
>
{/* One line tall and centred, so the mark lands on the FIRST line of a
label that wraps, without a margin nudging it there. */}
<span aria-hidden className="flex h-lh shrink-0 items-center">
{state === "met" ? (
// Met is the Stepper's completed tick on a filled success disc: the
// one mark in the list with colour, so "done" is findable at a glance.
// The tick is disc furniture, not a label glyph, so its stroke is
// pinned (1.25px of ink in a 10px box: stroke = ink * 24 / box)
// rather than reading `--stroke-icon`, which tracks label weight.
<span className="flex size-icon items-center justify-center rounded-full bg-success">
<IconCheck className="size-2.5 text-success-foreground [stroke-width:calc(1.25*24/10)]" />
</span>
) : (
<span
className={cn(
"size-icon rounded-full border border-border",
state === "optional" && "border-dashed"
)}
/>
)}
</span>
<span className="min-w-0 flex-1 text-pretty">
{/* The mark is hidden, so the state is said in words. Colour and
shape never carry it alone. */}
<span className="sr-only">{STATE_TEXT[state]}</span>
{children}
</span>
</li>
)
}
export { Checklist, ChecklistItem, type ChecklistState, type ChecklistSize }