2022-02-17 18:11:33 +08:00
|
|
|
import { useEffect, useRef } from 'react';
|
|
|
|
import useForceUpdate from '../../_util/hooks/useForceUpdate';
|
2022-05-07 14:31:54 +08:00
|
|
|
import type { ScreenMap } from '../../_util/responsiveObserve';
|
|
|
|
import ResponsiveObserve from '../../_util/responsiveObserve';
|
2020-03-15 20:53:23 +08:00
|
|
|
|
2022-02-17 18:11:33 +08:00
|
|
|
function useBreakpoint(refreshOnChange: boolean = true): ScreenMap {
|
|
|
|
const screensRef = useRef<ScreenMap>({});
|
|
|
|
const forceUpdate = useForceUpdate();
|
2020-03-15 20:53:23 +08:00
|
|
|
|
|
|
|
useEffect(() => {
|
2022-11-19 13:47:33 +08:00
|
|
|
const token = ResponsiveObserve.subscribe((supportScreens) => {
|
2022-02-17 18:11:33 +08:00
|
|
|
screensRef.current = supportScreens;
|
|
|
|
if (refreshOnChange) {
|
|
|
|
forceUpdate();
|
|
|
|
}
|
2020-03-15 20:53:23 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
return () => ResponsiveObserve.unsubscribe(token);
|
|
|
|
}, []);
|
|
|
|
|
2022-02-17 18:11:33 +08:00
|
|
|
return screensRef.current;
|
2020-03-15 20:53:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export default useBreakpoint;
|