chatbot-template/hooks/use-mobile.tsx

22 lines
585 B
TypeScript
Raw Normal View History

2024-11-14 12:16:05 -05:00
import * as React from 'react';
2024-11-14 12:16:05 -05:00
const MOBILE_BREAKPOINT = 768;
export function useIsMobile() {
2024-11-14 12:16:05 -05:00
const [isMobile, setIsMobile] = React.useState<boolean | undefined>(
2024-11-15 13:00:15 -05:00
undefined,
2024-11-14 12:16:05 -05:00
);
React.useEffect(() => {
2024-11-14 12:16:05 -05:00
const mql = window.matchMedia(`(max-width: ${MOBILE_BREAKPOINT - 1}px)`);
const onChange = () => {
2024-11-14 12:16:05 -05:00
setIsMobile(window.innerWidth < MOBILE_BREAKPOINT);
};
mql.addEventListener('change', onChange);
setIsMobile(window.innerWidth < MOBILE_BREAKPOINT);
return () => mql.removeEventListener('change', onChange);
}, []);
2024-11-14 12:16:05 -05:00
return !!isMobile;
}