Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: #1281 fixed, [Dark/Light Mode Toggle Doesn't Close Automatically on Mobile] #1329

Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions components/DarkModeToggle.tsx
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -46,8 +46,30 @@
}
}, [theme, resolvedTheme]);

const dropdownRef = useRef<HTMLDivElement>(null);

useEffect(() => {
const handleClickOutside = (event: MouseEvent) => {
if (
dropdownRef.current &&
!dropdownRef.current.contains(event.target as Node)
) {
setShowSelect(false);

Check warning on line 57 in components/DarkModeToggle.tsx

View check run for this annotation

Codecov / codecov/patch

components/DarkModeToggle.tsx#L57

Added line #L57 was not covered by tests
}
};

document.addEventListener('click', handleClickOutside);

return () => {
document.removeEventListener('click', handleClickOutside);
};
}, []);

return (
<div className='relative w-10 h-10 dark-mode-toggle-container'>
<div
ref={dropdownRef}
className='relative w-10 h-10 dark-mode-toggle-container'
>
<button
onClick={() => setShowSelect(!showSelect)}
className='dark-mode-toggle rounded-md dark:hover:bg-gray-700 p-1.5 hover:bg-gray-100 transition duration-150 '
Expand Down
Loading