import { useState, useEffect } from "react";

function getWindowDimensions() {
  const width = window.innerWidth;
  const height = window.innerHeight
  return {
    width,
    height,
  };
}

export default function useWindowDimensions() {
  const [windowDimensions, setWindowDimensions] = useState(
    getWindowDimensions()
  );

  useEffect(() => {
    function handleResize() {
      setWindowDimensions(getWindowDimensions());
    }
    window.addEventListener("orientationchange", handleResize)
    window.addEventListener("resize", handleResize);
    return () => {
      window.removeEventListener('resize', handleResize)
      window.removeEventListener('orientationchange', handleResize)
    }
  }, []);

  return windowDimensions;
}
