Object List
File browser for useObjectList, with folders, list and grid views, search, sort, selection, move, upload, thumbnails, previews, and a file menu.
A file browser for the files your app stores. ObjectList renders a
useObjectList catalog with folders, list
and grid views, search, sort, and paging. Each hook you pass turns on a
feature: selection, upload, download, share, and delete.
The preview keeps its files in memory. Search, sort, switch to the grid, select with Shift-click or Ctrl+A, drop a file or a folder onto it, drag a row onto a folder, or open a row's menu.
Installation
import { ObjectList } from "@dimah-s3/ui";Complete Installation first: the package or registry
setup, the stylesheet, and one <Toaster /> in your layout.
Usage
"use client";
import {
useDelete,
useDownload,
useObjectList,
useObjectListSelection,
useUpload,
type ObjectListSource,
} from "@dimah-s3/react";
import { ObjectList, UploadTrigger } from "@dimah-s3/ui";
type LibraryFile = { id: string; key: string; name: string; size: number };
export function Library({ source }: { source: ObjectListSource<LibraryFile> }) {
const list = useObjectList(source);
const selection = useObjectListSelection(list);
const upload = useUpload({ route: "docs", maxFiles: 8, append: true });
const download = useDownload({ route: "docs" });
const remove = useDelete({ route: "docs" });
return (
<ObjectList
list={list}
selection={selection}
upload={upload}
download={download}
delete={remove}
toolbar={<UploadTrigger size="sm" />}
/>
);
}The component never fetches. source decides where the rows come from:
memoryObjectSource for records you hold, dbObjectSource with the
database plugin, shareObjectSource for a
shared folder, or your own API. See
useObjectList.
| Prop | Turns on |
|---|---|
selection | Checkboxes, the selection bar, Shift ranges, Ctrl+A, Escape, Space |
upload | Drop and paste into the open folder, with progress under the header |
download | Download in the file menu |
share | Copy link, signed by a second useDownload such as an inline one |
delete | Delete in the entry menu, bulk delete on the selection, and the Delete key |
thumbnails | Image thumbnails, signed as rows come into view |
What the source can do turns on the rest: New folder, Rename, Move to…, and Delete on folders. See Folders.
The list is the Upload,
Download, and
Delete root for its hooks, so their triggers
work inside it without a root of their own.
Anatomy
Pass children to arrange the parts yourself. Without children, the list renders a header, the usage, the upload status, and the content.
<ObjectList list={list} selection={selection} upload={upload} delete={remove}>
<ObjectListHeader>
<ObjectListSearch />
<ObjectListSortMenu />
<ObjectListViewToggle />
<UploadTrigger size="sm" />
</ObjectListHeader>
<ObjectListUsage />
<ObjectListUploadStatus />
<ObjectListContent />
</ObjectList>| Part | Description |
|---|---|
ObjectListHeader | Path, select-all, New folder, and your controls. Turns into the search or selection bar |
ObjectListBreadcrumb | The path. Shortens to fit, with a menu for the folders above |
ObjectListSearch | Search button that opens a field across the header |
ObjectListSearchBar | Where a search looks and how many results it found |
ObjectListSortMenu | Name, size, and date order, or your own fields |
ObjectListViewToggle | List and grid buttons |
ObjectListSelectAll | The select-all checkbox |
ObjectListSelectionBar | Count, your actions, Move, bulk delete, and Clear |
ObjectListBulkMove | Move, for a selection bar of your own |
ObjectListBulkDelete | Bulk delete, for a selection bar of your own |
ObjectListNewFolder | New folder, when the source can create folders |
ObjectListUsage | Catalog bytes and files, with a text-sized ring for the caps |
ObjectListUsageMeter | The cap ring, for custom content inside ObjectListUsage |
ObjectListUploadStatus | Upload progress and errors |
ObjectListContent | Rows or tiles, the loading, error, and empty states, and the next page |
ObjectListEmpty | The empty state, with your own text or actions |
ObjectListEntryMenu | The menu of one entry: download, copy link, rename, move, delete |
ObjectListRow, ObjectListTile | One entry, for renderEntry |
Render the header's controls as its direct children, so the search and New folder fields can take the bar from them.
Customize entries
Change part of every entry with a render* prop. Each receives the entry:
entry.kind is "file" or "folder", entry.item is your record, and
entry.file is what the source's getFile returned.
<ObjectList
list={list}
onOpenItem={(file) => openDetails(file)}
renderMeta={(entry) =>
entry.kind === "file" && entry.item.shared ? <Badge>Shared</Badge> : null
}
renderDescription={(entry) =>
entry.kind === "file" ? `${entry.item.owner} · ${entry.item.size}` : null
}
renderActions={(entry) =>
entry.kind === "file" ? (
<ObjectListEntryMenu
actions={[{ label: "Details", onClick: () => openDetails(entry.item) }]}
/>
) : null
}
/>| Prop | Replaces |
|---|---|
renderMeta | Nothing. Adds text beside the name, such as a badge |
renderDescription | The second line: a file's size and date, or a folder's file count and size |
renderActions | The trailing controls. Return null to hide the menu |
renderEntry | The whole row or tile. Return ObjectListRow or ObjectListTile to keep selection and keyboard |
onOpenItem runs when a file is opened; folders always open in place. Define
render* functions outside the component, or wrap them in useCallback, so
your renders do not redraw every entry.
Views
defaultView="grid" starts in the grid. Pass view and onViewChange to
keep the view in your own state, such as the URL.
Thumbnails
Pass thumbnails with the route that signs downloads, and image files show
their picture in rows and tiles:
<ObjectList list={list} thumbnails={{ route: "files" }} />Each image is signed with useObjectUrl once
its row or tile comes into view, and signed again before the URL expires. Its
type, or its extension, decides: PNG, JPEG, GIF, WebP, AVIF, SVG, BMP, and
ICO. A previewUrl from getFile wins. Pass api with the route when there
is no S3Provider.
Preview
Pass preview with the route that signs downloads, and a file opens in
ObjectPreviewDialog when it is clicked
or opened with Enter:
<ObjectList
list={list}
download={download}
thumbnails={{ route: "files" }}
preview={{ route: "files" }}
/>Previous, Next, and the arrow keys move through the loaded files, and the
dialog's Download uses download. Closing it focuses the row of the file it
showed last. preview also takes the dialog's kinds, maxTextBytes, and
render. A file opens one way: pass preview or onOpenItem, not both.
The dialog's title is the file's name, and Download saves its
fileName. A useDownload download also adds
Copy link to the file menu: pass share={false} to keep the dialog's Download
without it, as Entry menu shows.
Titles and file names
A row can show a title over the stored file, such as "Site plan" for
site-plan.pdf. Return the title as name and the stored file's name as
fileName from getFile:
const list = useObjectList({
query,
getId: (file) => file.id,
getFile: (file) => ({
name: file.title,
fileName: file.fileName,
objectKey: file.objectKey,
size: file.size,
contentType: file.contentType,
}),
});The row, the preview dialog's title, and the delete confirmation show name.
Download, Copy link, and the preview save the file as fileName, and the
file's icon reads its extension. Without fileName, they use name. Rename
edits name.
Search and sort
ObjectListSearch and ObjectListSortMenu set list.search and list.sort,
which reload the first page. The source decides what they mean:
memoryObjectSourcematches names in the open folder and every folder below it, and sorts by name, size, date, orsortValue.dbObjectSourcedoes the same on the server.- Your own query receives
search,searchScope, andsort.
Pass your own sort fields with options:
<ObjectListSortMenu
options={[
{ field: "name", label: "Name" },
{ field: "owner", label: "Owner" },
{ field: "updatedAt", label: "Modified", direction: "desc" },
]}
/>When the source has searchScopes,
users can search the open folder or everything.
Selection
Pass useObjectListSelection(list) as selection. Files can be selected by
default, and folders too when the source can delete or move them. Put your own
bulk actions in selectionActions:
const selection = useObjectListSelection(list);
<ObjectList
list={list}
selection={selection}
selectionActions={
<Button size="sm" onClick={() => share(selection.selectedItems)}>
Share
</Button>
}
/>;Delete
onDeleted receives the result of each confirmed delete. Delete, or
Cmd+Backspace on a Mac, deletes the selection, else the focused entry, after
the same confirmation.
Folders can be deleted when the source has deleteFolder. After the dialog is
confirmed, listFolderKeys names the files in each folder, delete removes
them with the selected files in one request, and deleteFolder removes each
folder whose files all went. A folder with a file that failed stays.
dbObjectSource sets both; with your own source:
const list = useObjectList({
...source,
listFolderKeys: ({ folder, signal }) => api.keysUnder(folder.id, { signal }),
deleteFolder: ({ folder }) => api.deleteFolder(folder.id),
});Without deleteFolder, folders have no Delete, and selected folders stay out
of a bulk delete.
Move
When the source has moveFile or moveFolder, entries have Move to… in
their menu, the selection bar has Move, and a row dragged onto a folder,
or onto the root or the parent in the path, moves there. The dialog opens in
the open folder: browse to another and choose Move here.
const list = useObjectList({
...source,
moveFile: ({ item, parent }) => api.moveFile(item.id, parent),
moveFolder: ({ folder, parent }) => api.moveFolder(folder.id, parent),
});A dragged row that is selected takes the selection with it. Moved entries
leave the open folder at once, and the list reloads. A folder cannot be
opened in the dialog while it moves, so nothing moves into itself.
dbObjectSource sets both.
Upload into folders
With upload, the list takes drops and pastes into the open folder. For a
file picker, add an UploadTrigger to the toolbar or the empty state:
<ObjectList
list={list}
upload={upload}
empty={
<ObjectListEmpty>
<UploadTrigger size="sm" />
</ObjectListEmpty>
}
/>The source's uploadOptions(parent) decides where files land.
dbObjectSource sets it for you. A dropped folder keeps its folders when the
source has createFolder: each one is found by name below the folder it lands
in, or created as its first file starts, and its files upload there. With your own source, send the folder as
clientMetadata and read it in the server's upload hook:
const list = useObjectList({
...source,
uploadOptions: (parent) => ({ clientMetadata: { folderId: parent ?? "" } }),
});Folders
ObjectList lists the folders the source returns on the first page.
memoryObjectSourcebuilds folders from/-separated paths withgetFolder.dbObjectSourcelists, creates, and renames the foldersdb()stores.- Your own source returns folders from its query. Add
createFolderto show New folder,renameFileorrenameFolderto show Rename,moveFileormoveFolderfor Move, anddeleteFolderfor folder deletes. Renaming and moving keep the object key.
Entry menu
ObjectListEntryMenu is the default trailing control, with Download,
Copy link, Rename, Move to…, and Delete when the list can do
them. Add your
own rows with actions, or with children:
<ObjectListEntryMenu>
<ObjectListEntryMenuItem onClick={() => star(entry.item)}>
<StarIcon />
Star
</ObjectListEntryMenuItem>
</ObjectListEntryMenu>Copy link signs a URL through share, else through a useDownload
download. Pass share={false} to leave Copy link out, such as when files
are shared through share links, and keep Download in the menu
and the preview:
<ObjectList
list={list}
download={download}
share={false}
preview={{ route: "files" }}
/>Frame and paging
Pass height to keep the header in place and scroll the entries, or
maxHeight to let the list grow up to a cap. Later pages load as the user
scrolls; pagination="button" shows Load more instead.
Storage usage
Return usage from the source's query. ObjectListUsage shows the bytes
and the file count on one line. For dbObjectSource and
memoryObjectSource that is the whole catalog, the same number in every
folder, against the db() quota. A folder's own size
is on its row. A cap adds a ring the size of the text: the byte cap when
maxBytes is set, and the file cap when it is not. Hover, focus, or click
the ring for the percent. The line uses the destructive color when either
cap is reached.
Pass usage to show another total, such as the open folder. Pass children
to replace the ring and the line. A function receives the state on screen,
and ObjectListUsageMeter is the ring.
<ObjectListUsage usage={folderUsage}>
{(state) => (
<>
<ObjectListUsageMeter />
<span>
{state.bytesLabel} · {state.filesLabel}
</span>
</>
)}
</ObjectListUsage>Toasts
The list shows toasts for its hooks by default, so it needs no
UploadToasts, DownloadToasts, or DeleteToasts. Pass toasts={false} to
turn them off.
Keyboard
The entries are one tab stop, so a long folder takes one Tab.
| Key | Action |
|---|---|
| Tab / Shift+Tab | Enter the entries, move through an entry's controls, leave |
| ArrowUp / ArrowDown | Move between entries (by row in the grid) |
| ArrowLeft / ArrowRight | List: the row's checkbox, name, and menu. Grid: next tile |
| Home / End | First or last entry |
| Enter | Open the folder, the file's preview, or call onOpenItem |
| Space | Toggle the focused entry. With Shift: the range |
| Ctrl+A / Cmd+A | Select every loaded entry |
| Escape | Clear the selection |
| Backspace / Alt+ArrowUp | Open the parent folder |
| Delete / Cmd+Backspace | Delete the selection, else the focused entry |
| F2 | Rename the focused entry |
| / | Open the search field |
Give your own controls in renderActions tabIndex={-1}, as the built-in
checkbox and menu have, so Tab stays on the entry.
API Reference
ObjectList
| Prop | Type | Default | Description |
|---|---|---|---|
list | UseObjectListReturn | required | The useObjectList return to render |
selection | UseObjectListSelectionReturn | — | Adds selection |
upload | UseUploadReturn | — | Adds drop, paste, and folder uploads |
download | AnyDownloadReturn | — | Adds Download to the file menu |
share | UseDownloadReturn | false | — | Signs Copy link, else a useDownload download does. false leaves it out |
delete | UseDeleteReturn | — | Adds Delete to the entry menu and the selection bar |
toasts | boolean | true | Toasts for the hooks above |
view | "list" | "grid" | — | Controlled view, with onViewChange |
defaultView | "list" | "grid" | "list" | Starting view |
onViewChange | (view) => void | — | Runs when the view changes |
onOpenItem | (item) => void | — | Runs when a file is clicked or opened with Enter |
preview | ObjectListPreview | — | Opens files in a preview dialog. Not with onOpenItem |
thumbnails | { route, api? } | — | Signs image thumbnails through this route |
onDeleted | (result: DeleteResult) => void | — | Runs after each confirmed delete |
rootLabel | string | "Files" | Root breadcrumb label and the list's accessible name |
breadcrumb | boolean | true | Shows the path in the default header |
toolbar | ReactNode | — | Controls at the end of the default header |
selectionActions | ReactNode | — | Controls on the selection bar, beside Delete |
empty | ReactNode | — | Replaces the empty state |
commands | ObjectListCommands | — | Opens New folder and entry menus from outside |
pagination | "auto" | "button" | "auto" | How later pages load |
height | number | string | — | Fixed frame height. Entries scroll inside |
maxHeight | number | string | — | Largest frame height |
renderEntry | (entry) => ReactNode | — | Replaces the whole row or tile |
renderActions | (entry) => ReactNode | Entry menu | Replaces the trailing controls |
renderDescription | (entry) => ReactNode | Size and date, or a folder's file count and size | Replaces the second line. Text only |
renderMeta | (entry) => ReactNode | — | Text beside the name |
className | string | — | Classes for the frame |
children | ReactNode | — | Your own layout from the list parts |
ObjectListHeader
| Prop | Type | Default | Description |
|---|---|---|---|
children | ReactNode | — | Trailing controls: search, sort, view, upload |
breadcrumb | boolean | The list's breadcrumb | Shows the path |
searchBar | boolean | true | Shows ObjectListSearchBar during a search |
selectionActions | ReactNode | The list's selectionActions | Controls on the selection bar |
ObjectListSearch
| Prop | Type | Default | Description |
|---|---|---|---|
placeholder | string | Where it looks | Placeholder and accessible name |
debounceMs | number | 250 | Wait after typing before the list reloads |
collapsible | boolean | true | In a header, starts as a button that opens across the bar |
ObjectListSortMenu
| Prop | Type | Default | Description |
|---|---|---|---|
options | { field, label, direction? }[] | Name, size, date | The choices |
ObjectListEmpty
| Prop | Type | Default | Description |
|---|---|---|---|
title | ReactNode | "This folder is empty", or "No results" | Heading |
description | ReactNode | — | Text under it |
children | ReactNode | Search actions while searching | Actions below the text |
ObjectListBulkMove and ObjectListBulkDelete
| Prop | Type | Default | Description |
|---|---|---|---|
className | string | — | Classes for the button |
ObjectListUsage
| Prop | Type | Default | Description |
|---|---|---|---|
usage | ObjectListUsageData | null | list.usage | Totals to show. null hides the line |
children | ReactNode | (state) => ReactNode | Ring and summary | Replaces the ring and the summary. A function receives state |
className | string | — | Classes for the row |
state is ObjectListUsageState:
| Field | Description |
|---|---|
usage | The totals on screen |
bytesLabel | "1.2 MB", or "1.2 MB of 10 MB" when maxBytes is set |
filesLabel | "3 files", or "3 of 4 files" when maxFiles is set |
byteRatio | totalBytes / maxBytes, or null |
fileRatio | objectCount / maxFiles, or null |
meterRatio | What the ring draws. Bytes when maxBytes is set, otherwise files, or null |
over | A byte cap or a file cap is reached |
ObjectListUsageMeter
| Prop | Type | Default | Description |
|---|---|---|---|
className | string | — | Classes for the ring |
ObjectListEntryMenu
| Prop | Type | Default | Description |
|---|---|---|---|
objectKey | string | The entry's key | Key of the object |
fileName | string | The entry's fileName, else its name | Download and Copy link name, and the delete dialog label |
fileSize | number | null | The entry's size | Size in the delete dialog |
share | UseDownloadReturn | The list's share | Signs Copy link |
expiresIn | number | — | Link lifetime in seconds, capped by the route |
actions | ObjectListEntryMenuAction[] | — | Rows { label, onClick, icon?, disabled?, destructive? } before Delete |
children | ReactNode | — | ObjectListEntryMenuItems after actions |
ObjectListRow and ObjectListTile
| Prop | Type | Default | Description |
|---|---|---|---|
entry | ObjectListEntry | required | The entry from renderEntry |
media | ReactNode | Glyph or thumbnail | Replaces the icon |
meta | ReactNode | renderMeta | Beside the name |
description | ReactNode | renderDescription | The second line |
actions | ReactNode | renderActions | The trailing controls |
useObjectListContext
Returns the list's state inside ObjectList, for parts of your own:
list, selection, upload, download, share, delete, toasts,
view, setView, rootLabel, and the commands startNewFolder(),
startRename(key), revealEntry(entry), startMove(entries, { trigger? }),
and announce(feedback). Inside an entry, useObjectListEntry() returns that
entry.
useObjectListCommands
Opens list UI from your own shortcuts. Pass the result to ObjectList as
commands.
const commands = useObjectListCommands();
<ObjectList list={list} commands={commands} />;
<Button onClick={() => commands.startNewFolder()}>New folder</Button>;| Property | Type | Description |
|---|---|---|
startNewFolder | () => void | Opens the New folder field |
openMenu | (entryKey: string) => void | Opens an entry's menu, when it is on screen |