Skip to content

Storybook: Add patterns for selecting local files - #86732

Open
ciampo wants to merge 56 commits into
trunkfrom
codex/file-selection-stories
Open

ciampo wants to merge 56 commits into
trunkfrom
codex/file-selection-stories

Conversation

@ciampo

@ciampo ciampo commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

What?

See #84181. Related: #65333 and #76935.

Adds a Selecting Local Files pattern page to Storybook. It shows how to let people choose files with FormFileUpload and @wordpress/ui: a drop area for one file, a list of several files, upload status, a panel that accepts dropped files, importing a file as soon as it's chosen, choosing a file for a form, and a menu with media sources.

Why?

Gutenberg chooses files in several ways, often with its own <input type="file">, and each screen solves focus, validation, and feedback on its own. The field design agreed on in this PR (James and Lucy) and Ann's prototype give one look for all of them, so the patterns can show it.

How?

  • Every example opens the native picker through FormFileUpload and composes @wordpress/ui components: Fieldset, Button, IconButton, Menu, Dialog, Progress, Badge, Card, EmptyState, and ValidityIndicator.
  • A shared row shows a chosen file: a thumbnail or file icon, the name, the type and size, and a visible remove button named after the file. While its text fits on one line, the row is the height of an input. A long name wraps instead of being cut, so it can be read in full on a touch screen, and the type and size move below it.
  • The drop area follows the field design: label, then the area, then the description, with the drop hint in the description. It's a control, so it uses the same neutral interactive tokens as an outline Button. The rows show chosen files, so they use surface tokens.
  • Each example checks every file, because accept only filters the picker. It also announces changes with speak() and moves focus to a sensible place after a removal or a retry.

Testing Instructions

Open Design System → Patterns → Selecting Local Files in Storybook. For each example:

  1. Drop area: choose a PNG, then drop another. The name and thumbnail change. Choose a text file: an error appears and the previous image stays. Remove the image: focus returns to "Add an image".
  2. Drop area for several files: add more than 5 files, including one over 2 MB. Five are added, and the error names the others and why. The area then reads "5 of 5 files added".
  3. Long names: make the canvas about 300px wide, then add a file with a long name. The whole name shows, over several lines, and nothing is cut.
  4. Upload status: add a PDF and two images. The images say "Uploaded". The PDF fails on its first try and shows "Couldn't upload" and "Retry". Select "Retry" and it uploads.
  5. Panel drop target: drop a .ttf or .woff2 file anywhere on the Fonts card. It's added to the list.
  6. Import on choose: choose a pattern exported as JSON. The button shows a spinner, then a snackbar confirms the import. Choose any other JSON file: an error appears below the button.
  7. Choose, then submit: select "Import" without a file. The error asks for one and focus moves to "Choose file…". Choose a JSON file and import it.
  8. Media sources: open "Select or add image". "Open Media Library…" opens a stand-in dialog; pick an image. "Upload…" opens the picker. The chosen image appears with a remove button.

Testing Instructions for Keyboard

  • Drop area: Tab to the drop area and press Space to open the picker. Choose a file, then Tab to the remove button and press Enter. Focus returns to the drop area.
  • Several files: remove a file in the middle of the list. Focus moves to the next row's remove button.
  • Upload status: after the PDF fails, press Enter on "Retry". Focus moves to "Cancel upload of …".
  • Media sources: on "Select or add image", press Down Arrow, then Enter on "Open Media Library…". Choose an image with Enter. Focus returns to "Select or add image".

Follow-ups

These came up while reviewing the examples. Each one belongs outside this PR.

Decisions for the UI package

  • Announcing a repeated rejection. When someone picks the same invalid file twice, the error text doesn't change, so assistive technology hears nothing the second time. The error stays visible and is linked to the control with aria-describedby, which is also how the @wordpress/ui form controls behave. Whether an identical, visible error should be announced again needs one decision across the package.
  • Checking files against accept. accept only filters the picker, so each example checks files again, by extension, by MIME type, or both. A file whose type the browser left empty, such as some PNGs, is then accepted by one example and rejected by another. One shared check belongs in FormFileUpload or the file control planned in UI: Add a file input control #84181.

Component bugs

  • Tooltip (@wordpress/ui): the popup has no maximum width or wrapping, so a remove button named after a long filename widens the page at 320px.
  • Snackbar (@wordpress/components): a long unbroken word, such as a pattern title, widens the page at 320px.
  • DropZone (@wordpress/components): when a drag carries both files and HTML, the overlay turns on only if onHTMLDrop is set, so a drop zone that takes only files ignores the drag. Its own comment says Windows Chrome sends both.

Storybook

  • Dark theme on docs pages: menus and dialogs portal outside the docs page's theme, so they stay light in the dark theme.

Screenshots

Drop area, empty Drop area, image chosen and focused
Cover image field with a dashed area reading Add an image Cover image field showing waves.png, its type and size, and a remove button, with a focus ring around the whole field
Several files Several files, narrow
Supporting documents with three one-line rows, one with a long filename shown in full The same rows at 320px: the long filename wraps over three lines and its type and size sit below it
Long names: cut before, wrapped now
Before (b49d477) After
At 360px, the uploaded row cuts the name to holiday-pho… and the failed row cuts it to insurance… At 360px, both names show in full, with the details, badge, and Retry below them
Drop area: rejected file, dragging, and disabled
Rejected file Dragging a file over the field
waves.png stays chosen while an error says brief.txt isn't a PNG Blue overlay over the whole field reading Drop to use as the cover image

Disabled cover image field

Limits and upload status

Five files added, the area reads 5 of 5 files added, and an error names the three rejected files and why

Uploading Uploaded and failed
Two rows with short progress bars in place of the type and size, and weaker names The image row says PNG, 390.6 KB, Uploaded; the PDF row shows a Couldn't upload badge and Retry
Panel drop target
Empty With fonts
Fonts card with an empty state asking to drop font files Fonts card listing two fonts and an error for brief.txt
Import on choose, choose then submit, and media sources
Imported on choose Submitted without a file
Snackbar below the button reading Imported Hero from JSON Pattern file field with an error asking to choose a file, and focus on Choose file
Media source menu Image chosen from the library
Select or add image menu with Open Media Library and Upload forest-trail.svg chosen below the menu trigger
Dark theme and right-to-left

Upload status in the dark theme and right-to-left, with an uploaded row and a failed row

Use of AI Tools

OpenAI Codex wrote the first version of the examples. Claude Code reworked them to match the shared designs, added the upload, import, panel, and media source examples, and captured the screenshots. Independent review rounds with GPT-6.2 Astra and Claude Opus 5.5 found the accessibility, validation, token, and layout fixes in the later commits, and the follow-ups above.

🤖 Generated with Claude Code

@github-actions github-actions Bot added the [Package] UI /packages/ui label Nov 12, 2026
@github-actions

github-actions Bot commented Nov 12, 2026 •

Copy link
Copy Markdown

🤖 PR meta 🤖

Preview in WordPress Playground Preview in Storybook

🎉 Props

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: ciampo <mciampini@git.wordpress.org>
Co-authored-by: jameskoster <jameskoster@git.wordpress.org>
Co-authored-by: jasmussen <joen@git.wordpress.org>
Co-authored-by: simison <simison@git.wordpress.org>
Co-authored-by: mirka <0mirka00@git.wordpress.org>
Co-authored-by: aduth <aduth@git.wordpress.org>
Co-authored-by: lucyneb <lucybbn@git.wordpress.org>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.


Props Bot commands

You can trigger Props Bot actions by commenting on this PR:

  • @propsbot add <username> to give props to a GitHub user. Requires write access.
  • @propsbot remove <username> to leave a user out of the props. Requires write access.
  • @propsbot refresh to refresh this list. Adding the props-bot label works too.

Updated as activity occurs, without notifying anyone named here. Add the props-bot label to refresh.

📦 Bundle size

Size Change: 0 B

Total Size: 8.73 MB

ed1a6b9 Run

🏁 Flaky tests

Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

Overlay menu interactions in Safari (@WebKit) in /test/e2e/specs/editor/blocks/navigation-frontend-interactivity.spec.js, passed after 1 failed attempt.
TimeoutError: locator.click: Timeout 10000ms exceeded.
Call log:
  - waiting for getByRole('region', { name: 'Editor top bar' }).getByRole('button', { name: 'Save', exact: true })
    - locator resolved to <button type="button" aria-disabled="true" class="components-button editor-post-publish-button editor-post-publish-button__button is-primary is-compact">Save</button>
  - attempting click action
    2 × waiting for element to be visible, enabled and stable
      - element is not enabled
    - retrying click action
    - waiting 20ms
    2 × waiting for element to be visible, enabled and stable
      - element is not enabled
    - retrying click action
      - waiting 100ms
    15 × waiting for element to be visible, enabled and stable
       - element is not enabled
     - retrying click action
       - waiting 500ms

    at Editor.saveSiteEditorEntities (/home/runner/work/gutenberg/gutenberg/packages/e2e-test-utils-playwright/src/editor/site-editor.ts:39:22)
    at /home/runner/work/gutenberg/gutenberg/test/e2e/specs/editor/blocks/navigation-frontend-interactivity.spec.js:40:4
should keep the overlay intact when reopened before the close cleanup runs in /test/e2e/specs/editor/blocks/image.spec.js, passed after 1 failed attempt.
Error: clock.pauseAt: Error: Cannot fast-forward to the past
    at ClockController._innerFastForwardTo (eval at evaluate (:370:30), <anonymous>:206:13)
    at ClockController.pauseAt (eval at evaluate (:370:30), <anonymous>:132:16)
    at async <anonymous>:381:23
    at ClockController._innerFastForwardTo (eval at evaluate (:370:30), <anonymous>:206:13)
    at ClockController.pauseAt (eval at evaluate (:370:30), <anonymous>:132:16)
    at async <anonymous>:381:23
    at /home/runner/work/gutenberg/gutenberg/test/e2e/specs/editor/blocks/image.spec.js:1403:21

ed1a6b9 Run

@ciampo ciampo added the [Type] Developer Documentation Documentation for developers label Nov 12, 2026
@ciampo ciampo self-assigned this Nov 12, 2026
@ciampo
ciampo marked this pull request as ready for review November 12, 2026 10:53
@ciampo
ciampo requested a review from a team as a code owner November 12, 2026 10:53
@ciampo
ciampo requested a review from a team November 12, 2026 10:53
@jasmussen

Copy link
Copy Markdown
Contributor

Forgive me for missing something obvious, but is there a way we can vertically centre the "Choose Files" button in the InputControl variant? It looks broken as-is. Perhaps even some light styling of the button inside?

@ciampo

ciampo commented Nov 12, 2026

Copy link
Copy Markdown
Contributor Author

That's what I had originally proposed, but @mirka 's reply (Personally I don't think we need "styling" at the moment) made me choose this approach, where we'd likely never render the file input "raw", but always composed with another UI element (button, menu item, potentially drag area...)

@simison

simison commented Nov 12, 2026

Copy link
Copy Markdown
Member

Do we need the input example for some other reasons here that I don't see? The copy in PR suggests technical example but I don't think the input version is something we should be promoting — rather in its current visual state we should warn against it? I.e. it really doesn't look good to me design wise.

image

@simison

simison commented Nov 12, 2026 •

Copy link
Copy Markdown
Member

Would a drag area example be good for "patterns"? 🤔 Visually it can differ a lot depending on specifics but there are UX and accessibility quirks that are probably worth documenting so that folks get those right.

@jasmussen

Copy link
Copy Markdown
Contributor

I don't think the input version is something we should be promoting

This is the key for me. IMO either we don't promote it, or we style it so it doesn't look broken. I don't have a strong opinion either way, so my instinct here is less against what Lena proposed, and more transversal.

@ciampo

ciampo commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor Author

At this point in time, this PR was more of an experimentation to get alignment on the approach (hence the "technical example" wording). Happy to remove that "raw" input example from the patterns.

Re. drag and drop, I've kept it out of this first iteration because base ui doesn't offer it (yet, it should be WIP). But I can look into it and see what we can do in the meantime.

@mirka

mirka commented Nov 12, 2026

Copy link
Copy Markdown
Member

What I meant in the issue is that the file input will be visually hidden, therefore it itself doesn't need styling. I agree the docs should show styled patterns worth reusing, not raw examples that just show code-level composition.

I also still think it's worth considering some kind of abstraction, rather than promote direct input use. There are some gotchas that have accumulated in FormFileUpload, for example the accept compat. The onClick trick to force a change event (see the prop description) is also worth considering as a built-in convenience.

@ciampo

ciampo commented Nov 12, 2026

Copy link
Copy Markdown
Contributor Author

I audited the custom file inputs and FormFileUpload consumers, here are some findings that could be useful to guide API design:

  • FormFileUpload already supports custom triggers, including menu items, through render({ openFileDialog }). It adds a wrapper and controls where the hidden input lives. Some consumers need the input separate from the trigger, especially outside a menu that closes before selection finishes.
  • It exposes no input ref or general input-props passthrough. disabled reaches the default button, but not the input. The next API should make input configuration and consistent disabled behavior easier.

There is also a form with a visible, labeled file input and a separate Import button. That doesn't establish a new design requirement, but we should account for it before assuming inputs will always be hidden. I'm happy to remove the current visible Input/InputControl examples while we agree on the recommended pattern.

Re. drag and drop, I can start from the existing DropZone docs and consumers (MediaPlaceholder already processes picked and dropped files through the same APIs), while we wait for Base UI primitives.

The examples will / should cover keyboard access, consistent validation and disabled behavior, selection/error feedback, cancellation, and choosing the same file again.

Re. having a visual component vs a hook, I guess that will depend on usage needs (@jasmussen also mentioned working on a related design)

@ciampo
ciampo force-pushed the codex/file-selection-stories branch from 59e0c55 to 14ffb8e Compare November 12, 2026 18:49
@ciampo ciampo changed the title Storybook: Show how to select local files from a field or menu Storybook: Show how to select local files from a button, menu, or drop area Nov 12, 2026
@ciampo

ciampo commented Nov 12, 2026

Copy link
Copy Markdown
Contributor Author

Updated the PR as per the plan proposed above.

@aduth

aduth commented Nov 13, 2026

Copy link
Copy Markdown
Member

Related prior discussion about standardization: #76935, #65333

@ciampo

ciampo commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor Author

Good links, @aduth .

It feels like we should get a clear consensus on the file upload patterns that we want to support. I will then implement them as Storybook examples in this PR, and that should be a great starting point towards cleaning up / improving / standardising this pattern.

@jasmussen / @jameskoster , happy to collaborate on those design specs!

@jasmussen

Copy link
Copy Markdown
Contributor

I'd love for us to at least loop in @lucyneb on design specs if possible, as there is some overlap with somthing she and Ann (AFK) have worked on.

@jameskoster

jameskoster commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

I wonder if the drop area example could be simpler. Does it need the button, or could the whole surface be a click target? Also wondering if it could be formatted like most other controls in the system; label -> input -> help text/details.

Screenshot 2026-11-17 at 10 40 01

I think the dashed border (which has received pushback before) is optional so long as the control has text explaining users can drop items.

@lucyneb

lucyneb commented Nov 18, 2026

Copy link
Copy Markdown

I wonder if the drop area example could be simpler. Does it need the button, or could the whole surface be a click target? Also wondering if it could be formatted like most other controls in the system; label -> input -> help text/details.

@jameskoster Oh ya, that's way better. Can you share the exact specs you used? whichever format, I'll incorporate into my latest designs.

@jameskoster

Copy link
Copy Markdown
Contributor

@lucyneb the specs could change during implementation, but it's basically a Field with a custom 'input'.

  • The 'input' has lg padding (16px)
  • The thumbnail and icon container is size md (32px)
  • The icon is the default size (24px)
  • I used surface tokens for the colors which might have been a mistake depending on the accessibility story

We might end up with something more like this, especially if we want this control to share more DNA with others:

Screenshot 2026-11-19 at 13 17 23

@lucyneb

lucyneb commented Nov 23, 2026

Copy link
Copy Markdown

@jameskoster I added this style to my prototype, attaching screenshots. Do you think we should also consider adding a remove button?
image
image

@jameskoster

Copy link
Copy Markdown
Contributor

Yes I think that's a good idea. An IconButton as the suffix might work. Important to note that the control is only about file selection prior to submission; so removing would clear the selection but doesn't manage uploaded files.

`Fieldset.Root` now lets its content shrink (#87300), so the examples
no longer need their own reset.
A cut filename can't be read in full on a touch screen, and two files
with the same start look the same. Let the name wrap instead, with a
tighter line height so a wrapped name reads as one item. When the type
and size, or a status and its Retry button, don't fit beside the name,
they move below it together.

The picker button now builds its name from the text it shows, with a
visually hidden "Replace" before it, so the type and size reach
assistive technology. Keep the progress bar on whole pixels so every
bar looks the same thickness, and keep the "Uploaded" note.
- Queue cancel and retry messages with the upload results, so a
  cancellation made right after adding a file is read last.
- Give each file list an explicit list role. Safari drops it when the
  markers are hidden.
- Let a long filename in an error message wrap, so the page doesn't
  scroll sideways at 320px.
The PNG and JSON fields accept a file by its MIME type or extension,
as their `accept` attribute does. The stand-in importer now requires
the same fields as the Patterns screen, so any other JSON file shows
the error.
Only the first try for a PDF fails, so the caller can say so, and the
upload no longer needs to store its attempt count.
Each entry gets its own ref when it's added, so the list examples no
longer keep a map of buttons and a callback that fills it.
The type comes from the filename, so a long extension could still
make the page scroll sideways.
Some images, such as HEIC photos, can't be shown in every browser. The
row now falls back to the file icon instead of an empty thumbnail.
The stand-in importer now also rejects a non-string `syncStatus`, and
lets `JSON.parse()` throw to the caller, which already shows the error.
A long confirmation grew upward from the bottom of a fixed frame and
covered the Import button. In the page flow it can't, and the frame
is no longer needed.
`DropZone` after `Card.Content` made the content a middle section,
which has no bottom padding.
A state updater can run twice, so it shouldn't create IDs or refs.
A browser leaves the type empty when it can't tell what a file is. An
image then still gets its thumbnail, and any other file falls back to
the file icon.
If the remove button has focus when the import finishes, move focus to
"Choose file…" before the row is removed.
Each example repeated the same live region and `ValidityIndicator`.
`FilePickerError` keeps the region mounted, so a new message is still
announced.
@ciampo ciampo changed the title Storybook: Add patterns for choosing and uploading files Storybook: Add patterns for selecting local files Nov 26, 2026
The drop area is a control, and surface tokens aren't for clickable
parts. It now uses the neutral interactive tokens, like an input
without a border, with matching hover and disabled states. The rows
show chosen files, so they keep the surface tokens.
- Compare the drop area with an outline Button in the docs, which uses
  the same tokens, and drop a sentence that no longer holds.
- Gray out a full or disabled drop area in forced-colors mode, as
  Button does, since it stays focusable.
- Give text inside a picker button the button's weaker color.
- Give the stand-in Media Library items their padding back and the same
  interactive tokens.
@ciampo

ciampo commented Nov 26, 2026

Copy link
Copy Markdown
Contributor Author

Thanks, @jameskoster !

I'd keep "Uploaded". The other cues are easy to miss if you look away, and a screen reader user going through the list later wouldn't hear the announcement again. It's also how you tell an uploaded file from one that's just chosen.

To make room, rows now wrap instead of cutting the name. I also switched the drop area to the interactive tokens, like you suspected. Screenshots and follow-ups are in the description.

I think this PR is now ready for an actual review @jameskoster @lucyneb @jasmussen @WordPress/gutenberg-components

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Package] UI /packages/ui [Type] Developer Documentation Documentation for developers

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants