All checks were successful
Verify and publish V2 container / build-and-push (push) Successful in 30s
Generation - Every quiz is NCLEX style; "Practice exam" writes 50 questions. - Calculation questions only when the material involves calculations (max 5 per exam). - Broad topics: the class's topics are listed in the prompt, a quick grouping call maps narrow labels onto broad ones, and leftover variants are merged. - Exact select-all count per batch; option order shuffled. - Python check: at most 3 runs, fixed problems count as passed, cosmetic remarks ignored. Assistant - Text, tool steps and cards appear in the order they happen, with paragraph breaks between steps. - Optional thinking box: fixed height, follows the newest text, can be scrolled back. - "+" opens a material picker listing recent items; items can be dragged in from class pages; shorter placeholder. MCP servers - Streamable HTTP client; encrypted headers. - Per-server instructions and switches for chat, Explain, generation and the quiz check. - Settings UI. Share links - Optional "Explain this" and "Ask about this material" per link, off by default. - Items are loaded on the server and checked against the share; no tools; rate-limited. - Migration 4 stores the switches. Study and documents - Standard flashcard mode (optionally repeat missed cards until known) next to spaced repetition. - Streak visuals restyled in the app palette. - Side by side / Original document views, with sandboxed previews of Word and PowerPoint files. Other - Built-in embedding model baked into the image; sharp stubbed if it cannot load; real load error shown in Settings. - Python runner blocks network and process modules in JavaScript too. - "View questions and answers" removed from the quiz start screen. - Docs updated. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
512 lines
23 KiB
TypeScript
512 lines
23 KiB
TypeScript
import { CircleAlert, Inbox, LoaderCircle, X } from 'lucide-react';
|
|
import { AlertDialog, Dialog as RDialog, DropdownMenu, Popover as RPopover, Switch as RSwitch, Tooltip as RTooltip } from 'radix-ui';
|
|
import { createContext, forwardRef, useCallback, useContext, useEffect, useId, useRef, useState, type ButtonHTMLAttributes, type InputHTMLAttributes, type ReactNode, type SelectHTMLAttributes, type TextareaHTMLAttributes } from 'react';
|
|
import { Link } from 'react-router';
|
|
import { cx } from '@/lib/util';
|
|
|
|
/* ------------------------------ Button ------------------------------ */
|
|
|
|
type Variant = 'primary' | 'secondary' | 'ghost' | 'danger' | 'marker';
|
|
type Size = 'sm' | 'md' | 'lg';
|
|
|
|
const VARIANT: Record<Variant, string> = {
|
|
primary: 'bg-ink text-paper hover:bg-ink/88 active:bg-ink shadow-1',
|
|
secondary: 'bg-surface text-ink border border-line-strong hover:bg-sunken active:bg-sunken shadow-1',
|
|
ghost: 'text-ink-2 hover:bg-ink/6 hover:text-ink active:bg-ink/10',
|
|
danger: 'bg-bad text-white hover:bg-bad/90 shadow-1 dark:text-[#1b1a17]',
|
|
marker: 'bg-marker text-marker-ink hover:brightness-95 active:brightness-90 shadow-1',
|
|
};
|
|
const SIZE: Record<Size, string> = {
|
|
sm: 'h-8 px-2.5 text-[13px] gap-1.5 rounded-[8px]',
|
|
md: 'h-10 px-3.5 text-sm gap-2 rounded-[10px]',
|
|
lg: 'h-12 px-5 text-[15px] gap-2 rounded-[12px]',
|
|
};
|
|
|
|
export interface ButtonProps extends ButtonHTMLAttributes<HTMLButtonElement> {
|
|
variant?: Variant;
|
|
size?: Size;
|
|
loading?: boolean;
|
|
icon?: ReactNode;
|
|
block?: boolean;
|
|
/** Renders a link to a page inside the app that looks like a button. */
|
|
to?: string;
|
|
/** Renders an ordinary link (downloads, external pages) that looks like a button. */
|
|
href?: string;
|
|
target?: string;
|
|
download?: string | boolean;
|
|
}
|
|
|
|
export const Button = forwardRef<HTMLButtonElement, ButtonProps>(function Button(
|
|
{ variant = 'secondary', size = 'md', loading, icon, block, className, children, disabled, type = 'button', to, href, target, download, ...rest },
|
|
ref,
|
|
) {
|
|
const classes = cx(
|
|
'inline-flex shrink-0 select-none items-center justify-center font-medium whitespace-nowrap transition-[background,color,filter,transform] duration-100 active:scale-[0.985] disabled:pointer-events-none disabled:opacity-50',
|
|
VARIANT[variant],
|
|
SIZE[size],
|
|
block && 'w-full',
|
|
className,
|
|
);
|
|
const content = (
|
|
<>
|
|
{loading ? <LoaderCircle className="size-4 animate-spin" aria-hidden /> : icon ? <span className="grid shrink-0 place-items-center [&>svg]:size-4" aria-hidden>{icon}</span> : null}
|
|
{children}
|
|
</>
|
|
);
|
|
// A disabled link is not a thing: fall through to a disabled button.
|
|
if (!disabled && !loading && to) {
|
|
return (
|
|
<Link to={to} className={classes} onClick={rest.onClick as never} aria-label={rest['aria-label']}>
|
|
{content}
|
|
</Link>
|
|
);
|
|
}
|
|
if (!disabled && !loading && href) {
|
|
return (
|
|
<a href={href} target={target} download={download} rel={target === '_blank' ? 'noreferrer' : undefined} className={classes} aria-label={rest['aria-label']}>
|
|
{content}
|
|
</a>
|
|
);
|
|
}
|
|
return (
|
|
<button ref={ref} type={type} disabled={disabled || loading} aria-busy={loading || undefined} className={classes} {...rest}>
|
|
{content}
|
|
</button>
|
|
);
|
|
});
|
|
|
|
export const IconButton = forwardRef<HTMLButtonElement, ButtonHTMLAttributes<HTMLButtonElement> & { label: string; size?: 'sm' | 'md'; active?: boolean; tooltip?: boolean }>(function IconButton(
|
|
{ label, size = 'md', active, tooltip = true, className, children, type = 'button', ...rest },
|
|
ref,
|
|
) {
|
|
const button = (
|
|
<button
|
|
ref={ref}
|
|
type={type}
|
|
aria-label={label}
|
|
className={cx(
|
|
'inline-grid shrink-0 place-items-center rounded-[9px] text-ink-2 transition-colors hover:bg-ink/7 hover:text-ink disabled:pointer-events-none disabled:opacity-40 [&>svg]:size-[18px]',
|
|
size === 'sm' ? 'size-8' : 'size-10',
|
|
active && 'bg-ink/8 text-ink',
|
|
className,
|
|
)}
|
|
{...rest}
|
|
>
|
|
{children}
|
|
</button>
|
|
);
|
|
return tooltip ? <Tooltip label={label}>{button}</Tooltip> : button;
|
|
});
|
|
|
|
/* ------------------------------ Tooltip ------------------------------ */
|
|
|
|
export function Tooltip({ label, children, side = 'bottom' }: { label: ReactNode; children: ReactNode; side?: 'top' | 'bottom' | 'left' | 'right' }) {
|
|
return (
|
|
<RTooltip.Root>
|
|
<RTooltip.Trigger asChild>{children}</RTooltip.Trigger>
|
|
<RTooltip.Portal>
|
|
<RTooltip.Content side={side} sideOffset={6} className="z-[80] max-w-xs animate-fade rounded-md bg-ink px-2 py-1 text-xs font-medium text-paper shadow-2">
|
|
{label}
|
|
</RTooltip.Content>
|
|
</RTooltip.Portal>
|
|
</RTooltip.Root>
|
|
);
|
|
}
|
|
export const TooltipProvider = RTooltip.Provider;
|
|
|
|
/* ------------------------------ Form controls ------------------------------ */
|
|
|
|
const CONTROL =
|
|
'w-full rounded-[10px] border border-line-strong bg-surface px-3 text-sm text-ink shadow-1 outline-none transition-colors placeholder:text-ink-3 hover:border-ink-3 focus:border-accent focus:ring-2 focus:ring-accent/20 disabled:opacity-60 aria-[invalid=true]:border-bad aria-[invalid=true]:ring-bad/15';
|
|
|
|
export const Input = forwardRef<HTMLInputElement, InputHTMLAttributes<HTMLInputElement>>(function Input({ className, ...rest }, ref) {
|
|
return <input ref={ref} className={cx(CONTROL, 'h-10', className)} {...rest} />;
|
|
});
|
|
|
|
export const Textarea = forwardRef<HTMLTextAreaElement, TextareaHTMLAttributes<HTMLTextAreaElement> & { autoGrow?: boolean; maxRows?: number }>(function Textarea(
|
|
{ className, autoGrow, maxRows = 16, onChange, value, rows = 3, ...rest },
|
|
ref,
|
|
) {
|
|
const inner = useRef<HTMLTextAreaElement | null>(null);
|
|
const resize = useCallback(() => {
|
|
const el = inner.current;
|
|
if (!el || !autoGrow) return;
|
|
el.style.height = 'auto';
|
|
const line = parseFloat(getComputedStyle(el).lineHeight) || 22;
|
|
el.style.height = `${Math.min(el.scrollHeight + 2, line * maxRows + 20)}px`;
|
|
}, [autoGrow, maxRows]);
|
|
useEffect(resize, [resize, value]);
|
|
return (
|
|
<textarea
|
|
ref={(el) => {
|
|
inner.current = el;
|
|
if (typeof ref === 'function') ref(el);
|
|
else if (ref) ref.current = el;
|
|
}}
|
|
rows={rows}
|
|
value={value}
|
|
onChange={(e) => {
|
|
onChange?.(e);
|
|
resize();
|
|
}}
|
|
className={cx(CONTROL, 'resize-y py-2 leading-relaxed', autoGrow && 'resize-none overflow-y-auto', className)}
|
|
{...rest}
|
|
/>
|
|
);
|
|
});
|
|
|
|
export const Select = forwardRef<HTMLSelectElement, SelectHTMLAttributes<HTMLSelectElement>>(function Select({ className, children, ...rest }, ref) {
|
|
return (
|
|
<select
|
|
ref={ref}
|
|
className={cx(
|
|
CONTROL,
|
|
'h-10 appearance-none bg-[length:16px] bg-[right_10px_center] bg-no-repeat pr-9',
|
|
"bg-[url('data:image/svg+xml;utf8,<svg xmlns=%22http://www.w3.org/2000/svg%22 viewBox=%220 0 24 24%22 fill=%22none%22 stroke=%22%238b867a%22 stroke-width=%222.2%22 stroke-linecap=%22round%22 stroke-linejoin=%22round%22><path d=%22m6 9 6 6 6-6%22/></svg>')]",
|
|
className,
|
|
)}
|
|
{...rest}
|
|
>
|
|
{children}
|
|
</select>
|
|
);
|
|
});
|
|
|
|
export function Field({ label, hint, error, children, className, optional }: { label: ReactNode; hint?: ReactNode; error?: ReactNode; children: ReactNode; className?: string; optional?: boolean }) {
|
|
return (
|
|
<label className={cx('block', className)}>
|
|
<span className="mb-1.5 flex items-baseline justify-between gap-2 text-[13px] font-medium text-ink">
|
|
<span>{label}</span>
|
|
{optional && <span className="text-xs font-normal text-ink-3">Optional</span>}
|
|
</span>
|
|
{children}
|
|
{error ? <span className="mt-1.5 flex items-start gap-1 text-[13px] text-bad"><CircleAlert className="mt-0.5 size-3.5 shrink-0" aria-hidden />{error}</span> : hint ? <span className="mt-1.5 block text-[13px] text-ink-3">{hint}</span> : null}
|
|
</label>
|
|
);
|
|
}
|
|
|
|
export function Switch({ checked, onChange, label, description, disabled }: { checked: boolean; onChange: (v: boolean) => void; label: ReactNode; description?: ReactNode; disabled?: boolean }) {
|
|
const id = useId();
|
|
return (
|
|
<div className="flex items-start justify-between gap-4">
|
|
<label htmlFor={id} className="min-w-0">
|
|
<span className="block text-sm font-medium text-ink">{label}</span>
|
|
{description && <span className="mt-0.5 block text-[13px] text-ink-3">{description}</span>}
|
|
</label>
|
|
<RSwitch.Root
|
|
id={id}
|
|
checked={checked}
|
|
onCheckedChange={onChange}
|
|
disabled={disabled}
|
|
className="relative mt-0.5 h-6 w-10 shrink-0 rounded-full bg-line-strong transition-colors data-[state=checked]:bg-accent disabled:opacity-50"
|
|
>
|
|
<RSwitch.Thumb className="block size-5 translate-x-0.5 rounded-full bg-white shadow-1 transition-transform data-[state=checked]:translate-x-[18px]" />
|
|
</RSwitch.Root>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
/** Segmented control for 2-4 mutually exclusive choices. */
|
|
export function Segmented<T extends string>({ value, onChange, options, size = 'md', className, label }: { value: T; onChange: (v: T) => void; options: { value: T; label: ReactNode; icon?: ReactNode }[]; size?: 'sm' | 'md'; className?: string; label?: string }) {
|
|
return (
|
|
<div role="radiogroup" aria-label={label} className={cx('inline-flex rounded-[11px] bg-sunken p-1', className)}>
|
|
{options.map((o) => (
|
|
<button
|
|
key={o.value}
|
|
type="button"
|
|
role="radio"
|
|
aria-checked={value === o.value}
|
|
onClick={() => onChange(o.value)}
|
|
className={cx(
|
|
'inline-flex flex-1 items-center justify-center gap-1.5 rounded-[8px] font-medium whitespace-nowrap text-ink-2 transition-colors [&>svg]:size-4',
|
|
size === 'sm' ? 'h-7 px-2.5 text-[13px]' : 'h-8 px-3 text-sm',
|
|
value === o.value ? 'bg-surface text-ink shadow-1' : 'hover:text-ink',
|
|
)}
|
|
>
|
|
{o.icon}
|
|
{o.label}
|
|
</button>
|
|
))}
|
|
</div>
|
|
);
|
|
}
|
|
|
|
/* ------------------------------ Feedback ------------------------------ */
|
|
|
|
export function Spinner({ className, label = 'Loading' }: { className?: string; label?: string }) {
|
|
return <LoaderCircle role="status" aria-label={label} className={cx('size-5 animate-spin text-ink-3', className)} />;
|
|
}
|
|
|
|
export function Badge({ children, tone = 'neutral', className }: { children: ReactNode; tone?: 'neutral' | 'accent' | 'good' | 'bad' | 'warn' | 'marker'; className?: string }) {
|
|
const tones = {
|
|
neutral: 'bg-ink/6 text-ink-2',
|
|
accent: 'bg-accent-soft text-accent',
|
|
good: 'bg-good-soft text-good',
|
|
bad: 'bg-bad-soft text-bad',
|
|
warn: 'bg-warn-soft text-warn',
|
|
marker: 'bg-marker text-marker-ink',
|
|
};
|
|
return <span className={cx('inline-flex h-[22px] items-center gap-1 rounded-full px-2 text-xs font-medium whitespace-nowrap [&>svg]:size-3', tones[tone], className)}>{children}</span>;
|
|
}
|
|
|
|
export function ProgressBar({ value, tone = 'accent', className, label }: { value: number; tone?: 'accent' | 'good' | 'ink' | 'tab'; className?: string; label?: string }) {
|
|
const v = Math.max(0, Math.min(1, value));
|
|
const tones = { accent: 'bg-accent', good: 'bg-good', ink: 'bg-ink', tab: 'bg-tab' };
|
|
return (
|
|
<div role="progressbar" aria-label={label} aria-valuemin={0} aria-valuemax={100} aria-valuenow={Math.round(v * 100)} className={cx('h-1.5 overflow-hidden rounded-full bg-ink/8', className)}>
|
|
<div className={cx('h-full rounded-full transition-[width] duration-300', tones[tone])} style={{ width: `${v * 100}%` }} />
|
|
</div>
|
|
);
|
|
}
|
|
|
|
export function Skeleton({ className }: { className?: string }) {
|
|
return <div aria-hidden className={cx('skeleton', className)} />;
|
|
}
|
|
|
|
export function PageLoading({ label = 'Loading' }: { label?: string }) {
|
|
return (
|
|
<div className="grid min-h-[40vh] place-items-center" role="status" aria-label={label}>
|
|
<Spinner className="size-6" />
|
|
</div>
|
|
);
|
|
}
|
|
|
|
export function EmptyState({ icon, title, children, action, className }: { icon?: ReactNode; title: string; children?: ReactNode; action?: ReactNode; className?: string }) {
|
|
return (
|
|
<div className={cx('mx-auto flex max-w-md flex-col items-center px-6 py-12 text-center', className)}>
|
|
<div className="mb-4 grid size-12 place-items-center rounded-2xl bg-sunken text-ink-3 [&>svg]:size-6">{icon ?? <Inbox />}</div>
|
|
<h3 className="display text-xl text-ink">{title}</h3>
|
|
{children && <p className="mt-2 text-sm leading-relaxed text-ink-2">{children}</p>}
|
|
{action && <div className="mt-5 flex flex-wrap justify-center gap-2">{action}</div>}
|
|
</div>
|
|
);
|
|
}
|
|
|
|
export function ErrorState({ title = 'Something went wrong', message, onRetry, className }: { title?: string; message?: string; onRetry?: () => void; className?: string }) {
|
|
return (
|
|
<div role="alert" className={cx('mx-auto flex max-w-md flex-col items-center px-6 py-12 text-center', className)}>
|
|
<div className="mb-4 grid size-12 place-items-center rounded-2xl bg-bad-soft text-bad">
|
|
<CircleAlert className="size-6" />
|
|
</div>
|
|
<h3 className="display text-xl">{title}</h3>
|
|
{message && <p className="mt-2 text-sm text-ink-2">{message}</p>}
|
|
{onRetry && (
|
|
<Button className="mt-5" onClick={onRetry}>
|
|
Try again
|
|
</Button>
|
|
)}
|
|
</div>
|
|
);
|
|
}
|
|
|
|
export function Callout({ tone = 'neutral', icon, title, children, action, className }: { tone?: 'neutral' | 'accent' | 'good' | 'bad' | 'warn'; icon?: ReactNode; title?: ReactNode; children?: ReactNode; action?: ReactNode; className?: string }) {
|
|
const tones = {
|
|
neutral: 'bg-sunken border-line text-ink-2',
|
|
accent: 'bg-accent-soft border-accent/20 text-ink',
|
|
good: 'bg-good-soft border-good/20 text-ink',
|
|
bad: 'bg-bad-soft border-bad/25 text-ink',
|
|
warn: 'bg-warn-soft border-warn/25 text-ink',
|
|
};
|
|
const iconTone = { neutral: 'text-ink-3', accent: 'text-accent', good: 'text-good', bad: 'text-bad', warn: 'text-warn' };
|
|
return (
|
|
<div className={cx('flex items-start gap-3 rounded-xl border px-3.5 py-3 text-sm', tones[tone], className)}>
|
|
{icon && <span className={cx('mt-0.5 shrink-0 [&>svg]:size-[18px]', iconTone[tone])}>{icon}</span>}
|
|
<div className="min-w-0 flex-1">
|
|
{title && <div className="font-medium text-ink">{title}</div>}
|
|
{children && <div className={cx('leading-relaxed', title && 'mt-0.5 text-ink-2')}>{children}</div>}
|
|
</div>
|
|
{action && <div className="shrink-0">{action}</div>}
|
|
</div>
|
|
);
|
|
}
|
|
|
|
export function Kbd({ children }: { children: ReactNode }) {
|
|
return <kbd className="kbd">{children}</kbd>;
|
|
}
|
|
|
|
/* ------------------------------ Dialog ------------------------------ */
|
|
|
|
export function Dialog({
|
|
open,
|
|
onOpenChange,
|
|
title,
|
|
description,
|
|
children,
|
|
footer,
|
|
size = 'md',
|
|
bare,
|
|
}: {
|
|
open: boolean;
|
|
onOpenChange: (open: boolean) => void;
|
|
title: ReactNode;
|
|
description?: ReactNode;
|
|
children: ReactNode;
|
|
footer?: ReactNode;
|
|
size?: 'sm' | 'md' | 'lg' | 'xl';
|
|
/** No padding around the body (for lists and custom layouts). */
|
|
bare?: boolean;
|
|
}) {
|
|
const widths = { sm: 'sm:max-w-sm', md: 'sm:max-w-lg', lg: 'sm:max-w-2xl', xl: 'sm:max-w-4xl' };
|
|
return (
|
|
<RDialog.Root open={open} onOpenChange={onOpenChange}>
|
|
<RDialog.Portal>
|
|
<RDialog.Overlay className="fixed inset-0 z-50 animate-fade bg-[#12110f]/45 backdrop-blur-[2px]" />
|
|
<RDialog.Content
|
|
aria-describedby={description ? undefined : undefined}
|
|
className={cx(
|
|
'fixed z-50 flex max-h-[92dvh] w-full flex-col bg-surface shadow-3 outline-none',
|
|
'inset-x-0 bottom-0 animate-sheet rounded-t-[20px] border-t border-line',
|
|
'sm:inset-x-auto sm:bottom-auto sm:top-1/2 sm:left-1/2 sm:-translate-x-1/2 sm:-translate-y-1/2 sm:animate-pop sm:rounded-[18px] sm:border',
|
|
widths[size],
|
|
)}
|
|
>
|
|
<div className="flex items-start gap-3 px-5 pt-5 pb-3 sm:px-6">
|
|
<div className="min-w-0 flex-1">
|
|
<RDialog.Title className="display text-[22px] text-ink">{title}</RDialog.Title>
|
|
{description ? <RDialog.Description className="mt-1 text-sm text-ink-2">{description}</RDialog.Description> : <RDialog.Description className="sr-only">{typeof title === 'string' ? title : 'Dialog'}</RDialog.Description>}
|
|
</div>
|
|
<RDialog.Close asChild>
|
|
<IconButton label="Close" size="sm" tooltip={false} className="-mt-1 -mr-2">
|
|
<X />
|
|
</IconButton>
|
|
</RDialog.Close>
|
|
</div>
|
|
<div className={cx('min-h-0 flex-1 overflow-y-auto', !bare && 'px-5 pb-5 sm:px-6')}>{children}</div>
|
|
{footer && <div className="safe-pb-3 flex flex-wrap items-center justify-end gap-2 border-t border-line bg-paper/60 px-5 pt-3 sm:rounded-b-[18px] sm:px-6">{footer}</div>}
|
|
</RDialog.Content>
|
|
</RDialog.Portal>
|
|
</RDialog.Root>
|
|
);
|
|
}
|
|
|
|
/* ------------------------------ Confirm ------------------------------ */
|
|
|
|
interface ConfirmOptions {
|
|
title: string;
|
|
message?: ReactNode;
|
|
confirmLabel?: string;
|
|
cancelLabel?: string;
|
|
danger?: boolean;
|
|
}
|
|
const ConfirmContext = createContext<(opts: ConfirmOptions) => Promise<boolean>>(() => Promise.resolve(false));
|
|
|
|
export function ConfirmProvider({ children }: { children: ReactNode }) {
|
|
const [state, setState] = useState<(ConfirmOptions & { resolve: (v: boolean) => void }) | null>(null);
|
|
const confirm = useCallback((opts: ConfirmOptions) => new Promise<boolean>((resolve) => setState({ ...opts, resolve })), []);
|
|
const close = (v: boolean) => {
|
|
state?.resolve(v);
|
|
setState(null);
|
|
};
|
|
return (
|
|
<ConfirmContext.Provider value={confirm}>
|
|
{children}
|
|
<AlertDialog.Root open={state !== null} onOpenChange={(o) => !o && close(false)}>
|
|
<AlertDialog.Portal>
|
|
<AlertDialog.Overlay className="fixed inset-0 z-[70] animate-fade bg-[#12110f]/45 backdrop-blur-[2px]" />
|
|
<AlertDialog.Content className="fixed top-1/2 left-1/2 z-[70] w-[calc(100vw-2rem)] max-w-sm -translate-x-1/2 -translate-y-1/2 animate-pop rounded-[18px] border border-line bg-surface p-5 shadow-3 outline-none">
|
|
<AlertDialog.Title className="display text-xl">{state?.title}</AlertDialog.Title>
|
|
<AlertDialog.Description asChild>
|
|
<div className="mt-2 text-sm leading-relaxed text-ink-2">{state?.message ?? 'This cannot be undone.'}</div>
|
|
</AlertDialog.Description>
|
|
<div className="mt-5 flex justify-end gap-2">
|
|
<AlertDialog.Cancel asChild>
|
|
<Button variant="ghost">{state?.cancelLabel ?? 'Cancel'}</Button>
|
|
</AlertDialog.Cancel>
|
|
<AlertDialog.Action asChild>
|
|
<Button variant={state?.danger ? 'danger' : 'primary'} onClick={() => close(true)}>
|
|
{state?.confirmLabel ?? 'Confirm'}
|
|
</Button>
|
|
</AlertDialog.Action>
|
|
</div>
|
|
</AlertDialog.Content>
|
|
</AlertDialog.Portal>
|
|
</AlertDialog.Root>
|
|
</ConfirmContext.Provider>
|
|
);
|
|
}
|
|
export const useConfirm = () => useContext(ConfirmContext);
|
|
|
|
/* ------------------------------ Menu ------------------------------ */
|
|
|
|
export function Menu({ trigger, children, align = 'end', side = 'bottom' }: { trigger: ReactNode; children: ReactNode; align?: 'start' | 'end' | 'center'; side?: 'top' | 'bottom' }) {
|
|
return (
|
|
<DropdownMenu.Root>
|
|
<DropdownMenu.Trigger asChild>{trigger}</DropdownMenu.Trigger>
|
|
<DropdownMenu.Portal>
|
|
<DropdownMenu.Content align={align} side={side} sideOffset={6} collisionPadding={10} className="z-[60] max-h-[70vh] min-w-[200px] animate-pop overflow-y-auto rounded-xl border border-line bg-surface p-1.5 shadow-2 outline-none">
|
|
{children}
|
|
</DropdownMenu.Content>
|
|
</DropdownMenu.Portal>
|
|
</DropdownMenu.Root>
|
|
);
|
|
}
|
|
|
|
export function MenuItem({ children, icon, onSelect, danger, disabled, hint }: { children: ReactNode; icon?: ReactNode; onSelect?: () => void; danger?: boolean; disabled?: boolean; hint?: ReactNode }) {
|
|
return (
|
|
<DropdownMenu.Item
|
|
disabled={disabled}
|
|
onSelect={onSelect}
|
|
className={cx(
|
|
'flex h-9 cursor-pointer items-center gap-2.5 rounded-lg px-2.5 text-sm outline-none select-none data-[disabled]:pointer-events-none data-[disabled]:opacity-45 data-[highlighted]:bg-ink/7 [&>svg]:size-4 [&>svg]:shrink-0',
|
|
danger ? 'text-bad [&>svg]:text-bad' : 'text-ink [&>svg]:text-ink-3',
|
|
)}
|
|
>
|
|
{icon}
|
|
<span className="flex-1 truncate">{children}</span>
|
|
{hint && <span className="text-xs text-ink-3">{hint}</span>}
|
|
</DropdownMenu.Item>
|
|
);
|
|
}
|
|
export function MenuSeparator() {
|
|
return <DropdownMenu.Separator className="my-1.5 h-px bg-line" />;
|
|
}
|
|
export function MenuLabel({ children }: { children: ReactNode }) {
|
|
return <DropdownMenu.Label className="eyebrow px-2.5 pt-1.5 pb-1">{children}</DropdownMenu.Label>;
|
|
}
|
|
|
|
/* ------------------------------ Popover ------------------------------ */
|
|
|
|
export function Popover({ trigger, children, align = 'start', className, open, onOpenChange }: { trigger: ReactNode; children: ReactNode; align?: 'start' | 'end' | 'center'; className?: string; open?: boolean; onOpenChange?: (o: boolean) => void }) {
|
|
return (
|
|
<RPopover.Root open={open} onOpenChange={onOpenChange}>
|
|
<RPopover.Trigger asChild>{trigger}</RPopover.Trigger>
|
|
<RPopover.Portal>
|
|
<RPopover.Content align={align} sideOffset={8} collisionPadding={10} className={cx('z-[60] w-80 max-w-[calc(100vw-1.5rem)] animate-pop rounded-xl border border-line bg-surface p-4 shadow-2 outline-none', className)}>
|
|
{children}
|
|
</RPopover.Content>
|
|
</RPopover.Portal>
|
|
</RPopover.Root>
|
|
);
|
|
}
|
|
|
|
/* ------------------------------ Layout bits ------------------------------ */
|
|
|
|
export function PageHeader({ eyebrow, title, description, actions, className }: { eyebrow?: ReactNode; title: ReactNode; description?: ReactNode; actions?: ReactNode; className?: string }) {
|
|
return (
|
|
<header className={cx('mb-6 flex flex-wrap items-end justify-between gap-x-6 gap-y-4', className)}>
|
|
<div className="min-w-0">
|
|
{eyebrow && <div className="eyebrow mb-1.5 flex items-center gap-1.5">{eyebrow}</div>}
|
|
<h1 className="display text-[30px] text-ink sm:text-[36px]">{title}</h1>
|
|
{description && <p className="mt-2 max-w-2xl text-[15px] text-ink-2">{description}</p>}
|
|
</div>
|
|
{actions && <div className="flex flex-wrap items-center gap-2">{actions}</div>}
|
|
</header>
|
|
);
|
|
}
|
|
|
|
export function Section({ title, action, children, className }: { title: ReactNode; action?: ReactNode; children: ReactNode; className?: string }) {
|
|
return (
|
|
<section className={cx('mb-8', className)}>
|
|
<div className="mb-3 flex items-center justify-between gap-3">
|
|
<h2 className="eyebrow shrink-0">{title}</h2>
|
|
{action}
|
|
</div>
|
|
{children}
|
|
</section>
|
|
);
|
|
}
|
|
|
|
/** Small coloured dot / spine used to identify a class. */
|
|
export function ClassDot({ color, className }: { color: string; className?: string }) {
|
|
return <span aria-hidden className={cx('inline-block size-2.5 shrink-0 rounded-[3px] bg-tab', `tab-${color}`, className)} />;
|
|
}
|