1- import { useState } from "react" ;
1+ import { useState , useEffect , useRef } from "react" ;
22import { CheckCircle , Circle , Plus , Trash2 , ListTodo } from "lucide-react" ;
33import { usePopup } from "../context/PopupContext" ;
44import { useAuth } from "../store/auth" ;
@@ -10,6 +10,8 @@ export default function TodoList() {
1010 const [ newTodo , setNewTodo ] = useState ( "" ) ;
1111 const [ open , setOpen ] = useState ( false ) ; // widget toggle state
1212
13+ const widgetRef = useRef ( null ) ;
14+
1315 const handleAdd = ( ) => {
1416 if ( ! isLoggedIn ) return ;
1517 if ( newTodo . trim ( ) ) {
@@ -21,6 +23,23 @@ export default function TodoList() {
2123 // Ensure todos is always an array
2224 const todoList = Array . isArray ( todos ) ? todos : [ ] ;
2325
26+ // 🔹 Close widget if clicked outside
27+ useEffect ( ( ) => {
28+ const handleClickOutside = ( e ) => {
29+ if ( widgetRef . current && ! widgetRef . current . contains ( e . target ) ) {
30+ setOpen ( false ) ;
31+ }
32+ } ;
33+
34+ if ( open ) {
35+ document . addEventListener ( "mousedown" , handleClickOutside ) ;
36+ }
37+
38+ return ( ) => {
39+ document . removeEventListener ( "mousedown" , handleClickOutside ) ;
40+ } ;
41+ } , [ open ] ) ;
42+
2443 return (
2544 < >
2645 { /* Floating Toggle Button */ }
@@ -45,6 +64,7 @@ export default function TodoList() {
4564 exit = { { opacity : 0 , scale : 0.8 , y : 40 } }
4665 transition = { { duration : 0.35 , ease : "easeOut" } }
4766 className = "fixed bottom-28 left-4 z-50 w-80 max-w-full"
67+ ref = { widgetRef } // 🔹 reference for outside click detection
4868 >
4969 < motion . div
5070 layout
@@ -60,8 +80,7 @@ export default function TodoList() {
6080 Todo List
6181 < span className = "text-sm text-gray-600 dark:text-dark-text-secondary flex items-center gap-2" >
6282 < CheckCircle className = "text-success" /> { " " }
63- { todoList . filter ( ( t ) => t . completed ) . length } /{ " " }
64- { todoList . length }
83+ { todoList . filter ( ( t ) => t . completed ) . length } / { todoList . length }
6584 </ span >
6685 </ motion . h2 >
6786
@@ -101,9 +120,7 @@ export default function TodoList() {
101120 >
102121 < button
103122 disabled = { ! isLoggedIn }
104- onClick = { ( ) =>
105- toggleTodoItem ( todo . _id || todo . id )
106- }
123+ onClick = { ( ) => toggleTodoItem ( todo . _id || todo . id ) }
107124 >
108125 { todo . completed ? (
109126 < CheckCircle className = "text-success" />
0 commit comments