From e9663faa42318332937d6d163caacb63b58071b8 Mon Sep 17 00:00:00 2001 From: Rudra Sankha Sinhamahapatra Date: Sun, 19 Jan 2025 15:24:03 +0530 Subject: [PATCH 1/2] Bug: #1281 fixed, [Dark/Light Mode Toggle Doesn't Close Automatically on Mobile] --- components/DarkModeToggle.tsx | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/components/DarkModeToggle.tsx b/components/DarkModeToggle.tsx index cadf3b7ac..e3fcfb35b 100644 --- a/components/DarkModeToggle.tsx +++ b/components/DarkModeToggle.tsx @@ -1,5 +1,5 @@ import { useTheme } from 'next-themes'; -import { useEffect, useState } from 'react'; +import { useEffect, useRef, useState } from 'react'; import React from 'react'; import Image from 'next/image'; @@ -46,8 +46,30 @@ export default function DarkModeToggle() { } }, [theme, resolvedTheme]); + const dropdownRef = useRef(null); + + useEffect(() => { + const handleClickOutside = (event: MouseEvent) => { + if ( + dropdownRef.current && + !dropdownRef.current.contains(event.target as Node) + ) { + setShowSelect(false); + } + }; + + document.addEventListener('click', handleClickOutside); + + return () => { + document.removeEventListener('click', handleClickOutside); + }; + }, []); + return ( -
+