Skip to content

Commit 4a67d4d

Browse files
committed
Add fallback preview size for DOM-only reference examples
1 parent 256581d commit 4a67d4d

File tree

1 file changed

+28
-8
lines changed

1 file changed

+28
-8
lines changed

src/components/CodeEmbed/index.jsx

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { useState, useEffect, useRef } from "preact/hooks";
2-
import { useLiveRegion } from '../hooks/useLiveRegion';
2+
import { useLiveRegion } from "../hooks/useLiveRegion";
33
import CodeMirror, { EditorView } from "@uiw/react-codemirror";
44
import { javascript } from "@codemirror/lang-javascript";
55
import { cdnLibraryUrl, cdnSoundUrl } from "@/src/globals/globals";
@@ -38,20 +38,34 @@ export const CodeEmbed = (props) => {
3838
);
3939

4040
let { previewWidth, previewHeight } = props;
41-
const canvasMatch = /createCanvas\(\s*(\d+),\s*(\d+)\s*(?:,\s*(?:P2D|WEBGL)\s*)?\)/m.exec(initialCode);
41+
const canvasMatch =
42+
/createCanvas\(\s*(\d+),\s*(\d+)\s*(?:,\s*(?:P2D|WEBGL)\s*)?\)/m.exec(
43+
initialCode,
44+
);
4245
if (canvasMatch) {
4346
previewWidth = previewWidth || parseFloat(canvasMatch[1]);
4447
previewHeight = previewHeight || parseFloat(canvasMatch[2]);
4548
}
4649

47-
const largeSketch = previewWidth && previewWidth > 770 - 60;
48-
4950
// Quick hack to make room for DOM that gets added below the canvas by default
50-
const domMatch = /create(Button|Select|P|Div|Input|ColorPicker)/.exec(initialCode);
51+
const domMatch = /create(Button|Select|P|Div|Input|ColorPicker)/.exec(
52+
initialCode,
53+
);
5154
if (domMatch && previewHeight) {
5255
previewHeight += 100;
5356
}
5457

58+
// Fallback preview size when no usable canvas dimensions are detected.
59+
// Ensures DOM-based examples (e.g., createCapture with noCanvas) are visible.
60+
const DEFAULT_PREVIEW_WIDTH = 400;
61+
const DEFAULT_PREVIEW_HEIGHT = 300;
62+
if (previewWidth === undefined && previewHeight === undefined) {
63+
previewWidth = DEFAULT_PREVIEW_WIDTH;
64+
previewHeight = DEFAULT_PREVIEW_HEIGHT;
65+
}
66+
67+
const largeSketch = previewWidth && previewWidth > 770 - 60;
68+
5569
const codeFrameRef = useRef(null);
5670

5771
const updateOrReRun = () => {
@@ -86,7 +100,7 @@ export const CodeEmbed = (props) => {
86100
>
87101
{props.previewable ? (
88102
<div
89-
className={`ml-0 flex w-fit gap-[20px] ${largeSketch ? "flex-col" : (props.allowSideBySide ? "" : "flex-col lg:flex-row")}`}
103+
className={`ml-0 flex w-fit gap-[20px] ${largeSketch ? "flex-col" : props.allowSideBySide ? "" : "flex-col lg:flex-row"}`}
90104
>
91105
<div>
92106
<CodeFrame
@@ -96,10 +110,16 @@ export const CodeEmbed = (props) => {
96110
base={props.base}
97111
frameRef={codeFrameRef}
98112
lazyLoad={props.lazyLoad}
99-
scripts={props.includeSound ? [cdnLibraryUrl, cdnSoundUrl] :[cdnLibraryUrl]}
113+
scripts={
114+
props.includeSound
115+
? [cdnLibraryUrl, cdnSoundUrl]
116+
: [cdnLibraryUrl]
117+
}
100118
/>
101119
</div>
102-
<div className={`flex gap-2.5 ${largeSketch ? "flex-row" : "md:flex-row lg:flex-col"}`}>
120+
<div
121+
className={`flex gap-2.5 ${largeSketch ? "flex-row" : "md:flex-row lg:flex-col"}`}
122+
>
103123
<CircleButton
104124
className="bg-bg-gray-40"
105125
onClick={updateOrReRun}

0 commit comments

Comments
 (0)