refactor hooks organization and month picker
This commit is contained in:
26
components/ui/use-mobile.ts
Normal file
26
components/ui/use-mobile.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
import * as React from "react";
|
||||
|
||||
const MOBILE_BREAKPOINT = 768;
|
||||
const MOBILE_MEDIA_QUERY = `(max-width: ${MOBILE_BREAKPOINT - 1}px)`;
|
||||
|
||||
export function useIsMobile() {
|
||||
const subscribe = React.useCallback((onStoreChange: () => void) => {
|
||||
if (typeof window === "undefined") {
|
||||
return () => {};
|
||||
}
|
||||
|
||||
const mediaQueryList = window.matchMedia(MOBILE_MEDIA_QUERY);
|
||||
mediaQueryList.addEventListener("change", onStoreChange);
|
||||
return () => mediaQueryList.removeEventListener("change", onStoreChange);
|
||||
}, []);
|
||||
|
||||
const getSnapshot = React.useCallback(() => {
|
||||
if (typeof window === "undefined") {
|
||||
return false;
|
||||
}
|
||||
|
||||
return window.matchMedia(MOBILE_MEDIA_QUERY).matches;
|
||||
}, []);
|
||||
|
||||
return React.useSyncExternalStore(subscribe, getSnapshot, () => false);
|
||||
}
|
||||
Reference in New Issue
Block a user