Ir al contenido
Español

Cómo funciona

One file at a time. The outline is a single SVG path recomputed each frame: along the pill its half-height is a smooth maximum of the pill and a rounded blob around the file, so the edge stretches over the file with soft fillets and closes behind it. The file's position follows the reported progress through a damped spring, and a fast move widens and flattens the stretch a little. When upload resolves the file rests in the swollen right cap, the success colour wipes across, a check appears and the pill shrinks to a short Uploaded badge with a few bubbles; after resetAfter it drains back. A refused file or a rejected upload shakes the button once and shows the retry state, and pressing it opens the picker again.

Úsalo para

  • A single, important file where the moment of sending deserves attention: a report, a signed form, an avatar.
  • Forms with one attachment, where a full drop zone and file list would be too heavy.
  • Landing and onboarding flows that want one expressive, obvious action.

Busca otra opción cuando

  • Several files at once, or a list the user manages afterwards: use the Upload component or the upload progress list.
  • Dense admin forms where many fields sit side by side; the motion draws the eye away from the rest.

Uso

import { StretchUpload } from './StretchUpload.jsx'

function send(file, { onProgress, signal }) {
  return new Promise((resolve, reject) => {
    const xhr = new XMLHttpRequest()
    xhr.open('POST', '/api/files')
    xhr.upload.onprogress = (event) => event.lengthComputable && onProgress((event.loaded / event.total) * 100)
    xhr.onload = () => (xhr.status < 300 ? resolve(JSON.parse(xhr.responseText)) : reject(new Error('Server error')))
    xhr.onerror = () => reject(new Error('Connection lost'))
    signal.addEventListener('abort', () => xhr.abort())
    const body = new FormData()
    body.append('file', file)
    xhr.send(body)
  })
}

export function ReportUpload() {
  return (
    <StretchUpload
      upload={send}
      accept=".pdf,.csv,.xlsx"
      maxSize={25 * 1024 * 1024}
      onComplete={(file, saved) => console.log('stored', file.name, saved)}
    />
  )
}

Propiedades

PropiedadTipoPor defectoQué hace
upload(file: File, { onProgress, signal }) => Promise<unknown>—Sends the file; the component makes no request itself. Call onProgress(0–100) as bytes go out, resolve on success, reject on failure, and stop when signal aborts.
acceptstring—Accepted types, as for <input accept>: .pdf, image/*, text/csv. A file that does not match fails without being sent.
maxSizenumber—Largest file in bytes; a bigger one fails without being sent.
onComplete(file: File, result: unknown) => void—Called when upload resolves, with what it resolved to.
onError(file: File, reason: string) => void—Called when a file is refused or upload rejects.
resetAfternumber | null2400Milliseconds the Uploaded badge stays before the button drains back to rest; null keeps it.
widthnumber296Width in pixels at rest; the button never grows wider than its container.
overbooleanfalseShows the drop invitation from outside, for drags that are not native file drags, such as a card dragged within your page.
disabledbooleanfalseDisables the button and ignores drops.
labelsobject—Replaces any text: idle, over, done, failed, percent(n), and the announcements uploading(name), progress(name), uploaded(name), failedLive(name, reason), rejectAccept(name), rejectSize(name, limit).
refRef<{ open, upload(file), reset, focus, button }>—open() shows the file picker, upload(file) starts with a file from elsewhere (paste, another drop zone), reset() cancels and returns to rest, button is the element.
...restButtonHTMLAttributes—Other native attributes go to the <button>.

Accesibilidad

  • A native <button>: Tab focuses it, Enter or Space opens the file picker, and the rim turns the accent colour on keyboard focus.
  • While a file is sending the button is aria-disabled and a progressbar carries the percent; the stretch, the paper and the visible percent are aria-hidden.
  • A polite status announces the start, the finish and failures with their reason, and is linked to the button with aria-describedby.
  • Status never rests on colour alone: the label says Release to upload, the file name, Upload failed and Uploaded, with an icon for each.
  • The hit area is the whole 52px pill, so it works on touch screens, where tapping opens the picker.
  • With reduced motion nothing stretches or travels: the percent counts up, the colour changes in place, and there are no bubbles or shake.

Movimiento

The file drops in on a spring (stiffness 210, damping 0.62) while the stretch swells with a slight overshoot; it follows progress on a critically damped spring. It rests 260ms in the right cap, the success wipe takes 420ms ease-in-out, the badge shrinks on a spring (190, 0.55) as its label opens over 360ms, bubbles rise for 1.1s, and the drain back takes 460ms. Drag-over scales the button to 1.025 with the spring curve and marches the dashed edge.

Añádelo a tu proyecto

Copia la instrucción en Codex o Claude Code. El agente lee el código publicado, te muestra lo que va a escribir y añade el componente junto a los tuyos.

StretchUpload.jsxstretch-upload.csstokens.cssREADME.md

Los dos archivos se instalan juntos. La hoja de estilos lee las variables de tokens.css y lleva valores de reserva, así que también funciona sola.

Formularios
Cuadrícula de disponibilidad viene con Pro. La vista previa aparece al desbloquearlo.
Operaciones booleanas viene con Pro. La vista previa aparece al desbloquearlo.
Selector en cascada viene con Pro. La vista previa aparece al desbloquearlo.