2023-08-07 16:04:55 +08:00
|
|
|
import { useRef } from 'react';
|
|
|
|
import useLayoutEffect from 'rc-util/lib/hooks/useLayoutEffect';
|
2022-02-17 18:11:33 +08:00
|
|
|
import useForceUpdate from '../../_util/hooks/useForceUpdate';
|
2022-12-22 10:11:08 +08:00
|
|
|
import type { ScreenMap } from '../../_util/responsiveObserver';
|
|
|
|
import useResponsiveObserver from '../../_util/responsiveObserver';
|
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();
|
2022-12-22 10:11:08 +08:00
|
|
|
const responsiveObserver = useResponsiveObserver();
|
2020-03-15 20:53:23 +08:00
|
|
|
|
2023-08-07 16:04:55 +08:00
|
|
|
useLayoutEffect(() => {
|
2022-12-22 10:11:08 +08:00
|
|
|
const token = responsiveObserver.subscribe((supportScreens) => {
|
2022-02-17 18:11:33 +08:00
|
|
|
screensRef.current = supportScreens;
|
|
|
|
if (refreshOnChange) {
|
|
|
|
forceUpdate();
|
|
|
|
}
|
2020-03-15 20:53:23 +08:00
|
|
|
});
|
|
|
|
|
2022-12-22 10:11:08 +08:00
|
|
|
return () => responsiveObserver.unsubscribe(token);
|
2020-03-15 20:53:23 +08:00
|
|
|
}, []);
|
|
|
|
|
2022-02-17 18:11:33 +08:00
|
|
|
return screensRef.current;
|
2020-03-15 20:53:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export default useBreakpoint;
|