fix(web): keep floating preview anchored after panel closes (#6547)
This commit is contained in:
1 parent
9bdd912933
commit
6e6d1b4941
1 file changed
+24
-7
@@ -2,7 +2,7 @@
|
||||
|
||||
import type { ScopedThreadRef } from "@t3tools/contracts";
|
||||
import { PanelRightIcon, PictureInPicture2, XIcon } from "lucide-react";
|
||||
import { type PointerEvent as ReactPointerEvent, useLayoutEffect, useRef } from "react";
|
||||
import { type PointerEvent as ReactPointerEvent, useLayoutEffect, useRef, useState } from "react";
|
||||
|
||||
import { BrowserSurfaceSlot } from "~/browser/BrowserSurfaceSlot";
|
||||
import { previewRuntimeTabId } from "~/browser/previewRuntimeTabId";
|
||||
@@ -17,6 +17,7 @@ import {
|
||||
clampPreviewMiniPlayerPosition,
|
||||
clampPreviewMiniPlayerSize,
|
||||
PREVIEW_MINI_PLAYER_DEFAULT_SIZE,
|
||||
PREVIEW_MINI_PLAYER_EDGE_GAP,
|
||||
} from "./previewMiniPlayerLayout";
|
||||
|
||||
interface DragState {
|
||||
@@ -31,6 +32,8 @@ interface ResizeState {
|
||||
readonly pointerId: number;
|
||||
readonly pointerX: number;
|
||||
readonly pointerY: number;
|
||||
readonly playerX: number;
|
||||
readonly playerY: number;
|
||||
readonly width: number;
|
||||
readonly height: number;
|
||||
}
|
||||
@@ -45,6 +48,7 @@ export function ThreadPreviewMiniPlayer({ threadRef, tabId, bottomInset }: Props
|
||||
const rootRef = useRef<HTMLElement | null>(null);
|
||||
const dragRef = useRef<DragState | null>(null);
|
||||
const resizeRef = useRef<ResizeState | null>(null);
|
||||
const [defaultLayoutVersion, setDefaultLayoutVersion] = useState("");
|
||||
const miniPlayer = usePreviewMiniPlayerStore((state) =>
|
||||
selectThreadPreviewMiniPlayer(state.byThreadKey, threadRef),
|
||||
);
|
||||
@@ -91,8 +95,12 @@ export function ThreadPreviewMiniPlayer({ threadRef, tabId, bottomInset }: Props
|
||||
bottomInset,
|
||||
);
|
||||
usePreviewMiniPlayerStore.getState().resize(threadRef, tabId, nextSize);
|
||||
if (!position) {
|
||||
setDefaultLayoutVersion(`${parent.clientWidth}:${parent.clientHeight}`);
|
||||
return;
|
||||
}
|
||||
const next = clampPreviewMiniPlayerPosition(
|
||||
position ?? { x: root.offsetLeft, y: root.offsetTop },
|
||||
position,
|
||||
{ width: parent.clientWidth, height: parent.clientHeight },
|
||||
nextSize,
|
||||
bottomInset,
|
||||
@@ -159,11 +167,16 @@ export function ThreadPreviewMiniPlayer({ threadRef, tabId, bottomInset }: Props
|
||||
const handleResizePointerDown = (event: ReactPointerEvent<HTMLButtonElement>) => {
|
||||
if (event.button !== 0) return;
|
||||
const root = rootRef.current;
|
||||
if (!root) return;
|
||||
const parent = root?.offsetParent;
|
||||
if (!root || !(parent instanceof HTMLElement)) return;
|
||||
const rootRect = root.getBoundingClientRect();
|
||||
const parentRect = parent.getBoundingClientRect();
|
||||
resizeRef.current = {
|
||||
pointerId: event.pointerId,
|
||||
pointerX: event.clientX,
|
||||
pointerY: event.clientY,
|
||||
playerX: rootRect.left - parentRect.left,
|
||||
playerY: rootRect.top - parentRect.top,
|
||||
width: root.offsetWidth,
|
||||
height: root.offsetHeight,
|
||||
};
|
||||
@@ -194,7 +207,7 @@ export function ThreadPreviewMiniPlayer({ threadRef, tabId, bottomInset }: Props
|
||||
);
|
||||
usePreviewMiniPlayerStore.getState().resize(threadRef, tabId, nextSize);
|
||||
const nextPosition = clampPreviewMiniPlayerPosition(
|
||||
position ?? { x: root.offsetLeft, y: root.offsetTop },
|
||||
{ x: resize.playerX, y: resize.playerY },
|
||||
{ width: parent.clientWidth, height: parent.clientHeight },
|
||||
nextSize,
|
||||
bottomInset,
|
||||
@@ -222,8 +235,8 @@ export function ThreadPreviewMiniPlayer({ threadRef, tabId, bottomInset }: Props
|
||||
position
|
||||
? { left: position.x, top: position.y, width: size.width, height: size.height }
|
||||
: {
|
||||
right: 16,
|
||||
top: 16,
|
||||
right: PREVIEW_MINI_PLAYER_EDGE_GAP,
|
||||
top: PREVIEW_MINI_PLAYER_EDGE_GAP,
|
||||
width: size.width,
|
||||
height: size.height,
|
||||
}
|
||||
@@ -290,7 +303,11 @@ export function ThreadPreviewMiniPlayer({ threadRef, tabId, bottomInset }: Props
|
||||
visible={Boolean(desktopOverlay?.hasWebContents)}
|
||||
cornerRadius={12}
|
||||
fitSourceContent
|
||||
layoutVersion={position ? `${position.x}:${position.y}` : `initial:${bottomInset}`}
|
||||
layoutVersion={
|
||||
position
|
||||
? `${position.x}:${position.y}`
|
||||
: `initial:${bottomInset}:${defaultLayoutVersion}`
|
||||
}
|
||||
className="absolute inset-0"
|
||||
/>
|
||||
<div className="pointer-events-none absolute inset-0 z-[31] rounded-xl ring-1 ring-inset ring-border/80" />
|
||||
|
||||
Reference in new issue
Block a user