A profile photo uploader built with Tailwind CSS and vanilla JavaScript. The selected image appears in a large circle before anything is saved, together with three smaller avatars that show how it will look in a profile, a comment and a menu. It suits account settings, onboarding steps and team directory pages.
How it works
The root is section.tpu-uploader with a data-tpu-max-mb attribute. Inside the form.tpu-form, the left column holds .tpu-circle with the .tpu-main image, a round camera button (.tpu-browse), the .tpu-state pill and the .tpu-zoom range input. The right column holds the label.tpu-drop drop zone with a visually hidden input.tpu-input, the .tpu-error message, the "Preview sizes" list and the Remove, Cancel and Save buttons.
The script initializes each .tpu-uploader on DOMContentLoaded and keeps two objects: saved (what the server has) and draft (what the user is previewing). Choosing a file, either through the input, the camera button or by dropping it anywhere on the component, checks the type against TYPES and the size against data-tpu-max-mb. A valid file is turned into an object URL with URL.createObjectURL and copied into every .tpu-img. The zoom slider sets the --tpu-zoom custom property, which style.css uses to scale all previews. "Remove photo" adds is-empty to the root, which hides the images and shows the .tpu-initials. Cancel restores saved, and Save shows the .tpu-spinner for SAVE_MS milliseconds before the draft becomes the saved photo. Object URLs that are no longer shown are released with URL.revokeObjectURL.
Customization
data-tpu-max-mbon.tpu-uploader: change the size limit; the error message uses the same value.TYPESinscript.js: allow other formats such asimage/gif, and update theacceptattribute of.tpu-inputto match.- The submit handler in
script.js: replace the timeout with the commentedFormDataandfetch()example to upload the file and the zoom value. h-44 w-44on.tpu-circle: make the main preview larger or smaller.min="100" max="200"on#tpu-zoom: change the zoom range, or remove the slider block entirely..tpu-state[data-state="pending"]and the other rules instyle.css: restyle the pill for the current, pending, removed and saved states.- The
AOtext in each.tpu-initials: set the user's initials for the empty state.
Use cases
- Account settings pages: users drop a photo and see a circular preview before saving
- Onboarding flows: a zoom slider lets people adjust their picture during sign-up
- Team directories: three small previews show how the photo looks in a profile, comment and menu
- Upload forms: type and size validation shows clear errors before anything is sent
Accessibility
The file input stays in the tab order inside the drop zone label, which shows a focus ring through has-[input:focus-visible]. The camera button has an aria-label, and the input is described by the format hint and the error message. Errors are announced through aria-live="assertive", and preview, removal and save results through a role="status" region. The main image alt text changes with the state; the small previews are decorative with empty alt. The zoom slider has a label and an output. Cancel and Save stay disabled until there is a change. Transitions are removed under prefers-reduced-motion.
Usage notes
The sample portrait comes from Unsplash; replace it with the user's current photo. The zoom is only a visual preview: send its value to the server or crop the image there.