Skip to content

Commit

Permalink
fix(file-tab): close button (#1198)
Browse files Browse the repository at this point in the history
* fix(file-tab): close button

* fix

* fix
  • Loading branch information
danilowoz authored Sep 11, 2024
1 parent 292a63c commit 58d1f54
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 36 deletions.
35 changes: 12 additions & 23 deletions sandpack-react/src/Playground.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import React from "react";

import { Sandpack } from "./";
import {
SandpackCodeEditor,
SandpackFileExplorer,
SandpackLayout,
SandpackProvider,
} from "./";

export default {
title: "Intro/Playground",
Expand All @@ -10,28 +15,12 @@ export default {
export const Basic: React.FC = () => {
return (
<div style={{ height: "400vh" }}>
<Sandpack
options={{
showTabs: true,
closableTabs: true,
experimental_enableServiceWorker: true,
experimental_enableStableServiceWorkerId: true,
}}
// customSetup={{
// dependencies: {
// "react-content-loader": "latest",
// "radix-ui": "latest",
// "styled-components": "latest",
// "react-dom": "latest",
// react: "latest",
// "react-table": "latest",
// },
// }}
// options={{
// bundlerURL: "https://ymxnqs-3000.csb.app",
// }}
template="react"
/>
<SandpackProvider>
<SandpackLayout>
<SandpackFileExplorer />
<SandpackCodeEditor closableTabs showTabs />
</SandpackLayout>
</SandpackProvider>
</div>
);
};
18 changes: 5 additions & 13 deletions sandpack-react/src/components/FileTabs/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -96,18 +96,6 @@ export const FileTabs = ({
const { activeFile, visibleFiles, setActiveFile } = sandpack;
const [hoveredIndex, setIsHoveredIndex] = React.useState<null | number>(null);

const handleCloseFile = (ev: React.MouseEvent<HTMLDivElement>): void => {
ev.stopPropagation();
const tabElm = (ev.target as HTMLElement).closest(
"[data-active]"
) as HTMLElement;
const pathToClose = tabElm?.getAttribute("title");
if (!pathToClose) {
return;
}
sandpack.closeFile(pathToClose);
};

const getTriggerText = (currentPath: string): string => {
const documentFileName = getFileName(currentPath);

Expand Down Expand Up @@ -224,7 +212,11 @@ export const FileTabs = ({
{closableTabs && visibleFiles.length > 1 && (
<span
className={classNames("close-button", [closeButtonClassName])}
onClick={handleCloseFile}
onClick={(ev) => {
ev.stopPropagation();

sandpack.closeFile(filePath);
}}
style={{
visibility:
filePath === activeFile || hoveredIndex === index
Expand Down

0 comments on commit 58d1f54

Please sign in to comment.