UNPKG

@wordpress/block-editor

Version:
8 lines (7 loc) 15.2 kB
{ "version": 3, "sources": ["../../src/hooks/position.js"], "sourcesContent": ["/**\n * External dependencies\n */\nimport clsx from 'clsx';\n\n/**\n * WordPress dependencies\n */\nimport { __, _x, sprintf } from '@wordpress/i18n';\nimport { getBlockSupport, hasBlockSupport } from '@wordpress/blocks';\nimport { BaseControl, CustomSelectControl } from '@wordpress/components';\nimport { useInstanceId } from '@wordpress/compose';\nimport { useSelect } from '@wordpress/data';\nimport { useMemo, Platform } from '@wordpress/element';\n\n/**\n * Internal dependencies\n */\nimport { useSettings } from '../components/use-settings';\nimport InspectorControls from '../components/inspector-controls';\nimport useBlockDisplayInformation from '../components/use-block-display-information';\nimport { cleanEmptyObject, useStyleOverride } from './utils';\nimport { store as blockEditorStore } from '../store';\n\nconst POSITION_SUPPORT_KEY = 'position';\n\nconst DEFAULT_OPTION = {\n\tkey: 'default',\n\tvalue: '',\n\tname: __( 'Default' ),\n};\n\nconst STICKY_OPTION = {\n\tkey: 'sticky',\n\tvalue: 'sticky',\n\tname: _x( 'Sticky', 'Name for the value of the CSS position property' ),\n\thint: __(\n\t\t'The block will stick to the top of the window instead of scrolling.'\n\t),\n};\n\nconst FIXED_OPTION = {\n\tkey: 'fixed',\n\tvalue: 'fixed',\n\tname: _x( 'Fixed', 'Name for the value of the CSS position property' ),\n\thint: __( 'The block will not move when the page is scrolled.' ),\n};\n\nconst POSITION_SIDES = [ 'top', 'right', 'bottom', 'left' ];\nconst VALID_POSITION_TYPES = [ 'sticky', 'fixed' ];\n\n/**\n * Get calculated position CSS.\n *\n * @param {Object} props Component props.\n * @param {string} props.selector Selector to use.\n * @param {Object} props.style Style object.\n * @return {string} The generated CSS rules.\n */\nexport function getPositionCSS( { selector, style } ) {\n\tlet output = '';\n\n\tconst { type: positionType } = style?.position || {};\n\n\tif ( ! VALID_POSITION_TYPES.includes( positionType ) ) {\n\t\treturn output;\n\t}\n\n\toutput += `${ selector } {`;\n\toutput += `position: ${ positionType };`;\n\n\tPOSITION_SIDES.forEach( ( side ) => {\n\t\tif ( style?.position?.[ side ] !== undefined ) {\n\t\t\toutput += `${ side }: ${ style.position[ side ] };`;\n\t\t}\n\t} );\n\n\tif ( positionType === 'sticky' || positionType === 'fixed' ) {\n\t\t// TODO: Replace hard-coded z-index value with a z-index preset approach in theme.json.\n\t\toutput += `z-index: 10`;\n\t}\n\toutput += `}`;\n\n\treturn output;\n}\n\n/**\n * Determines if there is sticky position support.\n *\n * @param {string|Object} blockType Block name or Block Type object.\n *\n * @return {boolean} Whether there is support.\n */\nexport function hasStickyPositionSupport( blockType ) {\n\tconst support = getBlockSupport( blockType, POSITION_SUPPORT_KEY );\n\treturn !! ( true === support || support?.sticky );\n}\n\n/**\n * Determines if there is fixed position support.\n *\n * @param {string|Object} blockType Block name or Block Type object.\n *\n * @return {boolean} Whether there is support.\n */\nexport function hasFixedPositionSupport( blockType ) {\n\tconst support = getBlockSupport( blockType, POSITION_SUPPORT_KEY );\n\treturn !! ( true === support || support?.fixed );\n}\n\n/**\n * Determines if there is position support.\n *\n * @param {string|Object} blockType Block name or Block Type object.\n *\n * @return {boolean} Whether there is support.\n */\nexport function hasPositionSupport( blockType ) {\n\tconst support = getBlockSupport( blockType, POSITION_SUPPORT_KEY );\n\treturn !! support;\n}\n\n/**\n * Checks if there is a current value in the position block support attributes.\n *\n * @param {Object} props Block props.\n * @return {boolean} Whether or not the block has a position value set.\n */\nexport function hasPositionValue( props ) {\n\treturn props.attributes.style?.position?.type !== undefined;\n}\n\n/**\n * Checks if the block is currently set to a sticky or fixed position.\n * This check is helpful for determining how to position block toolbars or other elements.\n *\n * @param {Object} attributes Block attributes.\n * @return {boolean} Whether or not the block is set to a sticky or fixed position.\n */\nexport function hasStickyOrFixedPositionValue( attributes ) {\n\tconst positionType = attributes?.style?.position?.type;\n\treturn positionType === 'sticky' || positionType === 'fixed';\n}\n\n/**\n * Resets the position block support attributes. This can be used when disabling\n * the position support controls for a block via a `ToolsPanel`.\n *\n * @param {Object} props Block props.\n * @param {Object} props.attributes Block's attributes.\n * @param {Object} props.setAttributes Function to set block's attributes.\n */\nexport function resetPosition( { attributes = {}, setAttributes } ) {\n\tconst { style = {} } = attributes;\n\n\tsetAttributes( {\n\t\tstyle: cleanEmptyObject( {\n\t\t\t...style,\n\t\t\tposition: {\n\t\t\t\t...style?.position,\n\t\t\t\ttype: undefined,\n\t\t\t\ttop: undefined,\n\t\t\t\tright: undefined,\n\t\t\t\tbottom: undefined,\n\t\t\t\tleft: undefined,\n\t\t\t},\n\t\t} ),\n\t} );\n}\n\n/**\n * Custom hook that checks if position settings have been disabled.\n *\n * @param {string} name The name of the block.\n *\n * @return {boolean} Whether padding setting is disabled.\n */\nexport function useIsPositionDisabled( { name: blockName } = {} ) {\n\tconst [ allowFixed, allowSticky ] = useSettings(\n\t\t'position.fixed',\n\t\t'position.sticky'\n\t);\n\tconst isDisabled = ! allowFixed && ! allowSticky;\n\n\treturn ! hasPositionSupport( blockName ) || isDisabled;\n}\n\n/*\n * Position controls rendered in an inspector control panel.\n *\n * @param {Object} props\n *\n * @return {Element} Position panel.\n */\nexport function PositionPanelPure( {\n\tstyle = {},\n\tclientId,\n\tname: blockName,\n\tsetAttributes,\n} ) {\n\tconst allowFixed = hasFixedPositionSupport( blockName );\n\tconst allowSticky = hasStickyPositionSupport( blockName );\n\tconst value = style?.position?.type;\n\n\tconst { firstParentClientId } = useSelect(\n\t\t( select ) => {\n\t\t\tconst { getBlockParents } = select( blockEditorStore );\n\t\t\tconst parents = getBlockParents( clientId );\n\t\t\treturn { firstParentClientId: parents[ parents.length - 1 ] };\n\t\t},\n\t\t[ clientId ]\n\t);\n\n\tconst blockInformation = useBlockDisplayInformation( firstParentClientId );\n\tconst stickyHelpText =\n\t\tallowSticky && value === STICKY_OPTION.value && blockInformation\n\t\t\t? sprintf(\n\t\t\t\t\t/* translators: %s: the name of the parent block. */\n\t\t\t\t\t__(\n\t\t\t\t\t\t'The block will stick to the scrollable area of the parent %s block.'\n\t\t\t\t\t),\n\t\t\t\t\tblockInformation.title\n\t\t\t )\n\t\t\t: null;\n\n\tconst options = useMemo( () => {\n\t\tconst availableOptions = [ DEFAULT_OPTION ];\n\t\t// Display options if they are allowed, or if a block already has a valid value set.\n\t\t// This allows for a block to be switched off from a position type that is not allowed.\n\t\tif ( allowSticky || value === STICKY_OPTION.value ) {\n\t\t\tavailableOptions.push( STICKY_OPTION );\n\t\t}\n\t\tif ( allowFixed || value === FIXED_OPTION.value ) {\n\t\t\tavailableOptions.push( FIXED_OPTION );\n\t\t}\n\t\treturn availableOptions;\n\t}, [ allowFixed, allowSticky, value ] );\n\n\tconst onChangeType = ( next ) => {\n\t\t// For now, use a hard-coded `0px` value for the position.\n\t\t// `0px` is preferred over `0` as it can be used in `calc()` functions.\n\t\t// In the future, it could be useful to allow for an offset value.\n\t\tconst placementValue = '0px';\n\n\t\tconst newStyle = {\n\t\t\t...style,\n\t\t\tposition: {\n\t\t\t\t...style?.position,\n\t\t\t\ttype: next,\n\t\t\t\ttop:\n\t\t\t\t\tnext === 'sticky' || next === 'fixed'\n\t\t\t\t\t\t? placementValue\n\t\t\t\t\t\t: undefined,\n\t\t\t},\n\t\t};\n\n\t\tsetAttributes( {\n\t\t\tstyle: cleanEmptyObject( newStyle ),\n\t\t} );\n\t};\n\n\tconst selectedOption = value\n\t\t? options.find( ( option ) => option.value === value ) || DEFAULT_OPTION\n\t\t: DEFAULT_OPTION;\n\n\t// Only display position controls if there is at least one option to choose from.\n\treturn Platform.select( {\n\t\tweb:\n\t\t\toptions.length > 1 ? (\n\t\t\t\t<InspectorControls group=\"position\">\n\t\t\t\t\t<BaseControl help={ stickyHelpText }>\n\t\t\t\t\t\t<CustomSelectControl\n\t\t\t\t\t\t\t__next40pxDefaultSize\n\t\t\t\t\t\t\tlabel={ __( 'Position' ) }\n\t\t\t\t\t\t\thideLabelFromVision\n\t\t\t\t\t\t\tdescribedBy={ sprintf(\n\t\t\t\t\t\t\t\t// translators: %s: Currently selected position.\n\t\t\t\t\t\t\t\t__( 'Currently selected position: %s' ),\n\t\t\t\t\t\t\t\tselectedOption.name\n\t\t\t\t\t\t\t) }\n\t\t\t\t\t\t\toptions={ options }\n\t\t\t\t\t\t\tvalue={ selectedOption }\n\t\t\t\t\t\t\tonChange={ ( { selectedItem } ) => {\n\t\t\t\t\t\t\t\tonChangeType( selectedItem.value );\n\t\t\t\t\t\t\t} }\n\t\t\t\t\t\t\tsize=\"__unstable-large\"\n\t\t\t\t\t\t/>\n\t\t\t\t\t</BaseControl>\n\t\t\t\t</InspectorControls>\n\t\t\t) : null,\n\t\tnative: null,\n\t} );\n}\n\nexport default {\n\tedit: function Edit( props ) {\n\t\tconst isPositionDisabled = useIsPositionDisabled( props );\n\t\tif ( isPositionDisabled ) {\n\t\t\treturn null;\n\t\t}\n\t\treturn <PositionPanelPure { ...props } />;\n\t},\n\tuseBlockProps,\n\tattributeKeys: [ 'style' ],\n\thasSupport( name ) {\n\t\treturn hasBlockSupport( name, POSITION_SUPPORT_KEY );\n\t},\n};\n\n// Used for generating the instance ID\nconst POSITION_BLOCK_PROPS_REFERENCE = {};\n\nfunction useBlockProps( { name, style } ) {\n\tconst hasPositionBlockSupport = hasBlockSupport(\n\t\tname,\n\t\tPOSITION_SUPPORT_KEY\n\t);\n\tconst isPositionDisabled = useIsPositionDisabled( { name } );\n\tconst allowPositionStyles = hasPositionBlockSupport && ! isPositionDisabled;\n\n\tconst id = useInstanceId( POSITION_BLOCK_PROPS_REFERENCE );\n\n\t// Higher specificity to override defaults in editor UI.\n\tconst positionSelector = `.wp-container-${ id }.wp-container-${ id }`;\n\n\t// Get CSS string for the current position values.\n\tlet css;\n\tif ( allowPositionStyles ) {\n\t\tcss =\n\t\t\tgetPositionCSS( {\n\t\t\t\tselector: positionSelector,\n\t\t\t\tstyle,\n\t\t\t} ) || '';\n\t}\n\n\t// Attach a `wp-container-` id-based class name.\n\tconst className = clsx( {\n\t\t[ `wp-container-${ id }` ]: allowPositionStyles && !! css, // Only attach a container class if there is generated CSS to be attached.\n\t\t[ `is-position-${ style?.position?.type }` ]:\n\t\t\tallowPositionStyles && !! css && !! style?.position?.type,\n\t} );\n\n\tuseStyleOverride( { css } );\n\n\treturn { className };\n}\n"], "mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,kBAAiB;AAKjB,kBAAgC;AAChC,oBAAiD;AACjD,wBAAiD;AACjD,qBAA8B;AAC9B,kBAA0B;AAC1B,qBAAkC;AAKlC,0BAA4B;AAC5B,gCAA8B;AAC9B,2CAAuC;AACvC,mBAAmD;AACnD,mBAA0C;AAyPpC;AAvPN,IAAM,uBAAuB;AAE7B,IAAM,iBAAiB;AAAA,EACtB,KAAK;AAAA,EACL,OAAO;AAAA,EACP,UAAM,gBAAI,SAAU;AACrB;AAEA,IAAM,gBAAgB;AAAA,EACrB,KAAK;AAAA,EACL,OAAO;AAAA,EACP,UAAM,gBAAI,UAAU,iDAAkD;AAAA,EACtE,UAAM;AAAA,IACL;AAAA,EACD;AACD;AAEA,IAAM,eAAe;AAAA,EACpB,KAAK;AAAA,EACL,OAAO;AAAA,EACP,UAAM,gBAAI,SAAS,iDAAkD;AAAA,EACrE,UAAM,gBAAI,oDAAqD;AAChE;AAEA,IAAM,iBAAiB,CAAE,OAAO,SAAS,UAAU,MAAO;AAC1D,IAAM,uBAAuB,CAAE,UAAU,OAAQ;AAU1C,SAAS,eAAgB,EAAE,UAAU,MAAM,GAAI;AACrD,MAAI,SAAS;AAEb,QAAM,EAAE,MAAM,aAAa,IAAI,OAAO,YAAY,CAAC;AAEnD,MAAK,CAAE,qBAAqB,SAAU,YAAa,GAAI;AACtD,WAAO;AAAA,EACR;AAEA,YAAU,GAAI,QAAS;AACvB,YAAU,aAAc,YAAa;AAErC,iBAAe,QAAS,CAAE,SAAU;AACnC,QAAK,OA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