medkit/src/kit/info-section.tsx

81 lines
2.7 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import * as React from "react"
import { CopyIcon, PencilIcon, type LucideIcon } from "lucide-react"
import { cn } from "@/lib/utils"
import { copyToClipboard } from "@/lib/clipboard"
/** Смысловой блок на мягкой подложке: иконка + заголовок, справа — действие; с `onEdit` — карандаш при наведении. */
export function InfoSection({
icon: Icon,
title,
action,
onEdit,
className,
children,
}: {
icon?: LucideIcon
title: React.ReactNode
action?: React.ReactNode
/** Редактирование блока — обычно открывает EditDialog. */
onEdit?: () => void
className?: string
children: React.ReactNode
}) {
return (
<section className={cn("group/section rounded-xl bg-muted/50", className)}>
<h3 className="flex min-h-6 items-center gap-2 px-4 pt-3.5 pb-1 text-sm font-semibold">
{Icon && <Icon className="size-4 text-primary" />}
{title}
{(action || onEdit) && (
<span className="ml-auto flex items-center gap-2 font-normal">
{action}
{onEdit && (
<button
type="button"
aria-label={`Редактировать: ${typeof title === "string" ? title : "блок"}`}
onClick={onEdit}
className="grid size-6 place-items-center rounded-md text-muted-foreground opacity-0 transition group-hover/section:opacity-100 hover:bg-background hover:text-primary focus-visible:opacity-100 focus-visible:ring-[3px] focus-visible:ring-ring/50 focus-visible:outline-none [@media(hover:none)]:opacity-100"
>
<PencilIcon className="size-3.5" />
</button>
)}
</span>
)}
</h3>
<div className="space-y-3 px-4 pt-2 pb-4">{children}</div>
</section>
)
}
/** Подпись + значение; с `copy` — кнопка копирования при наведении. */
export function Field({
label,
copy,
className,
children,
}: {
label: string
copy?: string
className?: string
children: React.ReactNode
}) {
return (
<div className={cn("group min-w-0 text-sm", className)}>
<div className="flex items-center gap-1 text-xs text-muted-foreground">
{label}
{copy && (
<button
type="button"
aria-label={`Скопировать: ${label}`}
onClick={() => copyToClipboard(copy)}
className="opacity-0 transition group-hover:opacity-100 hover:text-primary"
>
<CopyIcon className="size-3" />
</button>
)}
</div>
<div className="mt-0.5 break-words">{children}</div>
</div>
)
}