import { useSyncExternalStore } from "react"
import { Inline } from "~/components/ui/inline"
import { Stack } from "~/components/ui/stack"
import { Text } from "~/components/ui/text"
// Each surface with the text color meant for it.
const pairs = [
{
name: "background",
className:
"flex h-20 flex-col justify-between rounded-lg p-md ring-1 ring-border bg-background text-foreground",
},
{
name: "surface",
className:
"flex h-20 flex-col justify-between rounded-lg p-md ring-1 ring-border bg-surface text-surface-foreground",
},
{
name: "card",
className:
"flex h-20 flex-col justify-between rounded-lg p-md ring-1 ring-border bg-card text-card-foreground",
},
{
name: "overlay",
className:
"flex h-20 flex-col justify-between rounded-lg p-md ring-1 ring-border bg-overlay text-overlay-foreground",
},
{
name: "primary",
className:
"flex h-20 flex-col justify-between rounded-lg p-md ring-1 ring-border bg-primary text-primary-foreground",
},
{
name: "secondary",
className:
"flex h-20 flex-col justify-between rounded-lg p-md ring-1 ring-border bg-secondary text-secondary-foreground",
},
{
name: "muted",
className:
"flex h-20 flex-col justify-between rounded-lg p-md ring-1 ring-border bg-muted text-muted-foreground",
},
{
name: "accent",
className:
"flex h-20 flex-col justify-between rounded-lg p-md ring-1 ring-border bg-accent text-accent-foreground",
},
{
name: "destructive",
className:
"flex h-20 flex-col justify-between rounded-lg p-md ring-1 ring-border bg-destructive text-destructive-foreground",
},
{
name: "warning",
className:
"flex h-20 flex-col justify-between rounded-lg p-md ring-1 ring-border bg-warning text-warning-foreground",
},
{
name: "success",
className:
"flex h-20 flex-col justify-between rounded-lg p-md ring-1 ring-border bg-success text-success-foreground",
},
]
// Colors for lines and states, with no text on them.
const lines = [
{ name: "border", className: "size-4 rounded-sm ring-1 ring-border bg-border" },
{ name: "input", className: "size-4 rounded-sm ring-1 ring-border bg-input" },
{ name: "ring", className: "size-4 rounded-sm ring-1 ring-border bg-ring" },
{ name: "backdrop", className: "size-4 rounded-sm ring-1 ring-border bg-backdrop" },
]
const names = [...pairs, ...lines].map((color) => color.name)
/** The colors this system's theme sets, space-separated: a system may not have every one. */
function definedNames(): string {
const style = getComputedStyle(document.documentElement)
return names.filter((name) => style.getPropertyValue(`--${name}`).trim() !== "").join(" ")
}
// The theme doesn't change while the page is open, so there's nothing to subscribe to.
const subscribe = () => () => {}
export default function Example() {
const defined = new Set(
useSyncExternalStore(subscribe, definedNames, () => names.join(" ")).split(" "),
)
return (
<Stack gap="xl" className="w-full">
<div className="grid grid-cols-2 gap-sm sm:grid-cols-3 lg:grid-cols-4">
{pairs
.filter((color) => defined.has(color.name))
.map((color) => (
<div key={color.name} className={color.className}>
<Text size="lg" weight="semibold" family="heading">
Aa
</Text>
<Text size="xs" family="mono">
{color.name}
</Text>
</div>
))}
</div>
<Inline gap="lg">
{lines
.filter((color) => defined.has(color.name))
.map((color) => (
<Inline key={color.name} gap="xs">
<span className={color.className} />
<Text render={<span />} size="xs" family="mono" tone="muted">
{color.name}
</Text>
</Inline>
))}
</Inline>
</Stack>
)
}