File Browser
A working cross-platform file browser: directory tree, image thumbnails, double-click to open with the OS default handler, right-click to reveal.
Exercises: fs.readDir, fs.readBinary (zero-copy), shell.openPath, shell.revealInFolder, dialog.openDirectory with grantAccess, path.join.
Scaffold
Section titled “Scaffold”bunx @tynd/cli create file-browser --framework reactcd file-browserBackend — minimal
Section titled “Backend — minimal”No RPC needed — every operation is an OS call from the frontend.
import { app } from "@tynd/core";
app.start({ window: { title: "Files", width: 1100, height: 720, center: true },});Frontend
Section titled “Frontend”Install the required plugins:
tynd add fs shell dialogThe fs API only reaches the app’s own directories by default, not the home folder. The user picks the folder to browse in a dialog opened with grantAccess: true, which grants read access to that folder and its whole subtree; browsing stays inside it.
import { useEffect, useState } from "react";import { dialog } from "@tynd/plugin-dialog/client";import { fs } from "@tynd/plugin-fs/client";import { path } from "@tynd/core/client";import { shell } from "@tynd/plugin-shell/client";
interface Entry { name: string; path: string; isDir: boolean;}
export default function App() { // The folder the user granted; browsing never goes above it. const [root, setRoot] = useState<string>(""); const [cwd, setCwd] = useState<string>(""); const [entries, setEntries] = useState<Entry[]>([]);
async function pickRoot() { const picked = await dialog.openDirectory({ grantAccess: true }); if (!picked) return; setRoot(picked); setCwd(picked); }
useEffect(() => { if (!cwd) return; fs.readDir(cwd).then((dirEntries) => { const items = dirEntries.map((d) => ({ name: d.name, path: path.join(cwd, d.name), isDir: d.isDir, })); items.sort((a, b) => a.isDir === b.isDir ? a.name.localeCompare(b.name) : a.isDir ? -1 : 1, ); setEntries(items); }); }, [cwd]);
function goUp() { if (cwd !== root) setCwd(path.dirname(cwd)); }
async function onOpen(e: Entry) { if (e.isDir) setCwd(e.path); else await shell.openPath(e.path); }
return ( <div style={{ height: "100vh", display: "flex", flexDirection: "column" }}> <header style={{ display: "flex", gap: 8, padding: 8, borderBottom: "1px solid #ddd" }}> <button onClick={pickRoot}>Open folder…</button> <button onClick={goUp} disabled={!cwd || cwd === root}>↑</button> <code style={{ flex: 1, padding: "4px 8px", background: "#f4f4f4" }}>{cwd}</code> </header>
<ul style={{ listStyle: "none", margin: 0, padding: 0, overflow: "auto" }}> {entries.map((e) => ( <li key={e.path} onDoubleClick={() => onOpen(e)} onContextMenu={(ev) => { ev.preventDefault(); shell.revealInFolder(e.path); }} style={{ padding: "8px 12px", display: "flex", alignItems: "center", gap: 12, cursor: "pointer", }} > {e.isDir ? "📁" : isImage(e.name) ? <Thumbnail path={e.path} /> : "📄"} <span>{e.name}</span> </li> ))} </ul> </div> );}
function isImage(name: string) { return /\.(png|jpe?g|gif|webp|bmp)$/i.test(name);}
function Thumbnail({ path }: { path: string }) { const [url, setUrl] = useState<string | null>(null); useEffect(() => { let dead = false; fs.readBinary(path).then((bytes) => { if (dead) return; const blob = new Blob([bytes]); setUrl(URL.createObjectURL(blob)); }); return () => { dead = true; if (url) URL.revokeObjectURL(url); }; }, [path]); return url ? <img src={url} alt="" width={32} height={32} style={{ objectFit: "cover" }} /> : <span>🖼️</span>;}Why this matters
Section titled “Why this matters”fs.readBinarygoes through thetynd-bin://zero-copy channel — multi-MB thumbnails don’t stall the main thread.shell.openPathdelegates to the OS default handler — PDFs open in Preview/Acrobat, images in your image viewer, etc. No need to build your own viewer.- No backend RPC. Every operation is a direct OS call from the frontend. The backend is essentially a config file.
tynd buildThe packaged app includes Bun. Each launch starts with the folder picker: a runtime grant lasts for the session unless security.persistGrants is on.
Next ideas
Section titled “Next ideas”- Watch the current directory —
fs.watch(cwd, { recursive: false }, reload)(returns a handle; callunwatch()when leaving the directory) to pick up changes from other apps. - Search — recursive walk with
async function*yielding matches as they’re found. - Copy / move / delete —
fs.copy,fs.rename,fs.remove(orfs.trash), wrapped with adialog.confirmfor destructive ops. - Drag-drop to add files — frontend HTML5 drag-drop; the dropped paths are returned as native file paths.
Related
Section titled “Related”- fs API.
- Binary Data guide — why
fs.readBinaryis fast. - shell API.