Skip to content

Commit dffe8e5

Browse files
authored
Replaced multiple previews with one resizable preview. (#158)
1 parent dfcf795 commit dffe8e5

File tree

2 files changed

+34
-56
lines changed

2 files changed

+34
-56
lines changed

packages/plugin-ui/src/PluginUI.tsx

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ type PluginUIProps = {
3333
const frameworks: Framework[] = ["HTML", "Tailwind", "Flutter", "SwiftUI"];
3434

3535
export const PluginUI = (props: PluginUIProps) => {
36-
const [isResponsiveExpanded, setIsResponsiveExpanded] = useState(false);
3736
const isEmpty = props.code === "";
3837

3938
const warnings = props.warnings ?? [];
@@ -67,11 +66,7 @@ export const PluginUI = (props: PluginUIProps) => {
6766
<div className="flex flex-col h-full overflow-y-auto">
6867
<div className="flex flex-col items-center px-4 py-2 gap-2 dark:bg-transparent">
6968
{isEmpty === false && props.htmlPreview && (
70-
<Preview
71-
htmlPreview={props.htmlPreview}
72-
isResponsiveExpanded={isResponsiveExpanded}
73-
setIsResponsiveExpanded={setIsResponsiveExpanded}
74-
/>
69+
<Preview htmlPreview={props.htmlPreview} />
7570
)}
7671
{warnings.length > 0 && (
7772
<div className="flex flex-col bg-yellow-400 text-black dark:bg-yellow-500 dark:text-black p-3 w-full">

packages/plugin-ui/src/components/Preview.tsx

Lines changed: 33 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,49 @@
11
import { HTMLPreview } from "types";
2-
import ExpandIcon from "./ExpandIcon";
32

43
const Preview: React.FC<{
54
htmlPreview: HTMLPreview;
6-
isResponsiveExpanded: boolean;
7-
setIsResponsiveExpanded: (value: boolean) => void;
85
}> = (props) => {
9-
const previewWidths = [45, 80, 140];
10-
const labels = ["sm", "md", "lg"];
6+
const targetWidth = 240;
7+
const targetHeight = 120;
8+
const scaleFactor = Math.min(
9+
targetWidth / props.htmlPreview.size.width,
10+
targetHeight / props.htmlPreview.size.height,
11+
);
1112

1213
return (
1314
<div className="flex flex-col w-full">
1415
<div className="py-1.5 flex gap-2 w-full text-lg font-medium text-center dark:text-white rounded-lg justify-between">
1516
<span>Responsive Preview</span>
16-
<button
17-
className={`px-2 py-1 text-sm font-semibold border border-green-500 rounded-md shadow-sm hover:bg-green-500 dark:hover:bg-green-600 hover:text-white hover:border-transparent transition-all duration-300 ${"bg-neutral-100 dark:bg-neutral-700 text-neutral-700 dark:text-neutral-200 border-neutral-300 dark:border-neutral-600"}`}
18-
onClick={() => {
19-
props.setIsResponsiveExpanded(!props.isResponsiveExpanded);
20-
}}
21-
>
22-
<ExpandIcon size={16} />
23-
</button>
2417
</div>
2518
<div className="flex gap-2 justify-center items-center">
26-
{previewWidths.map((targetWidth, index) => {
27-
const targetHeight = props.isResponsiveExpanded ? 260 : 120;
28-
const scaleFactor = Math.min(
29-
targetWidth / props.htmlPreview.size.width,
30-
targetHeight / props.htmlPreview.size.height,
31-
);
32-
return (
19+
<div
20+
className="relative flex flex-col items-center"
21+
style={{
22+
width: targetWidth,
23+
resize: "both",
24+
overflow: "auto",
25+
minWidth: "100px",
26+
minHeight: "100px",
27+
}}
28+
>
29+
<div
30+
className="flex flex-col justify-center items-center border border-neutral-200 dark:border-neutral-700 rounded-md shadow-sm w-full h-full"
31+
style={{
32+
clipPath: "inset(0px round 6px)",
33+
}}
34+
>
3335
<div
34-
key={"preview " + index}
35-
className="relative flex flex-col items-center"
36-
style={{ width: targetWidth }}
37-
>
38-
<div
39-
className="flex flex-col justify-center items-center border border-neutral-200 dark:border-neutral-700 rounded-md shadow-sm"
40-
style={{
41-
width: targetWidth,
42-
height: targetHeight,
43-
clipPath: "inset(0px round 6px)",
44-
}}
45-
>
46-
<div
47-
style={{
48-
zoom: scaleFactor,
49-
width: "100%",
50-
height: "100%",
51-
display: "flex",
52-
}}
53-
dangerouslySetInnerHTML={{
54-
__html: props.htmlPreview.content,
55-
}}
56-
/>
57-
</div>
58-
<span className="mt-auto text-xs text-gray-500">
59-
{labels[index]}
60-
</span>
61-
</div>
62-
);
63-
})}
36+
style={{
37+
zoom: scaleFactor,
38+
width: "100%",
39+
height: "100%",
40+
}}
41+
dangerouslySetInnerHTML={{
42+
__html: props.htmlPreview.content,
43+
}}
44+
/>
45+
</div>
46+
</div>
6447
</div>
6548
</div>
6649
);

0 commit comments

Comments
 (0)