Skip to content

[Dropzone] Render the preview of each file from a template in multiple mode - #4144

Merged
Kocal merged 1 commit into
symfony:3.xfrom
ker0x:feat/dropzone-preview-template
Nov 2, 2026
Merged

Kocal merged 1 commit into
symfony:3.xfrom
ker0x:feat/dropzone-preview-template

Conversation

@ker0x

@ker0x ker0x commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor
Q A
Bug fix? no
New feature? yes
Deprecations? no
Documentation? yes
Issues #4142
License MIT

Second step of the RFC in #4142.

In multiple mode, the controller builds the preview of each file in JS, with fixed markup and CSS classes (dropzone-preview-list-item, dropzone-preview-image, ...). An app can only restyle those classes in CSS. It cannot use utility classes, put an icon component in the remove button, or add markup such as the file size.

This adds an optional previewItemTemplate target. When it is present, each preview is a clone of the template's root element, and the controller fills the elements marked with data-dropzone-slot, the root element included:

  • image: the image preview, as the src of an <img> or as the background image of any other element. It has the hidden attribute until the preview is loaded, and keeps it for files that are not images.
  • filename: the file name, as text.
  • remove: removes the file on click, with the same aria-label as the default button.

All slots are optional, and a slot can be on several elements. The template must contain exactly one root element: otherwise the controller does not connect and Stimulus logs the error, before any file is added. To render the template without copying the whole widget, the form theme renders a new, empty dropzone_preview_item_template block inside the container in multiple mode. An app overrides only that block:

{% block dropzone_preview_item_template %}
    <template data-symfony--ux-dropzone--dropzone-target="previewItemTemplate">
        <li class="relative size-20">
            <div data-dropzone-slot="image" class="size-full bg-cover bg-center"></div>
            <span data-dropzone-slot="filename" class="sr-only"></span>
            <button type="button" data-dropzone-slot="remove" class="absolute top-1 right-1">
                {{ ux_icon('lucide:x') }}
            </button>
        </li>
    </template>
{% endblock %}

Without the block override, nothing changes: no <template> is rendered and the JS-built markup stays the default.

Tests: unit tests for the template (slots, slots on the root element, a slot on several elements, escaping of the file name, remove, <img> and background previews, no slots, a template with no or several root elements, and a remove button without type that must not submit the form), and PHPUnit tests that the block is rendered inside the container in multiple mode only. The new unit tests and the multiple mode PHPUnit test fail on the base branch.

@carsonbot carsonbot added Documentation Improvements or additions to documentation Dropzone Feature New Feature Status: Needs Review Needs to be reviewed labels Oct 26, 2026
@github-actions

github-actions Bot commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

📊 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 1.34 kB / 544 B 1.57 kB+17% 📈 / 592 B+9% 📈
controller.js 7.61 kB / 2.18 kB 9.03 kB+19% 📈 / 2.56 kB+17% 📈

@ker0x
ker0x force-pushed the feat/dropzone-preview-template branch from ec9e3ee to 9433102 Compare October 26, 2026 10:29
ker0x added a commit to ker0x/symfony-ux that referenced this pull request Oct 26, 2026
| Q              | A
| -------------- | ---
| Bug fix?       | no
| New feature?   | yes
| Deprecations?  | no
| Documentation? | yes
| Issues         | Fix symfony#4142
| License        | MIT

Sixth and last step of the RFC in symfony#4142. Based on symfony#4143, symfony#4145, symfony#4144, symfony#4146, symfony#4148 and symfony#4149, replayed in that order: only the last commit belongs to this PR.

The two modes of the controller had grown into two code paths. Single-file mode showed one file with the preview of the form theme, cleared the input when the controller connected, and dispatched `dropzone:clear`; `multiple` mode kept a list, re-added the files already in the input on connect, and never dispatched `dropzone:clear`. Each feature of the RFC had to be written twice, and the modes differed in ways nobody chose: a prevented or rejected selection emptied the drop zone in single-file mode but kept the files in `multiple` mode, and the preview template only worked in `multiple` mode.

Single-file mode is now the stored list of files capped at one, and both modes go through the same steps for each selection: the input is put back to the stored files right away (also while `dropzone:pre-add` listeners are processing), the dropped files are checked against `accept`, the `dropzone:pre-add` listeners run, then the files are added (single-file mode: the new file replaces the previous one, and the files over the first one are rejected with `max_files`), the input is updated, the preview is rendered and `dropzone:change` is dispatched.

| Case                                              | Before                                         | After
| ------------------------------------------------- | ---------------------------------------------- | ---
| Single-file mode, controller connects with a file | input emptied, `dropzone:clear`                | file kept and shown
| `multiple` mode, last file removed                | no `dropzone:clear`                            | `dropzone:clear`
| Form reset                                        | drop zone keeps showing the files              | cleared, `dropzone:clear`
| Single-file mode, selection prevented by a `dropzone:pre-add` listener | drop zone emptied     | previous file kept, as in `multiple` mode
| Single-file mode, preview template                | not used                                       | rendered in the preview, its remove slot clears
| `multiple` mode, selection of files already in the list | `dropzone:change`          | no event
| Drag of text or a link over the drop zone         | `dragging`                                     | ignored
| Drag over a child of the drop zone in Safari      | `dragging` ends (`relatedTarget` is `null`)    | `dragging` kept
| Image previews                                    | read again on every render, an outdated read can show the image of a replaced file | read once per file, never shown for another file

`dropzone:change` keeps its payload (a `File` in single-file mode, a `FileList` in `multiple` mode), to stay compatible: changing it is left for 4.0. The form theme now renders the `dropzone_preview_item_template` block and the `remove_label` value in both modes. The drag state keeps the elements a drag has entered, from `dragenter` and `dragleave`, instead of reading `relatedTarget`, which WebKit does not set, and forgets the elements removed during the drag. `onMultipleChange()` is merged into `onInputChange()`. `src/Dropzone/UPGRADE.md` explains the changes for code listening to `dropzone:clear` on connect or to `dropzone:change`, and for controllers that extend this one.

Tests: unit tests for a file kept on connect without `dropzone:clear`, a prevented selection and a pending one in single-file mode, a selection of several files in single-file mode, an outdated image read, a form reset, a prevented form reset, a disconnect during a form reset, the clear button after a failed connect, the preview template in single-file mode, `dropzone:clear` after the last removal in `multiple` mode, a selection that adds no file in `multiple` mode, one read per image, an image read again after a failed read, a drag without files, a drag over a child without `relatedTarget`, and a drag over an element removed during the drag; PHPUnit tests for the template block and the `remove_label` value in single-file mode; Playwright tests for a form reset and the last removal in `multiple` mode. The new tests fail on the base of this PR. Two tests of the base changed: the single-file mode `file chosen` test now picks the file with a writable `files` property like the other tests, as `@testing-library/user-event` makes it read-only while the controller now writes the input on every selection; and the test of a prevented selection after a pending one now expects the previous file to be kept.
…le` mode

| Q              | A
| -------------- | ---
| Bug fix?       | no
| New feature?   | yes
| Deprecations?  | no
| Documentation? | yes
| Issues         | symfony#4142
| License        | MIT

Second step of the RFC in symfony#4142.

In `multiple` mode, the controller builds the preview of each file in JS, with fixed markup and CSS classes (`dropzone-preview-list-item`, `dropzone-preview-image`, ...). An app can only restyle those classes in CSS. It cannot use utility classes, put an icon component in the remove button, or add markup such as the file size.

This adds an optional `previewItemTemplate` target. When it is present, each preview is a clone of the template's root element, and the controller fills the elements marked with `data-dropzone-slot`, the root element included:

- `image`: the image preview, as the `src` of an `<img>` or as the background image of any other element. It has the `hidden` attribute until the preview is loaded, and keeps it for files that are not images.
- `filename`: the file name, as text.
- `remove`: removes the file on click, with the same `aria-label` as the default button.

All slots are optional, and a slot can be on several elements. The template must contain exactly one root element: otherwise the controller does not connect and Stimulus logs the error, before any file is added. To render the template without copying the whole widget, the form theme renders a new, empty `dropzone_preview_item_template` block inside the container in `multiple` mode. An app overrides only that block:

```twig
{% block dropzone_preview_item_template %}
    <template data-symfony--ux-dropzone--dropzone-target="previewItemTemplate">
        <li class="relative size-20">
            <div data-dropzone-slot="image" class="size-full bg-cover bg-center"></div>
            <span data-dropzone-slot="filename" class="sr-only"></span>
            <button type="button" data-dropzone-slot="remove" class="absolute top-1 right-1">
                {{ ux_icon('lucide:x') }}
            </button>
        </li>
    </template>
{% endblock %}
```

Without the block override, nothing changes: no `<template>` is rendered and the JS-built markup stays the default.

Tests: unit tests for the template (slots, slots on the root element, a slot on several elements, escaping of the file name, remove, `<img>` and background previews, no slots, a template with no or several root elements, and a remove button without type that must not submit the form), and PHPUnit tests that the block is rendered inside the container in `multiple` mode only. The new unit tests and the `multiple` mode PHPUnit test fail on the base branch.

The unit tests now stop the Stimulus applications after each test, with the same code as symfony#4146, and share the `selectFiles()` helper of symfony#4143.
@Kocal
Kocal force-pushed the feat/dropzone-preview-template branch from 9433102 to 1dc30b0 Compare November 2, 2026 18:36
@Kocal

Kocal commented Nov 2, 2026

Copy link
Copy Markdown
Member

Thank you @ker0x.

@Kocal
Kocal merged commit c8f5331 into symfony:3.x Nov 2, 2026
38 checks passed
@ker0x
ker0x deleted the feat/dropzone-preview-template branch November 2, 2026 21:38
Kocal pushed a commit to ker0x/symfony-ux that referenced this pull request Nov 2, 2026
| Q              | A
| -------------- | ---
| Bug fix?       | no
| New feature?   | yes
| Deprecations?  | no
| Documentation? | yes
| Issues         | Fix symfony#4142
| License        | MIT

Sixth and last step of the RFC in symfony#4142. Based on symfony#4143, symfony#4145, symfony#4144, symfony#4146, symfony#4148 and symfony#4149, replayed in that order: only the last commit belongs to this PR.

The two modes of the controller had grown into two code paths. Single-file mode showed one file with the preview of the form theme, cleared the input when the controller connected, and dispatched `dropzone:clear`; `multiple` mode kept a list, re-added the files already in the input on connect, and never dispatched `dropzone:clear`. Each feature of the RFC had to be written twice, and the modes differed in ways nobody chose: a prevented or rejected selection emptied the drop zone in single-file mode but kept the files in `multiple` mode, and the preview template only worked in `multiple` mode.

Single-file mode is now the stored list of files capped at one, and both modes go through the same steps for each selection: the input is put back to the stored files right away (also while `dropzone:pre-add` listeners are processing), the dropped files are checked against `accept`, the `dropzone:pre-add` listeners run, then the files are added (single-file mode: the new file replaces the previous one, and the files over the first one are rejected with `max_files`), the input is updated, the preview is rendered and `dropzone:change` is dispatched.

| Case                                              | Before                                         | After
| ------------------------------------------------- | ---------------------------------------------- | ---
| Single-file mode, controller connects with a file | input emptied, `dropzone:clear`                | file kept and shown
| `multiple` mode, last file removed                | no `dropzone:clear`                            | `dropzone:clear`
| Form reset                                        | drop zone keeps showing the files              | cleared, `dropzone:clear`
| Single-file mode, selection prevented by a `dropzone:pre-add` listener | drop zone emptied     | previous file kept, as in `multiple` mode
| Single-file mode, preview template                | not used                                       | rendered in the preview, its remove slot clears
| `multiple` mode, selection of files already in the list | `dropzone:change`          | no event
| Drag of text or a link over the drop zone         | `dragging`                                     | ignored
| Drag over a child of the drop zone in Safari      | `dragging` ends (`relatedTarget` is `null`)    | `dragging` kept
| Image previews                                    | read again on every render, an outdated read can show the image of a replaced file | read once per file, never shown for another file

`dropzone:change` keeps its payload (a `File` in single-file mode, a `FileList` in `multiple` mode), to stay compatible: changing it is left for 4.0. The form theme now renders the `dropzone_preview_item_template` block and the `remove_label` value in both modes. The drag state keeps the elements a drag has entered, from `dragenter` and `dragleave`, instead of reading `relatedTarget`, which WebKit does not set, and forgets the elements removed during the drag. `onMultipleChange()` is merged into `onInputChange()`. `src/Dropzone/UPGRADE.md` explains the changes for code listening to `dropzone:clear` on connect or to `dropzone:change`, and for controllers that extend this one.

Tests: unit tests for a file kept on connect without `dropzone:clear`, a prevented selection and a pending one in single-file mode, a selection of several files in single-file mode, an outdated image read, a form reset, a prevented form reset, a disconnect during a form reset, the clear button after a failed connect, the preview template in single-file mode, `dropzone:clear` after the last removal in `multiple` mode, a selection that adds no file in `multiple` mode, one read per image, an image read again after a failed read, a drag without files, a drag over a child without `relatedTarget`, and a drag over an element removed during the drag; PHPUnit tests for the template block and the `remove_label` value in single-file mode; Playwright tests for a form reset and the last removal in `multiple` mode. The new tests fail on the base of this PR. Two tests of the base changed: the single-file mode `file chosen` test now picks the file with a writable `files` property like the other tests, as `@testing-library/user-event` makes it read-only while the controller now writes the input on every selection; and the test of a prevented selection after a pending one now expects the previous file to be kept.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Documentation Improvements or additions to documentation Dropzone Feature New Feature Status: Needs Review Needs to be reviewed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants