Displaying files
The FileBrowser
component only has one required prop - files
. This prop defines
what files are actually shown to the user. Minimal FileBrowser
usage could look
like this:
const FilesExample = () => {const files: FileArray = [// `FileArray` is a type exported by Chonkynull, // Will show loading animation{ id: 'xWbZ', name: 'Instructions.txt' },{ id: 'xWbZ', name: 'Tools', isDir: true },];return (<FileBrowser files={files}><FileNavbar /><FileToolbar /><FileList /><FileContextMenu /></FileBrowser>);};
The files
prop should be an array of file descriptions or nulls. File
descriptions are plain JS objects that follow the FileData
type (more on it below).
Nulls represent loading files - a placeholder loading animation will be shown in
their place. The formal type for this prop is defined as follows:
type FileArray = Nullable<FileData>[];// i.e. Array of nulls and `FileData` objects mixed together
The files
array should be immutable. If you're not sure what this means, please
read the Immutability section from the sidebar. This is
important.
The FileData
type
The FileData
type is shown below. It might look intimidating, but please note that
id
and name
are the only required fields. All other fields are optional, and are
there to give you more control over how Chonky displays your files. This type is
exported from Chonky for your convenience.
export interface FileData {id: string; // (Required) String that uniquely identifies the filename: string; // (Required) Full name, e.g. `MyImage.jpg`ext?: string; // File extension, e.g. `.jpg`isDir?: boolean; // Is a directory, default: falseisHidden?: boolean; // Is a hidden file, default: falseisSymlink?: boolean; // Is a symlink, default: falseisEncrypted?: boolean; // Is encrypted in some way, default: falseopenable?: boolean; // Can be opened, default: trueselectable?: boolean; // Can be selected, default: truedraggable?: boolean; // Can be dragged, default: truedroppable?: boolean; // Can have files dropped into it, default: true for folderssize?: number; // File size in bytesmodDate?: Date | string; // Last change date (or its string representation)childrenCount?: number; // Number of files inside of a folder (only for folders)// Default preview overridingcolor?: string; // Color to use for this fileicon?: ChonkyIconName | string | any; // Icon to use for this filethumbnailUrl?: string; // Automatically load thumbnail from this URL[property: string]: any; // Any other user-defined property}
It is very important that file IDs are unique.
If file IDs are not unique, the consequences can be dire - e.g. your users can irrecoverably delete the wrong file. Chonky tries to protect you from this by scanning your file array for duplicate IDs, but you should put some extra checks in your code too. You can never be too safe.
The file extension is automatically extracted from the name
field, so you don't
need to provide an ext
field yourself. In the rare case where the automatic
extension detection fails (for example: file.tar.gz
), you can provide the ext
field explicitly. Please make sure the extension is included in both name
and ext
:
const myFile: FileData = {id: 'AsVz',name: 'file.tar.gz',ext: '.tar.gz',};console.log(myFile.name.endsWith(myFile.ext)); // Should always print `true`
Example file array
The live example below shows different possible file configurations.