-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #420 from jerempy/feature/useMediaQuery
useMediaQuery
- Loading branch information
Showing
6 changed files
with
82 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { useEffect, RefObject, useState } from 'react'; | ||
|
||
export function useOutsideAlerter<T extends HTMLElement>( | ||
ref: RefObject<T>, | ||
handler: () => void, | ||
additionalDeps: unknown[], | ||
) { | ||
useEffect(() => { | ||
function handleClickOutside(this: Document, event: MouseEvent) { | ||
if (ref.current && !ref.current.contains(event.target as Node)) { | ||
handler(); | ||
} | ||
} | ||
document.addEventListener('mousedown', handleClickOutside); | ||
return () => { | ||
document.removeEventListener('mousedown', handleClickOutside); | ||
}; | ||
}, [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 desktopQuery = '(min-width: 960px)'; | ||
const [isMobile, setIsMobile] = useState(false); | ||
const [isDesktop, setIsDesktop] = useState(false); | ||
|
||
useEffect(() => { | ||
const mobileMedia = window.matchMedia(mobileQuery); | ||
const desktopMedia = window.matchMedia(desktopQuery); | ||
const updateMediaQueries = () => { | ||
setIsMobile(mobileMedia.matches); | ||
setIsDesktop(desktopMedia.matches); | ||
}; | ||
updateMediaQueries(); | ||
const listener = () => updateMediaQueries(); | ||
window.addEventListener('resize', listener); | ||
return () => { | ||
window.removeEventListener('resize', listener); | ||
}; | ||
}, [mobileQuery, desktopQuery]); | ||
|
||
return { isMobile, isDesktop }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5cb3df6
There was a problem hiding this comment.
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-brown.vercel.app
docs-gpt-arc53.vercel.app
docs-gpt-git-main-arc53.vercel.app
5cb3df6
There was a problem hiding this comment.
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.vercel.app
nextra-docsgpt-git-main-arc53.vercel.app
docs.docsgpt.co.uk
nextra-docsgpt-arc53.vercel.app