style the FileView component

This commit is contained in:
Moritz Hölting 2024-06-20 20:40:59 +02:00
parent d5e9ec7dfc
commit 05cdc423fc
5 changed files with 83 additions and 27 deletions

View File

@ -24,6 +24,7 @@
"astro": "^4.10.2", "astro": "^4.10.2",
"react": "^18.3.1", "react": "^18.3.1",
"react-dom": "^18.3.1", "react-dom": "^18.3.1",
"react-icons": "^5.2.1",
"sharp": "^0.32.6", "sharp": "^0.32.6",
"shiki": "^1.7.0", "shiki": "^1.7.0",
"starlight-links-validator": "^0.7.1", "starlight-links-validator": "^0.7.1",

View File

@ -47,6 +47,9 @@ dependencies:
react-dom: react-dom:
specifier: ^18.3.1 specifier: ^18.3.1
version: 18.3.1(react@18.3.1) version: 18.3.1(react@18.3.1)
react-icons:
specifier: ^5.2.1
version: 5.2.1(react@18.3.1)
sharp: sharp:
specifier: ^0.32.6 specifier: ^0.32.6
version: 0.32.6 version: 0.32.6
@ -4572,6 +4575,14 @@ packages:
scheduler: 0.23.2 scheduler: 0.23.2
dev: false dev: false
/react-icons@5.2.1(react@18.3.1):
resolution: {integrity: sha512-zdbW5GstTzXaVKvGSyTaBalt7HSfuK5ovrzlpyiWHAFXndXTdd/1hdDHI4xBM1Mn7YriT6aqESucFl9kEXzrdw==}
peerDependencies:
react: '*'
dependencies:
react: 18.3.1
dev: false
/react-is@16.13.1: /react-is@16.13.1:
resolution: {integrity: sha512-24e6ynE2H+OKt4kqsOvNd8kBpV65zoxbA4BVsEOB3ARVWQki/DHzaUoC5KuON/BiccDaCCTZBuOcfZs70kR8bQ==} resolution: {integrity: sha512-24e6ynE2H+OKt4kqsOvNd8kBpV65zoxbA4BVsEOB3ARVWQki/DHzaUoC5KuON/BiccDaCCTZBuOcfZs70kR8bQ==}
dev: false dev: false

View File

@ -36,9 +36,9 @@ export default function Playground() {
"test.mcfunction": { "test.mcfunction": {
content: "", content: "",
language: "mcfunction", language: "mcfunction",
} },
} },
} },
}, },
files: { files: {
"pack.toml": { "pack.toml": {

View File

@ -1,5 +1,9 @@
import type { Directory, SetState } from "@components/Playground"; import type { Directory, SetState } from "@components/Playground";
import React, { useState } from "react"; import React, { useState } from "react";
import {
GoChevronDown as ChevDown,
GoChevronRight as ChevRight,
} from "react-icons/go";
export default function FileView({ export default function FileView({
root, root,
@ -14,28 +18,31 @@ export default function FileView({
}) { }) {
return ( return (
<div className={className}> <div className={className}>
{Object.entries(root.dirs ?? {}).map(([name, dir]) => { <h3>Explorer</h3>
return ( <div className="entries">
<DirElement {Object.entries(root.dirs ?? {}).map(([name, dir]) => {
key={name} return (
name={name} <DirElement
dir={dir} key={name}
fileName={fileName.slice(name.length + 1)}
setSelectedFileName={setSelectedFileName}
/>
);
})}
{Object.entries(root.files ?? {}).map(([name, _]) => {
return (
<span key={name}>
<FileElement
name={name} name={name}
disabled={fileName == name} dir={dir}
onClick={() => setSelectedFileName(name)} fileName={fileName.slice(name.length + 1)}
setSelectedFileName={setSelectedFileName}
/> />
</span> );
); })}
})} {Object.entries(root.files ?? {}).map(([name, _]) => {
return (
<span key={name}>
<FileElement
name={name}
disabled={fileName == name}
onClick={() => setSelectedFileName(name)}
/>
</span>
);
})}
</div>
</div> </div>
); );
} }
@ -50,7 +57,7 @@ function FileElement({
onClick?: React.MouseEventHandler<HTMLButtonElement>; onClick?: React.MouseEventHandler<HTMLButtonElement>;
}) { }) {
return ( return (
<button disabled={disabled} onClick={onClick}> <button disabled={disabled} onClick={onClick} className="file">
{name} {name}
</button> </button>
); );
@ -75,15 +82,31 @@ function DirElement({
setSelectedFileName(name + "/" + selected); setSelectedFileName(name + "/" + selected);
}; };
const chevStyles: React.CSSProperties = {
marginBottom: "-2px",
};
const hasChildren =
Object.keys(currentDir.dirs ?? {}).length > 0 ||
Object.keys(currentDir.files ?? {}).length > 0;
return ( return (
<div key={name}> <div key={name} className="dir">
<button <button
style={{ display: "block" }} style={{ display: "block" }}
onClick={() => setCollapsed(!collapsed)} onClick={() => setCollapsed(!collapsed)}
> >
{name}/ {collapsed ? (
<ChevRight
aria-description="collapsed"
style={chevStyles}
/>
) : (
<ChevDown aria-description="expanded" style={chevStyles} />
)}{" "}
{name + "/" + (collapsed && hasChildren ? "..." : "")}
</button> </button>
<div style={{ marginLeft: ".25cm" }}> <div style={{ marginLeft: "0.5cm" }} className="dirChildren">
{collapsed ? null : ( {collapsed ? null : (
<div> <div>
{Object.entries(currentDir.dirs ?? {}).map( {Object.entries(currentDir.dirs ?? {}).map(

View File

@ -12,6 +12,27 @@
grid-area: files; grid-area: files;
display: flex; display: flex;
flex-direction: column; flex-direction: column;
margin-right: 0.5cm;
.entries {
button {
border: none;
width: 100%;
text-align: left;
background-color: transparent;
cursor: pointer;
&:disabled {
cursor: default;
color: var(--sl-color-text);
background-color: #333;
}
&:hover {
background-color: #444444;
}
}
}
} }
> .editor { > .editor {
grid-area: editor; grid-area: editor;