Motion file inputs
Five file inputs that share one dropzone and differ only in movement: a zone that reacts while you drag, a spring pop, a plain fade, a slide and an idle pulse. Drag and drop is real, and every animation is gated on reduced motion so the picker stays usable without it.
Installation
The styled layer is free and installs like any other ai2 component.
Run the following command
npx shadcn@latest add @ai2/file-input-motionDependencies, the @ai2/tokens theme and the component file are installed together.
Install dependencies
npm install motion lucide-reactCopy the source
components/ui/file-input-motion.tsx"use client"
import * as React from "react"
import { AnimatePresence, motion, useReducedMotion } from "motion/react"
import { File as FileIcon, Upload, X } from "lucide-react"
import { cn } from "@/lib/utils"
/* Motion file-input family: 5 decorative pickers separated by their drag and entry motion. Every export wraps a real <input type="file"> and handles real onDragOver/onDragLeave/onDrop (with preventDefault). Every animation is turned off with useReducedMotion; under reduced motion the state changes instantly instead of transforming. Colour comes ONLY from semantic tokens, transparency through color-mix. Deterministic (no Date.now and no Math.random). */
export type StyledSize = "sm" | "md" | "lg" | "xl"
type FileInputProps = {
className?: string
size?: StyledSize
multiple?: boolean
accept?: string
onFilesChange?: (files: File[]) => void
}
function fileKey(file: File): string {
return `${file.name}:${file.size}:${file.lastModified}`
}
function formatSize(bytes: number): string {
if (bytes < 1024) return `${bytes} B`
if (bytes < 1024 * 1024) return `${Math.round(bytes / 1024)} KB`
return `${Math.round(bytes / (1024 * 1024))} MB`
}
function useFiles(multiple: boolean | undefined, onFilesChange?: (files: File[]) => void) {
const [files, setFiles] = React.useState<File[]>([])
const add = React.useCallback(
(incoming: FileList | File[] | null) => {
if (!incoming) return
const list = Array.from(incoming)
if (list.length === 0) return
setFiles((prev) => {
if (!multiple) {
const next = [list[0]]
onFilesChange?.(next)
return next
}
const map = new Map(prev.map((f) => [fileKey(f), f]))
for (const f of list) map.set(fileKey(f), f)
const next = Array.from(map.values())
onFilesChange?.(next)
return next
})
},
[multiple, onFilesChange]
)
const remove = React.useCallback(
(key: string) => {
setFiles((prev) => {
const next = prev.filter((f) => fileKey(f) !== key)
onFilesChange?.(next)
return next
})
},
[onFilesChange]
)
return { files, add, remove }
}
const dropSurface: Record<StyledSize, string> = {
sm: "gap-2 rounded-lg p-4 text-sm",
md: "gap-3 rounded-xl p-6 text-sm",
lg: "gap-3 rounded-2xl p-8 text-base",
xl: "gap-4 rounded-2xl p-10 text-base",
}
const iconSize: Record<StyledSize, string> = {
sm: "size-5",
md: "size-6",
lg: "size-8",
xl: "size-9",
}
const clearBtn =
"relative shrink-0 rounded-md text-muted-foreground outline-none transition-colors hover:text-foreground focus-visible:ring-[3px] focus-visible:ring-ring/50 after:absolute after:-inset-1.5 motion-reduce:transition-none"
const surfaceBase =
"flex w-full flex-col items-center justify-center border border-dashed border-border bg-transparent text-center text-muted-foreground outline-none transition-colors duration-(--motion-base) ease-(--motion-ease) hover:border-primary/60 focus-visible:ring-[3px] focus-visible:ring-ring/50 data-[drag-active]:border-primary data-[drag-active]:bg-[color-mix(in_oklab,var(--color-primary)_10%,transparent)] data-[drag-active]:text-foreground motion-reduce:transition-none [&>svg]:shrink-0 [&>i]:leading-none"
const springy = { type: "spring" as const, stiffness: 420, damping: 30 }
/* Ortak dosya listesi. Hareket tipi disaridan verilir. */
function FileList({
files,
remove,
reduce,
itemMotion,
}: {
files: File[]
remove: (key: string) => void
reduce: boolean | null
itemMotion?: {
initial: Record<string, number>
animate: Record<string, number>
exit: Record<string, number>
transition?: Record<string, unknown>
}
}) {
return (
<ul data-slot="styled-file-input-list" className="mt-3 flex flex-col gap-2">
<AnimatePresence initial={false}>
{files.map((file) => (
<motion.li
key={fileKey(file)}
layout={!reduce}
initial={reduce || !itemMotion ? undefined : itemMotion.initial}
animate={reduce || !itemMotion ? undefined : itemMotion.animate}
exit={reduce || !itemMotion ? undefined : itemMotion.exit}
transition={itemMotion?.transition}
className="flex items-center gap-2 rounded-lg border border-border bg-surface-2 px-3 py-2 text-sm"
>
<FileIcon className="size-4 shrink-0 text-muted-foreground" />
<span className="min-w-0 flex-1 truncate text-foreground">{file.name}</span>
<span className="shrink-0 text-xs text-muted-foreground">{formatSize(file.size)}</span>
<button
type="button"
aria-label={`Remove ${file.name}`}
onClick={() => remove(fileKey(file))}
className={clearBtn}
>
<X className="size-4" />
</button>
</motion.li>
))}
</AnimatePresence>
</ul>
)
}
/* 1) DragFile: the surface grows while dragging and the icon lifts. Under reduced motion only the token colour state changes. */
export function DragFile({ className, size = "md", multiple = true, accept, onFilesChange }: FileInputProps) {
const reduce = useReducedMotion()
const id = React.useId()
const inputRef = React.useRef<HTMLInputElement>(null)
const [dragActive, setDragActive] = React.useState(false)
const { files, add, remove } = useFiles(multiple, onFilesChange)
return (
<div data-slot="styled-file-input" className={cn("w-full", className)}>
<label htmlFor={id} className="sr-only">
Choose files to upload
</label>
<input
id={id}
ref={inputRef}
type="file"
multiple={multiple}
accept={accept}
className="sr-only"
onChange={(e) => add(e.currentTarget.files)}
/>
<motion.button
type="button"
data-slot="styled-file-input-surface"
data-drag-active={dragActive || undefined}
onClick={() => inputRef.current?.click()}
onDragOver={(e) => {
e.preventDefault()
setDragActive(true)
}}
onDragLeave={(e) => {
e.preventDefault()
setDragActive(false)
}}
onDrop={(e) => {
e.preventDefault()
setDragActive(false)
add(e.dataTransfer.files)
}}
initial={false}
animate={reduce ? undefined : { scale: dragActive ? 1.02 : 1 }}
transition={springy}
className={cn(surfaceBase, dropSurface[size])}
>
<motion.span
initial={false}
animate={reduce ? undefined : { y: dragActive ? -4 : 0 }}
transition={springy}
className={cn("flex items-center justify-center text-primary", iconSize[size])}
>
<Upload className="size-full" />
</motion.span>
<span className="font-medium text-foreground">
{dragActive ? "Release to drop" : "Drag files onto this zone"}
</span>
<span className="text-xs text-muted-foreground">The zone reacts while you drag</span>
</motion.button>
<FileList
files={files}
remove={remove}
reduce={reduce}
itemMotion={{
initial: { opacity: 0, y: -6 },
animate: { opacity: 1, y: 0 },
exit: { opacity: 0, y: -6 },
}}
/>
</div>
)
}
/* 2) PopFile: the selected files enter with a springy pop. */
export function PopFile({ className, size = "md", multiple = true, accept, onFilesChange }: FileInputProps) {
const reduce = useReducedMotion()
const id = React.useId()
const inputRef = React.useRef<HTMLInputElement>(null)
const [dragActive, setDragActive] = React.useState(false)
const { files, add, remove } = useFiles(multiple, onFilesChange)
return (
<div data-slot="styled-file-input" className={cn("w-full", className)}>
<label htmlFor={id} className="sr-only">
Choose files to upload
</label>
<input
id={id}
ref={inputRef}
type="file"
multiple={multiple}
accept={accept}
className="sr-only"
onChange={(e) => add(e.currentTarget.files)}
/>
<button
type="button"
data-slot="styled-file-input-surface"
data-drag-active={dragActive || undefined}
onClick={() => inputRef.current?.click()}
onDragOver={(e) => {
e.preventDefault()
setDragActive(true)
}}
onDragLeave={(e) => {
e.preventDefault()
setDragActive(false)
}}
onDrop={(e) => {
e.preventDefault()
setDragActive(false)
add(e.dataTransfer.files)
}}
className={cn(surfaceBase, dropSurface[size])}
>
<Upload className={cn(iconSize[size], "text-primary")} />
<span className="font-medium text-foreground">Drop files to pop them in</span>
</button>
<FileList
files={files}
remove={remove}
reduce={reduce}
itemMotion={{
initial: { opacity: 0, scale: 0.9 },
animate: { opacity: 1, scale: 1 },
exit: { opacity: 0, scale: 0.9 },
transition: springy,
}}
/>
</div>
)
}
/* 3) FadeFile: the files enter and leave with a plain opacity transition. */
export function FadeFile({ className, size = "md", multiple = true, accept, onFilesChange }: FileInputProps) {
const reduce = useReducedMotion()
const id = React.useId()
const inputRef = React.useRef<HTMLInputElement>(null)
const [dragActive, setDragActive] = React.useState(false)
const { files, add, remove } = useFiles(multiple, onFilesChange)
return (
<div data-slot="styled-file-input" className={cn("w-full", className)}>
<label htmlFor={id} className="sr-only">
Choose files to upload
</label>
<input
id={id}
ref={inputRef}
type="file"
multiple={multiple}
accept={accept}
className="sr-only"
onChange={(e) => add(e.currentTarget.files)}
/>
<motion.button
type="button"
data-slot="styled-file-input-surface"
data-drag-active={dragActive || undefined}
onClick={() => inputRef.current?.click()}
onDragOver={(e) => {
e.preventDefault()
setDragActive(true)
}}
onDragLeave={(e) => {
e.preventDefault()
setDragActive(false)
}}
onDrop={(e) => {
e.preventDefault()
setDragActive(false)
add(e.dataTransfer.files)
}}
initial={false}
animate={reduce ? undefined : { opacity: dragActive ? 1 : 0.85 }}
transition={{ duration: 0.2, ease: "easeOut" }}
className={cn(surfaceBase, dropSurface[size])}
>
<Upload className={cn(iconSize[size], "text-primary")} />
<span className="font-medium text-foreground">Drop files to fade them in</span>
</motion.button>
<FileList
files={files}
remove={remove}
reduce={reduce}
itemMotion={{
initial: { opacity: 0 },
animate: { opacity: 1 },
exit: { opacity: 0 },
transition: { duration: 0.24, ease: "easeOut" },
}}
/>
</div>
)
}
/* 4) SlideFile: dosyalar yandan kayarak girer, karsi yone kayarak cikar. */
export function SlideFile({ className, size = "md", multiple = true, accept, onFilesChange }: FileInputProps) {
const reduce = useReducedMotion()
const id = React.useId()
const inputRef = React.useRef<HTMLInputElement>(null)
const [dragActive, setDragActive] = React.useState(false)
const { files, add, remove } = useFiles(multiple, onFilesChange)
return (
<div data-slot="styled-file-input" className={cn("w-full", className)}>
<label htmlFor={id} className="sr-only">
Choose files to upload
</label>
<input
id={id}
ref={inputRef}
type="file"
multiple={multiple}
accept={accept}
className="sr-only"
onChange={(e) => add(e.currentTarget.files)}
/>
<button
type="button"
data-slot="styled-file-input-surface"
data-drag-active={dragActive || undefined}
onClick={() => inputRef.current?.click()}
onDragOver={(e) => {
e.preventDefault()
setDragActive(true)
}}
onDragLeave={(e) => {
e.preventDefault()
setDragActive(false)
}}
onDrop={(e) => {
e.preventDefault()
setDragActive(false)
add(e.dataTransfer.files)
}}
className={cn(surfaceBase, dropSurface[size])}
>
<Upload className={cn(iconSize[size], "text-primary")} />
<span className="font-medium text-foreground">Drop files to slide them in</span>
</button>
<FileList
files={files}
remove={remove}
reduce={reduce}
itemMotion={{
initial: { opacity: 0, x: -16 },
animate: { opacity: 1, x: 0 },
exit: { opacity: 0, x: 16 },
transition: springy,
}}
/>
</div>
)
}
/* 5) PulseFile: the surface breathes very lightly while empty and stops once a file is selected. The pulse is completely off under reduced motion. */
export function PulseFile({ className, size = "md", multiple = true, accept, onFilesChange }: FileInputProps) {
const reduce = useReducedMotion()
const id = React.useId()
const inputRef = React.useRef<HTMLInputElement>(null)
const [dragActive, setDragActive] = React.useState(false)
const { files, add, remove } = useFiles(multiple, onFilesChange)
const idle = files.length === 0 && !dragActive && !reduce
return (
<div data-slot="styled-file-input" className={cn("w-full", className)}>
<label htmlFor={id} className="sr-only">
Choose files to upload
</label>
<input
id={id}
ref={inputRef}
type="file"
multiple={multiple}
accept={accept}
className="sr-only"
onChange={(e) => add(e.currentTarget.files)}
/>
<button
type="button"
data-slot="styled-file-input-surface"
data-drag-active={dragActive || undefined}
onClick={() => inputRef.current?.click()}
onDragOver={(e) => {
e.preventDefault()
setDragActive(true)
}}
onDragLeave={(e) => {
e.preventDefault()
setDragActive(false)
}}
onDrop={(e) => {
e.preventDefault()
setDragActive(false)
add(e.dataTransfer.files)
}}
className={cn(surfaceBase, dropSurface[size])}
>
<motion.span
initial={false}
animate={idle ? { scale: [1, 1.08, 1], opacity: [1, 0.7, 1] } : { scale: 1, opacity: 1 }}
transition={idle ? { duration: 1.8, repeat: Infinity, ease: "easeInOut" } : { duration: 0.2 }}
className={cn("flex items-center justify-center text-primary", iconSize[size])}
>
<Upload className="size-full" />
</motion.span>
<span className="font-medium text-foreground">Waiting for files</span>
<span className="text-xs text-muted-foreground">The icon settles once you pick one</span>
</button>
<FileList
files={files}
remove={remove}
reduce={reduce}
itemMotion={{
initial: { opacity: 0, scale: 0.96 },
animate: { opacity: 1, scale: 1 },
exit: { opacity: 0, scale: 0.96 },
transition: springy,
}}
/>
</div>
)
}Manual installs skip the @ai2/tokens theme, so add the token CSS from the theming guide or the tone colors will be missing.
Variations
5 takes on the same idea. Each is its own export, and every one accepts a size prop (sm, md, lg, xl) aligned to the base Button scale.
Drag
The zone scales and the icon lifts while you drag over it.
import { DragFile } from "@/components/ui/file-input-motion"
<DragFile />Pop
Picked files pop in on a spring.
import { PopFile } from "@/components/ui/file-input-motion"
<PopFile />Fade
Files fade in and out with no transform.
import { FadeFile } from "@/components/ui/file-input-motion"
<FadeFile />Slide
Files slide in from the left and leave to the right.
import { SlideFile } from "@/components/ui/file-input-motion"
<SlideFile />Pulse
The empty zone breathes until a file arrives.
import { PulseFile } from "@/components/ui/file-input-motion"
<PulseFile />ai2 Motion file inputs: 5 styled variations on the token system
The ai2 Motion file inputs are a set of 5 decorative button variations from the styled layer of the @ai2 design system, built around dropzones with drag and entrance motion. They are free and MIT licensed, and every color comes from a semantic token, so they theme with the rest of ai2 in light and dark.
Motion runs on framer-motion: framer-motion drives the drag-over reaction and the enter and exit of each file row. The styled layer is opt-in, so the dependency only lands if you use it; the base components stay lean. Under reduced motion, every transform and the idle pulse are skipped and the picker still works.
What is in the ai2 Motion file inputs?
5 exports in one file: Drag, Pop, Fade, Slide and Pulse. Each renders a native button and takes a size prop (sm, md, lg, xl) aligned to the base Button. They are separate from the base Button on purpose: the base keeps its clean variant, tone and size axes, while the styled layer carries the effects.
You own the file. Copy the one category file and you have all 5 variations, with no runtime dependency on ai2 itself.
Why use it
- On-system by construction: Every color resolves to an ai2 semantic token, so the buttons follow your theme in light and dark with no extra work.
- Effect without the sprawl: The decorations live in a dedicated styled file, so the base Button keeps its clean, predictable API.
- Accessible and honest: Each renders a real button element, keeps a visible focus ring, and respects prefers-reduced-motion.
Features
- Token-driven color: No hardcoded hex or oklch; the look recolors with your theme tokens.
- framer-motion: framer-motion drives the drag-over reaction and the enter and exit of each file row.
- Reduced-motion aware: Under prefers-reduced-motion, every transform and the idle pulse are skipped and the picker still works.
- Size aligned to the base: Every variation takes sm, md, lg and xl matching the base Button height scale, so styled and base buttons line up in a row.
Production tips
- Use it for emphasis, not everywhere: Styled buttons draw the eye. Reserve them for the one action you want people to take on a screen, and use the base Button for the rest.
- Keep labels as verbs: The decoration adds weight, so a clear action label keeps the button scannable.
- Pick one variation per surface: The variations share a family; using two different ones in the same view competes for attention.
Works with the rest of ai2
The Motion file inputs sit alongside the base Button and the rest of the @ai2 registry. They share the same token file, so a styled action next to a base button or a badge stays visually consistent in both modes.