50 lines
1.9 KiB
TypeScript
50 lines
1.9 KiB
TypeScript
"use client";
|
|
|
|
import { useRef, type ReactNode } from "react";
|
|
import { Ellipsis, X } from "lucide-react";
|
|
import { Button } from "@/components/ui/button";
|
|
import { useT } from "@/components/I18n";
|
|
|
|
/**
|
|
* Button that opens a side dialog (native <dialog>) with content rendered on the server.
|
|
* Default trigger is "⋯" (page settings); pass `trigger` for a labelled button such as «Создать задачу».
|
|
*/
|
|
export function SettingsDialog({ title, trigger, primary, children }: { title: string; trigger?: ReactNode; /** PEP-160: the page's main action. */ primary?: boolean; children: ReactNode }) {
|
|
const t = useT();
|
|
const ref = useRef<HTMLDialogElement>(null);
|
|
const open = () => {
|
|
ref.current?.showModal();
|
|
// PEP-160: a form in the dialog starts at its first field, not at the close button.
|
|
ref.current?.querySelector<HTMLElement>("[data-autofocus]")?.focus();
|
|
};
|
|
return (
|
|
<>
|
|
{trigger ? (
|
|
<Button size="sm" variant={primary ? "default" : undefined} onClick={open}>
|
|
{trigger}
|
|
</Button>
|
|
) : (
|
|
<Button variant="ghost" size="icon" aria-label={title} title={title} onClick={open}>
|
|
<Ellipsis />
|
|
</Button>
|
|
)}
|
|
<dialog
|
|
ref={ref}
|
|
aria-label={title}
|
|
onClick={(e) => e.target === ref.current && ref.current.close()}
|
|
className="m-0 ml-auto h-dvh max-h-dvh w-[400px] max-w-full border-l bg-sidebar p-0 text-foreground backdrop:bg-black/50"
|
|
>
|
|
<div className="flex flex-col gap-4 px-4 py-5">
|
|
<div className="flex items-center gap-2">
|
|
<h2 className="m-0 text-[15px] font-semibold">{title}</h2>
|
|
<Button variant="ghost" size="icon" className="ml-auto" aria-label={t("Закрыть")} onClick={() => ref.current?.close()}>
|
|
<X />
|
|
</Button>
|
|
</div>
|
|
{children}
|
|
</div>
|
|
</dialog>
|
|
</>
|
|
);
|
|
}
|