Drop in a screenshot and convert it to clean code (HTML/Tailwind/React/Vue)
Python
Exit select mode with Escape and make selection distinct from hover
Abi Raja6f28e31a86c7
diff --git a/frontend/src/App.tsx b/frontend/src/App.tsxindex 29dd1c1e..3fe11fd2 100644--- a/frontend/src/App.tsx+++ b/frontend/src/App.tsx@@ -25,10 +25,8 @@ import {import { useAppStore } from "./store/app-store";import { useProjectStore } from "./store/project-store";import { useDesignSystems } from "./hooks/useDesignSystems";-import {- buildSelectedElementInstruction,- removeHighlight,-} from "./components/select-and-edit/utils";+import { buildSelectedElementInstruction } from "./components/select-and-edit/utils";+import { useEscapeToExitSelectMode } from "./components/select-and-edit/useEscapeToExitSelectMode";import Sidebar from "./components/sidebar/Sidebar";import IconStrip from "./components/sidebar/IconStrip";import HistoryDisplay from "./components/history/HistoryDisplay";@@ -163,6 +161,8 @@ function App() {// Indicate coding state using the browser tab's favicon and titleuseBrowserTabIndicator(appState === AppState.CODING);+ useEscapeToExitSelectMode();+// When the user already has the settings in local storage, newly added keys// do not get added to the settings so if it's falsy, we populate it with the default// value@@ -642,9 +642,10 @@ function App() {let modifiedUpdateInstruction = updateInstruction;let selectedElementHtml: string | undefined;- // Send in a reference to the selected element if it exists+ // Send in a reference to the selected element if it exists. Selection+ // visuals are overlays, so the element's outerHTML is already clean.if (selectedElement) {- const elementHtml = removeHighlight(selectedElement).outerHTML;+ const elementHtml = selectedElement.outerHTML;selectedElementHtml = elementHtml;modifiedUpdateInstruction = buildSelectedElementInstruction(updateInstruction,diff --git a/frontend/src/components/preview/PreviewComponent.tsx b/frontend/src/components/preview/PreviewComponent.tsxindex 0c62fb4c..04eb5b75 100644--- a/frontend/src/components/preview/PreviewComponent.tsx+++ b/frontend/src/components/preview/PreviewComponent.tsx@@ -2,14 +2,16 @@ import { useCallback, useEffect, useRef, useState } from "react";import classNames from "classnames";import useThrottle from "../../hooks/useThrottle";import { useAppStore } from "../../store/app-store";-import { addHighlight, removeHighlight } from "../select-and-edit/utils";import {applySelectModeCursor,hideHoverOverlay,+ hideSelectionOverlay,removeHoverOverlay,removeSelectModeCursor,+ removeSelectionOverlay,showHoverOverlay,-} from "../select-and-edit/hoverOverlay";+ showSelectionOverlay,+} from "../select-and-edit/overlays";interface Props {code: string;@@ -83,7 +85,7 @@ function PreviewComponent({hideHoverOverlay((event.target as HTMLElement)?.ownerDocument);}, []);- // Keep the ring glued to the hovered element while the page scrolls or+ // Keep the rings glued to their elements while the page scrolls or// resizes under a stationary cursor.const handleIframeReposition = useCallback(() => {if (!inSelectAndEditModeRef.current) return;@@ -91,6 +93,19 @@ function PreviewComponent({if (hovered && hovered.isConnected) {showHoverOverlay(hovered);}+ const selected = useAppStore.getState().selectedElement;+ if (selected && selected.isConnected) {+ showSelectionOverlay(selected);+ }+ }, []);++ // Escape exits select mode even when focus is inside the iframe.+ const handleIframeKeyDown = useCallback((event: KeyboardEvent) => {+ if (!inSelectAndEditModeRef.current) return;+ if (event.key !== "Escape") return;+ event.preventDefault();+ event.stopPropagation();+ useAppStore.getState().disableInSelectAndEditMode();}, []);const {@@ -113,18 +128,22 @@ function PreviewComponent({const targetElement = clickEvent.target as HTMLElement;if (!targetElement) return;- // Remove highlight from previous element- if (selectedElement) {- removeHighlight(selectedElement);- }-- // Highlight and store the new selected element- addHighlight(targetElement);setSelectedElement(targetElement);- }, [clickEvent, setSelectedElement]); // eslint-disable-line react-hooks/exhaustive-deps+ }, [clickEvent, setSelectedElement]);++ // Render the selection ring for whatever element is currently selected+ // (clearing it when the selection is cleared from anywhere, e.g. the+ // sidebar's X button or after submitting an edit).+ useEffect(() => {+ if (selectedElement && selectedElement.isConnected) {+ showSelectionOverlay(selectedElement);+ return;+ }+ hideSelectionOverlay(iframeRef.current?.contentWindow?.document);+ }, [selectedElement]);- // Apply/remove select-mode side effects (cursor, hover ring, highlight)- // when the mode toggles.+ // Apply/remove select-mode side effects (cursor, hover and selection+ // rings) when the mode toggles.useEffect(() => {const doc = iframeRef.current?.contentWindow?.document;if (inSelectAndEditMode) {@@ -132,11 +151,11 @@ function PreviewComponent({return;}if (selectedElement) {- removeHighlight(selectedElement);setSelectedElement(null);}hoveredElementRef.current = null;removeHoverOverlay(doc);+ removeSelectionOverlay(doc);removeSelectModeCursor(doc);}, [inSelectAndEditMode]); // eslint-disable-line react-hooks/exhaustive-deps@@ -206,6 +225,7 @@ function PreviewComponent({win.addEventListener("mouseout", handleIframeMouseOut, true);win.addEventListener("scroll", handleIframeReposition, true);win.addEventListener("resize", handleIframeReposition);+ win.addEventListener("keydown", handleIframeKeyDown, true);win.document.addEventListener("click", handleIframeLinkClick);// A reload replaces the document, so re-apply mode side effects.if (inSelectAndEditModeRef.current) {@@ -233,6 +253,7 @@ function PreviewComponent({win.removeEventListener("mouseout", handleIframeMouseOut, true);win.removeEventListener("scroll", handleIframeReposition, true);win.removeEventListener("resize", handleIframeReposition);+ win.removeEventListener("keydown", handleIframeKeyDown, true);win.document.removeEventListener("click", handleIframeLinkClick);}};@@ -243,6 +264,7 @@ function PreviewComponent({handleIframeMouseOver,handleIframeMouseOut,handleIframeReposition,+ handleIframeKeyDown,]);useEffect(() => {diff --git a/frontend/src/components/select-and-edit/hoverOverlay.ts b/frontend/src/components/select-and-edit/hoverOverlay.tsdeleted file mode 100644index 477b753a..00000000--- a/frontend/src/components/select-and-edit/hoverOverlay.ts+++ /dev/null@@ -1,95 +0,0 @@-// Devtools-style hover indicator for select-and-edit mode. Rendered as a-// pointer-events-none overlay (plus a crosshair cursor style) inside the-// preview iframe's document, so hovered elements are never mutated and a-// selected element's captured outerHTML stays clean. Both nodes attach-// outside <body>, which also keeps body-level selections clean.-const OVERLAY_ID = "__s2c-hover-overlay";-const LABEL_ID = "__s2c-hover-overlay-label";-const CURSOR_STYLE_ID = "__s2c-select-cursor";--function ensureOverlay(doc: Document): HTMLElement {- let overlay = doc.getElementById(OVERLAY_ID);- if (overlay) return overlay;-- overlay = doc.createElement("div");- overlay.id = OVERLAY_ID;- Object.assign(overlay.style, {- position: "fixed",- top: "0",- left: "0",- width: "0",- height: "0",- pointerEvents: "none",- zIndex: "2147483646",- border: "1.5px solid rgba(124, 58, 237, 0.95)",- borderRadius: "4px",- background: "rgba(139, 92, 246, 0.09)",- boxShadow: "0 0 0 3px rgba(139, 92, 246, 0.15)",- transition:- "top 90ms ease-out, left 90ms ease-out, width 90ms ease-out, height 90ms ease-out",- display: "none",- });-- const label = doc.createElement("div");- label.id = LABEL_ID;- Object.assign(label.style, {- position: "absolute",- left: "-1.5px",- padding: "2px 7px",- background: "rgb(124, 58, 237)",- color: "#fff",- font: "600 11px/1.4 ui-monospace, SFMono-Regular, Menlo, monospace",- borderRadius: "4px",- whiteSpace: "nowrap",- boxShadow: "0 1px 4px rgba(0, 0, 0, 0.25)",- });- overlay.appendChild(label);-- // Outside <body> so a body-level selection never captures the overlay.- doc.documentElement.appendChild(overlay);- return overlay;-}--export function showHoverOverlay(element: HTMLElement) {- const doc = element.ownerDocument;- if (!doc || !doc.documentElement) return;- if (element === doc.documentElement || element.id === OVERLAY_ID) return;-- const overlay = ensureOverlay(doc);- const rect = element.getBoundingClientRect();- overlay.style.display = "block";- overlay.style.top = `${rect.top}px`;- overlay.style.left = `${rect.left}px`;- overlay.style.width = `${rect.width}px`;- overlay.style.height = `${rect.height}px`;-- const label = doc.getElementById(LABEL_ID);- if (label) {- label.textContent = `<${element.tagName.toLowerCase()}>`;- // Flip the label inside the box when the element touches the top edge.- label.style.top = rect.top > 26 ? "-24px" : "3px";- }-}--export function hideHoverOverlay(doc: Document | null | undefined) {- const overlay = doc?.getElementById(OVERLAY_ID);- if (overlay) overlay.style.display = "none";-}--export function removeHoverOverlay(doc: Document | null | undefined) {- doc?.getElementById(OVERLAY_ID)?.remove();-}--// Crosshair cursor everywhere in the page while selecting, overriding-// per-element cursors (buttons, links) that would otherwise flicker.-export function applySelectModeCursor(doc: Document | null | undefined) {- if (!doc || doc.getElementById(CURSOR_STYLE_ID)) return;- const style = doc.createElement("style");- style.id = CURSOR_STYLE_ID;- style.textContent = "* { cursor: crosshair !important; }";- (doc.head ?? doc.documentElement)?.appendChild(style);-}--export function removeSelectModeCursor(doc: Document | null | undefined) {- doc?.getElementById(CURSOR_STYLE_ID)?.remove();-}diff --git a/frontend/src/components/select-and-edit/overlays.ts b/frontend/src/components/select-and-edit/overlays.tsnew file mode 100644index 00000000..c63b4683--- /dev/null+++ b/frontend/src/components/select-and-edit/overlays.ts@@ -0,0 +1,162 @@+// Devtools-style indicators for select-and-edit mode, rendered as+// pointer-events-none overlays (plus a crosshair cursor style) inside the+// preview iframe's document, so page elements are never mutated and a+// selected element's captured outerHTML stays clean. All nodes attach+// outside <body>, which also keeps body-level selections clean.+//+// Two overlay kinds with deliberately distinct looks:+// - "hover": light ring + soft fill + tag chip — what you're pointing at+// - "selection": solid offset ring + stronger fill + "✓ tag" chip — what+// is locked in for the edit+const CURSOR_STYLE_ID = "__s2c-select-cursor";++type OverlayKind = "hover" | "selection";++const OVERLAY_IDS: Record<OverlayKind, string> = {+ hover: "__s2c-hover-overlay",+ selection: "__s2c-selection-overlay",+};++// The selection ring sits 3px outside the element so the two rings stay+// readable when the selected element is also hovered.+const SELECTION_INSET = 3;++const OVERLAY_STYLES: Record<OverlayKind, Partial<CSSStyleDeclaration>> = {+ hover: {+ zIndex: "2147483646",+ border: "1.5px solid rgba(124, 58, 237, 0.95)",+ background: "rgba(139, 92, 246, 0.09)",+ boxShadow: "0 0 0 3px rgba(139, 92, 246, 0.15)",+ },+ selection: {+ zIndex: "2147483645",+ border: "2.5px solid rgb(109, 40, 217)",+ background: "rgba(124, 58, 237, 0.16)",+ boxShadow:+ "0 0 0 2px rgba(255, 255, 255, 0.9), 0 2px 12px rgba(109, 40, 217, 0.45)",+ },+};++const LABEL_STYLES: Record<OverlayKind, Partial<CSSStyleDeclaration>> = {+ hover: {+ background: "rgb(124, 58, 237)",+ },+ selection: {+ background: "rgb(91, 33, 182)",+ },+};++function ensureOverlay(doc: Document, kind: OverlayKind): HTMLElement {+ let overlay = doc.getElementById(OVERLAY_IDS[kind]);+ if (overlay) return overlay;++ overlay = doc.createElement("div");+ overlay.id = OVERLAY_IDS[kind];+ Object.assign(overlay.style, {+ position: "fixed",+ top: "0",+ left: "0",+ width: "0",+ height: "0",+ pointerEvents: "none",+ borderRadius: "4px",+ transition:+ "top 90ms ease-out, left 90ms ease-out, width 90ms ease-out, height 90ms ease-out",+ display: "none",+ ...OVERLAY_STYLES[kind],+ });++ const label = doc.createElement("div");+ Object.assign(label.style, {+ position: "absolute",+ left: "-2px",+ padding: "2px 7px",+ color: "#fff",+ font: "600 11px/1.4 ui-monospace, SFMono-Regular, Menlo, monospace",+ borderRadius: "4px",+ whiteSpace: "nowrap",+ boxShadow: "0 1px 4px rgba(0, 0, 0, 0.25)",+ ...LABEL_STYLES[kind],+ });+ overlay.appendChild(label);++ // Outside <body> so a body-level selection never captures the overlay.+ doc.documentElement.appendChild(overlay);+ return overlay;+}++function showOverlay(element: HTMLElement, kind: OverlayKind) {+ const doc = element.ownerDocument;+ if (!doc || !doc.documentElement) return;+ if (+ element === doc.documentElement ||+ element.id === OVERLAY_IDS.hover ||+ element.id === OVERLAY_IDS.selection+ ) {+ return;+ }++ const overlay = ensureOverlay(doc, kind);+ const rect = element.getBoundingClientRect();+ const inset = kind === "selection" ? SELECTION_INSET : 0;+ overlay.style.display = "block";+ overlay.style.top = `${rect.top - inset}px`;+ overlay.style.left = `${rect.left - inset}px`;+ overlay.style.width = `${rect.width + inset * 2}px`;+ overlay.style.height = `${rect.height + inset * 2}px`;++ const label = overlay.firstChild as HTMLElement | null;+ if (label) {+ const tag = `<${element.tagName.toLowerCase()}>`;+ label.textContent = kind === "selection" ? `✓ ${tag}` : tag;+ // Flip the label inside the box when the element touches the top edge.+ label.style.top = rect.top - inset > 26 ? "-24px" : "3px";+ }+}++function hideOverlay(doc: Document | null | undefined, kind: OverlayKind) {+ const overlay = doc?.getElementById(OVERLAY_IDS[kind]);+ if (overlay) overlay.style.display = "none";+}++function removeOverlay(doc: Document | null | undefined, kind: OverlayKind) {+ doc?.getElementById(OVERLAY_IDS[kind])?.remove();+}++export function showHoverOverlay(element: HTMLElement) {+ showOverlay(element, "hover");+}++export function hideHoverOverlay(doc: Document | null | undefined) {+ hideOverlay(doc, "hover");+}++export function removeHoverOverlay(doc: Document | null | undefined) {+ removeOverlay(doc, "hover");+}++export function showSelectionOverlay(element: HTMLElement) {+ showOverlay(element, "selection");+}++export function hideSelectionOverlay(doc: Document | null | undefined) {+ hideOverlay(doc, "selection");+}++export function removeSelectionOverlay(doc: Document | null | undefined) {+ removeOverlay(doc, "selection");+}++// Crosshair cursor everywhere in the page while selecting, overriding+// per-element cursors (buttons, links) that would otherwise flicker.+export function applySelectModeCursor(doc: Document | null | undefined) {+ if (!doc || doc.getElementById(CURSOR_STYLE_ID)) return;+ const style = doc.createElement("style");+ style.id = CURSOR_STYLE_ID;+ style.textContent = "* { cursor: crosshair !important; }";+ (doc.head ?? doc.documentElement)?.appendChild(style);+}++export function removeSelectModeCursor(doc: Document | null | undefined) {+ doc?.getElementById(CURSOR_STYLE_ID)?.remove();+}diff --git a/frontend/src/components/select-and-edit/useEscapeToExitSelectMode.ts b/frontend/src/components/select-and-edit/useEscapeToExitSelectMode.tsnew file mode 100644index 00000000..4e14a227--- /dev/null+++ b/frontend/src/components/select-and-edit/useEscapeToExitSelectMode.ts@@ -0,0 +1,18 @@+import { useEffect } from "react";+import { useAppStore } from "../../store/app-store";++// Escape exits select-and-edit mode. Key presses inside the preview iframe+// are handled in PreviewComponent; this covers the rest of the app.+export function useEscapeToExitSelectMode() {+ useEffect(() => {+ const onKeyDown = (event: KeyboardEvent) => {+ if (event.key !== "Escape") return;+ const store = useAppStore.getState();+ if (store.inSelectAndEditMode) {+ store.disableInSelectAndEditMode();+ }+ };+ window.addEventListener("keydown", onKeyDown);+ return () => window.removeEventListener("keydown", onKeyDown);+ }, []);+}diff --git a/frontend/src/components/select-and-edit/utils.ts b/frontend/src/components/select-and-edit/utils.tsindex a0ddb68f..bbc0232d 100644--- a/frontend/src/components/select-and-edit/utils.ts+++ b/frontend/src/components/select-and-edit/utils.ts@@ -2,29 +2,6 @@// full current code, so the snippet only needs to identify the element.const MAX_ELEMENT_HTML_LENGTH = 12000;-export function removeHighlight(element: HTMLElement) {- element.style.outline = element.dataset.s2cPrevOutline ?? "";- element.style.backgroundColor = element.dataset.s2cPrevBackgroundColor ?? "";- delete element.dataset.s2cPrevOutline;- delete element.dataset.s2cPrevBackgroundColor;- // Avoid leaving an empty style attribute behind so the captured outerHTML- // matches the source markup as closely as possible.- if (!element.getAttribute("style")) {- element.removeAttribute("style");- }- return element;-}--export function addHighlight(element: HTMLElement) {- if (element.dataset.s2cPrevOutline === undefined) {- element.dataset.s2cPrevOutline = element.style.outline;- element.dataset.s2cPrevBackgroundColor = element.style.backgroundColor;- }- element.style.outline = "2px dashed #1846db";- element.style.backgroundColor = "#bfcbf5";- return element;-}-export function buildSelectedElementInstruction(instruction: string,elementHtml: stringdiff --git a/frontend/src/components/sidebar/Sidebar.tsx b/frontend/src/components/sidebar/Sidebar.tsxindex 984d0731..4555b9df 100644--- a/frontend/src/components/sidebar/Sidebar.tsx+++ b/frontend/src/components/sidebar/Sidebar.tsx@@ -12,7 +12,6 @@ import AgentActivity from "../agent/AgentActivity";import WorkingPulse from "../core/WorkingPulse";import ImageLightbox from "../ImageLightbox";import { Commit } from "../commits/types";-import { removeHighlight } from "../select-and-edit/utils";import { CodeGenerationModel } from "../../lib/models";import DesignSystemSelector, {DesignSystemSelectorProps,@@ -497,10 +496,7 @@ function Sidebar({</span></div><button- onClick={() => {- removeHighlight(selectedElement);- setSelectedElement(null);- }}+ onClick={() => setSelectedElement(null)}className="shrink-0 ml-3 p-0.5 text-violet-400 hover:text-violet-700 dark:hover:text-violet-200 transition-colors"title="Clear selection">