Skip to content

Commit cb77ddd

Browse files
committed
feat(sidebar): implement accessibility audit and reorganize handoff tools
- Add basic-accessibility plugin with WCAG contrast scanning and interactive fixes - Implement one-click copy functionality in basic-styleguide - Reorder sidebar to prioritize auditing tools - Add specialized greenish styling for audit-related accordions - Refine typography token readability in Style Guide - Resolve TypeScript lints and global scope declarations
1 parent d71a67d commit cb77ddd

6 files changed

Lines changed: 179 additions & 3 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ All notable changes to this project will be documented in this file. We will log
1414
- **Custom HTML Sandbox**: Added a new sandbox plugin allowing users to test their current theme against custom markup with real-time feedback.
1515
- **Theme Presets (Personas)**: Introduced a library of "One-Click Personas" (Midnight Pro, Cyberpunk, Corporate Clean, Minimalist) for rapid theme experimentation.
1616
- **Dev Handoff: Copy to Clipboard**: Added one-click copy functionality to the Style Guide, allowing users to instantly copy design tokens as CSS variables.
17+
- **Accessibility Master Audit**: Implemented a dedicated audit preview module that scans for WCAG contrast violations across all semantic color pairs and provides one-click "Nudge" fixes.
1718

1819
### Changed
1920

src/app/preview-registry.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { ThemeConfig } from '../compiler/types';
2+
import { accessibilityPreviewModule } from '../plugins/basic-accessibility';
23
import { alertPreviewModule } from '../plugins/basic-alert';
34
import { buttonsPreviewModule } from '../plugins/basic-buttons';
45
import { cardPreviewModule } from '../plugins/basic-card';
@@ -27,6 +28,7 @@ export type PreviewModule = {
2728
export const previewModules = [
2829
sandboxPreviewModule,
2930
styleguidePreviewModule,
31+
accessibilityPreviewModule,
3032
layoutPreviewModule,
3133
dashboardPreviewModule,
3234
colorsPreviewModule,

src/app/ui.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { accessibilityControlModule } from '../plugins/basic-accessibility';
12
import { alertControlModule } from '../plugins/basic-alert';
23
import { buttonsControlModule } from '../plugins/basic-buttons';
34
import { cardControlModule } from '../plugins/basic-card';
@@ -21,11 +22,12 @@ import type { ControlsRegistry } from './registry';
2122

2223
// here we will place all controls we will build
2324
export const controlsRegistry: ControlsRegistry = {
25+
sandbox: sandboxControlModule,
26+
accessibility: accessibilityControlModule,
27+
styleguide: styleguideControlModule,
2428
name: themeNameControlModule,
2529
presets: presetsControlModule,
26-
sandbox: sandboxControlModule,
2730
layout: layoutControlModule,
28-
styleguide: styleguideControlModule,
2931
colors: colorsControlModule,
3032
typography: typographyControlModule,
3133
surface: surfaceControlModule,

src/main.ts

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { createPreview } from './app/preview';
44
import { getConfig, subscribe, updateConfig, undo, redo, subscribeHistory } from './app/state';
55
import { controlsRegistry } from './app/ui';
66
import type { ThemeMode } from './compiler/types';
7+
import { nudgeContrast, generateScale } from './utils/colors';
78

89
const root = document.querySelector<HTMLDivElement>('#app');
910

@@ -86,14 +87,42 @@ const controlApi = {
8687
subscribe,
8788
setActivePreview: (ids: string[]) => preview.setActive(ids),
8889
};
90+
91+
declare global {
92+
var auditFix: (key: string, baseBg: string, targetFg: string) => void;
93+
}
94+
95+
// Global Accessibility Fixer for Preview Modules
96+
globalThis.auditFix = (key: string, baseBg: string, targetFg: string) => {
97+
const fixed = nudgeContrast(baseBg, targetFg);
98+
updateConfig(cfg => {
99+
const next = { ...cfg };
100+
if (!next.colors) return next;
101+
102+
// If the key is a semantic scale, regenerate it
103+
if (key === 'primary') next.colors.primary = generateScale(fixed);
104+
else if (key === 'secondary') next.colors.secondary = generateScale(fixed);
105+
else if (key === 'tertiary') next.colors.tertiary = generateScale(fixed);
106+
else if (key === 'danger') next.colors.danger = generateScale(fixed);
107+
else if (key === 'success') next.colors.success = generateScale(fixed);
108+
else if (key === 'warning') next.colors.warning = generateScale(fixed);
109+
else if (key === 'neutral-900') next.colors.neutral = { ...next.colors.neutral, 900: fixed };
110+
else if (key === 'on-primary') next.colors.primary = generateScale(fixed); // Nudge background to fix on-color
111+
112+
return next;
113+
});
114+
};
115+
89116
const controlCleanups: (() => void)[] = [];
90117
const openIds = new Set<string>();
91118
const accordions: { details: HTMLDetailsElement; id: string }[] = [];
92119

93120
// Build accordion per control module (single-open behavior)
94121
Object.values(controlsRegistry).forEach((module, index) => {
95122
const details = document.createElement('details');
96-
details.className = 'control-accordion';
123+
const isAudit = ['sandbox', 'accessibility', 'styleguide'].includes(module.id);
124+
details.className = isAudit ? 'control-accordion audit-accordion' : 'control-accordion';
125+
97126
if (index === 0) details.open = true;
98127
const summary = document.createElement('summary');
99128
summary.textContent = module.title;
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
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+
};

src/style.css

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,19 @@ body {
127127
margin-top: 10px;
128128
}
129129

130+
.control-accordion.audit-accordion {
131+
border-color: #1e3a2f;
132+
background: #0a1f16;
133+
}
134+
135+
.control-accordion.audit-accordion > summary {
136+
color: #a7f3d0;
137+
}
138+
139+
.control-accordion.audit-accordion > summary::before {
140+
border-color: #34d399;
141+
}
142+
130143
.control-accordion > summary {
131144
position: relative;
132145
list-style: none;

0 commit comments

Comments
 (0)