@wordpress/block-editor
Version:
8 lines (7 loc) • 3.29 kB
Source Map (JSON)
{
"version": 3,
"sources": ["../../../src/components/block-visibility/use-block-visibility.js"],
"sourcesContent": ["/**\n * WordPress dependencies\n */\nimport { useViewportMatch } from '@wordpress/compose';\nimport { useMemo } from '@wordpress/element';\n\n/**\n * Internal dependencies\n */\nimport { BLOCK_VISIBILITY_VIEWPORTS } from './constants';\n\n/**\n * Returns information about the current block visibility state.\n *\n * @param {Object} options Parameters to avoid extra store subscriptions.\n * @param {Object|boolean} options.blockVisibility Block visibility metadata.\n * @param {string} options.deviceType Current device type ('desktop', 'tablet', 'mobile').\n * @return {Object} Object with `isBlockCurrentlyHidden` and `currentViewport` boolean properties.\n */\nexport default function useBlockVisibility( options = {} ) {\n\tconst {\n\t\tblockVisibility = undefined,\n\t\tdeviceType = BLOCK_VISIBILITY_VIEWPORTS.desktop.key,\n\t} = options;\n\n\tconst isLargerThanMobile = useViewportMatch( 'mobile', '>=' ); // >= 480px\n\tconst isLargerThanTablet = useViewportMatch( 'medium', '>=' ); // >= 782px\n\n\t/*\n\t * Priority:\n\t * 1. Device type override (Mobile/Tablet) - uses device type to determine viewport\n\t * 2. Actual window size (Desktop mode) - uses viewport detection\n\t */\n\tconst currentViewport = useMemo( () => {\n\t\tif ( deviceType === BLOCK_VISIBILITY_VIEWPORTS.mobile.key ) {\n\t\t\treturn BLOCK_VISIBILITY_VIEWPORTS.mobile.key;\n\t\t}\n\t\tif ( deviceType === BLOCK_VISIBILITY_VIEWPORTS.tablet.key ) {\n\t\t\treturn BLOCK_VISIBILITY_VIEWPORTS.tablet.key;\n\t\t}\n\t\tif ( ! isLargerThanMobile ) {\n\t\t\treturn BLOCK_VISIBILITY_VIEWPORTS.mobile.key;\n\t\t}\n\t\tif ( isLargerThanMobile && ! isLargerThanTablet ) {\n\t\t\treturn BLOCK_VISIBILITY_VIEWPORTS.tablet.key;\n\t\t}\n\t\treturn BLOCK_VISIBILITY_VIEWPORTS.desktop.key;\n\t}, [ deviceType, isLargerThanMobile, isLargerThanTablet ] );\n\n\t// Determine if block is currently hidden.\n\tconst isBlockCurrentlyHidden = useMemo( () => {\n\t\tif ( blockVisibility === false ) {\n\t\t\treturn true;\n\t\t}\n\n\t\tif (\n\t\t\twindow.__experimentalHideBlocksBasedOnScreenSize &&\n\t\t\tblockVisibility?.[ currentViewport ] === false\n\t\t) {\n\t\t\treturn true;\n\t\t}\n\n\t\treturn false;\n\t}, [ blockVisibility, currentViewport ] );\n\n\treturn {\n\t\tisBlockCurrentlyHidden,\n\t\tcurrentViewport,\n\t};\n}\n"],
"mappings": ";AAGA,SAAS,wBAAwB;AACjC,SAAS,eAAe;AAKxB,SAAS,kCAAkC;AAU5B,SAAR,mBAAqC,UAAU,CAAC,GAAI;AAC1D,QAAM;AAAA,IACL,kBAAkB;AAAA,IAClB,aAAa,2BAA2B,QAAQ;AAAA,EACjD,IAAI;AAEJ,QAAM,qBAAqB,iBAAkB,UAAU,IAAK;AAC5D,QAAM,qBAAqB,iBAAkB,UAAU,IAAK;AAO5D,QAAM,kBAAkB,QAAS,MAAM;AACtC,QAAK,eAAe,2BAA2B,OAAO,KAAM;AAC3D,aAAO,2BAA2B,OAAO;AAAA,IAC1C;AACA,QAAK,eAAe,2BAA2B,OAAO,KAAM;AAC3D,aAAO,2BAA2B,OAAO;AAAA,IAC1C;AACA,QAAK,CAAE,oBAAqB;AAC3B,aAAO,2BAA2B,OAAO;AAAA,IAC1C;AACA,QAAK,sBAAsB,CAAE,oBAAqB;AACjD,aAAO,2BAA2B,OAAO;AAAA,IAC1C;AACA,WAAO,2BAA2B,QAAQ;AAAA,EAC3C,GAAG,CAAE,YAAY,oBAAoB,kBAAmB,CAAE;AAG1D,QAAM,yBAAyB,QAAS,MAAM;AAC7C,QAAK,oBAAoB,OAAQ;AAChC,aAAO;AAAA,IACR;AAEA,QACC,OAAO,6CACP,kBAAmB,eAAgB,MAAM,OACxC;AACD,aAAO;AAAA,IACR;AAEA,WAAO;AAAA,EACR,GAAG,CAAE,iBAAiB,eAAgB,CAAE;AAExC,SAAO;AAAA,IACN;AAAA,IACA;AAAA,EACD;AACD;",
"names": []
}