diff --git a/packages/studio/src/App.tsx b/packages/studio/src/App.tsx
index b8e397197..4f191dcb6 100644
--- a/packages/studio/src/App.tsx
+++ b/packages/studio/src/App.tsx
@@ -192,6 +192,7 @@ export function StudioApp() {
projectId,
blockName,
activeCompPath,
+ previewIframe: previewIframeRef.current,
timelineElements,
readProjectFile: fileManager.readProjectFile,
writeProjectFile: fileManager.writeProjectFile,
@@ -235,6 +236,7 @@ export function StudioApp() {
blockName,
activeCompPath,
placement,
+ previewIframe: previewIframeRef.current,
timelineElements,
readProjectFile: fileManager.readProjectFile,
writeProjectFile: fileManager.writeProjectFile,
@@ -265,6 +267,7 @@ export function StudioApp() {
blockName,
activeCompPath,
visualPosition: position,
+ previewIframe: previewIframeRef.current,
timelineElements,
readProjectFile: fileManager.readProjectFile,
writeProjectFile: fileManager.writeProjectFile,
diff --git a/packages/studio/src/components/editor/PropertyPanel.tsx b/packages/studio/src/components/editor/PropertyPanel.tsx
index 96a220862..d40ab0974 100644
--- a/packages/studio/src/components/editor/PropertyPanel.tsx
+++ b/packages/studio/src/components/editor/PropertyPanel.tsx
@@ -323,7 +323,7 @@ export const PropertyPanel = memo(function PropertyPanel({
onSetStyle("z-index", next)}
diff --git a/packages/studio/src/utils/blockInstaller.ts b/packages/studio/src/utils/blockInstaller.ts
index cc8452241..816c9faaf 100644
--- a/packages/studio/src/utils/blockInstaller.ts
+++ b/packages/studio/src/utils/blockInstaller.ts
@@ -9,12 +9,28 @@ import { formatTimelineAttributeNumber } from "../player/components/timelineEdit
import { saveProjectFilesWithHistory } from "./studioFileHistory";
import type { EditHistoryKind } from "./editHistory";
+function getMaxZIndexFromIframe(iframe: HTMLIFrameElement | null): number {
+ try {
+ const doc = iframe?.contentDocument;
+ if (!doc) return 0;
+ let max = 0;
+ for (const el of doc.body.querySelectorAll("*")) {
+ const z = parseInt(getComputedStyle(el).zIndex, 10);
+ if (Number.isFinite(z) && z > max) max = z;
+ }
+ return max;
+ } catch {
+ return 0;
+ }
+}
+
interface AddBlockOptions {
projectId: string;
blockName: string;
activeCompPath: string | null;
placement?: { start: number; track: number };
visualPosition?: { left: number; top: number };
+ previewIframe?: HTMLIFrameElement | null;
timelineElements: TimelineElement[];
readProjectFile: (path: string) => Promise;
writeProjectFile: (path: string, content: string) => Promise;
@@ -126,12 +142,7 @@ export async function addBlockToProject(
? Math.max(...relevantElements.map((te) => te.track)) + 1
: 1);
- const zIndexMatches = originalContent.matchAll(/z-index:\s*(\d+)/g);
- let maxExistingZ = 0;
- for (const m of zIndexMatches) {
- maxExistingZ = Math.max(maxExistingZ, parseInt(m[1]!, 10));
- }
- const zIndex = maxExistingZ + 1;
+ const zIndex = getMaxZIndexFromIframe(opts.previewIframe ?? null) + 1;
const width = isBlock
? (block as { dimensions: { width: number } }).dimensions.width