Skip to content

Commit

Permalink
Merge pull request singodiyashubham87#428 from Utsavladia/auto-dropdo…
Browse files Browse the repository at this point in the history
…wn-close

Added the auto dropdown close feature
  • Loading branch information
singodiyashubham87 authored Jul 1, 2024
2 parents 37e5b8b + 936ec61 commit 809d0ee
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/components/DrawingShapes.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useEffect, useState } from "react";
import { useEffect, useRef, useState } from "react";
import rectImg from "../assets/images/rectangle.svg";
import circleImg from "../assets/images/circle.svg";
import triangleImg from "../assets/images/triangle.svg";
Expand All @@ -19,6 +19,21 @@ const DrawingShapes = ({
const [snapshot, setSnapshot] = useState(null);
const [isDropdownOpen, setIsDropdownOpen] = useState(false);

const shapeDropRef = useRef(null);

const handleoutsideClick = (event) => {
if (shapeDropRef.current && !shapeDropRef.current.contains(event.target)) {
setIsDropdownOpen(false);
}
};

useEffect(() => {
document.addEventListener("mousedown", handleoutsideClick);
return () => {
document.removeEventListener("mousedown", handleoutsideClick);
};
}, [isDropdownOpen]);

useEffect(() => {
const canvas = canvasRef.current;
if (!canvas) return;
Expand Down Expand Up @@ -151,6 +166,7 @@ const DrawingShapes = ({
<div
className="drawing-container flex flex-shrink-0"
onClick={toggleDropDown}
ref={shapeDropRef}
>
<div className="relative controls">
<ul className="options flex relative w-[50px]">
Expand Down

0 comments on commit 809d0ee

Please sign in to comment.