fix: add nous-research/ui package

This commit is contained in:
Austin Pickett 2026-04-19 10:48:56 -04:00
parent 957ca79e8e
commit 923539a46b
26 changed files with 798 additions and 637 deletions

View file

@ -1,53 +1,54 @@
import { useState, useRef, useEffect, useCallback } from "react";
import { useCallback, useEffect, useRef, useState } from "react";
import { Palette, Check } from "lucide-react";
import { useTheme } from "@/themes";
import { BUILTIN_THEMES, useTheme } from "@/themes";
import { useI18n } from "@/i18n";
import { cn } from "@/lib/utils";
/**
* Compact theme picker for the dashboard header.
* Shows a palette icon + current theme name; opens a dropdown of all
* available themes with color swatches for instant preview.
* Compact theme picker mounted next to the language switcher in the header.
* Each dropdown row shows a 3-stop swatch (background / midground / warm
* glow) so users can preview the palette before committing. User-defined
* themes from `~/.hermes/dashboard-themes/*.yaml` that aren't in
* `BUILTIN_THEMES` render without swatches and apply the default palette.
*/
export function ThemeSwitcher() {
const { themeName, availableThemes, setTheme } = useTheme();
const { t } = useI18n();
const [open, setOpen] = useState(false);
const ref = useRef<HTMLDivElement>(null);
const wrapperRef = useRef<HTMLDivElement>(null);
const close = useCallback(() => setOpen(false), []);
// Close on outside click.
useEffect(() => {
if (!open) return;
const handler = (e: MouseEvent) => {
if (ref.current && !ref.current.contains(e.target as Node)) close();
const onMouseDown = (e: MouseEvent) => {
if (wrapperRef.current && !wrapperRef.current.contains(e.target as Node)) {
close();
}
};
document.addEventListener("mousedown", handler);
return () => document.removeEventListener("mousedown", handler);
}, [open, close]);
// Close on Escape.
useEffect(() => {
if (!open) return;
const handler = (e: KeyboardEvent) => {
const onKey = (e: KeyboardEvent) => {
if (e.key === "Escape") close();
};
document.addEventListener("keydown", handler);
return () => document.removeEventListener("keydown", handler);
document.addEventListener("mousedown", onMouseDown);
document.addEventListener("keydown", onKey);
return () => {
document.removeEventListener("mousedown", onMouseDown);
document.removeEventListener("keydown", onKey);
};
}, [open, close]);
const current = availableThemes.find((t) => t.name === themeName);
const current = availableThemes.find((th) => th.name === themeName);
const label = current?.label ?? themeName;
return (
<div ref={ref} className="relative">
<div ref={wrapperRef} className="relative">
<button
type="button"
onClick={() => setOpen((o) => !o)}
className={cn(
"group relative inline-flex items-center gap-1.5 px-2 py-1 text-xs",
"text-muted-foreground hover:text-foreground transition-colors",
"cursor-pointer focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring",
"text-muted-foreground hover:text-foreground transition-colors cursor-pointer",
"focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-midground",
)}
title={t.theme?.switchTheme ?? "Switch theme"}
aria-label={t.theme?.switchTheme ?? "Switch theme"}
@ -55,56 +56,66 @@ export function ThemeSwitcher() {
aria-haspopup="listbox"
>
<Palette className="h-3.5 w-3.5" />
<span className="hidden sm:inline font-display tracking-wide uppercase text-[0.65rem]">
{current?.label ?? themeName}
<span className="hidden sm:inline font-mondwest tracking-wide uppercase text-[0.65rem]">
{label}
</span>
</button>
{open && (
<div
role="listbox"
aria-label={t.theme?.title ?? "Theme"}
className={cn(
"absolute right-0 top-full mt-1 z-50 min-w-[200px]",
"border border-border bg-popover text-popover-foreground shadow-lg",
"animate-[fade-in_100ms_ease-out]",
"absolute right-0 top-full mt-1 z-50 min-w-[240px]",
"border border-current/20 bg-background-base/95 backdrop-blur-sm",
"shadow-[0_12px_32px_-8px_rgba(0,0,0,0.6)]",
)}
>
<div className="px-3 py-2 border-b border-border">
<span className="font-display text-[0.7rem] tracking-[0.12em] uppercase text-muted-foreground">
<div className="border-b border-current/20 px-3 py-2">
<span className="font-mondwest text-[0.65rem] tracking-[0.15em] uppercase text-midground/70">
{t.theme?.title ?? "Theme"}
</span>
</div>
{availableThemes.map((theme) => {
const isActive = theme.name === themeName;
{availableThemes.map((th) => {
const isActive = th.name === themeName;
const preset = BUILTIN_THEMES[th.name];
return (
<button
key={theme.name}
key={th.name}
type="button"
role="option"
aria-selected={isActive}
onClick={() => {
setTheme(theme.name);
setTheme(th.name);
close();
}}
className={cn(
"flex w-full items-center gap-2.5 px-3 py-2 text-left text-sm transition-colors cursor-pointer",
"hover:bg-foreground/10",
isActive ? "text-foreground" : "text-muted-foreground",
"flex w-full items-center gap-3 px-3 py-2 text-left transition-colors cursor-pointer",
"hover:bg-midground/10",
isActive ? "text-midground" : "text-midground/60",
)}
>
{preset ? <ThemeSwatch theme={preset.name} /> : <PlaceholderSwatch />}
<div className="flex min-w-0 flex-1 flex-col gap-0.5">
<span className="truncate font-mondwest text-[0.75rem] tracking-wide uppercase">
{th.label}
</span>
{th.description && (
<span className="truncate font-sans text-[0.65rem] normal-case tracking-normal text-midground/50">
{th.description}
</span>
)}
</div>
<Check
className={cn(
"h-3 w-3 shrink-0",
"h-3 w-3 shrink-0 text-midground",
isActive ? "opacity-100" : "opacity-0",
)}
/>
<div className="flex flex-col gap-0.5 min-w-0">
<span className="font-medium text-xs truncate">{theme.label}</span>
<span className="text-[0.65rem] text-muted-foreground truncate">
{theme.description}
</span>
</div>
</button>
);
})}
@ -113,3 +124,28 @@ export function ThemeSwitcher() {
</div>
);
}
function ThemeSwatch({ theme }: { theme: string }) {
const preset = BUILTIN_THEMES[theme];
if (!preset) return <PlaceholderSwatch />;
const { background, midground, warmGlow } = preset.palette;
return (
<div
aria-hidden
className="flex h-4 w-9 shrink-0 overflow-hidden border border-current/20"
>
<span className="flex-1" style={{ background: background.hex }} />
<span className="flex-1" style={{ background: midground.hex }} />
<span className="flex-1" style={{ background: warmGlow }} />
</div>
);
}
function PlaceholderSwatch() {
return (
<div
aria-hidden
className="h-4 w-9 shrink-0 border border-dashed border-current/20"
/>
);
}