Skip to content

[Dropzone] Add multiple file preview #2747

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: 2.x
Choose a base branch
from

Conversation

phasdev
Copy link

@phasdev phasdev commented May 18, 2025

Q A
Bug fix? no
New feature? yes
Docs? no
Issues Fix #2567 Fix #2552
License MIT

Updated Dropzone component to support multiple file preview.
No breaking changes. Backwards compatible by default. User can opt-in to new features.

Example:

$form = $this->createFormBuilder()
    ->add('files', DropzoneType::class, [
        'multiple' => true,
        'preview' => [
            'style' => 'block',
            'can_toggle_placeholder' => true,
            'can_open_file_picker' => true,
        ]
    ])
    ->getForm();

Demo repo


Basic support for legacy style:
(File list shown in tooltip by setting file name element's title attribute)

New inline style:

New block style:

New block style with preview.can_toggle_placeholder = false:


  • Added preview.style option. The (default) legacy option reproduces existing behaviour. The block and inline options enable new features.

  • Added preview.can_toggle_placeholder option to control whether placeholder remains visible when preview is rendered. Can be true, false or auto. The auto option will hide placeholder for single-file uploads; adding additional files will show placeholder again.

  • Added preview.can_open_file_picker option to control whether clicking preview container opens input file picker.

  • New component handles drop event directly, making cross-browser behaviour consistent (#2552); i.e. it does not rely on file input for drop handling.

  • Each selected file has its own preview. Thumbnails are shown for image files and a placeholder for other files. Files can be added/removed individually.

  • File preview template, button image and placeholder image markup can be overridden by adding content to corresponding form theme blocks, allowing markup customization without having to override entire form theme.

  • File input hidden to improve component style flexibility[1]. Input still visible to screen readers.

  • File input wrapped within label so label can obtain focus, allowing keyboard/screen reader navigation.

  • Component is outlined on drag hover, providing feedback that mouse is detected within dropzone.

  • Legacy code clearly marked so it can be removed from component during next major release (if desired).


Related issues:

#2567
#2552
#1025
#7

Related PR's:

#2704
#2642
#512

@carsonbot carsonbot added Dropzone Feature New Feature Status: Needs Review Needs to be reviewed labels May 18, 2025
Copy link

github-actions bot commented May 18, 2025

📊 Packages dist files size difference

Thanks for the PR! Here is the difference in size of the packages dist files between the base branch and the PR.
Please review the changes and make sure they are expected.

FileBefore (Size / Gzip)After (Size / Gzip)
Dropzone
controller.d.ts 675 B / 314 B 1.45 kB+120% 📈 / 520 B+66% 📈
controller.js 3.12 kB / 816 B 9.52 kB+205% 📈 / 2.07 kB+160% 📈
style.min.css 944 B / 468 B 3.75 kB+307% 📈 / 987 B+111% 📈

readonly previewFilenameTarget: HTMLDivElement;
readonly previewImageTarget: HTMLDivElement;
readonly placeholderTarget: HTMLElement;
readonly previewTargets: HTMLElement[];
Copy link
Author

@phasdev phasdev May 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Possible breaking change? Some controller targets have been removed and I've changed types on placeholderTarget and previewTarget so they're more generic. For example, the new placeholder is a <label> not a <div>, so I changed placeholderTarget type from HTMLDivElement to the more generic HTMLElement.

Since the documentation suggests using event listeners to extend behaviour I assumed the controller class wouldn't be extended directly, so perhaps these aren't breaking changes.

@phasdev
Copy link
Author

phasdev commented May 18, 2025

I can add tests/docs when we're nearly ready to merge. Didn't want to add tests for features that may be dropped during review.

@phasdev phasdev force-pushed the dropzone-multiple branch 2 times, most recently from c137c69 to 79759ae Compare May 18, 2025 06:45
@phasdev phasdev force-pushed the dropzone-multiple branch from 79759ae to 78a7709 Compare May 18, 2025 21:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Dropzone Feature New Feature Status: Needs Review Needs to be reviewed
Projects
None yet
2 participants