Animated List
Animated
A live list that animates rows arriving, leaving, and the rows they push aside.
$npx @dinachi/cli@latest add animated-list
Deploy finished
web · 2m 14s
Rate limit at 80%
api · us-east-1
3 new sign-ups
since 09:00
fromdurationdismissedactivity-feed.tsx
import { useState } from "react"
import { X } from "lucide-react"
import { AnimatedList, AnimatedListItem } from "@/components/ui/animated-list"
type Row = { id: string; title: string; detail: string }
// Rows arrive from the top, so prepend new ones to `rows`.
export function ActivityFeed({ rows, onDismiss }: { rows: Row[]; onDismiss: (id: string) => void }) {
const [dismissed, setDismissed] = useState<string | null>(null)
return (
<AnimatedList dismissed={dismissed} className="space-y-2">
{rows.map((row) => (
<AnimatedListItem key={row.id} itemKey={row.id}>
<div className="flex items-center gap-3 rounded-xl border px-4 py-3">
<div className="min-w-0 flex-1">
<p className="text-sm font-medium">{row.title}</p>
<p className="text-xs text-muted-foreground">{row.detail}</p>
</div>
<button
type="button"
aria-label={`Dismiss ${row.title}`}
onClick={() => {
setDismissed(row.id)
onDismiss(row.id)
}}
>
<X className="h-4 w-4" />
</button>
</div>
</AnimatedListItem>
))}
</AnimatedList>
)
}Usage
import { useState } from "react"
import { AnimatedList, AnimatedListItem } from "@/components/ui/animated-list"
export function Notifications({ initial }: { initial: { id: string; title: string }[] }) {
const [rows, setRows] = useState(initial)
const [dismissed, setDismissed] = useState<string | null>(null)
const dismiss = (id: string) => {
setDismissed(id)
setRows((current) => current.filter((row) => row.id !== id))
}
return (
<AnimatedList dismissed={dismissed}>
{rows.map((row) => (
<AnimatedListItem key={row.id} itemKey={row.id}>
{row.title}
<button type="button" onClick={() => dismiss(row.id)}>
Dismiss
</button>
</AnimatedListItem>
))}
</AnimatedList>
)
}Examples
Deploy finished
web · 2m 14s
Rate limit at 80%
api · us-east-1
3 new sign-ups
since 09:00
A feed whose rows enter, leave and make room for each other.
import { AnimatedList, AnimatedListItem } from '@/components/ui/animated-list';
export function Example() {
const [rows, setRows] = React.useState(SEED);
const [dismissed, setDismissed] = React.useState<string | null>(null);
const dismiss = (id: string) => {
setDismissed(id);
setRows((current) => current.filter((row) => row.id !== id));
};
return (
<AnimatedList dismissed={dismissed} className="space-y-2">
{rows.map((row) => (
<AnimatedListItem key={row.id} itemKey={row.id}>
<Row row={row} onDismiss={() => dismiss(row.id)} />
</AnimatedListItem>
))}
</AnimatedList>
);
}Deploy finished
web · 2m 14s
Rate limit at 80%
api · us-east-1
3 new sign-ups
since 09:00
A row the reader closed leaves differently from one that was retracted.
// The reader closed it: recorded in the same update that removes it.
const dismiss = (id: string) => {
setDismissed(id);
setRows((current) => current.filter((row) => row.id !== id));
};
// Removal the reader did not ask for: no `dismissed`, so it collapses in place.
const retract = (id: string) =>
setRows((current) => current.filter((row) => row.id !== id));Deploy finished
web · 2m 14s
Rate limit at 80%
api · us-east-1
A log grows downward, so its rows arrive from below.
import { AnimatedList, AnimatedListItem } from '@/components/ui/animated-list';
export function Example() {
return (
<AnimatedList from="bottom" className="space-y-2">
{rows.map((row) => (
<AnimatedListItem key={row.id} itemKey={row.id}>
<Row row={row} />
</AnimatedListItem>
))}
</AnimatedList>
);
}Notes
Motion tier
Needs motion. add --all skips it; install with add --motion or by name.
What animates
- A new row fades in from the
fromedge. - A removed row leaves layout at once, so the gap closes immediately.
- Every other row springs to its new position.
Dismissed rows
Pass the key of a row the reader closed as dismissed, in the same update that removes it. That row slides out sideways; other removals collapse in place.
const dismiss = (id: string) => {
setDismissed(id)
setRows((current) => current.filter((row) => row.id !== id))
}When to use it
- A feed, inbox or activity stream that changes a row or two at a time while someone watches.
- For a list that only appears, use Stagger List.
- Skip it when the whole list changes on every keystroke.
Behaviour
- Nothing animates on first render.
AnimatedListItemthrows outside anAnimatedList.
API
from"top" | "bottom" = "top"
The edge new rows arrive from. A feed that grows downward wants bottom.dismissedstring | null = null
The itemKey of the row the reader closed. Set it in the same update that removes the row.durationnumber = 0.3
Seconds for each row's own animation. The reflow spring is scaled from it.itemKeystring
The same string as React's key, which a component cannot read back. Only needed with dismissed.Accessibility
- Keeps ul and li semantics.
- Reduced motion drops the travel; rows still move to their new place in one frame.
- Changes are not announced. Use a live region if a new row matters.