@fragments-sdk/viewer 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +84 -0
- package/index.html +28 -0
- package/package.json +71 -0
- package/src/__tests__/a11y-fixes.test.ts +358 -0
- package/src/__tests__/jsx-parser.test.ts +502 -0
- package/src/__tests__/render-utils.test.ts +232 -0
- package/src/__tests__/style-utils.test.ts +404 -0
- package/src/app/index.ts +1 -0
- package/src/assets/fragments-logo.ts +4 -0
- package/src/assets/fragments_logo.png +0 -0
- package/src/components/AccessibilityPanel.tsx +1457 -0
- package/src/components/ActionCapture.tsx +172 -0
- package/src/components/ActionsPanel.tsx +332 -0
- package/src/components/AllVariantsPreview.tsx +78 -0
- package/src/components/App.tsx +604 -0
- package/src/components/BottomPanel.tsx +288 -0
- package/src/components/CodePanel.naming.test.tsx +59 -0
- package/src/components/CodePanel.tsx +118 -0
- package/src/components/CommandPalette.tsx +392 -0
- package/src/components/ComponentDocView.tsx +164 -0
- package/src/components/ComponentGraph.tsx +380 -0
- package/src/components/ComponentHeader.tsx +88 -0
- package/src/components/ContractPanel.tsx +241 -0
- package/src/components/DeviceMockup.tsx +156 -0
- package/src/components/EmptyVariantMessage.tsx +54 -0
- package/src/components/ErrorBoundary.tsx +97 -0
- package/src/components/FigmaEmbed.tsx +238 -0
- package/src/components/FragmentEditor.tsx +525 -0
- package/src/components/FragmentRenderer.tsx +61 -0
- package/src/components/HeaderSearch.tsx +24 -0
- package/src/components/HealthDashboard.tsx +441 -0
- package/src/components/HmrStatusIndicator.tsx +61 -0
- package/src/components/Icons.tsx +479 -0
- package/src/components/InteractionsPanel.tsx +757 -0
- package/src/components/IsolatedPreviewFrame.tsx +390 -0
- package/src/components/IsolatedRender.tsx +113 -0
- package/src/components/KeyboardShortcutsHelp.tsx +53 -0
- package/src/components/LandingPage.tsx +420 -0
- package/src/components/Layout.tsx +27 -0
- package/src/components/LeftSidebar.tsx +472 -0
- package/src/components/LoadErrorMessage.tsx +102 -0
- package/src/components/MultiViewportPreview.tsx +527 -0
- package/src/components/NoVariantsMessage.tsx +59 -0
- package/src/components/PanelShell.tsx +161 -0
- package/src/components/PerformancePanel.tsx +304 -0
- package/src/components/PreviewArea.tsx +254 -0
- package/src/components/PreviewAside.tsx +168 -0
- package/src/components/PreviewFrameHost.tsx +304 -0
- package/src/components/PreviewToolbar.tsx +80 -0
- package/src/components/PropsEditor.tsx +506 -0
- package/src/components/PropsTable.tsx +111 -0
- package/src/components/RelationsSection.tsx +88 -0
- package/src/components/ResizablePanel.tsx +271 -0
- package/src/components/RightSidebar.tsx +102 -0
- package/src/components/RuntimeToolsRegistrar.tsx +17 -0
- package/src/components/ScreenshotButton.tsx +90 -0
- package/src/components/ShadowPreview.tsx +204 -0
- package/src/components/Sidebar.tsx +169 -0
- package/src/components/SkeletonLoader.tsx +161 -0
- package/src/components/ThemeProvider.tsx +42 -0
- package/src/components/Toast.tsx +3 -0
- package/src/components/TokenStylePanel.tsx +699 -0
- package/src/components/TopToolbar.tsx +159 -0
- package/src/components/Untitled +1 -0
- package/src/components/UsageSection.tsx +95 -0
- package/src/components/VariantMatrix.tsx +391 -0
- package/src/components/VariantRenderer.tsx +131 -0
- package/src/components/VariantTabs.tsx +40 -0
- package/src/components/ViewerHeader.tsx +69 -0
- package/src/components/ViewerStateSync.tsx +52 -0
- package/src/components/ViewportSelector.tsx +172 -0
- package/src/components/WebMCPDevTools.tsx +503 -0
- package/src/components/WebMCPIntegration.tsx +47 -0
- package/src/components/WebMCPStatusIndicator.tsx +60 -0
- package/src/components/_future/CreatePage.tsx +835 -0
- package/src/components/viewer-utils.ts +16 -0
- package/src/composition-renderer.ts +381 -0
- package/src/constants/index.ts +1 -0
- package/src/constants/ui.ts +166 -0
- package/src/entry.tsx +335 -0
- package/src/hooks/index.ts +2 -0
- package/src/hooks/useA11yCache.ts +383 -0
- package/src/hooks/useA11yService.ts +364 -0
- package/src/hooks/useActions.ts +138 -0
- package/src/hooks/useAppState.ts +147 -0
- package/src/hooks/useCompiledFragments.ts +42 -0
- package/src/hooks/useFigmaIntegration.ts +132 -0
- package/src/hooks/useHmrStatus.ts +109 -0
- package/src/hooks/useKeyboardShortcuts.ts +270 -0
- package/src/hooks/usePreviewBridge.ts +347 -0
- package/src/hooks/useScrollSpy.ts +78 -0
- package/src/hooks/useShadowStyles.ts +221 -0
- package/src/hooks/useUrlState.ts +318 -0
- package/src/hooks/useViewSettings.ts +111 -0
- package/src/intelligence/healthReport.ts +505 -0
- package/src/intelligence/styleDrift.ts +340 -0
- package/src/intelligence/usageScanner.ts +309 -0
- package/src/jsx-parser.ts +486 -0
- package/src/preview-frame-entry.tsx +25 -0
- package/src/preview-frame.html +148 -0
- package/src/render-template.html +68 -0
- package/src/render-utils.ts +311 -0
- package/src/shared/ComponentDocContent.module.scss +10 -0
- package/src/shared/ComponentDocContent.module.scss.d.ts +2 -0
- package/src/shared/ComponentDocContent.tsx +274 -0
- package/src/shared/DocsHeaderBar.tsx +129 -0
- package/src/shared/DocsPageAsideHost.tsx +89 -0
- package/src/shared/DocsPageShell.tsx +124 -0
- package/src/shared/DocsSearchCommand.tsx +99 -0
- package/src/shared/DocsSidebarNav.tsx +66 -0
- package/src/shared/PropsTable.module.scss +68 -0
- package/src/shared/PropsTable.module.scss.d.ts +2 -0
- package/src/shared/PropsTable.tsx +76 -0
- package/src/shared/VariantPreviewCard.module.scss +114 -0
- package/src/shared/VariantPreviewCard.module.scss.d.ts +2 -0
- package/src/shared/VariantPreviewCard.tsx +137 -0
- package/src/shared/docs-data/index.ts +32 -0
- package/src/shared/docs-data/mcp-configs.ts +72 -0
- package/src/shared/docs-data/palettes.ts +75 -0
- package/src/shared/docs-data/setup-examples.ts +55 -0
- package/src/shared/docs-layout.scss +28 -0
- package/src/shared/docs-layout.scss.d.ts +2 -0
- package/src/shared/index.ts +34 -0
- package/src/shared/types.ts +53 -0
- package/src/style-utils.ts +414 -0
- package/src/styles/globals.css +278 -0
- package/src/types/a11y.ts +197 -0
- package/src/utils/a11y-fixes.ts +509 -0
- package/src/utils/actionExport.ts +372 -0
- package/src/utils/colorSchemes.ts +201 -0
- package/src/utils/contrast.ts +246 -0
- package/src/utils/detectRelationships.ts +256 -0
- package/src/webmcp/__tests__/analytics.test.ts +108 -0
- package/src/webmcp/analytics.ts +165 -0
- package/src/webmcp/index.ts +3 -0
- package/src/webmcp/posthog-bridge.ts +39 -0
- package/src/webmcp/runtime-tools.ts +152 -0
- package/src/webmcp/scan-utils.ts +135 -0
- package/src/webmcp/use-tool-analytics.ts +69 -0
- package/src/webmcp/viewer-state.ts +45 -0
- package/tsconfig.json +20 -0
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Accessibility Types
|
|
3
|
+
*
|
|
4
|
+
* TypeScript interfaces for the accessibility checking system.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import type { ImpactValue } from 'axe-core';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Serialized violation node for storage
|
|
11
|
+
*/
|
|
12
|
+
export interface SerializedNode {
|
|
13
|
+
/** HTML snippet of the affected element */
|
|
14
|
+
html: string;
|
|
15
|
+
/** CSS selector path to the element */
|
|
16
|
+
target: string[];
|
|
17
|
+
/** Failure summary explaining the issue */
|
|
18
|
+
failureSummary?: string;
|
|
19
|
+
/** Any data collected during the check */
|
|
20
|
+
any?: Array<{
|
|
21
|
+
id: string;
|
|
22
|
+
data?: unknown;
|
|
23
|
+
relatedNodes?: Array<{ html: string; target: string[] }>;
|
|
24
|
+
impact?: string;
|
|
25
|
+
message: string;
|
|
26
|
+
}>;
|
|
27
|
+
/** All data required for the check to pass */
|
|
28
|
+
all?: Array<{
|
|
29
|
+
id: string;
|
|
30
|
+
data?: unknown;
|
|
31
|
+
relatedNodes?: Array<{ html: string; target: string[] }>;
|
|
32
|
+
impact?: string;
|
|
33
|
+
message: string;
|
|
34
|
+
}>;
|
|
35
|
+
/** None data that should not be present */
|
|
36
|
+
none?: Array<{
|
|
37
|
+
id: string;
|
|
38
|
+
data?: unknown;
|
|
39
|
+
relatedNodes?: Array<{ html: string; target: string[] }>;
|
|
40
|
+
impact?: string;
|
|
41
|
+
message: string;
|
|
42
|
+
}>;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Serialized violation for storage and display
|
|
47
|
+
*/
|
|
48
|
+
export interface SerializedViolation {
|
|
49
|
+
/** Unique rule identifier (e.g., 'color-contrast') */
|
|
50
|
+
id: string;
|
|
51
|
+
/** Impact level of the violation */
|
|
52
|
+
impact: ImpactValue | null;
|
|
53
|
+
/** Brief description of the rule */
|
|
54
|
+
description: string;
|
|
55
|
+
/** Longer help text explaining how to fix */
|
|
56
|
+
help: string;
|
|
57
|
+
/** URL to detailed documentation */
|
|
58
|
+
helpUrl: string;
|
|
59
|
+
/** WCAG tags (e.g., 'wcag2a', 'wcag143') */
|
|
60
|
+
tags: string[];
|
|
61
|
+
/** Affected elements */
|
|
62
|
+
nodes: SerializedNode[];
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Cached accessibility result for a component
|
|
67
|
+
*/
|
|
68
|
+
export interface CachedA11yResult {
|
|
69
|
+
/** Component name */
|
|
70
|
+
componentName: string;
|
|
71
|
+
/** Variant that was scanned (optional) */
|
|
72
|
+
variant?: string;
|
|
73
|
+
/** Full violation details for display */
|
|
74
|
+
violations: SerializedViolation[];
|
|
75
|
+
/** Number of passing checks */
|
|
76
|
+
passes: number;
|
|
77
|
+
/** Number of incomplete checks */
|
|
78
|
+
incomplete: number;
|
|
79
|
+
/** When the scan was performed */
|
|
80
|
+
scannedAt: number;
|
|
81
|
+
/** Counts by severity */
|
|
82
|
+
counts: {
|
|
83
|
+
critical: number;
|
|
84
|
+
serious: number;
|
|
85
|
+
moderate: number;
|
|
86
|
+
minor: number;
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Summary data derived from cache
|
|
92
|
+
*/
|
|
93
|
+
export interface A11ySummary {
|
|
94
|
+
/** Components with no critical/serious violations */
|
|
95
|
+
accessibleComponents: number;
|
|
96
|
+
/** Total components scanned */
|
|
97
|
+
totalComponents: number;
|
|
98
|
+
/** Violations aggregated by impact */
|
|
99
|
+
violationsByImpact: {
|
|
100
|
+
critical: number;
|
|
101
|
+
serious: number;
|
|
102
|
+
moderate: number;
|
|
103
|
+
minor: number;
|
|
104
|
+
};
|
|
105
|
+
/** Top violations across all components */
|
|
106
|
+
topViolations: Array<{
|
|
107
|
+
ruleId: string;
|
|
108
|
+
description: string;
|
|
109
|
+
affectedComponents: string[];
|
|
110
|
+
impact: ImpactValue | null;
|
|
111
|
+
}>;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Event data for a11y cache invalidation
|
|
116
|
+
*/
|
|
117
|
+
export interface A11yInvalidateEvent {
|
|
118
|
+
/** Component names to invalidate */
|
|
119
|
+
components: string[];
|
|
120
|
+
/** File that changed */
|
|
121
|
+
file: string;
|
|
122
|
+
/** Timestamp of the change */
|
|
123
|
+
timestamp: number;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* A11y service configuration
|
|
128
|
+
*/
|
|
129
|
+
export interface A11yServiceConfig {
|
|
130
|
+
/** Debounce delay after HMR file change (ms) */
|
|
131
|
+
hmrDebounceMs: number;
|
|
132
|
+
/** Debounce delay after navigation (ms) */
|
|
133
|
+
navigationDebounceMs: number;
|
|
134
|
+
/** Minimum time between same component scans (ms) */
|
|
135
|
+
scanCooldownMs: number;
|
|
136
|
+
/** Maximum concurrent scans */
|
|
137
|
+
maxConcurrentScans: number;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Default configuration values
|
|
142
|
+
*/
|
|
143
|
+
export const DEFAULT_A11Y_CONFIG: A11yServiceConfig = {
|
|
144
|
+
hmrDebounceMs: 500,
|
|
145
|
+
navigationDebounceMs: 200,
|
|
146
|
+
scanCooldownMs: 1000,
|
|
147
|
+
maxConcurrentScans: 2,
|
|
148
|
+
};
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* Static fix suggestion for a violation
|
|
152
|
+
*/
|
|
153
|
+
export interface StaticFixSuggestion {
|
|
154
|
+
/** Why this issue matters for users */
|
|
155
|
+
whyItMatters: string;
|
|
156
|
+
/** Generic fix description */
|
|
157
|
+
howToFix: string;
|
|
158
|
+
/** Example of bad code */
|
|
159
|
+
badExample?: string;
|
|
160
|
+
/** Example of good code */
|
|
161
|
+
goodExample?: string;
|
|
162
|
+
/** WCAG criterion reference */
|
|
163
|
+
wcagCriterion?: {
|
|
164
|
+
id: string;
|
|
165
|
+
name: string;
|
|
166
|
+
level: 'A' | 'AA' | 'AAA';
|
|
167
|
+
url: string;
|
|
168
|
+
};
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* Specific fix for an element
|
|
173
|
+
*/
|
|
174
|
+
export interface ElementFix {
|
|
175
|
+
/** The original HTML */
|
|
176
|
+
originalHtml: string;
|
|
177
|
+
/** The suggested fix HTML */
|
|
178
|
+
fixedHtml: string;
|
|
179
|
+
/** Explanation of the change */
|
|
180
|
+
explanation: string;
|
|
181
|
+
/** Whether this is an auto-fixable change */
|
|
182
|
+
autoFixable: boolean;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* Scan status for tracking progress
|
|
187
|
+
*/
|
|
188
|
+
export type ScanStatus = 'idle' | 'pending' | 'scanning' | 'completed' | 'error';
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* Component scan state
|
|
192
|
+
*/
|
|
193
|
+
export interface ComponentScanState {
|
|
194
|
+
status: ScanStatus;
|
|
195
|
+
lastScanAt?: number;
|
|
196
|
+
error?: string;
|
|
197
|
+
}
|
|
@@ -0,0 +1,509 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A11y Fixes - Static Fix Generators
|
|
3
|
+
*
|
|
4
|
+
* Rule-specific fix suggestions without AI. Provides:
|
|
5
|
+
* - "Why it matters" explanations
|
|
6
|
+
* - Before/after code examples
|
|
7
|
+
* - WCAG references
|
|
8
|
+
* - Specific fixes based on violation data
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import type { ImpactValue } from 'axe-core';
|
|
12
|
+
import type { StaticFixSuggestion, ElementFix, SerializedNode } from '../types/a11y.js';
|
|
13
|
+
import { parseColor, suggestFix, rgbToHex } from './contrast.js';
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* WCAG criteria references
|
|
17
|
+
*/
|
|
18
|
+
const WCAG_CRITERIA: Record<string, { id: string; name: string; level: 'A' | 'AA' | 'AAA'; url: string }> = {
|
|
19
|
+
// Level A
|
|
20
|
+
'1.1.1': {
|
|
21
|
+
id: '1.1.1',
|
|
22
|
+
name: 'Non-text Content',
|
|
23
|
+
level: 'A',
|
|
24
|
+
url: 'https://www.w3.org/WAI/WCAG22/Understanding/non-text-content',
|
|
25
|
+
},
|
|
26
|
+
'1.3.1': {
|
|
27
|
+
id: '1.3.1',
|
|
28
|
+
name: 'Info and Relationships',
|
|
29
|
+
level: 'A',
|
|
30
|
+
url: 'https://www.w3.org/WAI/WCAG22/Understanding/info-and-relationships',
|
|
31
|
+
},
|
|
32
|
+
'2.1.1': {
|
|
33
|
+
id: '2.1.1',
|
|
34
|
+
name: 'Keyboard',
|
|
35
|
+
level: 'A',
|
|
36
|
+
url: 'https://www.w3.org/WAI/WCAG22/Understanding/keyboard',
|
|
37
|
+
},
|
|
38
|
+
'2.4.4': {
|
|
39
|
+
id: '2.4.4',
|
|
40
|
+
name: 'Link Purpose (In Context)',
|
|
41
|
+
level: 'A',
|
|
42
|
+
url: 'https://www.w3.org/WAI/WCAG22/Understanding/link-purpose-in-context',
|
|
43
|
+
},
|
|
44
|
+
'4.1.2': {
|
|
45
|
+
id: '4.1.2',
|
|
46
|
+
name: 'Name, Role, Value',
|
|
47
|
+
level: 'A',
|
|
48
|
+
url: 'https://www.w3.org/WAI/WCAG22/Understanding/name-role-value',
|
|
49
|
+
},
|
|
50
|
+
// Level AA
|
|
51
|
+
'1.4.3': {
|
|
52
|
+
id: '1.4.3',
|
|
53
|
+
name: 'Contrast (Minimum)',
|
|
54
|
+
level: 'AA',
|
|
55
|
+
url: 'https://www.w3.org/WAI/WCAG22/Understanding/contrast-minimum',
|
|
56
|
+
},
|
|
57
|
+
'1.4.4': {
|
|
58
|
+
id: '1.4.4',
|
|
59
|
+
name: 'Resize Text',
|
|
60
|
+
level: 'AA',
|
|
61
|
+
url: 'https://www.w3.org/WAI/WCAG22/Understanding/resize-text',
|
|
62
|
+
},
|
|
63
|
+
'2.4.6': {
|
|
64
|
+
id: '2.4.6',
|
|
65
|
+
name: 'Headings and Labels',
|
|
66
|
+
level: 'AA',
|
|
67
|
+
url: 'https://www.w3.org/WAI/WCAG22/Understanding/headings-and-labels',
|
|
68
|
+
},
|
|
69
|
+
'2.5.8': {
|
|
70
|
+
id: '2.5.8',
|
|
71
|
+
name: 'Target Size (Minimum)',
|
|
72
|
+
level: 'AA',
|
|
73
|
+
url: 'https://www.w3.org/WAI/WCAG22/Understanding/target-size-minimum',
|
|
74
|
+
},
|
|
75
|
+
// Level AAA
|
|
76
|
+
'1.4.6': {
|
|
77
|
+
id: '1.4.6',
|
|
78
|
+
name: 'Contrast (Enhanced)',
|
|
79
|
+
level: 'AAA',
|
|
80
|
+
url: 'https://www.w3.org/WAI/WCAG22/Understanding/contrast-enhanced',
|
|
81
|
+
},
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Static fix suggestions for common accessibility rules
|
|
86
|
+
*/
|
|
87
|
+
const STATIC_FIXES: Record<string, StaticFixSuggestion> = {
|
|
88
|
+
'color-contrast': {
|
|
89
|
+
whyItMatters:
|
|
90
|
+
'Low contrast text is difficult to read for users with low vision, color blindness, or in bright environments like outdoor settings.',
|
|
91
|
+
howToFix:
|
|
92
|
+
'Increase the contrast ratio between text and background colors. Normal text requires 4.5:1, large text (18pt or 14pt bold) requires 3:1.',
|
|
93
|
+
badExample: '<button style="color: #888; background: #fff">Submit</button>',
|
|
94
|
+
goodExample: '<button style="color: #333; background: #fff">Submit</button>',
|
|
95
|
+
wcagCriterion: WCAG_CRITERIA['1.4.3'],
|
|
96
|
+
},
|
|
97
|
+
'image-alt': {
|
|
98
|
+
whyItMatters:
|
|
99
|
+
'Screen reader users cannot perceive images and rely on alternative text to understand visual content.',
|
|
100
|
+
howToFix:
|
|
101
|
+
'Add descriptive alt text that conveys the purpose or content of the image. For decorative images, use alt="".',
|
|
102
|
+
badExample: '<img src="hero.jpg">',
|
|
103
|
+
goodExample: '<img src="hero.jpg" alt="Team collaboration in modern office">',
|
|
104
|
+
wcagCriterion: WCAG_CRITERIA['1.1.1'],
|
|
105
|
+
},
|
|
106
|
+
'button-name': {
|
|
107
|
+
whyItMatters:
|
|
108
|
+
'Screen reader users need accessible names to understand what a button does. Without a name, they hear "button" with no context.',
|
|
109
|
+
howToFix:
|
|
110
|
+
'Add text content, an aria-label, or aria-labelledby attribute to give the button an accessible name.',
|
|
111
|
+
badExample: '<button><svg>...</svg></button>',
|
|
112
|
+
goodExample: '<button aria-label="Close dialog"><svg>...</svg></button>',
|
|
113
|
+
wcagCriterion: WCAG_CRITERIA['4.1.2'],
|
|
114
|
+
},
|
|
115
|
+
'link-name': {
|
|
116
|
+
whyItMatters:
|
|
117
|
+
'Screen reader users navigate by links and need descriptive text to understand each link\'s destination or purpose.',
|
|
118
|
+
howToFix:
|
|
119
|
+
'Use descriptive link text instead of generic phrases. Avoid "click here" or "read more" without context.',
|
|
120
|
+
badExample: '<a href="/docs">Click here</a>',
|
|
121
|
+
goodExample: '<a href="/docs">View documentation</a>',
|
|
122
|
+
wcagCriterion: WCAG_CRITERIA['2.4.4'],
|
|
123
|
+
},
|
|
124
|
+
'label': {
|
|
125
|
+
whyItMatters:
|
|
126
|
+
'Form inputs without labels are difficult for screen reader users to understand and for all users to identify.',
|
|
127
|
+
howToFix:
|
|
128
|
+
'Associate a <label> element with the input using the for/id attributes, or wrap the input in a label element.',
|
|
129
|
+
badExample: '<input type="text" placeholder="Email">',
|
|
130
|
+
goodExample: '<label for="email">Email</label>\n<input type="text" id="email">',
|
|
131
|
+
wcagCriterion: WCAG_CRITERIA['1.3.1'],
|
|
132
|
+
},
|
|
133
|
+
'aria-valid-attr': {
|
|
134
|
+
whyItMatters:
|
|
135
|
+
'Invalid ARIA attributes are ignored by assistive technologies and may indicate other accessibility issues.',
|
|
136
|
+
howToFix:
|
|
137
|
+
'Use only valid ARIA attributes from the WAI-ARIA specification. Check spelling and ensure attributes apply to the element type.',
|
|
138
|
+
badExample: '<button aria-expanded="true" aria-describedby="">Submit</button>',
|
|
139
|
+
goodExample: '<button aria-expanded="true" aria-describedby="help-text">Submit</button>',
|
|
140
|
+
wcagCriterion: WCAG_CRITERIA['4.1.2'],
|
|
141
|
+
},
|
|
142
|
+
'aria-valid-attr-value': {
|
|
143
|
+
whyItMatters:
|
|
144
|
+
'ARIA attributes with invalid values are ignored or misinterpreted by assistive technologies.',
|
|
145
|
+
howToFix:
|
|
146
|
+
'Ensure ARIA attribute values match the expected format (boolean, ID reference, token, etc.).',
|
|
147
|
+
badExample: '<div aria-hidden="yes">Hidden content</div>',
|
|
148
|
+
goodExample: '<div aria-hidden="true">Hidden content</div>',
|
|
149
|
+
wcagCriterion: WCAG_CRITERIA['4.1.2'],
|
|
150
|
+
},
|
|
151
|
+
'aria-required-attr': {
|
|
152
|
+
whyItMatters:
|
|
153
|
+
'ARIA roles require specific attributes to function correctly with assistive technologies.',
|
|
154
|
+
howToFix:
|
|
155
|
+
'Add all required ARIA attributes for the role being used.',
|
|
156
|
+
badExample: '<div role="checkbox">Accept terms</div>',
|
|
157
|
+
goodExample: '<div role="checkbox" aria-checked="false">Accept terms</div>',
|
|
158
|
+
wcagCriterion: WCAG_CRITERIA['4.1.2'],
|
|
159
|
+
},
|
|
160
|
+
'heading-order': {
|
|
161
|
+
whyItMatters:
|
|
162
|
+
'Screen reader users navigate by headings. Skipped heading levels make it harder to understand document structure.',
|
|
163
|
+
howToFix:
|
|
164
|
+
'Use headings in sequential order (h1 → h2 → h3). Don\'t skip levels for styling purposes.',
|
|
165
|
+
badExample: '<h1>Page Title</h1>\n<h4>Section</h4>',
|
|
166
|
+
goodExample: '<h1>Page Title</h1>\n<h2>Section</h2>',
|
|
167
|
+
wcagCriterion: WCAG_CRITERIA['1.3.1'],
|
|
168
|
+
},
|
|
169
|
+
'tabindex': {
|
|
170
|
+
whyItMatters:
|
|
171
|
+
'Positive tabindex values create unexpected keyboard navigation order, confusing keyboard-only users.',
|
|
172
|
+
howToFix:
|
|
173
|
+
'Use tabindex="0" to add elements to natural tab order, or tabindex="-1" to remove them. Avoid positive values.',
|
|
174
|
+
badExample: '<button tabindex="5">First</button>\n<button tabindex="1">Second</button>',
|
|
175
|
+
goodExample: '<button>First</button>\n<button>Second</button>',
|
|
176
|
+
wcagCriterion: WCAG_CRITERIA['2.1.1'],
|
|
177
|
+
},
|
|
178
|
+
'duplicate-id': {
|
|
179
|
+
whyItMatters:
|
|
180
|
+
'Duplicate IDs can cause aria-labelledby, aria-describedby, and label associations to reference the wrong element.',
|
|
181
|
+
howToFix:
|
|
182
|
+
'Ensure each id attribute value is unique within the page.',
|
|
183
|
+
badExample: '<input id="name">\n<input id="name">',
|
|
184
|
+
goodExample: '<input id="first-name">\n<input id="last-name">',
|
|
185
|
+
wcagCriterion: WCAG_CRITERIA['4.1.2'],
|
|
186
|
+
},
|
|
187
|
+
'target-size-minimum': {
|
|
188
|
+
whyItMatters:
|
|
189
|
+
'Small touch targets are difficult to activate for users with motor impairments or when using touch input.',
|
|
190
|
+
howToFix:
|
|
191
|
+
'Increase the interactive target to at least 24x24 CSS pixels, or add sufficient padding around the control.',
|
|
192
|
+
badExample: '<button style="width: 16px; height: 16px">×</button>',
|
|
193
|
+
goodExample: '<button style="min-width: 24px; min-height: 24px; padding: 4px">×</button>',
|
|
194
|
+
wcagCriterion: WCAG_CRITERIA['2.5.8'],
|
|
195
|
+
},
|
|
196
|
+
};
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* Get static fix suggestion for a rule
|
|
200
|
+
*/
|
|
201
|
+
export function getStaticFix(ruleId: string): StaticFixSuggestion | null {
|
|
202
|
+
return STATIC_FIXES[ruleId] || null;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* Parse contrast ratio data from axe-core node data
|
|
207
|
+
*/
|
|
208
|
+
function parseContrastData(node: SerializedNode): {
|
|
209
|
+
fgColor?: string;
|
|
210
|
+
bgColor?: string;
|
|
211
|
+
contrastRatio?: number;
|
|
212
|
+
requiredRatio?: number;
|
|
213
|
+
fontSize?: string;
|
|
214
|
+
fontWeight?: string;
|
|
215
|
+
} | null {
|
|
216
|
+
const anyData = node.any?.[0]?.data as Record<string, unknown> | undefined;
|
|
217
|
+
if (!anyData) return null;
|
|
218
|
+
|
|
219
|
+
return {
|
|
220
|
+
fgColor: anyData.fgColor as string | undefined,
|
|
221
|
+
bgColor: anyData.bgColor as string | undefined,
|
|
222
|
+
contrastRatio: anyData.contrastRatio as number | undefined,
|
|
223
|
+
requiredRatio: anyData.expectedContrastRatio as number | undefined,
|
|
224
|
+
fontSize: anyData.fontSize as string | undefined,
|
|
225
|
+
fontWeight: anyData.fontWeight as string | undefined,
|
|
226
|
+
};
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* Generate a specific fix for a color-contrast violation
|
|
231
|
+
*/
|
|
232
|
+
function generateContrastFix(node: SerializedNode): ElementFix | null {
|
|
233
|
+
const data = parseContrastData(node);
|
|
234
|
+
if (!data?.fgColor || !data?.bgColor) return null;
|
|
235
|
+
|
|
236
|
+
const { fgColor, bgColor, contrastRatio: ratio, requiredRatio } = data;
|
|
237
|
+
|
|
238
|
+
// Safely convert to number and format
|
|
239
|
+
const currentRatioNum = typeof ratio === 'number' ? ratio : parseFloat(String(ratio || ''));
|
|
240
|
+
const requiredRatioNum = typeof requiredRatio === 'number' ? requiredRatio : parseFloat(String(requiredRatio || ''));
|
|
241
|
+
|
|
242
|
+
const currentRatio = !isNaN(currentRatioNum) ? currentRatioNum.toFixed(2) : 'unknown';
|
|
243
|
+
const required = !isNaN(requiredRatioNum) ? requiredRatioNum : 4.5;
|
|
244
|
+
|
|
245
|
+
// Use real color math to suggest a corrected foreground color
|
|
246
|
+
try {
|
|
247
|
+
const fgRgb = parseColor(fgColor);
|
|
248
|
+
const bgRgb = parseColor(bgColor);
|
|
249
|
+
const correctedHex = suggestFix(fgRgb, bgRgb, required);
|
|
250
|
+
|
|
251
|
+
return {
|
|
252
|
+
originalHtml: node.html,
|
|
253
|
+
fixedHtml: node.html,
|
|
254
|
+
explanation: `Text color ${fgColor} on background ${bgColor} has insufficient contrast (${currentRatio}:1, need ${required}:1). Suggested foreground: ${correctedHex}`,
|
|
255
|
+
autoFixable: true,
|
|
256
|
+
};
|
|
257
|
+
} catch {
|
|
258
|
+
// Fall back to non-auto-fixable if color parsing fails
|
|
259
|
+
return {
|
|
260
|
+
originalHtml: node.html,
|
|
261
|
+
fixedHtml: node.html,
|
|
262
|
+
explanation: `Text color ${fgColor} on background ${bgColor} has insufficient contrast (${currentRatio}:1, need ${required}:1). Adjust the foreground color to meet the required ratio.`,
|
|
263
|
+
autoFixable: false,
|
|
264
|
+
};
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
/**
|
|
269
|
+
* Generate a specific fix for an image-alt violation
|
|
270
|
+
*/
|
|
271
|
+
function generateAltTextFix(node: SerializedNode): ElementFix | null {
|
|
272
|
+
const html = node.html;
|
|
273
|
+
|
|
274
|
+
// Extract src to help generate meaningful alt text suggestion
|
|
275
|
+
const srcMatch = html.match(/src=["']([^"']+)["']/);
|
|
276
|
+
const src = srcMatch?.[1] || '';
|
|
277
|
+
const filename = src.split('/').pop()?.replace(/\.[^.]+$/, '') || 'image';
|
|
278
|
+
|
|
279
|
+
// Generate suggested alt text based on filename
|
|
280
|
+
const suggestedAlt = filename
|
|
281
|
+
.replace(/[-_]/g, ' ')
|
|
282
|
+
.replace(/([a-z])([A-Z])/g, '$1 $2')
|
|
283
|
+
.toLowerCase();
|
|
284
|
+
|
|
285
|
+
// Create fixed HTML
|
|
286
|
+
let fixedHtml = html;
|
|
287
|
+
if (html.includes('alt=""') || html.includes("alt=''")) {
|
|
288
|
+
// Decorative image marked correctly, but flagged - might need real alt
|
|
289
|
+
fixedHtml = html.replace(/alt=["'][^"']*["']/, `alt="${suggestedAlt}"`);
|
|
290
|
+
} else if (html.includes('alt=')) {
|
|
291
|
+
// Has alt but it's not descriptive enough
|
|
292
|
+
fixedHtml = html.replace(/alt=["'][^"']*["']/, `alt="${suggestedAlt}"`);
|
|
293
|
+
} else {
|
|
294
|
+
// Missing alt entirely
|
|
295
|
+
fixedHtml = html.replace(/<img/, `<img alt="${suggestedAlt}"`);
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
return {
|
|
299
|
+
originalHtml: html,
|
|
300
|
+
fixedHtml,
|
|
301
|
+
explanation: `Add descriptive alt text that describes the image content or purpose. If decorative, use alt="".`,
|
|
302
|
+
autoFixable: true,
|
|
303
|
+
};
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
/**
|
|
307
|
+
* Generate a specific fix for a button-name violation
|
|
308
|
+
*/
|
|
309
|
+
function generateButtonNameFix(node: SerializedNode): ElementFix | null {
|
|
310
|
+
const html = node.html;
|
|
311
|
+
|
|
312
|
+
// Check if it's an icon-only button
|
|
313
|
+
const hasOnlySvg = /<button[^>]*>[\s]*<svg[^>]*>[\s\S]*<\/svg>[\s]*<\/button>/i.test(html);
|
|
314
|
+
const hasOnlyImg = /<button[^>]*>[\s]*<img[^>]*\/?>\s*<\/button>/i.test(html);
|
|
315
|
+
|
|
316
|
+
let fixedHtml = html;
|
|
317
|
+
let explanation = '';
|
|
318
|
+
|
|
319
|
+
if (hasOnlySvg || hasOnlyImg) {
|
|
320
|
+
// Add aria-label for icon buttons
|
|
321
|
+
fixedHtml = html.replace(/<button/, '<button aria-label="[describe action]"');
|
|
322
|
+
explanation = 'Add aria-label to describe the button\'s action for screen reader users.';
|
|
323
|
+
} else {
|
|
324
|
+
// Add visible text content
|
|
325
|
+
fixedHtml = html.replace(/<\/button>/, '[Button text]</button>');
|
|
326
|
+
explanation = 'Add visible text content or aria-label that describes the button\'s purpose.';
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
return {
|
|
330
|
+
originalHtml: html,
|
|
331
|
+
fixedHtml,
|
|
332
|
+
explanation,
|
|
333
|
+
autoFixable: false, // Requires human decision on button purpose
|
|
334
|
+
};
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
/**
|
|
338
|
+
* Generate a specific fix for a link-name violation
|
|
339
|
+
*/
|
|
340
|
+
function generateLinkNameFix(node: SerializedNode): ElementFix | null {
|
|
341
|
+
const html = node.html;
|
|
342
|
+
|
|
343
|
+
// Check common patterns
|
|
344
|
+
const hasOnlySvg = /<a[^>]*>[\s]*<svg[^>]*>[\s\S]*<\/svg>[\s]*<\/a>/i.test(html);
|
|
345
|
+
const hasOnlyImg = /<a[^>]*>[\s]*<img[^>]*\/?>\s*<\/a>/i.test(html);
|
|
346
|
+
const isEmpty = /<a[^>]*>\s*<\/a>/i.test(html);
|
|
347
|
+
|
|
348
|
+
let fixedHtml = html;
|
|
349
|
+
let explanation = '';
|
|
350
|
+
|
|
351
|
+
if (hasOnlySvg || hasOnlyImg) {
|
|
352
|
+
fixedHtml = html.replace(/<a/, '<a aria-label="[describe destination]"');
|
|
353
|
+
explanation = 'Add aria-label to describe where the link goes.';
|
|
354
|
+
} else if (isEmpty) {
|
|
355
|
+
fixedHtml = html.replace(/<\/a>/, '[Link text]</a>');
|
|
356
|
+
explanation = 'Add descriptive text content that indicates the link\'s destination.';
|
|
357
|
+
} else {
|
|
358
|
+
explanation = 'Ensure link text is descriptive. Avoid generic text like "click here" or "read more".';
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
return {
|
|
362
|
+
originalHtml: html,
|
|
363
|
+
fixedHtml,
|
|
364
|
+
explanation,
|
|
365
|
+
autoFixable: false,
|
|
366
|
+
};
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
/**
|
|
370
|
+
* Generate a specific fix for a label violation
|
|
371
|
+
*/
|
|
372
|
+
function generateLabelFix(node: SerializedNode): ElementFix | null {
|
|
373
|
+
const html = node.html;
|
|
374
|
+
|
|
375
|
+
// Extract input type and any existing identifiers
|
|
376
|
+
const typeMatch = html.match(/type=["']([^"']+)["']/);
|
|
377
|
+
const type = typeMatch?.[1] || 'text';
|
|
378
|
+
const idMatch = html.match(/id=["']([^"']+)["']/);
|
|
379
|
+
const id = idMatch?.[1];
|
|
380
|
+
|
|
381
|
+
let fixedHtml = html;
|
|
382
|
+
let explanation = '';
|
|
383
|
+
|
|
384
|
+
if (id) {
|
|
385
|
+
// Has ID, suggest adding label element
|
|
386
|
+
const labelText = type.charAt(0).toUpperCase() + type.slice(1);
|
|
387
|
+
explanation = `Add a <label for="${id}">${labelText}</label> element before this input.`;
|
|
388
|
+
} else {
|
|
389
|
+
// No ID, add one and suggest label
|
|
390
|
+
const suggestedId = `input-${type}-${Date.now()}`;
|
|
391
|
+
fixedHtml = html.replace(/<input/, `<input id="${suggestedId}"`);
|
|
392
|
+
explanation = `Add an id attribute and associate with a label: <label for="${suggestedId}">Label text</label>`;
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
return {
|
|
396
|
+
originalHtml: html,
|
|
397
|
+
fixedHtml,
|
|
398
|
+
explanation,
|
|
399
|
+
autoFixable: false,
|
|
400
|
+
};
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
/**
|
|
404
|
+
* Fix generator registry
|
|
405
|
+
*/
|
|
406
|
+
type FixGenerator = (node: SerializedNode) => ElementFix | null;
|
|
407
|
+
|
|
408
|
+
const FIX_GENERATORS: Record<string, FixGenerator> = {
|
|
409
|
+
'color-contrast': generateContrastFix,
|
|
410
|
+
'image-alt': generateAltTextFix,
|
|
411
|
+
'button-name': generateButtonNameFix,
|
|
412
|
+
'link-name': generateLinkNameFix,
|
|
413
|
+
'label': generateLabelFix,
|
|
414
|
+
};
|
|
415
|
+
|
|
416
|
+
/**
|
|
417
|
+
* Generate a specific fix for a violation node
|
|
418
|
+
*/
|
|
419
|
+
export function generateElementFix(ruleId: string, node: SerializedNode): ElementFix | null {
|
|
420
|
+
const generator = FIX_GENERATORS[ruleId];
|
|
421
|
+
if (!generator) return null;
|
|
422
|
+
return generator(node);
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
/**
|
|
426
|
+
* Get impact description for UI display
|
|
427
|
+
*/
|
|
428
|
+
export function getImpactDescription(impact: ImpactValue | null): string {
|
|
429
|
+
switch (impact) {
|
|
430
|
+
case 'critical':
|
|
431
|
+
return 'Critical issues completely block access for some users';
|
|
432
|
+
case 'serious':
|
|
433
|
+
return 'Serious issues make content very difficult to access';
|
|
434
|
+
case 'moderate':
|
|
435
|
+
return 'Moderate issues cause some difficulty for users';
|
|
436
|
+
case 'minor':
|
|
437
|
+
return 'Minor issues may cause slight inconvenience';
|
|
438
|
+
default:
|
|
439
|
+
return 'Impact level not determined';
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
/**
|
|
444
|
+
* Get impact styling for inline styles and Badge variant mapping
|
|
445
|
+
*/
|
|
446
|
+
export function getImpactColorClass(impact: ImpactValue | null): {
|
|
447
|
+
variant: 'error' | 'warning' | 'info' | 'default';
|
|
448
|
+
color: string;
|
|
449
|
+
bg: string;
|
|
450
|
+
text: string;
|
|
451
|
+
border: string;
|
|
452
|
+
borderLeft: string;
|
|
453
|
+
} {
|
|
454
|
+
switch (impact) {
|
|
455
|
+
case 'critical':
|
|
456
|
+
return {
|
|
457
|
+
variant: 'error',
|
|
458
|
+
color: 'var(--color-danger)',
|
|
459
|
+
bg: 'var(--color-danger-bg)',
|
|
460
|
+
text: 'var(--color-danger)',
|
|
461
|
+
border: 'var(--color-danger)',
|
|
462
|
+
borderLeft: 'var(--color-danger)',
|
|
463
|
+
};
|
|
464
|
+
case 'serious':
|
|
465
|
+
return {
|
|
466
|
+
variant: 'error',
|
|
467
|
+
color: 'var(--color-danger)',
|
|
468
|
+
bg: 'var(--color-danger-bg)',
|
|
469
|
+
text: 'var(--color-danger)',
|
|
470
|
+
border: 'var(--color-danger)',
|
|
471
|
+
borderLeft: 'var(--color-danger)',
|
|
472
|
+
};
|
|
473
|
+
case 'moderate':
|
|
474
|
+
return {
|
|
475
|
+
variant: 'warning',
|
|
476
|
+
color: 'var(--color-warning)',
|
|
477
|
+
bg: 'var(--color-warning-bg)',
|
|
478
|
+
text: 'var(--color-warning)',
|
|
479
|
+
border: 'var(--color-warning)',
|
|
480
|
+
borderLeft: 'var(--color-warning)',
|
|
481
|
+
};
|
|
482
|
+
case 'minor':
|
|
483
|
+
default:
|
|
484
|
+
return {
|
|
485
|
+
variant: 'info',
|
|
486
|
+
color: 'var(--color-info)',
|
|
487
|
+
bg: 'var(--color-info-bg)',
|
|
488
|
+
text: 'var(--color-info)',
|
|
489
|
+
border: 'var(--color-info)',
|
|
490
|
+
borderLeft: 'var(--color-info)',
|
|
491
|
+
};
|
|
492
|
+
}
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
/**
|
|
496
|
+
* Extract WCAG tags from a violation's tags array
|
|
497
|
+
*/
|
|
498
|
+
export function extractWcagTags(tags: string[]): string[] {
|
|
499
|
+
return tags.filter(tag => tag.startsWith('wcag'));
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
/**
|
|
503
|
+
* Get readable WCAG criterion from tag (e.g., 'wcag143' -> '1.4.3')
|
|
504
|
+
*/
|
|
505
|
+
export function parseWcagTag(tag: string): string | null {
|
|
506
|
+
const match = tag.match(/wcag(\d)(\d)(\d)/);
|
|
507
|
+
if (!match) return null;
|
|
508
|
+
return `${match[1]}.${match[2]}.${match[3]}`;
|
|
509
|
+
}
|