@clayui/shared
Version:
ClayShared component
22 lines (21 loc) • 693 B
JavaScript
/**
* SPDX-FileCopyrightText: © 2022 Liferay, Inc. <https://liferay.com>
* SPDX-License-Identifier: BSD-3-Clause
*/
import { useEffect, useState } from 'react';
import { throttle } from "./throttle.js";
const getIsMobile = () => window.document.body.clientWidth < 768;
export function useIsMobileDevice() {
const [isMobile, setIsMobile] = useState(getIsMobile());
useEffect(() => {
if (typeof window === 'undefined') {
return;
}
const handleResize = throttle(() => setIsMobile(getIsMobile()), 200);
window.addEventListener('resize', handleResize);
return () => {
window.removeEventListener('resize', handleResize);
};
}, []);
return isMobile;
}