Daniel Antoni 902bf616a0
fix(gallery): open style cards from the keyboard and stop nested clicks (#578)
The style card was a <div onClick> with no focusable control, so keyboard
and screen reader users could not open the detail modal (#573, item D1).
The card title is now a real <button aria-haspopup="dialog">; making the
whole card a button would nest its section toggles, copy buttons, color
swatches and checklist inside it. The focus mark is an outline, which
forced-colors mode keeps.

The detail modal now behaves as a dialog for those users: it has
role="dialog", aria-modal and its title as label, moves focus to Close on
open, keeps Tab and Shift+Tab inside, and returns focus to the element
that opened it when it closes.

The card's click handler also fired for every control inside it, so
expanding "CSS Code", copying a color or ticking a checklist item opened
the detail modal too, by mouse or keyboard. Clicks that start on a control
inside the card, or that end a text selection inside it, now leave the
modal closed; any other click on the card still opens it.

The code copy button was reachable with Tab but stayed at opacity 0 unless
hovered; it now shows while focused.
2026-11-16 08:11:28 +07:00

51 lines
1.7 KiB
TypeScript

"use client";
import { useState } from "react";
interface CodeSnippetProps {
code: string;
label?: string;
}
export function CodeSnippet({ code, label }: CodeSnippetProps) {
const [copied, setCopied] = useState(false);
async function handleCopy() {
try {
await navigator.clipboard.writeText(code);
setCopied(true);
setTimeout(() => setCopied(false), 2000);
} catch {
// Fallback: ignore
}
}
return (
<div className="relative group">
{label && (
<span className="text-[10px] font-medium text-gray-400 uppercase tracking-wider">
{label}
</span>
)}
<pre className="mt-1 p-3 text-xs bg-gray-50 dark:bg-gray-900 rounded-lg overflow-x-auto scrollbar-thin font-mono text-gray-700 dark:text-gray-300 leading-relaxed">
{code}
</pre>
<button
onClick={handleCopy}
className="absolute top-2 right-2 p-1.5 rounded-md bg-gray-200/80 dark:bg-gray-700/80 text-gray-500 dark:text-gray-400 opacity-0 group-hover:opacity-100 focus:opacity-100 transition-opacity hover:bg-gray-300 dark:hover:bg-gray-600"
title="Copy to clipboard"
>
{copied ? (
<svg className="w-3.5 h-3.5 text-green-500" fill="none" viewBox="0 0 24 24" stroke="currentColor">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M5 13l4 4L19 7" />
</svg>
) : (
<svg className="w-3.5 h-3.5" fill="none" viewBox="0 0 24 24" stroke="currentColor">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M8 16H6a2 2 0 01-2-2V6a2 2 0 012-2h8a2 2 0 012 2v2m-6 12h8a2 2 0 002-2v-8a2 2 0 00-2-2h-8a2 2 0 00-2 2v8a2 2 0 002 2z" />
</svg>
)}
</button>
</div>
);
}