Skip to content

Commit 5752883

Browse files
author
Ives van Hoorne
committed
Basic File Explorer
1 parent 0979a30 commit 5752883

File tree

9 files changed

+55
-7
lines changed

9 files changed

+55
-7
lines changed

packages/react-sandpack/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
"mapCoverage": true
7777
},
7878
"dependencies": {
79+
"classnames": "^2.2.5",
7980
"codemirror": "^5.35.0",
8081
"codesandbox-import-utils": "^1.1.21",
8182
"react-broadcast": "^0.6.2",
@@ -88,6 +89,7 @@
8889
"devDependencies": {
8990
"@storybook/addon-knobs": "^3.3.14",
9091
"@storybook/react": "^3.3.14",
92+
"@types/classnames": "^2.2.3",
9193
"@types/codemirror": "^0.0.55",
9294
"@types/jest": "^22.0.0",
9395
"@types/node": "^9.4.6",

packages/react-sandpack/src/components/FileExplorer/File/File.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import * as React from 'react';
2+
import classNames from 'classnames';
23

34
import cn from '../../../utils/cn';
45

56
export interface Props {
67
path: string;
78
selectFile: (path: string) => void;
9+
active?: boolean;
810
}
911

1012
export default class File extends React.PureComponent<Props> {
@@ -18,8 +20,12 @@ export default class File extends React.PureComponent<Props> {
1820
.filter(Boolean)
1921
.pop();
2022

23+
const className = classNames(cn('File', 'container'), {
24+
[cn('File', 'active')]: this.props.active,
25+
});
26+
2127
return (
22-
<div onClick={this.selectFile} className={cn('File', 'container')}>
28+
<div onClick={this.selectFile} className={className}>
2329
{fileName}
2430
</div>
2531
);
Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,23 @@
11
.sandpack-File__container {
2+
transition: 0.3s ease all;
23
font-family: sans-serif;
3-
line-height: 1.6;
44

5-
background-color: #eee;
5+
font-size: 0.875em;
6+
padding: 0.3em 0.5em;
7+
padding-left: 1rem;
8+
color: rgb(220, 220, 220);
9+
10+
border-left: 2px solid transparent;
11+
12+
cursor: pointer;
13+
14+
&:hover {
15+
color: rgb(255, 255, 255);
16+
}
17+
}
18+
19+
.sandpack-File__active {
20+
background-color: rgba(0, 0, 0, 0.3);
21+
22+
border-left: 2px solid #6caedd;
623
}

packages/react-sandpack/src/components/FileExplorer/FileExplorer.tsx

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,28 @@ import * as React from 'react';
33
import ModuleList from './ModuleList';
44
import SandpackConsumer from '../SandpackConsumer';
55

6-
export interface Props {}
6+
import cn from '../../utils/cn';
7+
8+
export interface Props {
9+
style?: Object;
10+
className?: string;
11+
}
712

813
export default class FileExplorer extends React.PureComponent<Props> {
914
render() {
15+
const { style, className = '' } = this.props;
1016
return (
1117
<SandpackConsumer>
1218
{sandpack => (
13-
<div>
19+
<div
20+
className={`${className} ${cn('FileExplorer', 'container')}`}
21+
style={style}
22+
>
1423
<ModuleList
1524
selectFile={sandpack.openFile}
1625
files={sandpack.files}
1726
prefixedPath="/"
27+
openedPath={sandpack.openedPath}
1828
/>
1929
</div>
2030
)}

packages/react-sandpack/src/components/FileExplorer/ModuleList/ModuleList.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,13 @@ export interface Props {
77
prefixedPath: string;
88
files: IFiles;
99
selectFile: (path: string) => void;
10+
openedPath: string;
1011
}
1112

1213
export default class ModuleList extends React.PureComponent<Props> {
1314
render() {
15+
const { openedPath } = this.props;
16+
1417
const filesToShow: { path: string; code: string }[] = [];
1518
const directoriesToShow: { path: string }[] = [];
1619
const pathParts = this.props.prefixedPath.split('/');
@@ -44,6 +47,7 @@ export default class ModuleList extends React.PureComponent<Props> {
4447
key={file.path}
4548
selectFile={this.props.selectFile}
4649
path={file.path}
50+
active={openedPath === file.path}
4751
/>
4852
))}
4953
</div>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.sandpack-FileExplorer__container {
2+
background-color: #24282a;
3+
color: white;
4+
padding-top: 0.5em;
5+
}

packages/react-sandpack/src/components/SandpackProvider/SandpackProvider.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as React from 'react';
22
import { Broadcast } from 'react-broadcast';
33
import { Manager } from 'sandpack';
44

5-
import { IFileProps, IFiles } from '../../types';
5+
import { IFileProps, IFile, IFiles } from '../../types';
66

77
export interface State {
88
files: IFiles;

packages/react-sandpack/stories/InTheWild.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ stories.addWithJSX('with one file', () => (
3333
entry="/index.js"
3434
>
3535
<div style={{ display: 'flex', width: '100%', height: '100%' }}>
36-
<FileExplorer />
36+
<FileExplorer style={{ width: 300 }} />
3737
<CodeEditor style={{ flex: 1, height: '100%' }} />
3838
<BrowserPreview style={{ flex: 1, height: '100%' }} />
3939
</div>

yarn.lock

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,10 @@
522522
react-treebeard "^2.1.0"
523523
redux "^3.7.2"
524524

525+
"@types/classnames@^2.2.3":
526+
version "2.2.3"
527+
resolved "https://registry.yarnpkg.com/@types/classnames/-/classnames-2.2.3.tgz#3f0ff6873da793870e20a260cada55982f38a9e5"
528+
525529
"@types/codemirror@^0.0.55":
526530
version "0.0.55"
527531
resolved "https://registry.yarnpkg.com/@types/codemirror/-/codemirror-0.0.55.tgz#f4292b7a5f33b2aab509501c3db7e45d19a7c057"

0 commit comments

Comments
 (0)