Skip to content

Commit

Permalink
fix(file-explorer): introduce initialCollapsedFolders (#756)
Browse files Browse the repository at this point in the history
  • Loading branch information
danilowoz authored Feb 22, 2023
1 parent f4235df commit 27b075e
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 1 deletion.
6 changes: 5 additions & 1 deletion sandpack-react/src/components/FileExplorer/Directory.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,11 @@ export const Directory: React.FC<Props> = ({
depth,
autoHiddenFiles,
visibleFiles,
initialCollapsedFolder,
}) => {
const [open, setOpen] = React.useState(true);
const [open, setOpen] = React.useState(
!initialCollapsedFolder?.includes(prefixedPath)
);

const toggle = (): void => setOpen((prev) => !prev);

Expand All @@ -45,6 +48,7 @@ export const Directory: React.FC<Props> = ({
autoHiddenFiles={autoHiddenFiles}
depth={depth + 1}
files={files}
initialCollapsedFolder={initialCollapsedFolder}
prefixedPath={prefixedPath}
selectFile={selectFile}
visibleFiles={visibleFiles}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,43 @@ export const Component: React.FC = () => (
</>
);

export const InitialCollapsedFolder: React.FC = () => (
<>
<SandpackProvider
customSetup={{
entry: "/index.tsx",
}}
files={{
"/index.tsx": "",
"/src/app.tsx": "",
"/src/components/button.tsx": "",
}}
>
<SandpackLayout>
<SandpackFileExplorer initialCollapsedFolder={["/src/components/"]} />
<SandpackCodeEditor closableTabs />
</SandpackLayout>
</SandpackProvider>

<SandpackProvider
customSetup={{
entry: "/index.tsx",
}}
files={{
"/index.tsx": "",
"/src/app.tsx": "",
"/src/components/button.tsx": "",
"/src/components/really-loooooooong-naaameeee.tsx": "",
}}
theme="dark"
>
<SandpackLayout>
<SandpackFileExplorer initialCollapsedFolder={["/src/components/"]} />
</SandpackLayout>
</SandpackProvider>
</>
);

export const LongFileTree: React.FC = () => (
<SandpackProvider
customSetup={{
Expand Down
2 changes: 2 additions & 0 deletions sandpack-react/src/components/FileExplorer/ModuleList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export const ModuleList: React.FC<ModuleListProps> = ({
files,
autoHiddenFiles,
visibleFiles,
initialCollapsedFolder,
}) => {
const { directories, modules } = fromPropsToModules({
visibleFiles,
Expand All @@ -43,6 +44,7 @@ export const ModuleList: React.FC<ModuleListProps> = ({
autoHiddenFiles={autoHiddenFiles}
depth={depth}
files={files}
initialCollapsedFolder={initialCollapsedFolder}
prefixedPath={dir}
selectFile={selectFile}
visibleFiles={visibleFiles}
Expand Down
4 changes: 4 additions & 0 deletions sandpack-react/src/components/FileExplorer/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,14 @@ export interface SandpackFileExplorerProp {
* @default false
*/
autoHiddenFiles?: boolean;

initialCollapsedFolder?: string[];
}

export const SandpackFileExplorer = ({
className,
autoHiddenFiles = false,
initialCollapsedFolder = [],
...props
}: SandpackFileExplorerProp &
React.HTMLAttributes<HTMLDivElement>): JSX.Element | null => {
Expand Down Expand Up @@ -83,6 +86,7 @@ export const SandpackFileExplorer = ({
activeFile={activeFile}
autoHiddenFiles={autoHiddenFiles}
files={orderedFiles}
initialCollapsedFolder={initialCollapsedFolder}
prefixedPath="/"
selectFile={openFile}
visibleFiles={visibleFilesFromProps}
Expand Down
1 change: 1 addition & 0 deletions website/docs/src/pages/advanced-usage/components.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,7 @@ export default () => (
| :---------------- | :--------------------------------------- | :--- | :------ |
| `autoHiddenFiles` | Enable auto hidden file in file explorer |
| `boolean` | `false` |
| `initialCollapsedFolder` | Initial state of folder (collapsed or not) | `string[]` e.g. `["/components/src/"]` | `undefined` |
</details>
Expand Down

1 comment on commit 27b075e

@vercel
Copy link

@vercel vercel bot commented on 27b075e Feb 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

sandpack-docs – ./website/docs

sandpack.vercel.app
sandpack-docs-codesandbox1.vercel.app
sandpack-docs-git-main-codesandbox1.vercel.app

Please sign in to comment.