1"use client"
2
3// npx shadcn-ui@latest add checkbox
4// npm i react-use-measure
5import { Dispatch, ReactNode, SetStateAction, useState } from "react"
6import { Trash } from "lucide-react"
7import {
8 AnimatePresence,
9 LayoutGroup,
10 motion,
11 Reorder,
12 useDragControls,
13} from "motion/react"
14import useMeasure from "react-use-measure"
15
16import { cn } from "@/lib/utils"
17import { Checkbox } from "@/components/ui/checkbox"
18
19export type Item = {
20 text: string
21 checked: boolean
22 id: number
23 description: string
24}
25
26interface SortableListItemProps {
27 item: Item
28 order: number
29 onCompleteItem: (id: number) => void
30 onRemoveItem: (id: number) => void
31 renderExtra?: (item: Item) => React.ReactNode
32 isExpanded?: boolean
33 className?: string
34 handleDrag: () => void
35}
36
37function SortableListItem({
38 item,
39 order,
40 onCompleteItem,
41 onRemoveItem,
42 renderExtra,
43 handleDrag,
44 isExpanded,
45 className,
46}: SortableListItemProps) {
47 let [ref, bounds] = useMeasure()
48 const [isDragging, setIsDragging] = useState(false)
49 // const [isDraggable, setIsDraggable] = useState(true)
50 const dragControls = useDragControls()
51
52 const handleDragStart = (event: any) => {
53 setIsDragging(true)
54 dragControls.start(event, { snapToCursor: true })
55 handleDrag()
56 }
57
58 const handleDragEnd = () => {
59 setIsDragging(false)
60 }
61
62 return (
63 <motion.div className={cn("", className)} key={item.id}>
64 <div className="flex w-full items-center">
65 <Reorder.Item
66 value={item}
67 className={cn(
68 "relative z-auto grow",
69 "h-full rounded-xl bg-primary dark:bg-primary-foreground",
70 "shadow-[0px_1px_0px_0px_hsla(0,0%,100%,.03)_inset,0px_0px_0px_1px_hsla(0,0%,100%,.03)_inset,0px_0px_0px_1px_rgba(0,0,0,.1),0px_2px_2px_0px_rgba(0,0,0,.1),0px_4px_4px_0px_rgba(0,0,0,.1),0px_8px_8px_0px_rgba(0,0,0,.1)]",
71 item.checked ? "cursor-not-allowed" : "cursor-grab",
72 item.checked && !isDragging ? "w-7/10" : "w-full"
73 )}
74 key={item.id}
75 initial={{ opacity: 0 }}
76 animate={{
77 opacity: 1,
78 height: bounds.height > 0 ? bounds.height : undefined,
79 transition: {
80 type: "spring",
81 bounce: 0,
82 duration: 0.4,
83 },
84 }}
85 exit={{
86 opacity: 0,
87 transition: {
88 duration: 0.05,
89 type: "spring",
90 bounce: 0.1,
91 },
92 }}
93 layout
94 layoutId={`item-${item.id}`}
95 dragListener={!item.checked}
96 dragControls={dragControls}
97 onDragEnd={handleDragEnd}
98 style={
99 isExpanded
100 ? {
101 zIndex: 9999,
102 marginTop: 10,
103 marginBottom: 10,
104 position: "relative",
105 overflow: "hidden",
106 }
107 : {
108 position: "relative",
109 overflow: "hidden",
110 }
111 }
112 whileDrag={{ zIndex: 9999 }}
113 >
114 <div ref={ref} className={cn(isExpanded ? "" : "", "z-20 ")}>
115 <motion.div
116 layout="position"
117 className="flex items-center justify-center "
118 >
119 <AnimatePresence>
120 {!isExpanded ? (
121 <motion.div
122 initial={{ opacity: 0, filter: "blur(4px)" }}
123 animate={{ opacity: 1, filter: "blur(0px)" }}
124 exit={{ opacity: 0, filter: "blur(4px)" }}
125 transition={{ duration: 0.001 }}
126 className="flex items-center space-x-2 "
127 >
128 {/* List Remove Actions */}
129 <div className="pl-3 pt-1">
130 <Checkbox
131 checked={item.checked}
132 id={`checkbox-${item.id}`}
133 aria-label="Mark to delete"
134 onCheckedChange={() => onCompleteItem(item.id)}
135 className=" h-5 w-5 rounded-md border-white/20 bg-black/30 data-[state=checked]:bg-black data-[state=checked]:text-red-200"
136 />
137 </div>
138 {/* List Order */}
139 <p className="font-mono text-xs pl-1 text-white/50">
140 {order + 1}
141 </p>
142
143 {/* List Title */}
144 <motion.div
145 key={`${item.checked}`}
146 className=" px-1 min-w-[150px]"
147 initial={{
148 opacity: 0,
149 filter: "blur(4px)",
150 }}
151 animate={{ opacity: 1, filter: "blur(0px)" }}
152 transition={{
153 bounce: 0.2,
154 delay: item.checked ? 0.2 : 0,
155 type: "spring",
156 }}
157 >
158 <h4
159 className={cn(
160 "tracking-tighter text-base md:text-lg ",
161 item.checked ? "text-red-400" : "text-white/70"
162 )}
163 >
164 {item.checked ? "Delete" : ` ${item.text}`}
165 </h4>
166 </motion.div>
167 </motion.div>
168 ) : null}
169 </AnimatePresence>
170
171 {/* List Item Children */}
172 {renderExtra && renderExtra(item)}
173 </motion.div>
174 </div>
175 <div
176 onPointerDown={handleDragStart}
177 style={{ touchAction: "none" }}
178 />
179 </Reorder.Item>
180 {/* List Delete Action Animation */}
181 <AnimatePresence mode="popLayout">
182 {item.checked ? (
183 <motion.div
184 layout
185 initial={{ opacity: 0, x: -10 }}
186 animate={{
187 opacity: 1,
188 x: 0,
189 transition: {
190 delay: 0.17,
191 duration: 0.17,
192 type: "spring",
193 bounce: 0.6,
194 },
195 zIndex: 5,
196 }}
197 exit={{
198 opacity: 0,
199 x: -5,
200 transition: {
201 delay: 0,
202 duration: 0.0,
203 type: "spring",
204 bounce: 0,
205 },
206 }}
207 className="-ml-[1px] h-[1.5rem] w-3 rounded-l-none rounded-r-none border-y border-y-white/5 border-r-white/10 bg-[#161716] "
208 />
209 ) : null}
210 </AnimatePresence>
211 <AnimatePresence mode="popLayout">
212 {item.checked ? (
213 <motion.div
214 layout
215 initial={{ opacity: 0, x: -5, filter: "blur(4px)" }}
216 animate={{
217 opacity: 1,
218 x: 0,
219 filter: "blur(0px)",
220 transition: {
221 delay: 0.3,
222 duration: 0.15,
223 type: "spring",
224 bounce: 0.9,
225 },
226 }}
227 exit={{
228 opacity: 0,
229 filter: "blur(4px)",
230 x: -10,
231 transition: { delay: 0, duration: 0.12 },
232 }}
233 className="inset-0 z-0 border-spacing-1 rounded-r-xl rounded-l-sm border-r-2 border-r-red-300/60 bg-[#161716]/80 shadow-[0_1px_0_0_rgba(255,255,255,0.03)_inset,0_0_0_1px_rgba(255,255,255,0.03)_inset,0_0_0_1px_rgba(0,0,0,0.1),0_2px_2px_0_rgba(0,0,0,0.1),0_4px_4px_0_rgba(0,0,0,0.1),0_8px_8px_0_rgba(0,0,0,0.1)] dark:bg-[#161716]/50"
234 >
235 <button
236 type="button"
237 className="inline-flex h-10 items-center justify-center whitespace-nowrap rounded-md px-3 text-sm font-medium transition-colors duration-150 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:pointer-events-none disabled:opacity-50"
238 onClick={() => onRemoveItem(item.id)}
239 >
240 <Trash className="h-4 w-4 text-red-400 transition-colors duration-150 fill-red-400/60 " />
241 </button>
242 </motion.div>
243 ) : null}
244 </AnimatePresence>
245 </div>
246 </motion.div>
247 )
248}
249
250SortableListItem.displayName = "SortableListItem"
251
252interface SortableListProps {
253 items: Item[]
254 setItems: Dispatch<SetStateAction<Item[]>>
255 onCompleteItem: (id: number) => void
256 renderItem: (
257 item: Item,
258 order: number,
259 onCompleteItem: (id: number) => void,
260 onRemoveItem: (id: number) => void
261 ) => ReactNode
262}
263
264function SortableList({
265 items,
266 setItems,
267 onCompleteItem,
268 renderItem,
269}: SortableListProps) {
270 if (items) {
271 return (
272 <LayoutGroup>
273 <Reorder.Group
274 axis="y"
275 values={items}
276 onReorder={setItems}
277 className="flex flex-col"
278 >
279 <AnimatePresence>
280 {items?.map((item, index) =>
281 renderItem(item, index, onCompleteItem, (id: number) =>
282 setItems((items) => items.filter((item) => item.id !== id))
283 )
284 )}
285 </AnimatePresence>
286 </Reorder.Group>
287 </LayoutGroup>
288 )
289 }
290 return null
291}
292
293SortableList.displayName = "SortableList"
294
295export { SortableList, SortableListItem }
296export default SortableList
297