FileDrop unstable
A headless file drop zone and picker trigger with shared accept,
multiple and maxSize filtering, drag-over state, and Picked /
Rejected events. Values stay controlled by the consumer: keep plain facts
(names, sizes) in your Model and pass File values to Commands.
Anatomy
| Part | Element |
|---|---|
root |
any element, usually a <div> or <section> |
trigger |
<button> that opens the browser file picker |
No part adds markup. Because root accepts any element, it can wrap
trigger or stack with it on a single <button>:
fileDrop.root(fileDrop.trigger(field.control(h.button))).
Always draw trigger for keyboard and screen-reader users: drag-and-drop
alone has no pointer-free equivalent.
Data attributes
Present with an empty value or absent:
root:data-drag-over,data-disabledtrigger:data-disabled
Keyboard
trigger is a native <button type="button">, so Enter and Space open the
picker and Tab moves focus.
Batch rules
Drops and HtmlFiles.pick results share one classifier:
- Empty batches (dismissed or blocked pickers, empty drops) emit nothing.
- When
multipleis nottrueand more than one file arrives, the whole batch emits onlyRejectedwithreason: "too-many-files". - Otherwise each file is checked against
accept, thenmaxSize. Nonempty groups emit in order:Picked,Rejectedwithreason: "invalid-type", thenRejectedwithreason: "file-too-large". Clearing an error onPickedand setting it onRejectedpreserves the rejection message.
With Field, Fieldset and Form
Compose fileDrop.trigger(field.control(h.button)) with
disabled: field.disabled (or fieldset.disabled). The field's label,
aria-describedby and aria-invalid land on the button so Form focuses
it after an invalid submit; pass disabled through because <fieldset disabled>
does not block drops on a <div>. No hidden input is rendered because Form
submits strings only.
Example
import { UiSchema } from "effect/ui"import { Html, HtmlServer } from "effect/ui/html"import { Field, FileDrop } from "effect/ui/kit"import { ByteSize, Effect, Schema } from "effect"
const Message = UiSchema.TaggedEnum({ PickedImages: { names: Schema.Array(Schema.String) }, RejectedImages: { reason: Schema.String }})
const Upload = Html.component({ name: "Upload", model: Schema.Struct({ names: Schema.Array(Schema.String), error: Schema.NullOr(Schema.String) }), message: Message}).make({ init: () => ({ names: [], error: null }), update: { PickedImages: ($, { names }) => ({ names: [...$.model.names, ...names], error: null }), RejectedImages: ($, { reason }) => $.evolve({ error: reason }) }, view: (h, model) => Field(h, { props: { invalid: model.error !== null }, view: (field) => FileDrop(h, { props: { accept: ["image/*"], multiple: true, maxSize: ByteSize.mebibytes(5), disabled: field.disabled }, onEmit: { Picked: ({ files }) => Message.PickedImages({ names: files.map((file) => file.name) }), Rejected: ({ reason }) => Message.RejectedImages({ reason }) }, view: (fileDrop) => field.root(h.div).children( field.label(h.label).children("Photos"), fileDrop.root(h.div.class("dropzone")).children( fileDrop.trigger(field.control(h.button)).children("Choose photos") ), field.error(h.div)?.children(model.error ?? "") ) }) })})
const { html } = await Effect.runPromise(HtmlServer.renderToHtml(Upload, {}))if (!/<button[^>]* type="button"[^>]*>Choose photos<\/button>/.test(html)) { throw new Error("the file drop trigger wasn't rendered as a button")}Components
A headless file drop zone and picker trigger.
Details
parts also exposes dragOver and disabled booleans. Own attributes win
over attributes set before the part: data-drag-over and data-disabled on
root; type="button", disabled, aria-disabled (removed) and
data-disabled on trigger. History restore is off (restore: false).
Signature
declare const FileDrop: Headless<{ readonly accept?: readonly Array<string>; readonly disabled?: boolean; readonly maxSize?: ByteSize; readonly multiple?: boolean;}, { readonly _tag: "Picked"; readonly files: readonly Array<File>;} | { readonly _tag: "Rejected"; readonly files: readonly Array<File>; readonly reason: "too-many-files" | "invalid-type" | "file-too-large";}, { disabled: boolean; dragOver: boolean; root: Part<string, [], never>; trigger: Part<"button", [], never>;}, never, never, never, never, never, never, { readonly dragOver: boolean;}, { readonly _tag: "ClickedTrigger";} | { readonly _tag: "Left";} | { readonly _tag: "Entered";} | { readonly _tag: "Dropped"; readonly files: readonly Array<File>;} | { readonly _tag: "CompletedPick"; readonly files: readonly Array<File>;}, QueryEntries<{}, {}>>Models
What a FileDrop emits.
Signature
type FileDropEvents = typeof FileDropEvents.TypeThe props of a FileDrop.
Signature
type FileDropProps = typeof FileDropProps.TypeWhy FileDrop rejected a file batch.
Signature
type FileDropRejectReason = typeof FileDropRejectReason.TypeSchemas
FileDropEvents
unstableEvents a FileDrop emits: Picked for accepted files and Rejected for
files rejected for count, type or size.
Signature
declare const FileDropEvents: TaggedEnum<{ Picked: TaggedStruct<"Picked", { readonly files: $Array<declare<File, File>>; }>; Rejected: TaggedStruct<"Rejected", { readonly files: $Array<declare<File, File>>; readonly reason: Literals<readonly ["too-many-files", "invalid-type", "file-too-large"]>; }>;}>FileDropProps
unstableProps for a FileDrop.
Details
accept: file types allowed for drops and passed toHtmlFiles.pick, matched without case:.extsuffix,type/*prefix, wildcard for any file, or exacttype/subtype(ignoring;...parameters). Omitted or empty allows every file.multiple: allows more than one file per drop or pick (falseby default).maxSize: inclusive per-file limit as aByteSize, such asByteSize.mebibytes(5). No limit when omitted.disabled: disablestrigger, setsdata-disabled, hidesdata-drag-overand ignores drags, drops and picks.rootkeeps its drop target so the browser does not open a dropped file in the tab.
Signature
declare const FileDropProps: Struct<{ readonly accept: optional<$Array<String>>; readonly disabled: optional<Boolean>; readonly maxSize: optional<ByteSize>; readonly multiple: optional<Boolean>;}>FileDropRejectReason
unstableWhy FileDrop rejected a file batch.
Signature
declare const FileDropRejectReason: Literals<readonly ["too-many-files", "invalid-type", "file-too-large"]>