@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.
Files changed (141) hide show
  1. package/LICENSE +84 -0
  2. package/index.html +28 -0
  3. package/package.json +71 -0
  4. package/src/__tests__/a11y-fixes.test.ts +358 -0
  5. package/src/__tests__/jsx-parser.test.ts +502 -0
  6. package/src/__tests__/render-utils.test.ts +232 -0
  7. package/src/__tests__/style-utils.test.ts +404 -0
  8. package/src/app/index.ts +1 -0
  9. package/src/assets/fragments-logo.ts +4 -0
  10. package/src/assets/fragments_logo.png +0 -0
  11. package/src/components/AccessibilityPanel.tsx +1457 -0
  12. package/src/components/ActionCapture.tsx +172 -0
  13. package/src/components/ActionsPanel.tsx +332 -0
  14. package/src/components/AllVariantsPreview.tsx +78 -0
  15. package/src/components/App.tsx +604 -0
  16. package/src/components/BottomPanel.tsx +288 -0
  17. package/src/components/CodePanel.naming.test.tsx +59 -0
  18. package/src/components/CodePanel.tsx +118 -0
  19. package/src/components/CommandPalette.tsx +392 -0
  20. package/src/components/ComponentDocView.tsx +164 -0
  21. package/src/components/ComponentGraph.tsx +380 -0
  22. package/src/components/ComponentHeader.tsx +88 -0
  23. package/src/components/ContractPanel.tsx +241 -0
  24. package/src/components/DeviceMockup.tsx +156 -0
  25. package/src/components/EmptyVariantMessage.tsx +54 -0
  26. package/src/components/ErrorBoundary.tsx +97 -0
  27. package/src/components/FigmaEmbed.tsx +238 -0
  28. package/src/components/FragmentEditor.tsx +525 -0
  29. package/src/components/FragmentRenderer.tsx +61 -0
  30. package/src/components/HeaderSearch.tsx +24 -0
  31. package/src/components/HealthDashboard.tsx +441 -0
  32. package/src/components/HmrStatusIndicator.tsx +61 -0
  33. package/src/components/Icons.tsx +479 -0
  34. package/src/components/InteractionsPanel.tsx +757 -0
  35. package/src/components/IsolatedPreviewFrame.tsx +390 -0
  36. package/src/components/IsolatedRender.tsx +113 -0
  37. package/src/components/KeyboardShortcutsHelp.tsx +53 -0
  38. package/src/components/LandingPage.tsx +420 -0
  39. package/src/components/Layout.tsx +27 -0
  40. package/src/components/LeftSidebar.tsx +472 -0
  41. package/src/components/LoadErrorMessage.tsx +102 -0
  42. package/src/components/MultiViewportPreview.tsx +527 -0
  43. package/src/components/NoVariantsMessage.tsx +59 -0
  44. package/src/components/PanelShell.tsx +161 -0
  45. package/src/components/PerformancePanel.tsx +304 -0
  46. package/src/components/PreviewArea.tsx +254 -0
  47. package/src/components/PreviewAside.tsx +168 -0
  48. package/src/components/PreviewFrameHost.tsx +304 -0
  49. package/src/components/PreviewToolbar.tsx +80 -0
  50. package/src/components/PropsEditor.tsx +506 -0
  51. package/src/components/PropsTable.tsx +111 -0
  52. package/src/components/RelationsSection.tsx +88 -0
  53. package/src/components/ResizablePanel.tsx +271 -0
  54. package/src/components/RightSidebar.tsx +102 -0
  55. package/src/components/RuntimeToolsRegistrar.tsx +17 -0
  56. package/src/components/ScreenshotButton.tsx +90 -0
  57. package/src/components/ShadowPreview.tsx +204 -0
  58. package/src/components/Sidebar.tsx +169 -0
  59. package/src/components/SkeletonLoader.tsx +161 -0
  60. package/src/components/ThemeProvider.tsx +42 -0
  61. package/src/components/Toast.tsx +3 -0
  62. package/src/components/TokenStylePanel.tsx +699 -0
  63. package/src/components/TopToolbar.tsx +159 -0
  64. package/src/components/Untitled +1 -0
  65. package/src/components/UsageSection.tsx +95 -0
  66. package/src/components/VariantMatrix.tsx +391 -0
  67. package/src/components/VariantRenderer.tsx +131 -0
  68. package/src/components/VariantTabs.tsx +40 -0
  69. package/src/components/ViewerHeader.tsx +69 -0
  70. package/src/components/ViewerStateSync.tsx +52 -0
  71. package/src/components/ViewportSelector.tsx +172 -0
  72. package/src/components/WebMCPDevTools.tsx +503 -0
  73. package/src/components/WebMCPIntegration.tsx +47 -0
  74. package/src/components/WebMCPStatusIndicator.tsx +60 -0
  75. package/src/components/_future/CreatePage.tsx +835 -0
  76. package/src/components/viewer-utils.ts +16 -0
  77. package/src/composition-renderer.ts +381 -0
  78. package/src/constants/index.ts +1 -0
  79. package/src/constants/ui.ts +166 -0
  80. package/src/entry.tsx +335 -0
  81. package/src/hooks/index.ts +2 -0
  82. package/src/hooks/useA11yCache.ts +383 -0
  83. package/src/hooks/useA11yService.ts +364 -0
  84. package/src/hooks/useActions.ts +138 -0
  85. package/src/hooks/useAppState.ts +147 -0
  86. package/src/hooks/useCompiledFragments.ts +42 -0
  87. package/src/hooks/useFigmaIntegration.ts +132 -0
  88. package/src/hooks/useHmrStatus.ts +109 -0
  89. package/src/hooks/useKeyboardShortcuts.ts +270 -0
  90. package/src/hooks/usePreviewBridge.ts +347 -0
  91. package/src/hooks/useScrollSpy.ts +78 -0
  92. package/src/hooks/useShadowStyles.ts +221 -0
  93. package/src/hooks/useUrlState.ts +318 -0
  94. package/src/hooks/useViewSettings.ts +111 -0
  95. package/src/intelligence/healthReport.ts +505 -0
  96. package/src/intelligence/styleDrift.ts +340 -0
  97. package/src/intelligence/usageScanner.ts +309 -0
  98. package/src/jsx-parser.ts +486 -0
  99. package/src/preview-frame-entry.tsx +25 -0
  100. package/src/preview-frame.html +148 -0
  101. package/src/render-template.html +68 -0
  102. package/src/render-utils.ts +311 -0
  103. package/src/shared/ComponentDocContent.module.scss +10 -0
  104. package/src/shared/ComponentDocContent.module.scss.d.ts +2 -0
  105. package/src/shared/ComponentDocContent.tsx +274 -0
  106. package/src/shared/DocsHeaderBar.tsx +129 -0
  107. package/src/shared/DocsPageAsideHost.tsx +89 -0
  108. package/src/shared/DocsPageShell.tsx +124 -0
  109. package/src/shared/DocsSearchCommand.tsx +99 -0
  110. package/src/shared/DocsSidebarNav.tsx +66 -0
  111. package/src/shared/PropsTable.module.scss +68 -0
  112. package/src/shared/PropsTable.module.scss.d.ts +2 -0
  113. package/src/shared/PropsTable.tsx +76 -0
  114. package/src/shared/VariantPreviewCard.module.scss +114 -0
  115. package/src/shared/VariantPreviewCard.module.scss.d.ts +2 -0
  116. package/src/shared/VariantPreviewCard.tsx +137 -0
  117. package/src/shared/docs-data/index.ts +32 -0
  118. package/src/shared/docs-data/mcp-configs.ts +72 -0
  119. package/src/shared/docs-data/palettes.ts +75 -0
  120. package/src/shared/docs-data/setup-examples.ts +55 -0
  121. package/src/shared/docs-layout.scss +28 -0
  122. package/src/shared/docs-layout.scss.d.ts +2 -0
  123. package/src/shared/index.ts +34 -0
  124. package/src/shared/types.ts +53 -0
  125. package/src/style-utils.ts +414 -0
  126. package/src/styles/globals.css +278 -0
  127. package/src/types/a11y.ts +197 -0
  128. package/src/utils/a11y-fixes.ts +509 -0
  129. package/src/utils/actionExport.ts +372 -0
  130. package/src/utils/colorSchemes.ts +201 -0
  131. package/src/utils/contrast.ts +246 -0
  132. package/src/utils/detectRelationships.ts +256 -0
  133. package/src/webmcp/__tests__/analytics.test.ts +108 -0
  134. package/src/webmcp/analytics.ts +165 -0
  135. package/src/webmcp/index.ts +3 -0
  136. package/src/webmcp/posthog-bridge.ts +39 -0
  137. package/src/webmcp/runtime-tools.ts +152 -0
  138. package/src/webmcp/scan-utils.ts +135 -0
  139. package/src/webmcp/use-tool-analytics.ts +69 -0
  140. package/src/webmcp/viewer-state.ts +45 -0
  141. package/tsconfig.json +20 -0
@@ -0,0 +1,383 @@
1
+ /**
2
+ * Shared A11y Cache
3
+ *
4
+ * Provides a global cache for accessibility scan results that:
5
+ * 1. Persists in sessionStorage across page navigations
6
+ * 2. Is shared between Dashboard and AccessibilityPanel
7
+ * 3. Stores full violation details for rich UI display
8
+ * 4. Supports component-level invalidation
9
+ */
10
+
11
+ import { BRAND } from '@fragments-sdk/core';
12
+ import type {
13
+ CachedA11yResult,
14
+ SerializedViolation,
15
+ A11ySummary,
16
+ } from '../types/a11y.js';
17
+
18
+ const CACHE_KEY = `${BRAND.storagePrefix}a11y-cache`;
19
+ const CACHE_VERSION = 2; // Bumped for new schema with full violations
20
+
21
+ /**
22
+ * Legacy interface for backward compatibility
23
+ * @deprecated Use CachedA11yResult instead
24
+ */
25
+ export interface ComponentA11yData {
26
+ violations: number;
27
+ passes: number;
28
+ incomplete: number;
29
+ critical: number;
30
+ serious: number;
31
+ moderate: number;
32
+ minor: number;
33
+ scannedAt: number;
34
+ variant?: string;
35
+ }
36
+
37
+ interface A11yCacheData {
38
+ version: number;
39
+ components: Record<string, CachedA11yResult>;
40
+ lastFullScan?: number;
41
+ }
42
+
43
+ /**
44
+ * Load cache from sessionStorage
45
+ */
46
+ function loadCache(): A11yCacheData {
47
+ try {
48
+ const stored = sessionStorage.getItem(CACHE_KEY);
49
+ if (stored) {
50
+ const data = JSON.parse(stored) as A11yCacheData;
51
+ if (data.version === CACHE_VERSION) {
52
+ return data;
53
+ }
54
+ // Version mismatch - clear old cache
55
+ sessionStorage.removeItem(CACHE_KEY);
56
+ }
57
+ } catch (e) {
58
+ // Ignore parse errors
59
+ }
60
+ return { version: CACHE_VERSION, components: {} };
61
+ }
62
+
63
+ /**
64
+ * Save cache to sessionStorage
65
+ */
66
+ function saveCache(data: A11yCacheData): void {
67
+ try {
68
+ sessionStorage.setItem(CACHE_KEY, JSON.stringify(data));
69
+ } catch (e) {
70
+ // Storage might be full - try clearing old data
71
+ try {
72
+ sessionStorage.removeItem(CACHE_KEY);
73
+ sessionStorage.setItem(CACHE_KEY, JSON.stringify(data));
74
+ } catch {
75
+ // Ignore storage errors
76
+ }
77
+ }
78
+ }
79
+
80
+ /**
81
+ * Get cached a11y result for a component (full data)
82
+ */
83
+ export function getComponentA11yResult(componentName: string): CachedA11yResult | null {
84
+ const cache = loadCache();
85
+ return cache.components[componentName] || null;
86
+ }
87
+
88
+ /**
89
+ * Get cached a11y data for a component (legacy format for backward compatibility)
90
+ */
91
+ export function getComponentA11y(componentName: string): ComponentA11yData | null {
92
+ const result = getComponentA11yResult(componentName);
93
+ if (!result) return null;
94
+
95
+ return {
96
+ violations: result.violations.length,
97
+ passes: result.passes,
98
+ incomplete: result.incomplete,
99
+ critical: result.counts.critical,
100
+ serious: result.counts.serious,
101
+ moderate: result.counts.moderate,
102
+ minor: result.counts.minor,
103
+ scannedAt: result.scannedAt,
104
+ variant: result.variant,
105
+ };
106
+ }
107
+
108
+ /**
109
+ * Get all cached a11y data (full results)
110
+ */
111
+ export function getAllA11yResults(): Record<string, CachedA11yResult> {
112
+ const cache = loadCache();
113
+ return cache.components;
114
+ }
115
+
116
+ /**
117
+ * Get all cached a11y data (legacy format)
118
+ */
119
+ export function getAllA11yData(): Record<string, ComponentA11yData> {
120
+ const cache = loadCache();
121
+ const result: Record<string, ComponentA11yData> = {};
122
+
123
+ for (const [name, data] of Object.entries(cache.components)) {
124
+ result[name] = {
125
+ violations: data.violations.length,
126
+ passes: data.passes,
127
+ incomplete: data.incomplete,
128
+ critical: data.counts.critical,
129
+ serious: data.counts.serious,
130
+ moderate: data.counts.moderate,
131
+ minor: data.counts.minor,
132
+ scannedAt: data.scannedAt,
133
+ variant: data.variant,
134
+ };
135
+ }
136
+
137
+ return result;
138
+ }
139
+
140
+ /**
141
+ * Get timestamp of last full scan
142
+ */
143
+ export function getLastFullScanTime(): number | undefined {
144
+ const cache = loadCache();
145
+ return cache.lastFullScan;
146
+ }
147
+
148
+ /**
149
+ * Update cache with full a11y result for a component
150
+ */
151
+ export function updateComponentA11yResult(
152
+ componentName: string,
153
+ result: Omit<CachedA11yResult, 'componentName' | 'scannedAt'> & { scannedAt?: number }
154
+ ): void {
155
+ const cache = loadCache();
156
+ cache.components[componentName] = {
157
+ componentName,
158
+ ...result,
159
+ scannedAt: result.scannedAt || Date.now(),
160
+ };
161
+ saveCache(cache);
162
+
163
+ // Dispatch event for listeners
164
+ window.dispatchEvent(new CustomEvent('a11y-cache-updated', {
165
+ detail: {
166
+ componentName,
167
+ data: cache.components[componentName],
168
+ // Include legacy format for backward compatibility
169
+ legacyData: {
170
+ violations: result.violations.length,
171
+ passes: result.passes,
172
+ incomplete: result.incomplete,
173
+ critical: result.counts.critical,
174
+ serious: result.counts.serious,
175
+ moderate: result.counts.moderate,
176
+ minor: result.counts.minor,
177
+ scannedAt: result.scannedAt || Date.now(),
178
+ variant: result.variant,
179
+ },
180
+ },
181
+ }));
182
+ }
183
+
184
+ /**
185
+ * Update cache for a single component (legacy format, converted to new format)
186
+ */
187
+ export function updateComponentA11y(
188
+ componentName: string,
189
+ data: Omit<ComponentA11yData, 'scannedAt'> & { scannedAt?: number },
190
+ violations?: SerializedViolation[]
191
+ ): void {
192
+ const cache = loadCache();
193
+
194
+ // Convert to new format
195
+ cache.components[componentName] = {
196
+ componentName,
197
+ violations: violations || [], // Use provided violations or empty array
198
+ passes: data.passes,
199
+ incomplete: data.incomplete,
200
+ scannedAt: data.scannedAt || Date.now(),
201
+ variant: data.variant,
202
+ counts: {
203
+ critical: data.critical,
204
+ serious: data.serious,
205
+ moderate: data.moderate,
206
+ minor: data.minor,
207
+ },
208
+ };
209
+ saveCache(cache);
210
+
211
+ // Dispatch event for listeners
212
+ window.dispatchEvent(new CustomEvent('a11y-cache-updated', {
213
+ detail: { componentName, data: cache.components[componentName] },
214
+ }));
215
+ }
216
+
217
+ /**
218
+ * Invalidate cache for specific components
219
+ * This marks them for re-scan without removing the data
220
+ */
221
+ export function invalidateComponents(componentNames: string[]): void {
222
+ const cache = loadCache();
223
+ let changed = false;
224
+
225
+ for (const name of componentNames) {
226
+ if (cache.components[name]) {
227
+ // Set scannedAt to 0 to mark as stale
228
+ cache.components[name].scannedAt = 0;
229
+ changed = true;
230
+ }
231
+ }
232
+
233
+ if (changed) {
234
+ saveCache(cache);
235
+
236
+ // Dispatch event for listeners
237
+ window.dispatchEvent(new CustomEvent('a11y-cache-invalidated', {
238
+ detail: { components: componentNames },
239
+ }));
240
+ }
241
+ }
242
+
243
+ /**
244
+ * Check if a component's cache is stale (needs re-scan)
245
+ */
246
+ export function isComponentStale(componentName: string, maxAgeMs: number = 30000): boolean {
247
+ const cache = loadCache();
248
+ const data = cache.components[componentName];
249
+
250
+ if (!data) return true;
251
+ if (data.scannedAt === 0) return true; // Explicitly invalidated
252
+
253
+ return Date.now() - data.scannedAt > maxAgeMs;
254
+ }
255
+
256
+ /**
257
+ * Mark that a full scan was completed
258
+ */
259
+ export function markFullScanComplete(): void {
260
+ const cache = loadCache();
261
+ cache.lastFullScan = Date.now();
262
+ saveCache(cache);
263
+ }
264
+
265
+ /**
266
+ * Clear all cached data
267
+ */
268
+ export function clearA11yCache(): void {
269
+ try {
270
+ sessionStorage.removeItem(CACHE_KEY);
271
+ } catch (e) {
272
+ // Ignore
273
+ }
274
+ window.dispatchEvent(new CustomEvent('a11y-cache-cleared'));
275
+ }
276
+
277
+ /**
278
+ * Clear cache for a specific component
279
+ */
280
+ export function clearComponentCache(componentName: string): void {
281
+ const cache = loadCache();
282
+ if (cache.components[componentName]) {
283
+ delete cache.components[componentName];
284
+ saveCache(cache);
285
+
286
+ window.dispatchEvent(new CustomEvent('a11y-cache-updated', {
287
+ detail: { componentName, data: null },
288
+ }));
289
+ }
290
+ }
291
+
292
+ /**
293
+ * Calculate summary from cached data
294
+ */
295
+ export function getA11ySummary(): A11ySummary {
296
+ const cache = loadCache();
297
+ const components = Object.values(cache.components);
298
+
299
+ const summary: A11ySummary = {
300
+ accessibleComponents: 0,
301
+ totalComponents: components.length,
302
+ violationsByImpact: {
303
+ critical: 0,
304
+ serious: 0,
305
+ moderate: 0,
306
+ minor: 0,
307
+ },
308
+ topViolations: [],
309
+ };
310
+
311
+ // Track violations by rule ID for top violations
312
+ const violationsByRule = new Map<string, {
313
+ ruleId: string;
314
+ description: string;
315
+ impact: 'critical' | 'serious' | 'moderate' | 'minor' | null;
316
+ affectedComponents: Set<string>;
317
+ }>();
318
+
319
+ for (const comp of components) {
320
+ // Count by impact
321
+ summary.violationsByImpact.critical += comp.counts.critical;
322
+ summary.violationsByImpact.serious += comp.counts.serious;
323
+ summary.violationsByImpact.moderate += comp.counts.moderate;
324
+ summary.violationsByImpact.minor += comp.counts.minor;
325
+
326
+ // Check if accessible (no critical/serious)
327
+ if (comp.counts.critical === 0 && comp.counts.serious === 0) {
328
+ summary.accessibleComponents++;
329
+ }
330
+
331
+ // Aggregate violations by rule
332
+ for (const violation of comp.violations) {
333
+ const existing = violationsByRule.get(violation.id);
334
+ if (existing) {
335
+ existing.affectedComponents.add(comp.componentName);
336
+ } else {
337
+ violationsByRule.set(violation.id, {
338
+ ruleId: violation.id,
339
+ description: violation.description,
340
+ impact: violation.impact,
341
+ affectedComponents: new Set([comp.componentName]),
342
+ });
343
+ }
344
+ }
345
+ }
346
+
347
+ // Sort by number of affected components and take top 5
348
+ summary.topViolations = Array.from(violationsByRule.values())
349
+ .sort((a, b) => b.affectedComponents.size - a.affectedComponents.size)
350
+ .slice(0, 5)
351
+ .map(v => ({
352
+ ruleId: v.ruleId,
353
+ description: v.description,
354
+ impact: v.impact,
355
+ affectedComponents: Array.from(v.affectedComponents),
356
+ }));
357
+
358
+ return summary;
359
+ }
360
+
361
+ /**
362
+ * Get legacy summary format for backward compatibility
363
+ */
364
+ export function getLegacyA11ySummary(): {
365
+ totalComponents: number;
366
+ accessibleComponents: number;
367
+ totalViolations: number;
368
+ totalCritical: number;
369
+ totalSerious: number;
370
+ } {
371
+ const summary = getA11ySummary();
372
+ return {
373
+ totalComponents: summary.totalComponents,
374
+ accessibleComponents: summary.accessibleComponents,
375
+ totalViolations:
376
+ summary.violationsByImpact.critical +
377
+ summary.violationsByImpact.serious +
378
+ summary.violationsByImpact.moderate +
379
+ summary.violationsByImpact.minor,
380
+ totalCritical: summary.violationsByImpact.critical,
381
+ totalSerious: summary.violationsByImpact.serious,
382
+ };
383
+ }