Repository navigation
[Dropzone] Render the preview of each file from a template in multiple mode - #4144
Merged
Merged
Conversation
Contributor
📊 Packages dist files size differenceThanks for the PR! Here is the difference in size of the packages dist files between the base branch and the PR.
|
||||||||||||
ker0x
force-pushed
the
feat/dropzone-preview-template
branch
from
October 26, 2026 10:29
ec9e3ee to
9433102
Compare
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
force-pushed
the
feat/dropzone-preview-template
branch
from
November 2, 2026 18:36
9433102 to
1dc30b0
Compare
Member
|
Thank you @ker0x. |
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Second step of the RFC in #4142.
In
multiplemode, 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
previewItemTemplatetarget. When it is present, each preview is a clone of the template's root element, and the controller fills the elements marked withdata-dropzone-slot, the root element included:image: the image preview, as thesrcof an<img>or as the background image of any other element. It has thehiddenattribute 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 samearia-labelas 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_templateblock inside the container inmultiplemode. 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 inmultiplemode only. The new unit tests and themultiplemode PHPUnit test fail on the base branch.