Morph Sort Dropdown
Card button that morphs into a dropdown panel using Framer Motion's shared layoutId animation.
Category
ButtonReact
CSS
Tailwind
Manual
Create a file and paste the following code into it.
src/sort-morph.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
"use client";
import { useState, useRef, useEffect, type Ref } from "react";
import { motion, AnimatePresence, LayoutGroup } from "motion/react";
import { SlidersHorizontal } from "@phosphor-icons/react";
import { cn } from "@/lib/cn";
/* ─── Constants ─── */
const SPRING_TRANSITION = {
type: "spring" as const,
stiffness: 300,
damping: 20,
mass: 0.8,
};
enum SortBy {
LATEST = "LATEST",
POPULAR = "POPULAR",
TRENDING = "TRENDING",
}
const SORT_OPTIONS = [
{ value: SortBy.LATEST, label: "Latest" },
{ value: SortBy.POPULAR, label: "Most popular" },
{ value: SortBy.TRENDING, label: "Trending" },
] as const;
const SORT_LABELS = Object.fromEntries(
SORT_OPTIONS.map((o) => [o.value, o.label]),
) as Record<SortBy, string>;
/* ─── SortTrigger ─── */
/**
* Compact card button that morphs into the dropdown panel.
* Uses layoutId="sort-morph" to share layout animation with SortDropdown.
* @param {SortBy} sortBy - Current sort value [Required]
* @param {() => void} onClick - Open handler [Required]
*/
const SortTrigger = forwardRef<
HTMLDivElement,
{ sortBy: SortBy; onClick: () => void }
>(({ sortBy, onClick }, ref) => (
<motion.div
ref={ref}
layoutId="sort-morph"
onClick={onClick}
role="button"
tabIndex={0}
onKeyDown={(e) => {
if (e.key === "Enter" || e.key === " ") {
e.preventDefault();
onClick();
}
}}
className="relative flex w-28 flex-shrink-0 cursor-pointer flex-col items-center gap-1 overflow-hidden rounded-2xl border border-neutral-200 bg-white p-1 transition-shadow hover:shadow-sm"
transition={SPRING_TRANSITION}
>
<div className="flex h-[100px] w-[104px] flex-col items-center justify-center gap-1 rounded-lg bg-neutral-50 text-neutral-500">
<motion.span
layoutId="sort-title"
transition={SPRING_TRANSITION}
className="text-xs font-medium"
>
Sort by
</motion.span>
<motion.div layoutId="sort-icon" transition={SPRING_TRANSITION}>
<SlidersHorizontal weight="duotone" className="size-5" />
</motion.div>
</div>
<div className="bg-white px-1">
<motion.span
layoutId="sort-value"
transition={SPRING_TRANSITION}
className="truncate text-center text-xs text-neutral-500"
>
{SORT_LABELS[sortBy]}
</motion.span>
</div>
</motion.div>
));
/* ─── SortDropdown ─── */
/**
* Dropdown panel that morphs from the trigger card.
* Shares layoutId="sort-morph" with SortTrigger for the container morph,
* plus "sort-icon" and "sort-title" for inner element transitions.
* @param {boolean} isOpen - Whether dropdown is visible [Required]
* @param {DOMRect | null} originRect - Trigger bounding rect for positioning [Required]
* @param {SortBy} sortBy - Current sort value [Required]
* @param {(value: SortBy) => void} onSelect - Selection handler [Required]
* @param {() => void} onClose - Close handler [Required]
*/
function SortDropdown({
isOpen,
originRect,
sortBy,
onSelect,
onClose,
}: {
isOpen: boolean;
originRect: DOMRect | null;
sortBy: SortBy;
onSelect: (value: SortBy) => void;
onClose: () => void;
}) {
useEffect(() => {
if (!isOpen) return;
const handler = (e: KeyboardEvent) => {
if (e.key === "Escape") onClose();
};
document.addEventListener("keydown", handler);
return () => document.removeEventListener("keydown", handler);
}, [isOpen, onClose]);
return (
<AnimatePresence>
{isOpen && (
<>
{/* Backdrop overlay */}
<motion.div
className="fixed inset-0 z-50 bg-black/40 backdrop-blur-sm"
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
exit={{ opacity: 0 }}
transition={{ duration: 0.2 }}
onClick={onClose}
key="sort-overlay"
/>
{/* Morphing dropdown panel */}
<motion.div
layoutId="sort-morph"
className="pointer-events-auto fixed z-50 w-[220px] rounded-2xl border border-neutral-200 bg-white p-2 shadow-lg"
style={{
top: originRect?.top,
left: originRect?.left,
}}
transition={SPRING_TRANSITION}
onClick={(e) => e.stopPropagation()}
>
<motion.div
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
transition={{ delay: 0.15, duration: 0.15 }}
>
{/* Header: icon + title */}
<div className="flex items-center gap-2 pb-3">
<motion.div layoutId="sort-icon" transition={SPRING_TRANSITION}>
<SlidersHorizontal weight="duotone" className="size-5" />
</motion.div>
<motion.span
layoutId="sort-title"
transition={SPRING_TRANSITION}
className="text-sm font-semibold text-neutral-900"
>
Sort by
</motion.span>
</div>
{/* Option list with staggered entry */}
<div className="flex flex-col gap-1">
{SORT_OPTIONS.map((option, index) => {
const isActive = sortBy === option.value;
return (
<motion.button
type="button"
key={option.value}
initial={{ opacity: 0, y: 8 }}
animate={{ opacity: 1, y: 0 }}
transition={{
...SPRING_TRANSITION,
delay: 0.15 + 0.05 * index,
}}
onClick={() => {
onSelect(option.value);
onClose();
}}
className={cn(
"rounded-lg px-3 py-2.5 text-left text-sm transition-colors",
isActive
? "bg-neutral-50 font-medium text-neutral-900"
: "text-neutral-500 hover:bg-neutral-50",
)}
>
{option.label}
</motion.button>
);
})}
</div>
</motion.div>
</motion.div>
</>
)}
</AnimatePresence>
);
}
/* ─── Usage Example ─── */
export default function SortMorphExample() {
const [isOpen, setIsOpen] = useState(false);
const [sortBy, setSortBy] = useState<SortBy>(SortBy.POPULAR);
const [originRect, setOriginRect] = useState<DOMRect | null>(null);
const triggerRef = useRef<HTMLDivElement>(null);
return (
<LayoutGroup>
<div className="relative">
{isOpen ? (
<div className="w-28 flex-shrink-0" />
) : (
<SortTrigger
ref={triggerRef}
sortBy={sortBy}
onClick={() => {
if (triggerRef.current) {
setOriginRect(triggerRef.current.getBoundingClientRect());
}
setIsOpen(true);
}}
/>
)}
<SortDropdown
isOpen={isOpen}
originRect={originRect}
sortBy={sortBy}
onSelect={setSortBy}
onClose={() => setIsOpen(false)}
/>
</div>
</LayoutGroup>
);
}
Update the import paths to match your project setup.