2026-02-09 03:13:34 +02:00
|
|
|
import React from 'react';
|
|
|
|
|
import { cn } from '@/lib/utils';
|
|
|
|
|
import { useUIStore } from '@/stores/useUIStore';
|
|
|
|
|
|
|
|
|
|
const RIGHT_SIDEBAR_MIN_WIDTH = 400;
|
|
|
|
|
const RIGHT_SIDEBAR_MAX_WIDTH = 860;
|
|
|
|
|
|
|
|
|
|
interface RightSidebarProps {
|
|
|
|
|
isOpen: boolean;
|
|
|
|
|
children: React.ReactNode;
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-24 17:44:43 +08:00
|
|
|
export const RightSidebar: React.FC<RightSidebarProps> = ({ isOpen, children }) => {
|
2026-02-09 03:13:34 +02:00
|
|
|
const rightSidebarWidth = useUIStore((state) => state.rightSidebarWidth);
|
|
|
|
|
const setRightSidebarWidth = useUIStore((state) => state.setRightSidebarWidth);
|
|
|
|
|
const [isResizing, setIsResizing] = React.useState(false);
|
|
|
|
|
const startXRef = React.useRef(0);
|
|
|
|
|
const startWidthRef = React.useRef(rightSidebarWidth || 420);
|
2026-03-02 02:11:33 +02:00
|
|
|
const resizingWidthRef = React.useRef<number | null>(null);
|
|
|
|
|
const activeResizePointerIDRef = React.useRef<number | null>(null);
|
|
|
|
|
const sidebarRef = React.useRef<HTMLElement | null>(null);
|
2026-02-09 03:13:34 +02:00
|
|
|
|
2026-03-02 02:11:33 +02:00
|
|
|
const clampRightSidebarWidth = React.useCallback((value: number) => {
|
|
|
|
|
return Math.min(RIGHT_SIDEBAR_MAX_WIDTH, Math.max(RIGHT_SIDEBAR_MIN_WIDTH, value));
|
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
const applyLiveWidth = React.useCallback((nextWidth: number) => {
|
|
|
|
|
const sidebar = sidebarRef.current;
|
|
|
|
|
if (!sidebar) {
|
2026-02-09 03:13:34 +02:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-02 02:11:33 +02:00
|
|
|
sidebar.style.setProperty('--oc-right-sidebar-width', `${nextWidth}px`);
|
|
|
|
|
}, []);
|
2026-02-09 03:13:34 +02:00
|
|
|
|
|
|
|
|
const appliedWidth = isOpen
|
|
|
|
|
? Math.min(RIGHT_SIDEBAR_MAX_WIDTH, Math.max(RIGHT_SIDEBAR_MIN_WIDTH, rightSidebarWidth || 420))
|
|
|
|
|
: 0;
|
|
|
|
|
|
|
|
|
|
const handlePointerDown = (event: React.PointerEvent) => {
|
|
|
|
|
if (!isOpen) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2026-03-02 02:11:33 +02:00
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
event.currentTarget.setPointerCapture(event.pointerId);
|
|
|
|
|
} catch {
|
|
|
|
|
// ignore
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
activeResizePointerIDRef.current = event.pointerId;
|
2026-02-09 03:13:34 +02:00
|
|
|
setIsResizing(true);
|
|
|
|
|
startXRef.current = event.clientX;
|
|
|
|
|
startWidthRef.current = appliedWidth;
|
2026-03-02 02:11:33 +02:00
|
|
|
resizingWidthRef.current = appliedWidth;
|
|
|
|
|
applyLiveWidth(appliedWidth);
|
2026-02-09 03:13:34 +02:00
|
|
|
event.preventDefault();
|
|
|
|
|
};
|
|
|
|
|
|
2026-03-02 02:11:33 +02:00
|
|
|
const handlePointerMove = (event: React.PointerEvent) => {
|
|
|
|
|
if (!isResizing || activeResizePointerIDRef.current !== event.pointerId) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const delta = startXRef.current - event.clientX;
|
|
|
|
|
const nextWidth = clampRightSidebarWidth(startWidthRef.current + delta);
|
|
|
|
|
if (resizingWidthRef.current === nextWidth) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
resizingWidthRef.current = nextWidth;
|
|
|
|
|
applyLiveWidth(nextWidth);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const handlePointerEnd = (event: React.PointerEvent) => {
|
|
|
|
|
if (activeResizePointerIDRef.current !== event.pointerId) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
event.currentTarget.releasePointerCapture(event.pointerId);
|
|
|
|
|
} catch {
|
|
|
|
|
// ignore
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const finalWidth = clampRightSidebarWidth(resizingWidthRef.current ?? appliedWidth);
|
|
|
|
|
activeResizePointerIDRef.current = null;
|
|
|
|
|
resizingWidthRef.current = null;
|
|
|
|
|
setIsResizing(false);
|
|
|
|
|
setRightSidebarWidth(finalWidth);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
React.useEffect(() => {
|
|
|
|
|
if (!isResizing) {
|
|
|
|
|
resizingWidthRef.current = null;
|
|
|
|
|
activeResizePointerIDRef.current = null;
|
|
|
|
|
}
|
|
|
|
|
}, [isResizing]);
|
|
|
|
|
|
2026-02-09 03:13:34 +02:00
|
|
|
return (
|
|
|
|
|
<aside
|
2026-03-02 02:11:33 +02:00
|
|
|
ref={sidebarRef}
|
2026-02-09 03:13:34 +02:00
|
|
|
className={cn(
|
2026-02-25 14:32:23 +02:00
|
|
|
'relative flex h-full overflow-hidden border-l border-border/40 bg-sidebar/50',
|
2026-02-09 03:13:34 +02:00
|
|
|
isResizing ? 'transition-none' : 'transition-[width] duration-300 ease-in-out',
|
|
|
|
|
!isOpen && 'border-l-0'
|
|
|
|
|
)}
|
|
|
|
|
style={{
|
2026-03-02 02:11:33 +02:00
|
|
|
width: 'var(--oc-right-sidebar-width)',
|
|
|
|
|
minWidth: 'var(--oc-right-sidebar-width)',
|
|
|
|
|
maxWidth: 'var(--oc-right-sidebar-width)',
|
|
|
|
|
['--oc-right-sidebar-width' as string]: `${isResizing ? (resizingWidthRef.current ?? appliedWidth) : appliedWidth}px`,
|
2026-02-09 03:13:34 +02:00
|
|
|
overflowX: 'clip',
|
|
|
|
|
}}
|
|
|
|
|
aria-hidden={!isOpen || appliedWidth === 0}
|
|
|
|
|
>
|
|
|
|
|
{isOpen && (
|
|
|
|
|
<div
|
|
|
|
|
className={cn(
|
|
|
|
|
'absolute left-0 top-0 z-20 h-full w-[4px] cursor-col-resize hover:bg-primary/50 transition-colors',
|
|
|
|
|
isResizing && 'bg-primary'
|
|
|
|
|
)}
|
|
|
|
|
onPointerDown={handlePointerDown}
|
2026-03-02 02:11:33 +02:00
|
|
|
onPointerMove={handlePointerMove}
|
|
|
|
|
onPointerUp={handlePointerEnd}
|
|
|
|
|
onPointerCancel={handlePointerEnd}
|
2026-02-09 03:13:34 +02:00
|
|
|
role="separator"
|
|
|
|
|
aria-orientation="vertical"
|
|
|
|
|
aria-label="Resize right panel"
|
|
|
|
|
/>
|
|
|
|
|
)}
|
|
|
|
|
<div
|
|
|
|
|
className={cn(
|
|
|
|
|
'relative z-10 flex h-full min-h-0 w-full flex-col transition-opacity duration-300 ease-in-out',
|
2026-03-02 02:11:33 +02:00
|
|
|
isResizing && 'pointer-events-none',
|
2026-02-09 03:13:34 +02:00
|
|
|
!isOpen && 'pointer-events-none select-none opacity-0'
|
|
|
|
|
)}
|
|
|
|
|
aria-hidden={!isOpen}
|
|
|
|
|
>
|
|
|
|
|
{isOpen ? children : null}
|
|
|
|
|
</div>
|
|
|
|
|
</aside>
|
|
|
|
|
);
|
|
|
|
|
};
|