|
| 1 | +import type { PreviewModule } from '../../app/preview-registry'; |
| 2 | +import type { ControlModule } from '../../app/registry'; |
| 3 | +import type { ThemeConfig } from '../../compiler/types'; |
| 4 | +import { |
| 5 | + getContrastRatio, |
| 6 | + getWCAGLevel, |
| 7 | +} from '../../utils/colors'; |
| 8 | + |
| 9 | +export const accessibilityControlModule: ControlModule = { |
| 10 | + id: 'accessibility', |
| 11 | + title: 'Accessibility Audit', |
| 12 | + mount: (container) => { |
| 13 | + container.innerHTML = ` |
| 14 | + <p class="controls-placeholder"> |
| 15 | + The Master Audit scans all semantic color pairs and foundation tokens to ensure WCAG compliance. |
| 16 | + </p> |
| 17 | + <div style="padding: 1rem; background: var(--surface-bg); border-radius: 8px; border: 1px solid rgba(128,128,128,0.1); font-size: 11px; opacity: 0.8;"> |
| 18 | + Use the live preview report to identify and fix contrast failures across your design system. |
| 19 | + </div> |
| 20 | + `; |
| 21 | + } |
| 22 | +}; |
| 23 | + |
| 24 | +type AuditResult = { |
| 25 | + key: string; |
| 26 | + name: string; |
| 27 | + fg: string; |
| 28 | + bg: string; |
| 29 | + ratio: number; |
| 30 | + level: string; |
| 31 | + pass: boolean; |
| 32 | +}; |
| 33 | + |
| 34 | +const renderAuditRow = (p: AuditResult) => { |
| 35 | + const statusColor = p.pass ? 'var(--color-success-500)' : 'var(--color-danger-500)'; |
| 36 | + const onStatusColor = p.pass ? 'var(--on-success)' : 'var(--on-danger)'; |
| 37 | + |
| 38 | + return ` |
| 39 | + <div style="display: grid; grid-template-columns: 2fr 1.5fr 1fr 1fr; align-items: center; gap: 1rem; padding: 1rem; background: var(--surface-card); border-radius: 8px; border: 1px solid ${p.pass ? 'rgba(128,128,128,0.1)' : 'var(--color-danger-500)'};"> |
| 40 | + <div> |
| 41 | + <div style="font-size: 12px; font-weight: 800;">${p.name}</div> |
| 42 | + <div style="font-size: 10px; opacity: 0.6; font-family: monospace;">${p.fg} on ${p.bg}</div> |
| 43 | + </div> |
| 44 | + |
| 45 | + <div style="display: flex; align-items: center; gap: 8px;"> |
| 46 | + <div style="width: 24px; height: 24px; border-radius: 4px; border: 1px solid rgba(128,128,128,0.2); background: ${p.bg}; position: relative; display: flex; align-items: center; justify-content: center;"> |
| 47 | + <span style="color: ${p.fg}; font-size: 14px; font-weight: 900;">A</span> |
| 48 | + </div> |
| 49 | + <span style="font-size: 11px; font-weight: 700;">${p.ratio.toFixed(2)}:1</span> |
| 50 | + </div> |
| 51 | +
|
| 52 | + <div> |
| 53 | + <span style="font-size: 10px; font-weight: 800; padding: 2px 8px; border-radius: 4px; background: ${statusColor}; color: ${onStatusColor};"> |
| 54 | + ${p.level} |
| 55 | + </span> |
| 56 | + </div> |
| 57 | +
|
| 58 | + <div style="text-align: right;"> |
| 59 | + ${p.pass ? '<span style="color: var(--color-success-500); font-size: 12px;">✔ Pass</span>' : ` |
| 60 | + <button onclick="window.parent.auditFix('${p.key}', '${p.bg}', '${p.fg}')" |
| 61 | + style="font-size: 10px; padding: 4px 8px; border-radius: 4px; background: var(--color-primary-500); color: var(--on-primary); border: none; cursor: pointer; font-weight: 700;"> |
| 62 | + Nudge Fix |
| 63 | + </button> |
| 64 | + `} |
| 65 | + </div> |
| 66 | + </div> |
| 67 | + `; |
| 68 | +}; |
| 69 | + |
| 70 | +export const accessibilityPreviewModule: PreviewModule = { |
| 71 | + id: 'accessibility', |
| 72 | + title: 'Accessibility Master Audit', |
| 73 | + render: (config: ThemeConfig) => { |
| 74 | + const colors = config.colors; |
| 75 | + if (!colors) return '<p>No color configuration found.</p>'; |
| 76 | + |
| 77 | + const n50 = colors.neutral?.[50] ?? '#fff'; |
| 78 | + const n900 = colors.neutral?.[900] ?? '#000'; |
| 79 | + |
| 80 | + const getOn = (bg: string) => { |
| 81 | + const r50 = getContrastRatio(bg, n50); |
| 82 | + const r900 = getContrastRatio(bg, n900); |
| 83 | + return r900 >= r50 ? n900 : n50; |
| 84 | + }; |
| 85 | + |
| 86 | + const auditPair = (key: string, name: string, fg: string, bg: string): AuditResult => { |
| 87 | + const ratio = getContrastRatio(fg, bg); |
| 88 | + const level = getWCAGLevel(ratio); |
| 89 | + return { key, name, fg, bg, ratio, level, pass: level !== 'Fail' }; |
| 90 | + }; |
| 91 | + |
| 92 | + const pairs: AuditResult[] = [ |
| 93 | + auditPair('primary', 'Primary vs Surface', colors.primary?.[500] ?? '', n50), |
| 94 | + auditPair('on-primary', 'Primary vs On-Primary', colors.primary?.[500] ?? '', getOn(colors.primary?.[500] ?? '')), |
| 95 | + auditPair('neutral-900', 'Text vs Surface', n900, n50), // Check text color vs background |
| 96 | + auditPair('success', 'Success vs On-Success', colors.success?.[500] ?? '', getOn(colors.success?.[500] ?? '')), |
| 97 | + auditPair('danger', 'Danger vs On-Danger', colors.danger?.[500] ?? '', getOn(colors.danger?.[500] ?? '')), |
| 98 | + auditPair('warning', 'Warning vs On-Warning', colors.warning?.[500] ?? '', getOn(colors.warning?.[500] ?? '')), |
| 99 | + ]; |
| 100 | + |
| 101 | + if (colors.secondary) { |
| 102 | + pairs.push(auditPair('secondary', 'Secondary vs Surface', colors.secondary[500], n50)); |
| 103 | + } |
| 104 | + if (colors.tertiary) { |
| 105 | + pairs.push(auditPair('tertiary', 'Tertiary vs Surface', colors.tertiary[500], n50)); |
| 106 | + } |
| 107 | + |
| 108 | + const score = Math.round((pairs.filter(p => p.pass).length / pairs.length) * 100); |
| 109 | + let scoreColor = 'var(--color-danger-500)'; |
| 110 | + if (score === 100) scoreColor = 'var(--color-success-500)'; |
| 111 | + else if (score > 70) scoreColor = 'var(--color-warning-500)'; |
| 112 | + |
| 113 | + return ` |
| 114 | + <div style="color: var(--on-background); font-family: var(--font-family, sans-serif);"> |
| 115 | + <div style="display: flex; align-items: center; justify-content: space-between; margin-bottom: 2rem; padding: 1.5rem; background: var(--surface-card); border-radius: 12px; border: 1px solid rgba(128,128,128,0.1);"> |
| 116 | + <div> |
| 117 | + <h3 style="margin: 0; font-size: 18px;">Theme Health Score</h3> |
| 118 | + <p style="margin: 4px 0 0 0; font-size: 12px; opacity: 0.6;">Weighted audit of semantic contrast pairs.</p> |
| 119 | + </div> |
| 120 | + <div style="font-size: 32px; font-weight: 800; color: ${scoreColor}">${score}%</div> |
| 121 | + </div> |
| 122 | +
|
| 123 | + <div style="display: grid; gap: 12px;"> |
| 124 | + ${pairs.map(p => renderAuditRow(p)).join('')} |
| 125 | + </div> |
| 126 | + </div> |
| 127 | + `; |
| 128 | + } |
| 129 | +}; |
0 commit comments