Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

643 B
Raw Blame
import { useCallback, useRef, useState } from "react";

const SDK_MOBILE_BREAKPOINT = 640;

export function useMobileLayout() {
  const [width, setWidth] = useState(0);
  const observerRef = useRef<ResizeObserver | null>(null);

  const ref = useCallback((node: HTMLElement | null) => {
    observerRef.current?.disconnect();

    if (node) {
      const observer = new ResizeObserver(([entry]) => {
        setWidth(entry.contentBoxSize[0].inlineSize);
      });
      observer.observe(node);
      observerRef.current = observer;
    }
  }, []);

  const isMobile = width > 0 && width < SDK_MOBILE_BREAKPOINT;

  return { ref, isMobile };
}