@wordpress/block-editor
Version:
45 lines (44 loc) • 1.59 kB
JavaScript
// packages/block-editor/src/components/block-visibility/use-block-visibility.js
import { useViewportMatch } from "@wordpress/compose";
import { useMemo } from "@wordpress/element";
import { BLOCK_VISIBILITY_VIEWPORTS } from "./constants.mjs";
function useBlockVisibility(options = {}) {
const {
blockVisibility = void 0,
deviceType = BLOCK_VISIBILITY_VIEWPORTS.desktop.key
} = options;
const isLargerThanMobile = useViewportMatch("mobile", ">=");
const isLargerThanTablet = useViewportMatch("medium", ">=");
const currentViewport = useMemo(() => {
if (deviceType === BLOCK_VISIBILITY_VIEWPORTS.mobile.key) {
return BLOCK_VISIBILITY_VIEWPORTS.mobile.key;
}
if (deviceType === BLOCK_VISIBILITY_VIEWPORTS.tablet.key) {
return BLOCK_VISIBILITY_VIEWPORTS.tablet.key;
}
if (!isLargerThanMobile) {
return BLOCK_VISIBILITY_VIEWPORTS.mobile.key;
}
if (isLargerThanMobile && !isLargerThanTablet) {
return BLOCK_VISIBILITY_VIEWPORTS.tablet.key;
}
return BLOCK_VISIBILITY_VIEWPORTS.desktop.key;
}, [deviceType, isLargerThanMobile, isLargerThanTablet]);
const isBlockCurrentlyHidden = useMemo(() => {
if (blockVisibility === false) {
return true;
}
if (window.__experimentalHideBlocksBasedOnScreenSize && blockVisibility?.[currentViewport] === false) {
return true;
}
return false;
}, [blockVisibility, currentViewport]);
return {
isBlockCurrentlyHidden,
currentViewport
};
}
export {
useBlockVisibility as default
};
//# sourceMappingURL=use-block-visibility.mjs.map