320 lines
11 KiB
TypeScript
320 lines
11 KiB
TypeScript
import { createContext, useContext, useId, type ComponentProps } from 'react';
|
|
import { Check } from 'lucide-react';
|
|
import { Slot } from 'radix-ui';
|
|
import { cn } from '../../lib/cn';
|
|
import { Card, CardFrame } from '../Card/Card';
|
|
import { Label } from '../ui/label';
|
|
import { RadioGroup, RadioGroupItem } from '../ui/radio-group';
|
|
import { Spinner } from '../Spinner/Spinner';
|
|
|
|
/* ---------------------------------------------------------------- Nav */
|
|
|
|
/** Left-hand navigation between settings pages: `SettingsNavLabel`s and `SettingsNavItem`s. */
|
|
export function SettingsNav({ className, ...props }: ComponentProps<'nav'>) {
|
|
return (
|
|
<nav
|
|
aria-label="Einstellungen"
|
|
data-slot="settings-nav"
|
|
className={cn('flex max-w-[240px] flex-[1_1_200px] flex-col gap-0.5', className)}
|
|
{...props}
|
|
/>
|
|
);
|
|
}
|
|
|
|
/** Group title, e.g. "Persönlich". */
|
|
export function SettingsNavLabel({ className, ...props }: ComponentProps<'div'>) {
|
|
return (
|
|
<div
|
|
data-slot="settings-nav-label"
|
|
className={cn('px-3 pb-2 text-xs font-bold text-fg-subtle not-first:pt-[18px]', className)}
|
|
{...props}
|
|
/>
|
|
);
|
|
}
|
|
|
|
export interface SettingsNavItemProps extends ComponentProps<'a'> {
|
|
active?: boolean;
|
|
asChild?: boolean;
|
|
}
|
|
|
|
/** Icon and label; the active one is framed with an accent icon. */
|
|
export function SettingsNavItem({ active, asChild, className, ...props }: SettingsNavItemProps) {
|
|
const Comp = asChild ? Slot.Root : 'a';
|
|
return (
|
|
<Comp
|
|
aria-current={active ? 'page' : undefined}
|
|
data-slot="settings-nav-item"
|
|
className={cn(
|
|
'flex h-[38px] items-center gap-2.5 rounded-lg px-3 font-bold text-fg-muted hover:text-fg [&>svg]:text-fg-subtle',
|
|
'aria-[current=page]:bg-side-active aria-[current=page]:text-fg aria-[current=page]:shadow-[inset_0_0_0_1px_var(--cl-side-active-line)] aria-[current=page]:[&>svg]:text-accent-fg',
|
|
className,
|
|
)}
|
|
{...props}
|
|
/>
|
|
);
|
|
}
|
|
|
|
/* ---------------------------------------------------------------- Section */
|
|
|
|
const SectionContext = createContext<string | undefined>(undefined);
|
|
|
|
/**
|
|
* A settings card: header, then `SettingsRow`s separated by hairlines, then an optional footer.
|
|
* Labelled by its `SettingsSectionTitle`. `className` goes to the frame.
|
|
*/
|
|
export function SettingsSection({ className, ...props }: ComponentProps<'section'>) {
|
|
const titleId = useId();
|
|
return (
|
|
<SectionContext value={titleId}>
|
|
<CardFrame className={className}>
|
|
<Card asChild padding="none">
|
|
<section aria-labelledby={titleId} data-slot="settings-section" {...props} />
|
|
</Card>
|
|
</CardFrame>
|
|
</SectionContext>
|
|
);
|
|
}
|
|
|
|
export function SettingsSectionHeader({ className, ...props }: ComponentProps<'div'>) {
|
|
return <div data-slot="settings-section-header" className={cn('px-[22px] pt-[18px] pb-4', className)} {...props} />;
|
|
}
|
|
|
|
export function SettingsSectionTitle({ className, ...props }: ComponentProps<'h2'>) {
|
|
return (
|
|
<h2
|
|
id={useContext(SectionContext)}
|
|
data-slot="settings-section-title"
|
|
className={cn('m-0 text-md font-bold', className)}
|
|
{...props}
|
|
/>
|
|
);
|
|
}
|
|
|
|
export function SettingsSectionDescription({ className, ...props }: ComponentProps<'p'>) {
|
|
return (
|
|
<p
|
|
data-slot="settings-section-description"
|
|
className={cn('mt-1 mb-0 text-sm leading-normal text-fg-subtle', className)}
|
|
{...props}
|
|
/>
|
|
);
|
|
}
|
|
|
|
/** Right-aligned actions, e.g. Verwerfen / Speichern. */
|
|
export function SettingsSectionFooter({ className, ...props }: ComponentProps<'div'>) {
|
|
return (
|
|
<div
|
|
data-slot="settings-section-footer"
|
|
className={cn(
|
|
'flex flex-wrap items-center justify-end gap-2.5 border-t border-line-subtle px-[22px] py-3.5',
|
|
className,
|
|
)}
|
|
{...props}
|
|
/>
|
|
);
|
|
}
|
|
|
|
export interface SettingsRowProps extends ComponentProps<'div'> {
|
|
/** Put the control on its own line under the text. */
|
|
stacked?: boolean;
|
|
}
|
|
|
|
/** `SettingsRowText` on the left, `SettingsRowControl` on the right; wraps on narrow screens. Also fits free content. */
|
|
export function SettingsRow({ stacked, className, ...props }: SettingsRowProps) {
|
|
return (
|
|
<div
|
|
data-slot="settings-row"
|
|
data-stacked={stacked || undefined}
|
|
className={cn(
|
|
'group/row border-t border-line-subtle px-[22px] py-4',
|
|
!stacked && 'flex flex-wrap items-center justify-between gap-x-6 gap-y-3',
|
|
className,
|
|
)}
|
|
{...props}
|
|
/>
|
|
);
|
|
}
|
|
|
|
export function SettingsRowText({ className, ...props }: ComponentProps<'div'>) {
|
|
return (
|
|
<div
|
|
data-slot="settings-row-text"
|
|
className={cn('min-w-0 flex-[1_1_260px] group-data-stacked/row:flex-none', className)}
|
|
{...props}
|
|
/>
|
|
);
|
|
}
|
|
|
|
/** A `<label>`; set `htmlFor` to the control's id. */
|
|
export function SettingsRowLabel({ className, ...props }: ComponentProps<typeof Label>) {
|
|
return <Label data-slot="settings-row-label" className={cn('block leading-tight font-bold', className)} {...props} />;
|
|
}
|
|
|
|
export function SettingsRowDescription({ className, ...props }: ComponentProps<'div'>) {
|
|
return (
|
|
<div
|
|
data-slot="settings-row-description"
|
|
className={cn('mt-[3px] text-sm leading-normal text-fg-subtle', className)}
|
|
{...props}
|
|
/>
|
|
);
|
|
}
|
|
|
|
export function SettingsRowControl({ className, ...props }: ComponentProps<'div'>) {
|
|
return (
|
|
<div
|
|
data-slot="settings-row-control"
|
|
className={cn('max-w-full flex-none group-data-stacked/row:mt-3.5', className)}
|
|
{...props}
|
|
/>
|
|
);
|
|
}
|
|
|
|
/* ---------------------------------------------------------------- Pickers */
|
|
|
|
/** Selectable card; the checked one gets an accent ring. */
|
|
const radioCard =
|
|
'group flex flex-col rounded-xl border border-line bg-inset text-fg hover:bg-hover data-[state=checked]:shadow-[0_0_0_2px_var(--cl-accent)] data-[state=checked]:focus-visible:shadow-[0_0_0_2px_var(--cl-accent),0_0_0_5px_var(--cl-accent-ring)]';
|
|
|
|
export type ThemeChoice = 'light' | 'dark' | 'system';
|
|
|
|
/** Radio cards for the theme: `ThemeOption`s. Arrow keys move the choice. */
|
|
export function ThemePicker({ className, ...props }: ComponentProps<typeof RadioGroup>) {
|
|
return (
|
|
<RadioGroup
|
|
aria-label="Design"
|
|
data-slot="theme-picker"
|
|
className={cn('grid-cols-[repeat(auto-fit,minmax(140px,1fr))]', className)}
|
|
{...props}
|
|
/>
|
|
);
|
|
}
|
|
|
|
/** One card: a `ThemePreview` and a `ThemeOptionLabel`. */
|
|
export function ThemeOption({ className, ...props }: ComponentProps<typeof RadioGroupItem>) {
|
|
return (
|
|
<RadioGroupItem
|
|
data-slot="theme-option"
|
|
className={cn(radioCard, 'gap-2.5 px-2 pt-2 pb-2.5 text-left', className)}
|
|
{...props}
|
|
/>
|
|
);
|
|
}
|
|
|
|
function MiniWindow({ variant, className }: { variant: 'light' | 'dark'; className?: string }) {
|
|
const c =
|
|
variant === 'light'
|
|
? { bg: '#F4F4F5', side: '#FFFFFF', card: '#FFFFFF', line: '#DEDEE3', bar: '#E4E4E7' }
|
|
: { bg: '#050505', side: '#0E0E0E', card: '#141414', line: '#2E2E2E', bar: '#262626' };
|
|
return (
|
|
<span
|
|
aria-hidden="true"
|
|
className={cn('box-border flex h-[74px] w-full gap-1.5 p-2', className)}
|
|
style={{ background: c.bg }}
|
|
>
|
|
<span className="w-[26%] rounded-[5px]" style={{ background: c.side }} />
|
|
<span className="flex flex-auto flex-col gap-[5px]">
|
|
<span
|
|
className="h-[26px] rounded-[5px]"
|
|
style={{ background: c.card, boxShadow: `inset 0 0 0 1px ${c.line}` }}
|
|
/>
|
|
<span className="h-2 w-[70%] rounded" style={{ background: c.bar }} />
|
|
<span className="h-2 w-[45%] rounded" style={{ background: c.bar }} />
|
|
</span>
|
|
</span>
|
|
);
|
|
}
|
|
|
|
/** Miniature app window in the given theme; `system` shows half light, half dark. */
|
|
export function ThemePreview({ theme, className }: { theme: ThemeChoice; className?: string }) {
|
|
if (theme !== 'system') return <MiniWindow variant={theme} className={cn('rounded-[9px]', className)} />;
|
|
return (
|
|
<span className={cn('flex h-[74px] w-full overflow-hidden rounded-[9px]', className)}>
|
|
<span className="flex flex-1 basis-0 overflow-hidden">
|
|
<MiniWindow variant="light" className="w-[200%] flex-none" />
|
|
</span>
|
|
<span className="flex flex-1 basis-0 justify-end overflow-hidden">
|
|
<MiniWindow variant="dark" className="w-[200%] flex-none" />
|
|
</span>
|
|
</span>
|
|
);
|
|
}
|
|
|
|
/** Name of the option, with a radio dot on the right. */
|
|
export function ThemeOptionLabel({ className, children, ...props }: ComponentProps<'span'>) {
|
|
return (
|
|
<span
|
|
data-slot="theme-option-label"
|
|
className={cn('flex items-center justify-between gap-2 px-1 text-sm font-bold', className)}
|
|
{...props}
|
|
>
|
|
{children}
|
|
<span className="box-border h-4 w-4 flex-none rounded-full border-[1.5px] border-line-control group-data-[state=checked]:border-accent group-data-[state=checked]:bg-accent group-data-[state=checked]:shadow-[inset_0_0_0_3px_var(--cl-inset)]" />
|
|
</span>
|
|
);
|
|
}
|
|
|
|
/** Accent color swatches ("Farbschema"): `ColorSchemeOption`s. */
|
|
export function ColorSchemePicker({ className, ...props }: ComponentProps<typeof RadioGroup>) {
|
|
return (
|
|
<RadioGroup
|
|
data-slot="color-scheme-picker"
|
|
className={cn('grid-cols-[repeat(auto-fill,minmax(88px,1fr))] gap-2.5', className)}
|
|
{...props}
|
|
/>
|
|
);
|
|
}
|
|
|
|
export interface ColorSchemeOptionProps extends ComponentProps<typeof RadioGroupItem> {
|
|
color: string;
|
|
}
|
|
|
|
/** Round swatch in the color (checked: with a check mark), then `children` as the name. */
|
|
export function ColorSchemeOption({ color, className, children, ...props }: ColorSchemeOptionProps) {
|
|
return (
|
|
<RadioGroupItem
|
|
data-slot="color-scheme-option"
|
|
className={cn(radioCard, 'items-center gap-2 px-1.5 pt-3 pb-2.5 text-xs font-bold', className)}
|
|
{...props}
|
|
>
|
|
<span
|
|
aria-hidden="true"
|
|
className="flex h-8 w-8 items-center justify-center rounded-full text-on-accent"
|
|
style={{
|
|
background: `linear-gradient(180deg, color-mix(in srgb, ${color} 88%, #fff) 0%, color-mix(in srgb, ${color} 90%, #000) 100%)`,
|
|
boxShadow: 'inset 0 1px 0 rgba(255,255,255,0.28), 0 1px 2px rgba(0,0,0,0.5)',
|
|
}}
|
|
>
|
|
<Check size={16} strokeWidth={3} className="hidden group-data-[state=checked]:block" />
|
|
</span>
|
|
{children}
|
|
</RadioGroupItem>
|
|
);
|
|
}
|
|
|
|
/* ---------------------------------------------------------------- Status */
|
|
|
|
export interface SaveStatusProps extends ComponentProps<'span'> {
|
|
state: 'idle' | 'saving' | 'saved';
|
|
}
|
|
|
|
/** Autosave indicator: "Wird gespeichert …" then "Gespeichert". Renders nothing while idle. */
|
|
export function SaveStatus({ state, className, ...props }: SaveStatusProps) {
|
|
if (state === 'idle') return null;
|
|
return (
|
|
<span
|
|
role="status"
|
|
data-slot="save-status"
|
|
className={cn(
|
|
'flex items-center gap-1.5 text-xs font-bold',
|
|
state === 'saving' ? 'text-fg-subtle' : 'text-success-fg',
|
|
className,
|
|
)}
|
|
{...props}
|
|
>
|
|
{state === 'saving' ? <Spinner size={14} /> : <Check size={14} strokeWidth={2.5} aria-hidden="true" />}
|
|
{state === 'saving' ? 'Wird gespeichert …' : 'Gespeichert'}
|
|
</span>
|
|
);
|
|
}
|