← All articles
Web Development

Your upload button is a downgrade

Replacing a native file input with a custom button can quietly remove drag and drop. A transparent input preserves the behavior without sacrificing the design.

A native file selector replaced by a custom upload button that opens the file picker.

Somewhere in a React app, a perfectly functional file input is being hidden so a nicer button can take its place.

You know the routine. Hide the input. Give it a ref. Call .click() when someone presses your button. Round the corners. Ship it.

A native file selector replaced by a custom upload button that opens the file picker.

import { useRef } from "react";

export function UploadButton({
  onFiles,
}: {
  onFiles: (files: File[]) => void;
}) {
  const inputRef = useRef<HTMLInputElement>(null);

  return (
    <>
      <input
        ref={inputRef}
        type="file"
        multiple
        hidden
        onChange={(event) => {
          onFiles(Array.from(event.currentTarget.files ?? []));
        }}
      />

      <button
        type="button"
        onClick={() => inputRef.current?.click()}
        className="rounded-lg bg-blue-700 px-5 py-3 text-white"
      >
        Upload files
      </button>
    </>
  );
}

Click the button. The picker opens. Select a file. Everything works.

Now drag a file onto it.

Nothing useful happens. Your beautiful upload button has no idea what a file is.

The input you hid did.

You could already drop files on it

A native <input type="file"> can accept files dragged onto it in supporting desktop browsers. No drop handler. No library. This behavior is even described in the HTML standard.

Files dragged directly onto a native file selector.

If you didn’t know that, you have company. Even senior and staff engineers miss this. All those years of experience, and we’re still removing browser features by accident.

The ref pattern preserves one interaction: clicking.

Someone who already has a folder open loses another. They’ve found the file. They’re holding it over your interface. Your button makes them open a second window and find it again.

That’s a horrible trade for rounded corners.

The file input had an image problem

The humble file selector has been hated by developers for years because it’s not easy to style. So we hide it, put a pretty button in its place, and call it an improvement. Good UI, worse UX.

Put the input on top

You can keep the custom appearance and the native interaction.

Render your button’s visual surface. Position the file input over the entire surface. Set its opacity to zero.

The input is invisible, but it’s still there to receive clicks and dropped files.

import { useId, useState } from "react";

export function FilePicker({
  onFiles,
}: {
  onFiles: (files: File[]) => void;
}) {
  const statusId = useId();
  const [names, setNames] = useState<string[]>([]);

  return (
    <div>
      <label className="group relative inline-block">
        <span
          className="
            block rounded-lg bg-blue-700 px-5 py-3 text-white
            group-hover:bg-blue-800
            group-focus-within:ring-2
            group-focus-within:ring-blue-700
            group-focus-within:ring-offset-2
          "
        >
          Choose files
        </span>

        <input
          type="file"
          name="files"
          multiple
          aria-describedby={statusId}
          className="
            absolute inset-0 z-10 h-full w-full
            cursor-pointer opacity-0
          "
          onChange={(event) => {
            const files = Array.from(event.currentTarget.files ?? []);
            setNames(files.map((file) => file.name));
            onFiles(files);
          }}
        />
      </label>

      <p id={statusId} role="status" className="mt-2 text-sm">
        {names.length
          ? names.join(", ")
          : "Choose files or drop them onto the button."}
      </p>
    </div>
  );
}

The visible “button” is a styled span inside a label. The input is the actual control, so there’s no second button competing for keyboard focus.

absolute inset-0 and the full width and height make the input cover the surface. opacity-0 hides its appearance while preserving interaction. Don’t add pointer-events-none; the whole point is for the input to receive those events.

Tailwind’s group-hover changes the visible surface when the pointer is over the group. group-focus-within gives keyboard users a visible focus indicator, even though the focused input is transparent.

The filename display matters too. Hiding the native control also hides its selection feedback, so the example puts that feedback back.

Click, and the native picker opens. Drop files onto the control in a supporting desktop browser, and the input receives them. Both selection paths feed the same onChange handler.

This handles file selection; your onFiles callback still needs to validate and upload the files. It also doesn’t turn the whole page into a drop zone or promise folder uploads. It preserves the native file input’s behavior in the space your button occupies.

You get the appearance you wanted, retain the interaction you nearly removed, and avoid writing a drag-and-drop implementation to replace something the browser already did.

Before you replace a native control, find out what you’re throwing away.