@wordpress/block-editor
Version:
8 lines (7 loc) • 3.43 kB
Source Map (JSON)
{
"version": 3,
"sources": ["../../../src/components/use-resize-canvas/index.js"],
"sourcesContent": ["/**\n * WordPress dependencies\n */\nimport { useEffect, useState } from '@wordpress/element';\n\n/**\n * Function to resize the editor window.\n *\n * @param {string} deviceType Used for determining the size of the container (e.g. Desktop, Tablet, Mobile)\n *\n * @return {Object} Inline styles to be added to resizable container.\n */\nexport default function useResizeCanvas( deviceType ) {\n\tconst [ actualWidth, updateActualWidth ] = useState( window.innerWidth );\n\n\tuseEffect( () => {\n\t\tif ( deviceType === 'Desktop' ) {\n\t\t\treturn;\n\t\t}\n\n\t\tconst resizeListener = () => updateActualWidth( window.innerWidth );\n\t\twindow.addEventListener( 'resize', resizeListener );\n\n\t\treturn () => {\n\t\t\twindow.removeEventListener( 'resize', resizeListener );\n\t\t};\n\t}, [ deviceType ] );\n\n\tconst getCanvasWidth = ( device ) => {\n\t\tlet deviceWidth;\n\n\t\t/*\n\t\t * Matches the breakpoints in packages/base-styles/_breakpoints.scss,\n\t\t * and breakpoints in packages/compose/src/hooks/use-viewport-match/index.js.\n\t\t * minus 1 to trigger the media query for device preview.\n\t\t */\n\t\tswitch ( device ) {\n\t\t\tcase 'Tablet':\n\t\t\t\tdeviceWidth = 782 - 1; // preview for useViewportMatch( 'medium', '<' )\n\t\t\t\tbreak;\n\t\t\tcase 'Mobile':\n\t\t\t\tdeviceWidth = 480 - 1; // preview for useViewportMatch( 'mobile', '<' )\n\t\t\t\tbreak;\n\t\t\tdefault:\n\t\t\t\treturn null;\n\t\t}\n\n\t\treturn deviceWidth < actualWidth ? deviceWidth : actualWidth;\n\t};\n\n\tconst contentInlineStyles = ( device ) => {\n\t\tconst height = device === 'Mobile' ? '768px' : '1024px';\n\t\tconst marginVertical = '40px';\n\t\tconst marginHorizontal = 'auto';\n\n\t\tswitch ( device ) {\n\t\t\tcase 'Tablet':\n\t\t\tcase 'Mobile':\n\t\t\t\treturn {\n\t\t\t\t\twidth: getCanvasWidth( device ),\n\t\t\t\t\t// Keeping margin styles separate to avoid warnings\n\t\t\t\t\t// when those props get overridden in the iframe component\n\t\t\t\t\tmarginTop: marginVertical,\n\t\t\t\t\tmarginBottom: marginVertical,\n\t\t\t\t\tmarginLeft: marginHorizontal,\n\t\t\t\t\tmarginRight: marginHorizontal,\n\t\t\t\t\theight,\n\t\t\t\t\toverflowY: 'auto',\n\t\t\t\t};\n\t\t\tdefault:\n\t\t\t\treturn {\n\t\t\t\t\tmarginLeft: marginHorizontal,\n\t\t\t\t\tmarginRight: marginHorizontal,\n\t\t\t\t};\n\t\t}\n\t};\n\n\treturn contentInlineStyles( deviceType );\n}\n"],
"mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,qBAAoC;AASrB,SAAR,gBAAkC,YAAa;AACrD,QAAM,CAAE,aAAa,iBAAkB,QAAI,yBAAU,OAAO,UAAW;AAEvE,gCAAW,MAAM;AAChB,QAAK,eAAe,WAAY;AAC/B;AAAA,IACD;AAEA,UAAM,iBAAiB,MAAM,kBAAmB,OAAO,UAAW;AAClE,WAAO,iBAAkB,UAAU,cAAe;AAElD,WAAO,MAAM;AACZ,aAAO,oBAAqB,UAAU,cAAe;AAAA,IACtD;AAAA,EACD,GAAG,CAAE,UAAW,CAAE;AAElB,QAAM,iBAAiB,CAAE,WAAY;AACpC,QAAI;AAOJ,YAAS,QAAS;AAAA,MACjB,KAAK;AACJ,sBAAc,MAAM;AACpB;AAAA,MACD,KAAK;AACJ,sBAAc,MAAM;AACpB;AAAA,MACD;AACC,eAAO;AAAA,IACT;AAEA,WAAO,cAAc,cAAc,cAAc;AAAA,EAClD;AAEA,QAAM,sBAAsB,CAAE,WAAY;AACzC,UAAM,SAAS,WAAW,WAAW,UAAU;AAC/C,UAAM,iBAAiB;AACvB,UAAM,mBAAmB;AAEzB,YAAS,QAAS;AAAA,MACjB,KAAK;AAAA,MACL,KAAK;AACJ,eAAO;AAAA,UACN,OAAO,eAAgB,MAAO;AAAA;AAAA;AAAA,UAG9B,WAAW;AAAA,UACX,cAAc;AAAA,UACd,YAAY;AAAA,UACZ,aAAa;AAAA,UACb;AAAA,UACA,WAAW;AAAA,QACZ;AAAA,MACD;AACC,eAAO;AAAA,UACN,YAAY;AAAA,UACZ,aAAa;AAAA,QACd;AAAA,IACF;AAAA,EACD;AAEA,SAAO,oBAAqB,UAAW;AACxC;",
"names": []
}