|
| 1 | +'use client'; |
| 2 | + |
| 3 | +import { useCallback, useEffect, useId, useState, type RefObject } from 'react'; |
| 4 | +import { useAccessibility, useFocusTrap } from '@/hooks/useAccessibility'; |
| 5 | +import { getWCAGLevel, type AccessibilityIssue } from '@/utils/accessibilityUtils'; |
| 6 | +import { AlertCircle, CheckCircle2, ClipboardList, X } from 'lucide-react'; |
| 7 | + |
| 8 | +/** |
| 9 | + * Development helper: heuristic WCAG-oriented checks over the document. |
| 10 | + * Does not replace manual testing with assistive technology. |
| 11 | + */ |
| 12 | +export function AccessibilityAudit() { |
| 13 | + const { runPageAudit, announce } = useAccessibility(); |
| 14 | + const [open, setOpen] = useState(false); |
| 15 | + const [issues, setIssues] = useState<AccessibilityIssue[]>([]); |
| 16 | + const [busy, setBusy] = useState(false); |
| 17 | + const titleId = useId(); |
| 18 | + const descId = useId(); |
| 19 | + const trapRef = useFocusTrap(open); |
| 20 | + |
| 21 | + const run = useCallback(() => { |
| 22 | + setBusy(true); |
| 23 | + window.requestAnimationFrame(() => { |
| 24 | + const found = runPageAudit(); |
| 25 | + setIssues(found); |
| 26 | + setBusy(false); |
| 27 | + const level = getWCAGLevel(found); |
| 28 | + const critical = found.filter((i) => i.severity === 'critical').length; |
| 29 | + announce( |
| 30 | + `Audit complete. ${found.length} findings. WCAG estimate ${level}.`, |
| 31 | + critical > 0 ? 'assertive' : 'polite', |
| 32 | + ); |
| 33 | + }); |
| 34 | + }, [runPageAudit, announce]); |
| 35 | + |
| 36 | + useEffect(() => { |
| 37 | + if (!open) return; |
| 38 | + run(); |
| 39 | + }, [open, run]); |
| 40 | + |
| 41 | + useEffect(() => { |
| 42 | + if (!open) return; |
| 43 | + const onKeyDown = (e: KeyboardEvent) => { |
| 44 | + if (e.key === 'Escape') { |
| 45 | + e.preventDefault(); |
| 46 | + setOpen(false); |
| 47 | + } |
| 48 | + }; |
| 49 | + document.addEventListener('keydown', onKeyDown); |
| 50 | + return () => document.removeEventListener('keydown', onKeyDown); |
| 51 | + }, [open]); |
| 52 | + |
| 53 | + const level = getWCAGLevel(issues); |
| 54 | + |
| 55 | + return ( |
| 56 | + <> |
| 57 | + <button |
| 58 | + type="button" |
| 59 | + className="fixed bottom-4 left-4 z-[9998] flex h-12 w-12 items-center justify-center rounded-full border border-amber-300 bg-amber-500 text-white shadow-lg focus:outline-none focus:ring-2 focus:ring-amber-400 focus:ring-offset-2 dark:border-amber-600 dark:bg-amber-600 dark:focus:ring-offset-gray-950" |
| 60 | + aria-label="Open accessibility audit (development only)" |
| 61 | + aria-expanded={open} |
| 62 | + aria-controls="a11y-audit-dialog" |
| 63 | + onClick={() => setOpen((o) => !o)} |
| 64 | + > |
| 65 | + <ClipboardList size={22} aria-hidden="true" /> |
| 66 | + </button> |
| 67 | + |
| 68 | + {open ? ( |
| 69 | + <> |
| 70 | + <div |
| 71 | + className="fixed inset-0 z-[9997] bg-black/30" |
| 72 | + aria-hidden="true" |
| 73 | + onClick={() => setOpen(false)} |
| 74 | + /> |
| 75 | + <section |
| 76 | + id="a11y-audit-dialog" |
| 77 | + ref={trapRef as RefObject<HTMLElement>} |
| 78 | + role="dialog" |
| 79 | + aria-modal="true" |
| 80 | + aria-labelledby={titleId} |
| 81 | + aria-describedby={descId} |
| 82 | + aria-busy={busy} |
| 83 | + className="fixed bottom-20 left-4 z-[9998] flex max-h-[min(70vh,28rem)] w-[min(calc(100vw-2rem),22rem)] flex-col rounded-xl border border-gray-200 bg-white shadow-2xl outline-none dark:border-gray-700 dark:bg-gray-900" |
| 84 | + > |
| 85 | + <p id={descId} className="sr-only"> |
| 86 | + Automated accessibility scan of the page. Results are heuristic and must be verified |
| 87 | + manually with keyboard and screen reader testing. |
| 88 | + </p> |
| 89 | + <header className="flex items-center justify-between gap-2 border-b border-gray-100 px-3 py-2 dark:border-gray-800"> |
| 90 | + <h2 id={titleId} className="text-sm font-semibold text-gray-900 dark:text-gray-50"> |
| 91 | + A11y audit |
| 92 | + </h2> |
| 93 | + <button |
| 94 | + type="button" |
| 95 | + className="rounded p-1 text-gray-500 hover:bg-gray-100 hover:text-gray-800 focus:outline-none focus:ring-2 focus:ring-amber-500 dark:hover:bg-gray-800 dark:hover:text-gray-100" |
| 96 | + aria-label="Close audit panel" |
| 97 | + onClick={() => setOpen(false)} |
| 98 | + > |
| 99 | + <X size={18} aria-hidden="true" /> |
| 100 | + </button> |
| 101 | + </header> |
| 102 | + |
| 103 | + <div className="flex flex-1 flex-col gap-2 overflow-hidden p-3"> |
| 104 | + <div className="flex items-center justify-between text-xs"> |
| 105 | + <span className="text-gray-600 dark:text-gray-400">Heuristic WCAG mapping</span> |
| 106 | + <span |
| 107 | + className={`rounded-full px-2 py-0.5 font-medium ${ |
| 108 | + level === 'Fail' |
| 109 | + ? 'bg-red-100 text-red-800 dark:bg-red-950 dark:text-red-200' |
| 110 | + : level === 'AAA' |
| 111 | + ? 'bg-green-100 text-green-800 dark:bg-green-950 dark:text-green-200' |
| 112 | + : 'bg-amber-100 text-amber-900 dark:bg-amber-950 dark:text-amber-100' |
| 113 | + }`} |
| 114 | + > |
| 115 | + {level} |
| 116 | + </span> |
| 117 | + </div> |
| 118 | + |
| 119 | + <button |
| 120 | + type="button" |
| 121 | + className="rounded-lg bg-amber-600 px-3 py-2 text-sm font-medium text-white hover:bg-amber-700 focus:outline-none focus:ring-2 focus:ring-amber-500 disabled:opacity-60" |
| 122 | + onClick={run} |
| 123 | + disabled={busy} |
| 124 | + > |
| 125 | + {busy ? 'Scanning…' : 'Run again'} |
| 126 | + </button> |
| 127 | + |
| 128 | + <div |
| 129 | + className="min-h-0 flex-1 overflow-y-auto text-sm" |
| 130 | + role="region" |
| 131 | + aria-label="Scan results" |
| 132 | + tabIndex={0} |
| 133 | + > |
| 134 | + {busy ? ( |
| 135 | + <p className="text-gray-500 dark:text-gray-400">Scanning DOM…</p> |
| 136 | + ) : issues.length === 0 ? ( |
| 137 | + <div className="flex flex-col items-center gap-2 py-6 text-center text-gray-600 dark:text-gray-400"> |
| 138 | + <CheckCircle2 className="text-green-600" size={36} aria-hidden="true" /> |
| 139 | + <p>No issues flagged by automated checks.</p> |
| 140 | + </div> |
| 141 | + ) : ( |
| 142 | + <ul className="space-y-2"> |
| 143 | + {issues.map((issue) => ( |
| 144 | + <li |
| 145 | + key={issue.id} |
| 146 | + className="rounded-lg border border-gray-100 bg-gray-50 p-2 dark:border-gray-800 dark:bg-gray-950" |
| 147 | + > |
| 148 | + <div className="flex gap-2"> |
| 149 | + <AlertCircle |
| 150 | + className="mt-0.5 shrink-0 text-amber-600 dark:text-amber-400" |
| 151 | + size={16} |
| 152 | + aria-hidden="true" |
| 153 | + /> |
| 154 | + <div> |
| 155 | + <p className="font-medium text-gray-900 dark:text-gray-100"> |
| 156 | + {issue.message} |
| 157 | + </p> |
| 158 | + <p className="mt-1 text-xs text-gray-600 dark:text-gray-400"> |
| 159 | + {issue.suggestion} |
| 160 | + </p> |
| 161 | + <p className="mt-1 text-xs text-gray-500"> |
| 162 | + WCAG: {issue.wcagCriteria.join(', ')} |
| 163 | + </p> |
| 164 | + </div> |
| 165 | + </div> |
| 166 | + </li> |
| 167 | + ))} |
| 168 | + </ul> |
| 169 | + )} |
| 170 | + </div> |
| 171 | + </div> |
| 172 | + </section> |
| 173 | + </> |
| 174 | + ) : null} |
| 175 | + </> |
| 176 | + ); |
| 177 | +} |
0 commit comments