Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

Studio: reopen the artifact panel after it has been dragged shut (#12278)

Daniel Han0cc9ed2d3a2f
diff --git a/studio/frontend/src/features/chat/artifacts/artifact-card.tsx b/studio/frontend/src/features/chat/artifacts/artifact-card.tsx
index 432905de3f5..d6be2f4d147 100644
--- a/studio/frontend/src/features/chat/artifacts/artifact-card.tsx
+++ b/studio/frontend/src/features/chat/artifacts/artifact-card.tsx
@@ -60,6 +60,10 @@ export function ArtifactCard({
const selectedArtifactId = useChatArtifactsStore(
(state) => state.selectedArtifactId,
);
+ const openView = useChatArtifactsStore((state) => state.requestedView);
+ const closeArtifactSurface = useChatArtifactsStore(
+ (state) => state.closeArtifactSurface,
+ );
const artifact = useMemo<ChatArtifact>(
() =>
createChatArtifact({
@@ -112,6 +116,10 @@ export function ArtifactCard({
const renderButton = (view: ArtifactViewMode) => {
const isCode = view === "source";
+ const showing =
+ surface === "panel" &&
+ selectedArtifactId === artifact.id &&
+ openView === view;
return (
<button
key={view}
@@ -122,7 +130,15 @@ export function ArtifactCard({
isStreaming &&
"border-border/80 bg-muted/20 dark:border-border/70 dark:bg-muted/15",
)}
- onClick={() => openArtifact(artifact, { surface, view })}
+ onClick={() => {
+ if (showing) {
+ closeArtifactSurface();
+ return;
+ }
+ openArtifact(artifact, { surface, view });
+ }}
+ // State rides on aria-expanded, not the name: the startup-bundle harness counts cards by name.
+ aria-expanded={showing}
aria-label={`Open ${artifact.title} ${isCode ? "code" : "preview"}`}
>
{isStreaming ? (
diff --git a/studio/frontend/src/features/chat/artifacts/artifact-surface.tsx b/studio/frontend/src/features/chat/artifacts/artifact-surface.tsx
index de9bc9a4d4e..48b1918094c 100644
--- a/studio/frontend/src/features/chat/artifacts/artifact-surface.tsx
+++ b/studio/frontend/src/features/chat/artifacts/artifact-surface.tsx
@@ -111,6 +111,9 @@ export function ArtifactSurface({
const [viewMode, setViewMode] = useState<ArtifactViewMode>("preview");
// Follow the view the opener asked for (Preview vs Code button), per artifact.
const requestedView = useChatArtifactsStore((state) => state.requestedView);
+ const setArtifactView = useChatArtifactsStore(
+ (state) => state.setArtifactView,
+ );
const [copied, setCopied] = useState(false);
const copyResetRef = useRef<ReturnType<typeof setTimeout> | null>(null);
const surfaceRef = useRef<HTMLElement>(null);
@@ -242,7 +245,11 @@ export function ArtifactSurface({
type="button"
role="tab"
disabled={isLoadingArtifact && !isPreview}
- onClick={() => setViewMode(mode)}
+ onClick={() => {
+ setViewMode(mode);
+ // Keeps the card's open/hide toggle in step with the tab on screen.
+ setArtifactView(mode);
+ }}
className={cn(
"flex size-8 items-center justify-center rounded-full text-muted-foreground transition-colors",
effectiveViewMode === mode
diff --git a/studio/frontend/src/features/chat/artifacts/store.ts b/studio/frontend/src/features/chat/artifacts/store.ts
index 3a078add822..9a0649ce520 100644
--- a/studio/frontend/src/features/chat/artifacts/store.ts
+++ b/studio/frontend/src/features/chat/artifacts/store.ts
@@ -22,6 +22,8 @@ export function clearAutoOpenedArtifacts(): void {
type ChatArtifactsState = {
artifactsById: Record<string, ChatArtifact>;
selectedArtifactId: string | null;
+ // Bumped on every open, so the panel can reopen after being dragged shut.
+ openSequence: number;
surface: ChatArtifactSurface;
// View the surface should show on the next open (Preview vs Code button).
requestedView: ArtifactViewMode;
@@ -31,6 +33,7 @@ type ChatArtifactsState = {
) => void;
updateArtifact: (artifact: ChatArtifact) => void;
closeArtifactSurface: () => void;
+ setArtifactView: (view: ArtifactViewMode) => void;
clearArtifactsForThread: (threadId: string | null | undefined) => void;
clearOrphanedArtifacts: () => void;
resetArtifacts: () => void;
@@ -39,6 +42,7 @@ type ChatArtifactsState = {
export const useChatArtifactsStore = create<ChatArtifactsState>((set) => ({
artifactsById: {},
selectedArtifactId: null,
+ openSequence: 0,
surface: "panel",
requestedView: "preview",
openArtifact: (artifact, options) =>
@@ -48,6 +52,7 @@ export const useChatArtifactsStore = create<ChatArtifactsState>((set) => ({
[artifact.id]: artifact,
},
selectedArtifactId: artifact.id,
+ openSequence: state.openSequence + 1,
surface: options?.surface ?? state.surface,
requestedView: options?.view ?? "preview",
})),
@@ -64,6 +69,7 @@ export const useChatArtifactsStore = create<ChatArtifactsState>((set) => ({
),
closeArtifactSurface: () =>
set({ selectedArtifactId: null, surface: "panel" }),
+ setArtifactView: (view) => set({ requestedView: view }),
clearArtifactsForThread: (threadId) =>
set((state) => {
if (!threadId) return state;
diff --git a/studio/frontend/src/features/chat/chat-page.tsx b/studio/frontend/src/features/chat/chat-page.tsx
index 7441f2ffe1d..45fd00d7aa5 100644
--- a/studio/frontend/src/features/chat/chat-page.tsx
+++ b/studio/frontend/src/features/chat/chat-page.tsx
@@ -382,6 +382,18 @@ const SingleContent = memo(function SingleContent({
: undefined,
);
const artifactPanelRef = useRef<PanelImperativeHandle | null>(null);
+ // Sampled on drag end, not onResize: that fires through the open/close animations too.
+ const artifactPanelWidthRef = useRef<string | null>(null);
+ const rememberArtifactPanelWidth = useCallback(() => {
+ const size = artifactPanelRef.current?.getSize().asPercentage;
+ if (size == null) return;
+ // A drag shut is a close; a zero-width panel still holding the artifact would make the next card click hide it.
+ if (size <= 5) {
+ onCloseArtifact();
+ return;
+ }
+ artifactPanelWidthRef.current = `${size}%`;
+ }, [onCloseArtifact]);
const hasInitializedArtifactPanelRef = useRef(false);
const [isArtifactLayoutAnimating, setIsArtifactLayoutAnimating] =
useState(false);
@@ -410,6 +422,25 @@ const SingleContent = memo(function SingleContent({
isArtifactPanelLayoutActive &&
!isArtifactLayoutAnimating;
+ const artifactPanelThread = threadId ?? activeThreadId ?? null;
+ // biome-ignore lint/correctness/useExhaustiveDependencies: resetting is the effect
+ useEffect(() => {
+ artifactPanelWidthRef.current = null;
+ }, [artifactPanelThread]);
+
+ const artifactOpenSequence = useChatArtifactsStore(
+ (state) => state.openSequence,
+ );
+ useEffect(() => {
+ if (!showContextPanel || artifactOpenSequence === 0) return;
+ const panel = artifactPanelRef.current;
+ if (!panel) return;
+ if (!panel.isCollapsed() && panel.getSize().asPercentage > 5) return;
+ // expand() alone restores the pre-collapse width, which is zero after a drag shut.
+ panel.expand();
+ panel.resize(artifactPanelWidthRef.current ?? ARTIFACT_PANEL_DEFAULT_SIZE);
+ }, [artifactOpenSequence, showContextPanel]);
+
useEffect(() => {
const panel = artifactPanelRef.current;
if (!panel) return;
@@ -429,7 +460,11 @@ const SingleContent = memo(function SingleContent({
let resizeFrameId = 0;
const prepFrameId = window.requestAnimationFrame(() => {
resizeFrameId = window.requestAnimationFrame(() => {
- panel.resize(showContextPanel ? ARTIFACT_PANEL_DEFAULT_SIZE : "0%");
+ panel.resize(
+ showContextPanel
+ ? (artifactPanelWidthRef.current ?? ARTIFACT_PANEL_DEFAULT_SIZE)
+ : "0%",
+ );
});
});
const surfaceTimerId = showContextPanel
@@ -488,6 +523,14 @@ const SingleContent = memo(function SingleContent({
</ResizablePanel>
<ResizableHandle
withHandle={false}
+ // The library's double-click reset would shut the panel without closing the artifact.
+ disableDoubleClick
+ onPointerDown={() => {
+ window.addEventListener("pointerup", rememberArtifactPanelWidth, {
+ once: true,
+ });
+ }}
+ onKeyUp={rememberArtifactPanelWidth}
className={cn(
"relative z-30 -ml-1 -mr-4 w-5 bg-transparent transition-[width,margin] duration-[260ms] ease-[var(--ease-out-cubic)] hover:bg-transparent hover:shadow-none active:bg-transparent active:shadow-none focus-visible:bg-transparent focus-visible:shadow-none focus-visible:ring-0 focus-visible:ring-offset-0 focus-visible:outline-none",
!artifactLayoutActive &&
diff --git a/studio/frontend/tests/artifact-panel-reopen.test.ts b/studio/frontend/tests/artifact-panel-reopen.test.ts
new file mode 100644
index 00000000000..7df53e90d19
--- /dev/null
+++ b/studio/frontend/tests/artifact-panel-reopen.test.ts
@@ -0,0 +1,55 @@
+// SPDX-License-Identifier: AGPL-3.0-only
+// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+import assert from "node:assert/strict";
+import { readFileSync } from "node:fs";
+import test from "node:test";
+import { fileURLToPath } from "node:url";
+
+import { useChatArtifactsStore } from "../src/features/chat/artifacts/store.ts";
+
+const read = (path: string) =>
+ readFileSync(fileURLToPath(new URL(path, import.meta.url)), "utf8");
+
+const artifact = {
+ id: "a1",
+ title: "Board",
+ code: "<p>hi</p>",
+} as Parameters<ReturnType<typeof useChatArtifactsStore.getState>["openArtifact"]>[0];
+
+test("every open bumps the sequence the panel reopens on, even for the artifact on screen", () => {
+ const store = useChatArtifactsStore.getState();
+ const before = store.openSequence;
+ store.openArtifact(artifact, { surface: "panel", view: "preview" });
+ store.openArtifact(artifact, { surface: "panel", view: "preview" });
+ assert.equal(useChatArtifactsStore.getState().openSequence, before + 2);
+});
+
+test("switching tabs in the surface updates the view the card compares against", () => {
+ const store = useChatArtifactsStore.getState();
+ store.openArtifact(artifact, { surface: "panel", view: "preview" });
+ store.setArtifactView("source");
+ assert.equal(useChatArtifactsStore.getState().requestedView, "source");
+});
+
+test("a panel dragged shut is reported closed, not left selected at no width", () => {
+ const page = read("../src/features/chat/chat-page.tsx");
+ const remember = page.indexOf("const rememberArtifactPanelWidth");
+ assert.ok(remember > 0);
+ const closeAt = page.indexOf("onCloseArtifact();", remember);
+ const widthAt = page.indexOf("artifactPanelWidthRef.current = `", remember);
+ assert.ok(closeAt > 0 && closeAt < widthAt, "a shut panel still records a width");
+});
+
+test("the card hides the panel it is already showing and says so on aria-expanded", () => {
+ const card = read("../src/features/chat/artifacts/artifact-card.tsx");
+ assert.match(card, /if \(showing\) \{\s*closeArtifactSurface\(\);/);
+ assert.match(card, /aria-expanded=\{showing\}/);
+});
+
+test("the panel's resize handle opts out of the double-click reset", () => {
+ const page = read("../src/features/chat/chat-page.tsx");
+ const handle = page.indexOf("<ResizableHandle", page.indexOf("const rememberArtifactPanelWidth"));
+ assert.ok(handle > 0);
+ assert.match(page.slice(handle, handle + 300), /disableDoubleClick/);
+});