feat(studio): auto-reveal source when selecting elements in Code tab

When the Code tab is active and a user clicks an element in the preview,
the code editor now auto-scrolls to the corresponding HTML source. This
removes the need for Alt+click — the existing click-to-source mechanism
fires automatically when the Code tab is already open.

- Add getTab() to LeftSidebarHandle for reading the active tab
- Add getSidebarTab getter to useDomEditSession
- Add effect that calls openSourceForSelection on selection change
  when Code tab is active
This commit is contained in:
Miguel Ángel
2026-05-21 15:25:51 -04:00
parent afa6530a10
commit e2de547a28
4 changed files with 40 additions and 3 deletions
@@ -3,6 +3,7 @@ import {
useState,
useCallback,
useImperativeHandle,
useRef,
forwardRef,
type ReactNode,
} from "react";
@@ -17,6 +18,7 @@ export type SidebarTab = "compositions" | "assets" | "code" | "blocks";
export interface LeftSidebarHandle {
selectTab: (tab: SidebarTab) => void;
getTab: () => SidebarTab;
}
const STORAGE_KEY = "hf-studio-sidebar-tab";
@@ -87,6 +89,8 @@ export const LeftSidebar = memo(
ref,
) {
const [tab, setTab] = useState<SidebarTab>(getPersistedTab);
const tabRef = useRef(tab);
tabRef.current = tab;
const selectTab = useCallback((t: SidebarTab) => {
setTab(t);
@@ -94,7 +98,9 @@ export const LeftSidebar = memo(
trackStudioEvent("tab_switch", { panel: "left_sidebar", tab: t });
}, []);
useImperativeHandle(ref, () => ({ selectTab }), [selectTab]);
const getTab = useCallback(() => tabRef.current, []);
useImperativeHandle(ref, () => ({ selectTab, getTab }), [selectTab, getTab]);
return (
<div