Skip to content

Commit

Permalink
Merge pull request #654 from adarsh-jha-dev/patch-2
Browse files Browse the repository at this point in the history
 Enhanced Hooks with Outside Click Handling and Dark Mode Detection
  • Loading branch information
dartpain authored Oct 23, 2023
2 parents 465c4af + 78b8d3e commit 0821d7a
Showing 1 changed file with 26 additions and 5 deletions.
31 changes: 26 additions & 5 deletions frontend/src/hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,42 +4,63 @@ export function useOutsideAlerter<T extends HTMLElement>(
ref: RefObject<T>,
handler: () => void,
additionalDeps: unknown[],
handleEscapeKey?: boolean,
) {
useEffect(() => {
function handleClickOutside(this: Document, event: MouseEvent) {
function handleClickOutside(event: MouseEvent) {
if (ref.current && !ref.current.contains(event.target as Node)) {
handler();
}
}

function handleEscape(event: KeyboardEvent) {
if (event.key === 'Escape') {
handler();
}
}

document.addEventListener('mousedown', handleClickOutside);
if (handleEscapeKey) {
document.addEventListener('keydown', handleEscape);
}

return () => {
document.removeEventListener('mousedown', handleClickOutside);
if (handleEscapeKey) {
document.removeEventListener('keydown', handleEscape);
}
};
}, [ref, ...additionalDeps]);
}

// Use isMobile for checking if the width is in the expected mobile range (less than 768px)
// use IsDesktop for effects you explicitly only want when width is wider than 960px.
export function useMediaQuery() {
const mobileQuery = '(max-width: 768px)';
const darkModeQuery = '(prefers-color-scheme: dark)'; // Detect dark mode
const desktopQuery = '(min-width: 960px)';
const [isMobile, setIsMobile] = useState(false);
const [isDesktop, setIsDesktop] = useState(false);
const [isDarkMode, setIsDarkMode] = useState(false);

useEffect(() => {
const mobileMedia = window.matchMedia(mobileQuery);
const desktopMedia = window.matchMedia(desktopQuery);
const darkModeMedia = window.matchMedia(darkModeQuery);

const updateMediaQueries = () => {
setIsMobile(mobileMedia.matches);
setIsDesktop(desktopMedia.matches);
setIsDarkMode(darkModeMedia.matches);
};

updateMediaQueries();

const listener = () => updateMediaQueries();
window.addEventListener('resize', listener);

return () => {
window.removeEventListener('resize', listener);
};
}, [mobileQuery, desktopQuery]);
}, [mobileQuery, desktopQuery, darkModeQuery]);

return { isMobile, isDesktop };
return { isMobile, isDesktop, isDarkMode };
}

2 comments on commit 0821d7a

@vercel
Copy link

@vercel vercel bot commented on 0821d7a Oct 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

docs-gpt – ./frontend

docs-gpt-arc53.vercel.app
docs-gpt-git-main-arc53.vercel.app
docs-gpt-brown.vercel.app

@vercel
Copy link

@vercel vercel bot commented on 0821d7a Oct 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

nextra-docsgpt – ./docs

nextra-docsgpt-arc53.vercel.app
docs.docsgpt.co.uk
nextra-docsgpt-git-main-arc53.vercel.app
nextra-docsgpt.vercel.app

Please sign in to comment.