Skip to main content
cngx-src documentation

CngxFileDrop

DirectivePrimaryv0.1.0WCAG AA

projects/forms/input/file-drop.directive.ts

Import#

import { CngxFileDrop } from '@cngx/forms/input'

Description#

Headless drag-and-drop file behavior on any element.

Handles dragenter/dragover/dragleave/drop events, validates files against accept and maxSize, and provides a browse() method for programmatic file picker access.

The host element is itself the activation control: click, Enter, or Space open the file picker, so it always carries role="button". This affordance is unconditional by design - there is no passive drop-only mode, and you must not nest interactive controls inside the zone.

<div cngxFileDrop [accept]="['image/*']" [maxSize]="5_000_000"
     #drop="cngxFileDrop" (filesChange)="upload($event)">
  @if (drop.dragging()) {
    <p>Drop files here</p>
  } @else {
    <p>Drag files here, or click / press Enter to browse</p>
  }
</div>

https://cngxjs.github.io/cngx/examples/#/forms/input/file-drop/image-upload

Metadata#

Host#

Index#

Inputs#

accept#string[]
input()

Accepted MIME types (e.g. 'image/*', '.pdf'). Empty = all.

default []
ariaLabel#string | undefined
input()

aria-label for the drop zone. Falls back to the global config (ariaLabels.fileDropZone), then the English default.

maxFiles#number | undefined
input()

Maximum number of accepted files. undefined = no limit. Falls back to global config.

maxSize#number | undefined
input()

Maximum file size in bytes. undefined = no limit. Falls back to global config.

multiple#boolean
input()

Allow multiple files.

default false

Bind an upload async state - shows busy/error/progress during upload. When set, uploading derives from state.isBusy() and uploadProgress from state.progress(). Drop/browse is disabled while uploading.

Outputs#

filesChange#File[]
output()

Emitted when valid files are dropped/selected.

rejectedChange#FileRejection[]
output()

Emitted when files are rejected.

Instance Properties#

dragging#Signal
Readonly

Whether the element is currently being dragged over.

this.draggingState.asReadonly()
files#Signal
Readonly

The dropped/selected files after validation.

this.filesState.asReadonly()
rejected#Signal
Readonly

Files rejected by accept/maxSize validation.

this.rejectedState.asReadonly()

Methods#

browse#void

Opens the native file picker programmatically. No-op while uploading.

clear#void

Clears the current file selection.

HostBindings#

BindingExpression
[class.cngx-file-drop--dragging]dragging()
[class.cngx-file-drop--has-files]files().length > 0
[class.cngx-file-drop--uploading]uploading()
[attr.aria-busy]uploading() || null
[attr.aria-disabled]uploading() || null
[attr.aria-label]resolvedAriaLabel()
[attr.tabindex]tabIndex()

HostListeners#

EventHandler
(dragenter)dragenter()
(dragover)dragover()
(dragleave)dragleave()
(drop)drop()
(click)click()
(keydown.enter)keydown.enter()
(keydown.space)keydown.space()