@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,131 @@
|
|
|
1
|
+
// @ts-nocheck
|
|
2
|
+
import React, { type ReactNode, Suspense } from 'react';
|
|
3
|
+
import type { FragmentVariant } from '@fragments-sdk/core';
|
|
4
|
+
import { ErrorBoundary } from './ErrorBoundary.js';
|
|
5
|
+
|
|
6
|
+
interface VariantRendererProps {
|
|
7
|
+
/** The variant to render */
|
|
8
|
+
variant: FragmentVariant;
|
|
9
|
+
|
|
10
|
+
/** Optional loading fallback for async components */
|
|
11
|
+
loadingFallback?: ReactNode;
|
|
12
|
+
|
|
13
|
+
/** Optional error fallback */
|
|
14
|
+
errorFallback?: ReactNode;
|
|
15
|
+
|
|
16
|
+
/** Callback when variant render throws */
|
|
17
|
+
onError?: (error: Error) => void;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Renders a single variant with error boundary and suspense support.
|
|
22
|
+
*/
|
|
23
|
+
export function VariantRenderer({
|
|
24
|
+
variant,
|
|
25
|
+
loadingFallback,
|
|
26
|
+
errorFallback,
|
|
27
|
+
onError,
|
|
28
|
+
}: VariantRendererProps): React.ReactElement {
|
|
29
|
+
return (
|
|
30
|
+
<ErrorBoundary
|
|
31
|
+
fallback={errorFallback}
|
|
32
|
+
onError={(error) => onError?.(error)}
|
|
33
|
+
>
|
|
34
|
+
<Suspense
|
|
35
|
+
fallback={
|
|
36
|
+
loadingFallback ?? (
|
|
37
|
+
<div
|
|
38
|
+
style={{
|
|
39
|
+
padding: '16px',
|
|
40
|
+
color: '#6b7280',
|
|
41
|
+
fontFamily: 'system-ui, sans-serif',
|
|
42
|
+
}}
|
|
43
|
+
>
|
|
44
|
+
Loading...
|
|
45
|
+
</div>
|
|
46
|
+
)
|
|
47
|
+
}
|
|
48
|
+
>
|
|
49
|
+
{variant.render()}
|
|
50
|
+
</Suspense>
|
|
51
|
+
</ErrorBoundary>
|
|
52
|
+
);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
interface VariantGridProps {
|
|
56
|
+
/** Variants to render */
|
|
57
|
+
variants: FragmentVariant[];
|
|
58
|
+
|
|
59
|
+
/** Number of columns in the grid */
|
|
60
|
+
columns?: number;
|
|
61
|
+
|
|
62
|
+
/** Gap between variants in pixels */
|
|
63
|
+
gap?: number;
|
|
64
|
+
|
|
65
|
+
/** Optional loading fallback */
|
|
66
|
+
loadingFallback?: ReactNode;
|
|
67
|
+
|
|
68
|
+
/** Optional error fallback */
|
|
69
|
+
errorFallback?: ReactNode;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Renders multiple variants in a grid layout.
|
|
74
|
+
*/
|
|
75
|
+
export function VariantGrid({
|
|
76
|
+
variants,
|
|
77
|
+
columns = 2,
|
|
78
|
+
gap = 24,
|
|
79
|
+
loadingFallback,
|
|
80
|
+
errorFallback,
|
|
81
|
+
}: VariantGridProps): React.ReactElement {
|
|
82
|
+
return (
|
|
83
|
+
<div
|
|
84
|
+
style={{
|
|
85
|
+
display: 'grid',
|
|
86
|
+
gridTemplateColumns: `repeat(${columns}, 1fr)`,
|
|
87
|
+
gap: `${gap}px`,
|
|
88
|
+
}}
|
|
89
|
+
>
|
|
90
|
+
{variants.map((variant) => (
|
|
91
|
+
<div key={variant.name}>
|
|
92
|
+
<div
|
|
93
|
+
style={{
|
|
94
|
+
marginBottom: '8px',
|
|
95
|
+
fontWeight: 600,
|
|
96
|
+
fontSize: '14px',
|
|
97
|
+
color: '#374151',
|
|
98
|
+
fontFamily: 'system-ui, sans-serif',
|
|
99
|
+
}}
|
|
100
|
+
>
|
|
101
|
+
{variant.name}
|
|
102
|
+
</div>
|
|
103
|
+
<div
|
|
104
|
+
style={{
|
|
105
|
+
marginBottom: '8px',
|
|
106
|
+
fontSize: '13px',
|
|
107
|
+
color: '#6b7280',
|
|
108
|
+
fontFamily: 'system-ui, sans-serif',
|
|
109
|
+
}}
|
|
110
|
+
>
|
|
111
|
+
{variant.description}
|
|
112
|
+
</div>
|
|
113
|
+
<div
|
|
114
|
+
style={{
|
|
115
|
+
padding: '16px',
|
|
116
|
+
border: '1px solid #e5e7eb',
|
|
117
|
+
borderRadius: '8px',
|
|
118
|
+
background: '#ffffff',
|
|
119
|
+
}}
|
|
120
|
+
>
|
|
121
|
+
<VariantRenderer
|
|
122
|
+
variant={variant}
|
|
123
|
+
loadingFallback={loadingFallback}
|
|
124
|
+
errorFallback={errorFallback}
|
|
125
|
+
/>
|
|
126
|
+
</div>
|
|
127
|
+
</div>
|
|
128
|
+
))}
|
|
129
|
+
</div>
|
|
130
|
+
);
|
|
131
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { Tabs, Stack } from '@fragments-sdk/ui';
|
|
2
|
+
import type { FragmentVariant } from '@fragments-sdk/core';
|
|
3
|
+
import { PlayIcon } from './Icons.js';
|
|
4
|
+
|
|
5
|
+
interface VariantTabsProps {
|
|
6
|
+
variants: FragmentVariant[];
|
|
7
|
+
activeIndex: number;
|
|
8
|
+
onSelect: (index: number) => void;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export function VariantTabs({ variants, activeIndex, onSelect }: VariantTabsProps) {
|
|
12
|
+
if (variants.length === 0) return null;
|
|
13
|
+
|
|
14
|
+
const activeValue = variants[activeIndex]?.name ?? '';
|
|
15
|
+
|
|
16
|
+
return (
|
|
17
|
+
<Tabs
|
|
18
|
+
value={activeValue}
|
|
19
|
+
onValueChange={(value: string | number) => {
|
|
20
|
+
const index = variants.findIndex(v => v.name === value);
|
|
21
|
+
if (index >= 0) onSelect(index);
|
|
22
|
+
}}
|
|
23
|
+
>
|
|
24
|
+
<Tabs.List variant="pills">
|
|
25
|
+
{variants.map((variant) => (
|
|
26
|
+
<Tabs.Tab key={variant.name} value={variant.name}>
|
|
27
|
+
<Stack direction="row" align="center" gap="xs" as="span">
|
|
28
|
+
{variant.name}
|
|
29
|
+
{variant.hasPlayFunction && (
|
|
30
|
+
<span style={{ display: 'inline-flex', width: '12px', height: '12px', color: 'var(--color-accent)' }}>
|
|
31
|
+
<PlayIcon />
|
|
32
|
+
</span>
|
|
33
|
+
)}
|
|
34
|
+
</Stack>
|
|
35
|
+
</Tabs.Tab>
|
|
36
|
+
))}
|
|
37
|
+
</Tabs.List>
|
|
38
|
+
</Tabs>
|
|
39
|
+
);
|
|
40
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import type { RefObject } from "react";
|
|
2
|
+
import { BRAND } from '@fragments-sdk/core';
|
|
3
|
+
import {
|
|
4
|
+
Header,
|
|
5
|
+
Stack,
|
|
6
|
+
Text,
|
|
7
|
+
Button,
|
|
8
|
+
ThemeToggle,
|
|
9
|
+
FragmentsLogo,
|
|
10
|
+
} from "@fragments-sdk/ui";
|
|
11
|
+
import { GitHubIcon } from "./Icons.js";
|
|
12
|
+
import { HeaderSearch } from "./HeaderSearch.js";
|
|
13
|
+
import { useTheme } from "./ThemeProvider.js";
|
|
14
|
+
import { WebMCPStatusIndicator } from "./WebMCPStatusIndicator.js";
|
|
15
|
+
|
|
16
|
+
interface ViewerHeaderProps {
|
|
17
|
+
showHealth: boolean;
|
|
18
|
+
searchQuery: string;
|
|
19
|
+
onSearchChange: (value: string) => void;
|
|
20
|
+
searchInputRef: RefObject<HTMLInputElement>;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function ViewerHeader({
|
|
24
|
+
showHealth,
|
|
25
|
+
searchQuery,
|
|
26
|
+
onSearchChange,
|
|
27
|
+
searchInputRef,
|
|
28
|
+
}: ViewerHeaderProps) {
|
|
29
|
+
const { setTheme, resolvedTheme } = useTheme();
|
|
30
|
+
return (
|
|
31
|
+
<Header aria-label="Fragments viewer header">
|
|
32
|
+
<Header.Trigger />
|
|
33
|
+
<Header.Brand>
|
|
34
|
+
<Stack direction="row" gap="sm" align="center">
|
|
35
|
+
<FragmentsLogo size={20} />
|
|
36
|
+
<Text weight="medium" size="sm">
|
|
37
|
+
{BRAND.name}
|
|
38
|
+
</Text>
|
|
39
|
+
<Text size="xs" color="tertiary">
|
|
40
|
+
{showHealth ? "health dashboard" : "preview"}
|
|
41
|
+
</Text>
|
|
42
|
+
</Stack>
|
|
43
|
+
</Header.Brand>
|
|
44
|
+
<HeaderSearch value={searchQuery} onChange={onSearchChange} inputRef={searchInputRef} />
|
|
45
|
+
<Header.Spacer />
|
|
46
|
+
<Header.Actions>
|
|
47
|
+
<WebMCPStatusIndicator />
|
|
48
|
+
<ThemeToggle
|
|
49
|
+
size="sm"
|
|
50
|
+
value={resolvedTheme}
|
|
51
|
+
onValueChange={(value) => setTheme(value)}
|
|
52
|
+
aria-label={`Theme: ${resolvedTheme}`}
|
|
53
|
+
/>
|
|
54
|
+
<Button
|
|
55
|
+
as="a"
|
|
56
|
+
variant="ghost"
|
|
57
|
+
size="sm"
|
|
58
|
+
icon
|
|
59
|
+
href="https://github.com/ConanMcN/fragments"
|
|
60
|
+
target="_blank"
|
|
61
|
+
rel="noopener noreferrer"
|
|
62
|
+
aria-label="View on GitHub"
|
|
63
|
+
>
|
|
64
|
+
<GitHubIcon />
|
|
65
|
+
</Button>
|
|
66
|
+
</Header.Actions>
|
|
67
|
+
</Header>
|
|
68
|
+
);
|
|
69
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { useEffect } from 'react';
|
|
2
|
+
import { setViewerState } from '../webmcp/viewer-state.js';
|
|
3
|
+
import { useUrlState } from '../hooks/useUrlState.js';
|
|
4
|
+
import { useAppState } from '../hooks/useAppState.js';
|
|
5
|
+
import { useViewSettings } from '../hooks/useViewSettings.js';
|
|
6
|
+
import { useTheme } from './ThemeProvider.js';
|
|
7
|
+
import { BRAND, type FragmentDefinition } from '@fragments-sdk/core';
|
|
8
|
+
|
|
9
|
+
interface ViewerStateSyncProps {
|
|
10
|
+
fragments: Array<{ path: string; fragment: FragmentDefinition }>;
|
|
11
|
+
activeVariantIndex: number;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export function ViewerStateSync({ fragments, activeVariantIndex }: ViewerStateSyncProps) {
|
|
15
|
+
const { state: urlState } = useUrlState();
|
|
16
|
+
const { state: uiState } = useAppState();
|
|
17
|
+
const viewSettings = useViewSettings();
|
|
18
|
+
const { resolvedTheme } = useTheme();
|
|
19
|
+
|
|
20
|
+
useEffect(() => {
|
|
21
|
+
const activeFragment = urlState.component
|
|
22
|
+
? fragments.find(f => f.fragment.meta.name.toLowerCase() === urlState.component!.toLowerCase())
|
|
23
|
+
: null;
|
|
24
|
+
|
|
25
|
+
const variants = activeFragment?.fragment.variants ?? [];
|
|
26
|
+
|
|
27
|
+
setViewerState({
|
|
28
|
+
currentComponent: activeFragment?.fragment.meta.name ?? null,
|
|
29
|
+
currentVariant: variants[activeVariantIndex]?.name ?? null,
|
|
30
|
+
variantIndex: activeVariantIndex,
|
|
31
|
+
totalVariants: variants.length,
|
|
32
|
+
viewport: viewSettings.viewport,
|
|
33
|
+
zoom: viewSettings.zoom,
|
|
34
|
+
theme: resolvedTheme,
|
|
35
|
+
panels: {
|
|
36
|
+
activePanel: uiState.activePanel,
|
|
37
|
+
panelOpen: uiState.panelOpen,
|
|
38
|
+
},
|
|
39
|
+
viewMode: {
|
|
40
|
+
matrixView: uiState.showMatrixView,
|
|
41
|
+
multiViewport: uiState.showMultiViewport,
|
|
42
|
+
comparison: uiState.showComparison,
|
|
43
|
+
},
|
|
44
|
+
designSystem: {
|
|
45
|
+
name: BRAND.name,
|
|
46
|
+
componentCount: fragments.length,
|
|
47
|
+
},
|
|
48
|
+
});
|
|
49
|
+
}, [urlState, uiState, viewSettings, resolvedTheme, fragments, activeVariantIndex]);
|
|
50
|
+
|
|
51
|
+
return null;
|
|
52
|
+
}
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
import { Button, Menu, Stack, Input, Text, Box } from '@fragments-sdk/ui';
|
|
2
|
+
import { VIEWPORT_PRESETS, type ViewportPreset } from '../constants/ui.js';
|
|
3
|
+
import {
|
|
4
|
+
ViewportIcon,
|
|
5
|
+
ChevronDownIcon,
|
|
6
|
+
ResponsiveIcon,
|
|
7
|
+
DesktopIcon,
|
|
8
|
+
TabletIcon,
|
|
9
|
+
MobileIcon,
|
|
10
|
+
SettingsIcon,
|
|
11
|
+
} from './Icons.js';
|
|
12
|
+
|
|
13
|
+
// Re-export for consumers
|
|
14
|
+
export type { ViewportPreset };
|
|
15
|
+
|
|
16
|
+
export interface ViewportSize {
|
|
17
|
+
width: number | null;
|
|
18
|
+
height: number | null;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
// Viewport presets with display metadata
|
|
22
|
+
const VIEWPORT_PRESETS_UI = Object.entries(VIEWPORT_PRESETS).map(([value, config]) => ({
|
|
23
|
+
value: value as ViewportPreset,
|
|
24
|
+
label: config.label,
|
|
25
|
+
width: config.width,
|
|
26
|
+
}));
|
|
27
|
+
|
|
28
|
+
function PresetIcon({ preset }: { preset: ViewportPreset }) {
|
|
29
|
+
const commonStyle = { width: '16px', height: '16px' };
|
|
30
|
+
|
|
31
|
+
switch (preset) {
|
|
32
|
+
case 'desktop':
|
|
33
|
+
return <DesktopIcon style={commonStyle} />;
|
|
34
|
+
case 'tablet':
|
|
35
|
+
return <TabletIcon style={commonStyle} />;
|
|
36
|
+
case 'mobile':
|
|
37
|
+
return <MobileIcon style={commonStyle} />;
|
|
38
|
+
case 'responsive':
|
|
39
|
+
default:
|
|
40
|
+
return <ResponsiveIcon style={commonStyle} />;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
interface ViewportSelectorProps {
|
|
45
|
+
viewport: ViewportPreset;
|
|
46
|
+
customSize: ViewportSize;
|
|
47
|
+
onViewportChange: (viewport: ViewportPreset) => void;
|
|
48
|
+
onCustomSizeChange: (size: ViewportSize) => void;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export function ViewportSelector({
|
|
52
|
+
viewport,
|
|
53
|
+
customSize,
|
|
54
|
+
onViewportChange,
|
|
55
|
+
onCustomSizeChange,
|
|
56
|
+
}: ViewportSelectorProps) {
|
|
57
|
+
const currentPreset = VIEWPORT_PRESETS_UI.find(p => p.value === viewport);
|
|
58
|
+
const isCustomViewport = viewport === 'custom';
|
|
59
|
+
const displayLabel = viewport === 'custom'
|
|
60
|
+
? `${customSize.width || '?'}\u00D7${customSize.height || 'auto'}`
|
|
61
|
+
: currentPreset?.label || 'Responsive';
|
|
62
|
+
|
|
63
|
+
return (
|
|
64
|
+
<Menu>
|
|
65
|
+
<Menu.Trigger asChild>
|
|
66
|
+
<Button variant="ghost" size="sm" title="Viewport size">
|
|
67
|
+
<Stack direction="row" gap="xs" align="center">
|
|
68
|
+
<span style={{ display: 'inline-flex', width: '14px', height: '14px' }}>
|
|
69
|
+
<ViewportIcon />
|
|
70
|
+
</span>
|
|
71
|
+
<span>{displayLabel}</span>
|
|
72
|
+
<span style={{ display: 'inline-flex', width: '12px', height: '12px' }}>
|
|
73
|
+
<ChevronDownIcon />
|
|
74
|
+
</span>
|
|
75
|
+
</Stack>
|
|
76
|
+
</Button>
|
|
77
|
+
</Menu.Trigger>
|
|
78
|
+
<Menu.Content side="bottom" align="end">
|
|
79
|
+
<Menu.RadioGroup
|
|
80
|
+
value={isCustomViewport ? 'custom' : viewport}
|
|
81
|
+
onValueChange={(value: string) => {
|
|
82
|
+
if (value === 'custom') {
|
|
83
|
+
onViewportChange('custom');
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
onViewportChange(value as ViewportPreset);
|
|
87
|
+
}}
|
|
88
|
+
>
|
|
89
|
+
{VIEWPORT_PRESETS_UI.map((preset) => (
|
|
90
|
+
<Menu.RadioItem key={preset.value} value={preset.value}>
|
|
91
|
+
<Stack direction="row" gap="sm" align="center" justify="between" style={{ width: '100%' }}>
|
|
92
|
+
<Stack direction="row" gap="sm" align="center">
|
|
93
|
+
<PresetIcon preset={preset.value} />
|
|
94
|
+
<span>{preset.label}</span>
|
|
95
|
+
</Stack>
|
|
96
|
+
{preset.width && (
|
|
97
|
+
<Text size="2xs" color="tertiary">
|
|
98
|
+
{preset.width}px
|
|
99
|
+
</Text>
|
|
100
|
+
)}
|
|
101
|
+
</Stack>
|
|
102
|
+
</Menu.RadioItem>
|
|
103
|
+
))}
|
|
104
|
+
</Menu.RadioGroup>
|
|
105
|
+
|
|
106
|
+
<Menu.Separator />
|
|
107
|
+
|
|
108
|
+
<Menu.Item onSelect={() => onViewportChange('custom')}>
|
|
109
|
+
<Stack direction="row" gap="sm" align="center" style={{
|
|
110
|
+
width: '100%',
|
|
111
|
+
fontWeight: isCustomViewport ? 600 : 400,
|
|
112
|
+
color: isCustomViewport ? 'var(--color-accent)' : undefined,
|
|
113
|
+
}}>
|
|
114
|
+
<SettingsIcon style={{ width: '16px', height: '16px' }} />
|
|
115
|
+
Custom size
|
|
116
|
+
</Stack>
|
|
117
|
+
</Menu.Item>
|
|
118
|
+
|
|
119
|
+
{isCustomViewport && (
|
|
120
|
+
<div
|
|
121
|
+
onClick={(e) => e.stopPropagation()}
|
|
122
|
+
onKeyDown={(e) => e.stopPropagation()}
|
|
123
|
+
>
|
|
124
|
+
<Box padding="sm">
|
|
125
|
+
<Stack gap="sm">
|
|
126
|
+
<Stack direction="row" gap="sm" align="center">
|
|
127
|
+
<Text size="2xs" color="tertiary" style={{ width: '20px' }}>W:</Text>
|
|
128
|
+
<Input
|
|
129
|
+
type="number"
|
|
130
|
+
size="sm"
|
|
131
|
+
value={customSize.width != null ? String(customSize.width) : ''}
|
|
132
|
+
onChange={(value) => {
|
|
133
|
+
const width = value ? parseInt(value, 10) : null;
|
|
134
|
+
onCustomSizeChange({ ...customSize, width });
|
|
135
|
+
onViewportChange('custom');
|
|
136
|
+
}}
|
|
137
|
+
placeholder="auto"
|
|
138
|
+
style={{ width: '64px' }}
|
|
139
|
+
/>
|
|
140
|
+
<Text size="2xs" color="tertiary">px</Text>
|
|
141
|
+
</Stack>
|
|
142
|
+
<Stack direction="row" gap="sm" align="center">
|
|
143
|
+
<Text size="2xs" color="tertiary" style={{ width: '20px' }}>H:</Text>
|
|
144
|
+
<Input
|
|
145
|
+
type="number"
|
|
146
|
+
size="sm"
|
|
147
|
+
value={customSize.height != null ? String(customSize.height) : ''}
|
|
148
|
+
onChange={(value) => {
|
|
149
|
+
const height = value ? parseInt(value, 10) : null;
|
|
150
|
+
onCustomSizeChange({ ...customSize, height });
|
|
151
|
+
onViewportChange('custom');
|
|
152
|
+
}}
|
|
153
|
+
placeholder="auto"
|
|
154
|
+
style={{ width: '64px' }}
|
|
155
|
+
/>
|
|
156
|
+
<Text size="2xs" color="tertiary">px</Text>
|
|
157
|
+
</Stack>
|
|
158
|
+
</Stack>
|
|
159
|
+
</Box>
|
|
160
|
+
</div>
|
|
161
|
+
)}
|
|
162
|
+
</Menu.Content>
|
|
163
|
+
</Menu>
|
|
164
|
+
);
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
// Get viewport width from preset
|
|
168
|
+
export function getViewportWidth(viewport: ViewportPreset, customSize: ViewportSize): number | null {
|
|
169
|
+
if (viewport === 'custom') return customSize.width;
|
|
170
|
+
if (viewport === 'responsive') return null;
|
|
171
|
+
return VIEWPORT_PRESETS[viewport]?.width ?? null;
|
|
172
|
+
}
|