@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,254 @@
1
+ /**
2
+ * PreviewArea - routes to the appropriate preview mode.
3
+ *
4
+ * Pure routing component that selects between matrix, multi-viewport,
5
+ * device mockup, Figma comparison, iframe-isolated, or direct-render views.
6
+ */
7
+
8
+ import type { ReactNode } from "react";
9
+ import type { FragmentVariant } from "@fragments-sdk/core";
10
+ import { ErrorBoundary } from "./ErrorBoundary.js";
11
+ import { FigmaEmbed } from "./FigmaEmbed.js";
12
+ import { VariantMatrix } from "./VariantMatrix.js";
13
+ import { MultiViewportPreview } from "./MultiViewportPreview.js";
14
+ import { IsolatedPreviewFrame } from "./IsolatedPreviewFrame.js";
15
+ import { ShadowPreview } from "./ShadowPreview.js";
16
+ import { DeviceMockup } from "./DeviceMockup.js";
17
+ import {
18
+ getViewportWidth,
19
+ type ZoomLevel,
20
+ type ViewportPreset,
21
+ type ViewportSize,
22
+ } from "../constants/ui.js";
23
+ import type { PreviewTheme } from "../hooks/useViewSettings.js";
24
+
25
+ interface PreviewAreaProps {
26
+ componentName: string;
27
+ fragmentPath: string;
28
+ variant: FragmentVariant | undefined;
29
+ variants: FragmentVariant[] | undefined;
30
+ zoom: ZoomLevel;
31
+ viewport: ViewportPreset;
32
+ customSize: ViewportSize;
33
+ previewTheme: PreviewTheme;
34
+ showMatrixView: boolean;
35
+ showMultiViewport: boolean;
36
+ showComparison: boolean;
37
+ figmaUrl?: string;
38
+ allFigmaUrls: string[];
39
+ onSelectVariant: (index: number) => void;
40
+ onRetry: () => void;
41
+ renderContent: () => ReactNode;
42
+ previewKey: string;
43
+ useIframeIsolation?: boolean;
44
+ }
45
+
46
+ export function PreviewArea({
47
+ componentName,
48
+ fragmentPath,
49
+ variant,
50
+ variants,
51
+ zoom,
52
+ viewport,
53
+ customSize,
54
+ previewTheme,
55
+ showMatrixView,
56
+ showMultiViewport,
57
+ showComparison,
58
+ figmaUrl,
59
+ allFigmaUrls,
60
+ onSelectVariant,
61
+ onRetry,
62
+ renderContent,
63
+ previewKey,
64
+ useIframeIsolation = false,
65
+ }: PreviewAreaProps) {
66
+ // Matrix view — all variants in a grid
67
+ if (showMatrixView && variants) {
68
+ return (
69
+ <VariantMatrix
70
+ variants={variants}
71
+ componentName={componentName}
72
+ fragmentPath={fragmentPath}
73
+ zoom={zoom}
74
+ previewTheme={previewTheme}
75
+ useIframeIsolation={useIframeIsolation}
76
+ onSelectVariant={onSelectVariant}
77
+ />
78
+ );
79
+ }
80
+
81
+ // Multi-viewport — mobile/tablet/desktop side by side
82
+ if (showMultiViewport && variant) {
83
+ return (
84
+ <MultiViewportPreview
85
+ componentName={componentName}
86
+ fragmentPath={fragmentPath}
87
+ variantName={variant.name}
88
+ renderContent={renderContent}
89
+ previewTheme={previewTheme}
90
+ zoom={zoom}
91
+ useIframeIsolation={useIframeIsolation}
92
+ />
93
+ );
94
+ }
95
+
96
+ const viewportWidth = getViewportWidth(viewport, customSize);
97
+ const isDevice = viewport === "tablet" || viewport === "mobile";
98
+
99
+ // Render either iframe-isolated or direct fallback content
100
+ const renderPreview = (width: number | string, height: number | string) => {
101
+ if (useIframeIsolation && variant) {
102
+ return (
103
+ <IsolatedPreviewFrame
104
+ fragmentPath={fragmentPath}
105
+ variantName={variant.name}
106
+ theme={previewTheme}
107
+ width={width}
108
+ height={height}
109
+ previewKey={previewKey}
110
+ />
111
+ );
112
+ }
113
+ return (
114
+ <ShadowPreview theme={previewTheme} width={width} height={height}>
115
+ <ErrorBoundary
116
+ key={previewKey}
117
+ componentName={componentName}
118
+ onRetry={onRetry}
119
+ >
120
+ {renderContent()}
121
+ </ErrorBoundary>
122
+ </ShadowPreview>
123
+ );
124
+ };
125
+
126
+ // Device mockup (tablet/mobile with physical chrome)
127
+ if (isDevice && viewportWidth && variant) {
128
+ const deviceScreenHeight = viewport === "mobile" ? 640 : 976;
129
+ const deviceContent = (
130
+ <DeviceMockup
131
+ type={viewport as "tablet" | "mobile"}
132
+ width={viewportWidth}
133
+ >
134
+ {renderPreview(viewportWidth, deviceScreenHeight)}
135
+ </DeviceMockup>
136
+ );
137
+
138
+ if (showComparison && figmaUrl) {
139
+ return (
140
+ <ComparisonLayout
141
+ figmaUrl={figmaUrl}
142
+ allFigmaUrls={allFigmaUrls}
143
+ zoom={zoom}
144
+ >
145
+ <div
146
+ style={{
147
+ flex: 1,
148
+ display: "flex",
149
+ alignItems: "center",
150
+ justifyContent: "center",
151
+ }}
152
+ >
153
+ {deviceContent}
154
+ </div>
155
+ </ComparisonLayout>
156
+ );
157
+ }
158
+
159
+ return (
160
+ <div
161
+ style={{
162
+ minHeight: "100%",
163
+ display: "flex",
164
+ alignItems: "center",
165
+ justifyContent: "center",
166
+ padding: 32,
167
+ }}
168
+ >
169
+ {deviceContent}
170
+ </div>
171
+ );
172
+ }
173
+
174
+ // Side-by-side Figma comparison (non-device)
175
+ if (showComparison && figmaUrl && variant) {
176
+ return (
177
+ <ComparisonLayout
178
+ figmaUrl={figmaUrl}
179
+ allFigmaUrls={allFigmaUrls}
180
+ zoom={zoom}
181
+ >
182
+ <div
183
+ style={{
184
+ flex: 1,
185
+ borderRadius: 8,
186
+ border: "1px solid var(--border)",
187
+ overflow: "auto",
188
+ }}
189
+ >
190
+ {renderPreview("100%", "100%")}
191
+ </div>
192
+ </ComparisonLayout>
193
+ );
194
+ }
195
+
196
+ // Default: full-size isolated preview or direct render fallback
197
+ return renderPreview("100%", "100%");
198
+ }
199
+
200
+ // --- Local helpers ---
201
+
202
+ const comparisonLabelStyle: React.CSSProperties = {
203
+ fontSize: 12,
204
+ fontWeight: 500,
205
+ color: "var(--text-tertiary)",
206
+ marginBottom: 8,
207
+ textAlign: "center",
208
+ };
209
+
210
+ interface ComparisonLayoutProps {
211
+ children: ReactNode;
212
+ figmaUrl: string;
213
+ allFigmaUrls: string[];
214
+ zoom: ZoomLevel;
215
+ }
216
+
217
+ function ComparisonLayout({
218
+ children,
219
+ figmaUrl,
220
+ allFigmaUrls,
221
+ zoom,
222
+ }: ComparisonLayoutProps) {
223
+ return (
224
+ <div
225
+ style={{
226
+ minHeight: "100%",
227
+ display: "flex",
228
+ flexDirection: "column",
229
+ padding: 24,
230
+ }}
231
+ >
232
+ <div style={{ display: "flex", gap: 16, flex: 1 }}>
233
+ <div style={{ flex: 1, display: "flex", flexDirection: "column" }}>
234
+ <div style={comparisonLabelStyle}>Rendered</div>
235
+ {children}
236
+ </div>
237
+ <div style={{ flex: 1, display: "flex", flexDirection: "column" }}>
238
+ <div style={comparisonLabelStyle}>Figma Design</div>
239
+ <FigmaEmbed
240
+ figmaUrl={figmaUrl}
241
+ allFigmaUrls={allFigmaUrls}
242
+ zoom={zoom}
243
+ style={{
244
+ flex: 1,
245
+ borderRadius: 8,
246
+ border: "1px solid var(--border)",
247
+ overflow: "hidden",
248
+ }}
249
+ />
250
+ </div>
251
+ </div>
252
+ </div>
253
+ );
254
+ }
@@ -0,0 +1,168 @@
1
+ import { useState, useEffect, useMemo } from "react";
2
+ import type { FragmentDefinition, FragmentVariant } from '@fragments-sdk/core';
3
+ import { Box, Stack, Text, Separator, Button, TableOfContents } from "@fragments-sdk/ui";
4
+ import { getVariantSectionId } from "./viewer-utils.js";
5
+
6
+ interface TocHeading {
7
+ id: string;
8
+ text: string;
9
+ level: 2 | 3;
10
+ }
11
+
12
+ interface PreviewAsideProps {
13
+ fragment: FragmentDefinition;
14
+ variants: FragmentVariant[];
15
+ focusedVariantIndex: number;
16
+ activePanel: string;
17
+ onSelectVariant: (index: number) => void;
18
+ onCopyLink: () => void;
19
+ onShowShortcuts: () => void;
20
+ }
21
+
22
+ function buildHeadings(fragment: FragmentDefinition): TocHeading[] {
23
+ const headings: TocHeading[] = [{ id: "setup", text: "Setup", level: 2 }];
24
+
25
+ if (fragment.variants.length > 0) {
26
+ headings.push({ id: "examples", text: "Examples", level: 2 });
27
+ }
28
+
29
+ headings.push({ id: "props", text: "Props", level: 2 });
30
+ headings.push({ id: "usage-guidelines", text: "Usage Guidelines", level: 2 });
31
+ headings.push({ id: "when-to-use", text: "When to use", level: 3 });
32
+ headings.push({ id: "when-not-to-use", text: "When not to use", level: 3 });
33
+
34
+ if ((fragment.usage?.guidelines?.length ?? 0) > 0) {
35
+ headings.push({ id: "best-practices", text: "Best practices", level: 3 });
36
+ }
37
+
38
+ if ((fragment.relations?.length ?? 0) > 0) {
39
+ headings.push({ id: "related-components", text: "Related Components", level: 2 });
40
+ }
41
+
42
+ return headings;
43
+ }
44
+
45
+ function useActiveHeading(ids: string[]): string | null {
46
+ const [activeId, setActiveId] = useState<string | null>(null);
47
+
48
+ useEffect(() => {
49
+ if (ids.length === 0) return;
50
+
51
+ const observer = new IntersectionObserver(
52
+ (entries) => {
53
+ for (const entry of entries) {
54
+ if (entry.isIntersecting) {
55
+ setActiveId(entry.target.id);
56
+ }
57
+ }
58
+ },
59
+ { rootMargin: "-80px 0px -60% 0px", threshold: 0 }
60
+ );
61
+
62
+ for (const id of ids) {
63
+ const el = document.getElementById(id);
64
+ if (el) observer.observe(el);
65
+ }
66
+
67
+ return () => observer.disconnect();
68
+ }, [ids]);
69
+
70
+ return activeId;
71
+ }
72
+
73
+ export function PreviewAside({
74
+ fragment,
75
+ variants,
76
+ focusedVariantIndex,
77
+ activePanel,
78
+ onSelectVariant,
79
+ onCopyLink,
80
+ onShowShortcuts,
81
+ }: PreviewAsideProps) {
82
+ const focusedVariant = variants[focusedVariantIndex] || null;
83
+
84
+ const headings = useMemo(() => buildHeadings(fragment), [fragment]);
85
+ const headingIds = useMemo(() => headings.map((h) => h.id), [headings]);
86
+ const activeId = useActiveHeading(headingIds);
87
+
88
+ return (
89
+ <Box padding="md" style={{ position: "sticky", top: "80px" }}>
90
+ <Stack gap="md">
91
+ {/* On This Page — same TOC component as docs site */}
92
+ <TableOfContents>
93
+ {headings.map((heading) => (
94
+ <TableOfContents.Item
95
+ key={heading.id}
96
+ id={heading.id}
97
+ active={activeId === heading.id}
98
+ indent={heading.level === 3}
99
+ >
100
+ {heading.text}
101
+ </TableOfContents.Item>
102
+ ))}
103
+ </TableOfContents>
104
+
105
+ {/* Variant list */}
106
+ {variants.length > 0 && (
107
+ <>
108
+ <Separator />
109
+ <Stack gap="xs">
110
+ <Text
111
+ size="xs"
112
+ color="tertiary"
113
+ style={{ textTransform: "uppercase", letterSpacing: "0.08em" }}
114
+ >
115
+ Variants
116
+ </Text>
117
+ {variants.map((variant, index) => {
118
+ const active = index === focusedVariantIndex;
119
+ const anchorId = getVariantSectionId(fragment.meta.name, variant.name);
120
+
121
+ return (
122
+ <Button
123
+ key={variant.name}
124
+ as="a"
125
+ variant={active ? "secondary" : "ghost"}
126
+ size="sm"
127
+ href={`#${anchorId}`}
128
+ onClick={(event: React.MouseEvent) => {
129
+ event.preventDefault();
130
+ onSelectVariant(index);
131
+ }}
132
+ style={{ justifyContent: "flex-start" }}
133
+ >
134
+ {variant.name}
135
+ </Button>
136
+ );
137
+ })}
138
+ </Stack>
139
+ </>
140
+ )}
141
+
142
+ <Separator />
143
+ <Stack gap="xs">
144
+ <Text size="sm" weight="medium">
145
+ {fragment.meta.name}
146
+ </Text>
147
+ <Text size="xs" color="secondary">
148
+ {focusedVariant
149
+ ? `Variant: ${focusedVariant.name}`
150
+ : "Select a variant"}
151
+ </Text>
152
+ <Text size="xs" color="tertiary">
153
+ Active panel: {activePanel}
154
+ </Text>
155
+ </Stack>
156
+ <Separator />
157
+ <Stack gap="xs">
158
+ <Button variant="ghost" size="sm" onClick={onCopyLink}>
159
+ Copy Link
160
+ </Button>
161
+ <Button variant="ghost" size="sm" onClick={onShowShortcuts}>
162
+ Keyboard Shortcuts
163
+ </Button>
164
+ </Stack>
165
+ </Stack>
166
+ </Box>
167
+ );
168
+ }