Skip to content

Storybook: Add patterns for selecting local files - #86732

Merged
ciampo merged 67 commits into
trunkfrom
codex/file-selection-stories
Dec 1, 2026
Merged

ciampo merged 67 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, Text, and ValidityIndicator.
  • A shared row shows a chosen file: a 40px thumbnail or file icon, the name with its type and size below it, and a visible remove button named after the file. The thumbnail, the first line of the name, and the remove button line up at the top. A long name wraps instead of being cut, so it can be read in full on a touch screen.
  • The drop area follows the field design: label, then the area, then the description. It's a control, so it uses the tokens and states of an outline neutral Button, including hover, press, and focus. The rows use Card's surface tokens, and all text uses Text variants as they are.
  • Each example checks every file, because accept only filters the picker. It states its limits up front: the description is linked to the picker button with aria-describedby, before any error.
  • Each handler renders its change with flushSync() before it moves focus, so the focused button already has its new name, description, and state. Changes are announced with speak().
  • Filenames keep their own direction on right-to-left pages: <bdi> in markup, and Unicode isolates in strings such as errors and tooltip labels.

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: the error clears and 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". Remove a file: the error stays, so the names to add again stay visible.
  3. Long names: make the canvas about 320px 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, and "Choose files…" moves from the empty state to below the list, with the supported formats.
  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, or one over 1 MB: 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 that isn't a pattern and select "Import": the error asks for a pattern, and focus moves to "Choose file…" again. Choose a pattern 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 …".
  • Panel drop target: Tab to "Choose files…" in the empty state, press Enter, and choose a font. Focus stays on "Choose files…", now below the list. Remove the font: focus returns to "Choose files…" in the empty state.
  • 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.
  • speak() (@wordpress/a11y): it removes anything that looks like an HTML tag, so a filename such as report <draft>.pdf is announced without <draft>. The page notes this limit.

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 with a 40px thumbnail, its type and size below the name, and a remove button, with a focus ring around the whole field and the hover background
Several files Several files, narrow
Supporting documents with three rows, each with a 40px thumbnail and the type and size below the name, one with a long filename shown in full The same rows at 320px: the long filename wraps over three lines, and its thumbnail and remove button stay level with its first line
Long names at 360px

At 360px, both names show in full, with the type, size, and Uploaded, or the 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
Three rows with progress bars below their names, and weaker names The image rows say PNG, size, and Uploaded below the name; 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, and Choose files below its description Fonts card listing two fonts, Choose files below the list with the supported formats, and an error for brief.txt
Import on choose, choose then submit, and media sources
Imported on choose Submitted without a file
Import pattern from JSON button with its 1 MB limit below it, and a snackbar 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, GPT-6.2 Sol, and Claude Opus 5.5 found the accessibility, validation, token, layout, and right-to-left 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: -788 B (-0.01%)

Total Size: 8.73 MB

📦 View Changed
Filename Size Change
build/scripts/core-data/index.min.js 78 kB -625 B (-0.8%)
build/scripts/editor/index.min.js 576 kB -163 B (-0.03%)

9f40c73 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.

ciampo and others added 15 commits November 30, 2026 15:55
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.
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.
Compare two layouts for long filenames with the current one: the type
and size always below the name, or always on the name's first line with
only the name wrapping. Remove the stories and the `layout` prop once a
layout is chosen.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Every file row now has one layout: a 40px thumbnail, the name, and the
type and size on a line below it. The thumbnail, the first line of the
name, and the remove button line up at the top. A long name wraps and
makes only its own row taller.

Remove the temporary stories that compared layouts.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The button in the card's header pushed the title out of place. Put it in
the empty state's actions, and below the list once fonts are added.

The button now moves when the first font arrives or the last one goes,
so render the change with `flushSync()` and focus the new button when
the old one had focus.
…import

Removing a cover image and retrying an upload moved focus before the
change rendered, so the focused button still had its old name. Render
the change with `flushSync()` first.

When an import fails because the file can't be used, move focus to
"Choose file…", as for any invalid field. Note in the docs that the
server still checks every file.
…ames

Each example decided on its own whether a change could rename the
button that gets focus, and several missed one: the error description
after an empty submit, the disabled state after a failed import, and
"Add files" after the last removal. Render the change with `flushSync()`
before every `focus()` call, and state that rule once in the checklist.

Wrap filenames in `<bdi>`, so a name such as `123 - report.png` keeps
its order on a right-to-left page.

Note in the docs that the picker button doesn't repeat the fieldset's
description, which the group already exposes.
A failed import moved focus to "Choose file…" even when people had moved
on to another field while it ran. Move focus only if it's still where it
was on submit; the live region still announces the error.

In the docs, describe how `Fieldset` links its description instead of
what screen readers do with it, say that a rejected replacement keeps
the previous file, and drop a sentence about narrow upload rows that no
longer matched the stacked layout.
… errors

The fieldset's description holds the picker's requirements, such as the
accepted types and limits, but it was linked only to the group. Give it
an `id`, and list it in the picker button's `aria-describedby` before
the error.

Removing a file cleared the error, so the names of rejected files went
away while people made room for them. Keep an error until the next file
is added.

Wrap filenames in error messages in Unicode isolates, as `<bdi>` does
in the rows, so they keep their order on a right-to-left page.
…bels

Adding the first font replaced the only text that listed the supported
formats. List them below the button too, and link the text to the
button in both states.

`IconButton` shows its label in a tooltip, so a remove button's label
mixed its filename with "Remove" on a right-to-left page. Isolate
filenames in every string that is shown, as in errors.
@ciampo
ciampo force-pushed the codex/file-selection-stories branch from 023ac0b to 2f686c0 Compare November 30, 2026 15:57
The checklist says to check every file's size, but both import examples
read the whole file first. Check it against a 1 MB limit before the
import reads it, and state the limit in the field's description.

Note in the docs that `speak()` drops text that looks like an HTML tag,
such as `<draft>` in a filename.
The import-on-choose example showed its 1 MB limit only after rejecting
a file. Show it below the button and link it with `aria-describedby`.

Reword the checklist so it asks for each field's own rules, as the
examples check type everywhere and size or count only where a field
limits them.
…er contrast

Both import examples treated every failure as a bad file, so a failed
request would ask for another file and move focus to the picker. Split
the stand-in into `readPattern()` and `savePattern()`: a file that can't
be read or isn't a pattern asks for another file, and a failed request
keeps the file and asks people to try again.

On hover, the picker's weaker text kept its resting color on the darker
background, at 4.32:1. Use the matching `-active` token.

Raise the preview snackbars' specificity with their own class, not with
`.components-snackbar-list`, which belongs to the component.
…stale errors

The picker said it used an outline `Button`'s tokens, but only switched
background and stroke, and only on hover. Switch the foreground too, on
hover, press, and focus, as `Button` does. Disabled weak text now uses
its own `-weak-disabled` role.

Filenames overrode `Text`'s `body-md` line height with `line-height-xs`,
a pairing the type scale doesn't define. Keep `body-md`, and close the
gap to the details below, so a one-line row keeps its height.

Removing the chosen file from a single-file field now clears its error
too, as the error was about that file. Lists still keep the names of
rejected files while people make room. Import on choose keeps no file,
so its failed-request error says to choose the file again.
@ciampo

ciampo commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor Author

@jameskoster "Choose files…" now sits in the empty state, and moves below the list once fonts are added. Focus follows it, so keyboard users don't lose their place.

I also did a few more review rounds since your last look. The main changes are mostly to align those patterns with "first-party" @wordpress/ui componentry: the drop area now matches an outline Button on hover, press and focus, and filenames use the regular body-md line height (rows are still 64px). Screenshots in the description are up to date.

@jameskoster jameskoster left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think we're good to go :)

@ciampo
ciampo merged commit e6d4c16 into trunk Dec 1, 2026
85 of 86 checks passed
@ciampo
ciampo deleted the codex/file-selection-stories branch December 1, 2026 15:44
@github-actions github-actions Bot added this to the Gutenberg 24.6 milestone Dec 1, 2026
@ciampo

ciampo commented Dec 1, 2026

Copy link
Copy Markdown
Contributor Author

The patterns are available at https://wordpress.github.io/gutenberg/?path=/docs/design-system-patterns-selecting-local-files--docs

Happy to iterate in follow-up PRs if there's any remaining feedback.

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