@modern-kit/react
Version:
35 lines (31 loc) • 982 B
JavaScript
;
var React = require('react');
var utils = require('@modern-kit/utils');
const DEFAULT_SIZE = { width: 0, height: 0 };
const subscribe = (onStoreChange, wait) => {
const debouncedCallback = utils.debounce(onStoreChange, wait ?? 0);
const handleStoreChange = utils.isNumber(wait) ? debouncedCallback : onStoreChange;
window.addEventListener("resize", handleStoreChange);
return () => {
window.removeEventListener("resize", handleStoreChange);
};
};
const getSnapshot = () => {
return JSON.stringify({
width: window.innerWidth,
height: window.innerHeight
});
};
const getServerSnapshot = () => {
return JSON.stringify(DEFAULT_SIZE);
};
function useWindowSize(debounceWait) {
const windowSize = React.useSyncExternalStore(
(onStoreChange) => subscribe(onStoreChange, debounceWait),
getSnapshot,
getServerSnapshot
);
return JSON.parse(windowSize);
}
exports.useWindowSize = useWindowSize;
//# sourceMappingURL=index.cjs.map