# Animated File Input for UI SDK (/docs/components/file-input)

A drop zone that wakes up when a file is over it, with new file rows arriving one after another.

```tsx title="example.tsx"
import * as React from "react"

import {
  FileInput,
  FileInputDropzone,
  FileInputDropzoneIcon,
  FileInputItem,
  FileInputItemRemove,
  FileInputList,
  FileInputTrigger,
  createFileInputController,
  formatFileSize,
} from "@/components/keyframery/file-input"

function photo(name: string, bytes: number) {
  return new File([new Uint8Array(bytes)], name, { type: "image/jpeg" })
}

function ImageIcon() {
  return (
    <svg
      viewBox="0 0 16 16"
      fill="none"
      stroke="currentColor"
      strokeWidth="1.5"
      strokeLinecap="round"
      strokeLinejoin="round"
      aria-hidden="true"
      className="size-4"
    >
      <rect x="2.25" y="2.75" width="11.5" height="10.5" rx="2" />
      <circle cx="6" cy="6.5" r="1" />
      <path d="m2.75 11.5 3.25-3 2.5 2.25L10.5 9l2.75 2.5" />
    </svg>
  )
}

export default function FileInputDemo() {
  const [files] = React.useState(createFileInputController)
  const [initial] = React.useState(() => [
    photo("first-dance.jpg", 2_412_000),
    photo("the-cake.jpg", 1_186_000),
  ])
  const addAlbum = () =>
    files.addFiles([
      photo("speeches.jpg", 3_208_000),
      photo("confetti.jpg", 1_744_000),
      photo("last-dance.jpg", 2_655_000),
    ])

  return (
    <div className="flex w-[380px] flex-col gap-4">
      <div>
        <h3 className="text-[15px] font-semibold tracking-tight">Wedding photos</h3>
        <p className="mt-1 text-[13px] text-muted-foreground">Everyone who was there can add theirs.</p>
      </div>
      <FileInput name="photos" multiple accept="image/*" maxFiles={8} defaultValue={initial} controller={files}>
        <FileInputDropzone>
          {(zone) => (
            <>
              <FileInputDropzoneIcon />
              <p className="font-medium text-foreground">
                {zone.dragReject ? "Photos only" : zone.dragAccept ? "Release to add" : "Drop photos here"}
              </p>
              <p className="mb-3 text-[12px] text-muted-foreground">JPEG, PNG or HEIC · up to 8 photos</p>
              <div className="flex items-center gap-2">
                <FileInputTrigger>Choose photos</FileInputTrigger>
                <button
                  type="button"
                  data-album
                  onClick={addAlbum}
                  className="inline-flex h-8 cursor-pointer items-center rounded-[9px] px-3 text-[13px] leading-none font-medium text-muted-foreground outline-none transition-[color,background-color,scale] duration-200 ease-pop hover:bg-foreground/[0.06] hover:text-foreground focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:ring-offset-background active:scale-[0.96] motion-reduce:transition-none"
                >
                  From your phone
                </button>
              </div>
            </>
          )}
        </FileInputDropzone>
        <FileInputList aria-label="Photos">
          <FileInputItem>
            {({ file }) => (
              <>
                <span className="grid size-8 shrink-0 place-items-center rounded-[7px] bg-muted text-muted-foreground">
                  <ImageIcon />
                </span>
                <span className="flex min-w-0 flex-col gap-0.5">
                  <span className="truncate font-medium">{file.name}</span>
                  <span className="text-[11px] leading-3 text-muted-foreground tabular-nums">
                    {formatFileSize(file.size)}
                  </span>
                </span>
                <FileInputItemRemove />
              </>
            )}
          </FileInputItem>
        </FileInputList>
      </FileInput>
    </div>
  )
}
```

## Installation [#installation]

```bash
npx shadcn@latest add @keyframery/file-input
```

This adds `components/keyframery/file-input.tsx` and its primitive, `components/keyframery/primitives/file-input.tsx`, to your project, and installs `@ui-sdk/react`. The motion tokens are added to your global CSS the first time you install a component.

## Usage [#usage]

```tsx
import {
  FileInput,
  FileInputDropzone,
  FileInputDropzoneIcon,
  FileInputItem,
  FileInputItemRemove,
  FileInputList,
  FileInputTrigger,
} from "@/components/keyframery/file-input"
```

The parts and props are the same as UI SDK's File Input. For every prop, see the [UI SDK file input reference](https://ui-sdk.dev/docs/components/file-input).

## What moves [#what-moves]

| Move        | What you see                                                    |
| ----------- | --------------------------------------------------------------- |
| **Spring**  | The drop zone lifts and its icon rises while a file is over it. |
| **Cascade** | New file rows arrive one after another.                         |
| **Press**   | Buttons press in.                                               |

Timing and springiness follow the [motion style](/docs/motion-styles) you set, so this component moves like the rest of your app.

## Swap the import [#swap-the-import]

Already using UI SDK's file input with your own styles? Keep your markup and change one line. The primitive adds motion and nothing else.

```tsx
// Before
import { … } from "@ui-sdk/react/file-input"

// After: same parts, same props, now animated
import { … } from "@/components/keyframery/primitives/file-input"
```

## Parts [#parts]

The finished component exports 7 parts:

`FileInput`, `FileInputDropzone`, `FileInputDropzoneIcon`, `FileInputItem`, `FileInputItemRemove`, `FileInputList`, `FileInputTrigger`

## Accessibility [#accessibility]

Keyboard, focus and screen-reader behaviour come from UI SDK and are not changed. With reduced motion turned on, nothing in the file input moves: changes happen at once or with a short fade. More in [Accessibility](/docs/accessibility).

## Source [#source]

The code that lands in your project. It is yours to change.

**components/keyframery/file-input.tsx**

```tsx
"use client"

/* Styled file input: the animated primitive plus the Keyframery look. */

import * as React from "react"

import {
  FileInput as FileInputRoot,
  FileInputDropzone as FileInputDropzonePart,
  FileInputItem as FileInputItemPart,
  FileInputItemRemove as FileInputItemRemovePart,
  FileInputList as FileInputListPart,
  FileInputTrigger as FileInputTriggerPart,
  createFileInputController,
  formatFileSize,
  relativePath,
} from "@/components/keyframery/primitives/file-input"
import { cn } from "@/lib/utils"

function FileInput({ className, ...props }: React.ComponentProps<typeof FileInputRoot>) {
  return <FileInputRoot className={cn("flex min-w-0 flex-col gap-3", className)} {...props} />
}

/* A dashed hairline at rest. While files are over it the line turns solid and the zone
   tints: ring colour when they would be taken, destructive when they would not. */
function FileInputDropzone({
  className,
  ...props
}: React.ComponentProps<typeof FileInputDropzonePart>) {
  return (
    <FileInputDropzonePart
      className={cn(
        "group/drop relative flex flex-col items-center justify-center gap-1 rounded-[14px] border border-dashed border-foreground/15 px-6 py-7 text-center text-[13px] outline-none",
        "hover:border-foreground/25",
        "data-dragging:shadow-[0_10px_28px_-12px_rgb(0_0_0/0.25)] dark:data-dragging:shadow-[0_12px_32px_-12px_rgb(0_0_0/0.8)]",
        "focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:ring-offset-background",
        "data-dragging:border-solid",
        "data-drag-accept:border-ring data-drag-accept:bg-ring/[0.07] data-drag-accept:ring-[3px] data-drag-accept:ring-ring/15",
        "data-drag-reject:border-destructive data-drag-reject:bg-destructive/[0.05] data-drag-reject:ring-[3px] data-drag-reject:ring-destructive/15",
        "data-disabled:cursor-not-allowed data-disabled:border-foreground/10 data-disabled:opacity-60",
        "[&>svg]:size-4",
        className,
      )}
      {...props}
    />
  )
}

/* The mark at the top of a drop zone: a small key holding the upload arrow. While files are
   over the zone it rises 8px with a pop, on top of the zone's own lift. Purely visual. */
function FileInputDropzoneIcon({ className, ...props }: React.ComponentProps<"span">) {
  return (
    <span
      aria-hidden="true"
      data-slot="file-input-dropzone-icon"
      className={cn(
        "mb-2 grid size-9 place-items-center rounded-[10px] bg-background text-foreground",
        "shadow-[0_0_0_1px_rgb(0_0_0/0.06),0_1px_1px_rgb(0_0_0/0.05),0_3px_6px_-2px_rgb(0_0_0/0.1)]",
        "dark:bg-accent dark:shadow-[0_0_0_1px_rgb(255_255_255/0.07),inset_0_1px_0_rgb(255_255_255/0.08),0_2px_6px_-1px_rgb(0_0_0/0.6)]",
        "[transition:translate_calc(480ms*var(--kf-speed,1))_var(--ease-pop),scale_calc(480ms*var(--kf-speed,1))_var(--ease-pop),color_calc(150ms*var(--kf-speed,1))_var(--ease-house)] motion-reduce:[transition:none]!",
        "group-data-dragging/drop:-translate-y-2 group-data-dragging/drop:scale-110 group-data-drag-accept/drop:text-ring group-data-drag-reject/drop:text-destructive",
        "[&_svg]:size-4",
        className,
      )}
      {...props}
    >
      <svg
        viewBox="0 0 16 16"
        fill="none"
        stroke="currentColor"
        strokeWidth="1.5"
        strokeLinecap="round"
        strokeLinejoin="round"
      >
        <path d="M8 10.5V2.75M4.75 6 8 2.75 11.25 6" />
        <path d="M2.75 10.5v1.25c0 .83.67 1.5 1.5 1.5h7.5c.83 0 1.5-.67 1.5-1.5V10.5" />
      </svg>
    </span>
  )
}

/* A key that opens the system picker. */
function FileInputTrigger({
  className,
  ...props
}: React.ComponentProps<typeof FileInputTriggerPart>) {
  return (
    <FileInputTriggerPart
      className={cn(
        "inline-flex h-8 shrink-0 cursor-pointer items-center justify-center gap-1.5 rounded-[9px] bg-background px-3 text-[13px] leading-none font-medium whitespace-nowrap text-foreground outline-none select-none",
        "shadow-[0_0_0_1px_rgb(0_0_0/0.06),0_1px_1px_rgb(0_0_0/0.05),0_3px_6px_-2px_rgb(0_0_0/0.1)]",
        "dark:bg-accent dark:shadow-[0_0_0_1px_rgb(255_255_255/0.07),inset_0_1px_0_rgb(255_255_255/0.08),0_2px_6px_-1px_rgb(0_0_0/0.6)]",
        "hover:bg-muted/50 dark:hover:bg-accent/80",
        "active:translate-y-px active:shadow-[0_0_0_1px_rgb(0_0_0/0.07),0_0_1px_rgb(0_0_0/0.08)] dark:active:shadow-[0_0_0_1px_rgb(255_255_255/0.06),0_0_1px_rgb(0_0_0/0.6)]",
        "focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:ring-offset-background",
        "data-invalid:ring-[3px] data-invalid:ring-destructive/25 data-invalid:shadow-[0_0_0_1px_var(--destructive)]",
        "data-disabled:pointer-events-none data-disabled:opacity-50",
        "[&_svg]:size-4 [&_svg]:shrink-0",
        className,
      )}
      {...props}
    />
  )
}

/* One Tab stop; arrow keys move between rows. */
function FileInputList({
  className,
  ...props
}: React.ComponentProps<typeof FileInputListPart>) {
  return (
    <FileInputListPart
      className={cn(
        "flex flex-col gap-1.5 rounded-[12px] outline-none empty:hidden",
        "focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:ring-offset-background",
        className,
      )}
      {...props}
    />
  )
}

/* A row; the primitive cascades the rows in. */
function FileInputItem({
  className,
  ...props
}: React.ComponentProps<typeof FileInputItemPart>) {
  return (
    <FileInputItemPart
      className={cn(
        "flex min-w-0 items-center gap-3 rounded-[10px] bg-background py-1.5 ps-1.5 pe-2 text-[13px] text-foreground outline-none",
        "shadow-[0_0_0_1px_rgb(0_0_0/0.08),0_1px_2px_rgb(0_0_0/0.04)] dark:bg-accent/40 dark:shadow-[0_0_0_1px_rgb(255_255_255/0.08)]",
        "focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:ring-offset-background",
        "data-disabled:opacity-60",
        className,
      )}
      {...props}
    />
  )
}

function FileInputItemRemove({
  className,
  children,
  ...props
}: React.ComponentProps<typeof FileInputItemRemovePart>) {
  return (
    <FileInputItemRemovePart
      className={cn(
        "ms-auto grid size-6 shrink-0 cursor-pointer place-items-center rounded-[7px] text-muted-foreground outline-none",
        "hover:bg-foreground/[0.06] hover:text-foreground",
        "focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:ring-offset-background",
        "data-disabled:pointer-events-none data-disabled:opacity-40",
        "[&_svg]:size-3.5",
        className,
      )}
      {...props}
    >
      {children ?? (
        <svg
          viewBox="0 0 16 16"
          fill="none"
          stroke="currentColor"
          strokeWidth="1.5"
          strokeLinecap="round"
          strokeLinejoin="round"
          aria-hidden="true"
        >
          <path d="m4.5 4.5 7 7m0-7-7 7" />
        </svg>
      )}
    </FileInputItemRemovePart>
  )
}

export {
  FileInput,
  FileInputDropzone,
  FileInputDropzoneIcon,
  FileInputItem,
  FileInputItemRemove,
  FileInputList,
  FileInputTrigger,
  createFileInputController,
  formatFileSize,
  relativePath,
}
```

**components/keyframery/primitives/file-input.tsx**

```tsx
"use client"

/* Animated drop-in for `@ui-sdk/react/file-input`: same parts, same props. Swap the import and keep
   your code. This file adds motion only. Colours, radii, shadows and spacing stay yours. */

import {
  FileInputDropzone as FileInputDropzonePart,
  FileInputItem as FileInputItemPart,
  FileInputItemRemove as FileInputItemRemovePart,
  FileInputTrigger as FileInputTriggerPart,
} from "@ui-sdk/react/file-input"
import * as React from "react"

import { cn } from "@/lib/utils"

export * from "@ui-sdk/react/file-input"

/* While files are over it, the zone lifts toward them on a spring. */
function FileInputDropzone({
  className,
  ...props
}: React.ComponentProps<typeof FileInputDropzonePart>) {
  return (
    <FileInputDropzonePart
      className={cn(
        "[transition:translate_calc(460ms*var(--kf-speed,1))_var(--ease-spring),scale_calc(460ms*var(--kf-speed,1))_var(--ease-spring),border-color_calc(150ms*var(--kf-speed,1))_var(--ease-house),background-color_calc(150ms*var(--kf-speed,1))_var(--ease-house),box-shadow_calc(200ms*var(--kf-speed,1))_var(--ease-house)]",
        "data-dragging:-translate-y-1.5 data-dragging:scale-[1.02]",
        "motion-reduce:[transition:none]!",
        className,
      )}
      {...props}
    />
  )
}

function FileInputTrigger({ className, ...props }: React.ComponentProps<typeof FileInputTriggerPart>) {
  return (
    <FileInputTriggerPart
      className={cn(
        "[transition:scale_calc(260ms*var(--kf-speed,1))_var(--ease-pop),translate_calc(260ms*var(--kf-speed,1))_var(--ease-pop),box-shadow_calc(120ms*var(--kf-speed,1))_var(--ease-house),background-color_calc(120ms*var(--kf-speed,1))_var(--ease-house)] active:scale-[0.96]",
        "motion-reduce:[transition:none]!",
        className,
      )}
      {...props}
    />
  )
}

/* Cascade: rows rise in out of a blur, each one 32ms after the row above it. Only opacity,
   translate and filter transition here, so the delay never slows anything else. */
function FileInputItem({ className, ...props }: React.ComponentProps<typeof FileInputItemPart>) {
  return (
    <FileInputItemPart
      className={cn(
        "transition-[opacity,translate,filter] duration-[calc(420ms*var(--kf-speed,1))] ease-spring [transition-delay:calc((min(sibling-index(),10)-1)*calc(32ms*var(--kf-speed,1)))]",
        "starting:translate-y-2 starting:opacity-0 starting:blur-[4px]",
        "motion-reduce:transition-none!",
        className,
      )}
      {...props}
    />
  )
}

function FileInputItemRemove({
  className,
  ...props
}: React.ComponentProps<typeof FileInputItemRemovePart>) {
  return (
    <FileInputItemRemovePart
      className={cn(
        "[transition:scale_calc(260ms*var(--kf-speed,1))_var(--ease-pop),color_calc(150ms*var(--kf-speed,1)),background-color_calc(150ms*var(--kf-speed,1))] active:scale-[0.82]",
        "motion-reduce:[transition:none]!",
        className,
      )}
      {...props}
    />
  )
}

export { FileInputDropzone, FileInputItem, FileInputItemRemove, FileInputTrigger }
```

## Questions

### How does the animated File Input for UI SDK move?

Spring: The drop zone lifts and its icon rises while a file is over it. Cascade: New file rows arrive one after another. Press: Buttons press in.

### Do I need Motion or Framer Motion for the animated File Input?

No. Every move is plain CSS, so there is nothing to install besides the component and UI SDK.

### Can I keep my own styling and only take the motion?

Yes. Import from `@/components/keyframery/primitives/file-input` instead. It has the same parts and props as `@ui-sdk/react/file-input` and adds motion only.

### What happens to the File Input when someone turns on reduced motion?

Nothing moves. State changes happen at once, or with a short fade.
