ShtukbaseStandard
package file→ .shtukbase/packages/keer-ui/library/hooks/use-mobile.ts

library/hooks/use-mobile.ts

22 lines, 662 B.

  1. import * as React from "react";
  2. // The phone width is the same as `--breakpoint-phone` in styles/tokens.css.
  3. const MOBILE_BREAKPOINT = 560;
  4. export function useIsMobile() {
  5. const [isMobile, setIsMobile] = React.useState<boolean | undefined>(
  6. undefined,
  7. );
  8. React.useEffect(() => {
  9. const mql = window.matchMedia(`(max-width: ${MOBILE_BREAKPOINT - 1}px)`);
  10. const onChange = () => {
  11. setIsMobile(window.innerWidth < MOBILE_BREAKPOINT);
  12. };
  13. mql.addEventListener("change", onChange);
  14. setIsMobile(window.innerWidth < MOBILE_BREAKPOINT);
  15. return () => mql.removeEventListener("change", onChange);
  16. }, []);
  17. return !!isMobile;
  18. }