Open Anooj-Pai opened 1 year ago
More ideas from GPT4:
To add drag-and-drop reordering for the tabs, we can use the HTML Drag and Drop API. Below are the steps to implement this feature:
draggable="true"
to the Tab
component.onDragStart
, onDragOver
, and onDrop
.tabList
array.Here's how you can modify the provided code to support drag & drop reordering:
// Inside TabList component:
const [draggedTabIndex, setDraggedTabIndex] = useState<number | null>(null);
const handleDragStart = (index: number) => {
setDraggedTabIndex(index);
};
const handleDrop = (index: number) => {
if (draggedTabIndex !== null && draggedTabIndex !== index) {
const newTabList = [...tabList];
const [removed] = newTabList.splice(draggedTabIndex, 1);
newTabList.splice(index, 0, removed);
setTabList(newTabList);
}
setDraggedTabIndex(null);
};
return (
<div className="vz-tab-list">
{files &&
tabList.map((fileId: FileId, index: number) => (
<Tab
key={fileId}
fileId={fileId}
index={index}
onDragStart={() => handleDragStart(index)}
onDrop={() => handleDrop(index)}
isActive={fileId === activeFileId}
setActiveFileId={setActiveFileId}
closeTabs={closeTabs}
fileName={files[fileId].name}
/>
))}
</div>
);
// Inside Tab component:
const { index, onDragStart, onDrop } = props;
return (
<div
className={isActive ? 'tab active' : 'tab'}
onClick={() => {
setActiveFileId(fileId);
}}
draggable="true"
onDragStart={onDragStart}
onDragOver={(e) => e.preventDefault()}
onDrop={onDrop}
>
{tabName}
<div
className={'bx bx-x tab-close'}
onClick={handleCloseClick}
></div>
</div>
);
Note:
tabList
is a state in the TabList
component. If it is passed down as props, you might need to lift the state to a higher component or use context to handle reordering.onDragEnd
event.react-dnd
.TODO drop a video of what VSCode does
Some ideas from AI: