x4js gallery

FileDrop

A zone where files are dropped, or chosen with the file dialog of the system after a click. FileDialog is that dialog alone.

import { FileDrop, FileDialog } from "x4js"

Change

Files dropped on the zone, or chosen after a click (or the space bar): change gives them.

export function change( ) {
	const label = new Label( { text: "" } );

	return new HBox( { content: [
		new FileDrop( { width: 260, accept: "", multiple: true, change: ( ev ) => {
			const names = Array.from( ev.files ).map( f => `${f.name} (${f.size} bytes)` );
			label.setText( names.join( ", " ) );
		} } ),
		label,
	] } );
}

Images

accept limits the files offered: here images, shown as soon as one is dropped.

export function images( ) {
	const preview = new Image( { cls: "dropped", src: "", width: 160, height: 120, fit: "contain" } );

	return new HBox( { content: [
		new FileDrop( { width: 260, accept: "image/*", icon: icon_image, label: "Drop an image here", change: ( ev ) => {
			preview.setImage( URL.createObjectURL( ev.files[0] ) );
		} } ),
		preview,
	] } );
}
.x4image.dropped {
	border: 1px dashed var( --border-hover );
}

FileDialog

The file dialog alone, opened by a button of your own: showDialog, then its callback gets the files.

export function dialog( ) {
	const label = new Label( { text: "" } );
	const files = new FileDialog( { accept: ".txt,.md,.json", multiple: false, callback: ( list ) => {
		label.setText( list.length ? list[0].name : "" );
	} } );

	return new HBox( { content: [
		files,
		new Button( { icon: icon_file, label: "Open a text file...", click: ( ) => files.showDialog( ) } ),
		label,
	] } );
}

For an assistant: llms.txt, llms-full.txt.