index.js
127 lines
| 1 | import { __ } from '@wordpress/i18n'; |
| 2 | import PanelArea from '../../../../components/panel-area'; |
| 3 | import getIcon from '../../../../utils/get-icon'; |
| 4 | import { useContext } from '@wordpress/element'; |
| 5 | import ControlsContext from '../../../../block-context'; |
| 6 | import DeviceControls from './components/device-controls'; |
| 7 | import getDeviceType from '../../../../utils/get-device-type'; |
| 8 | import useDeviceAttributes from '../../../../hooks/useDeviceAttributes'; |
| 9 | import getResponsivePlaceholder from '../../../../utils/get-responsive-placeholder'; |
| 10 | import DimensionsControl from '../../../../components/dimensions'; |
| 11 | |
| 12 | export default function Spacing( { attributes, setAttributes, computedStyles } ) { |
| 13 | const device = getDeviceType(); |
| 14 | const { id, supports: { spacing } } = useContext( ControlsContext ); |
| 15 | const [ deviceAttributes, setDeviceAttributes ] = useDeviceAttributes( attributes, setAttributes ); |
| 16 | |
| 17 | const { |
| 18 | inlineWidth, |
| 19 | inlineWidthTablet, |
| 20 | inlineWidthMobile, |
| 21 | stack, |
| 22 | stackTablet, |
| 23 | stackMobile, |
| 24 | fillHorizontalSpace, |
| 25 | fillHorizontalSpaceTablet, |
| 26 | fillHorizontalSpaceMobile, |
| 27 | } = attributes; |
| 28 | |
| 29 | const paddingAttributes = [ 'paddingTop', 'paddingLeft', 'paddingRight', 'paddingBottom' ]; |
| 30 | const marginAttributes = [ 'marginTop', 'marginLeft', 'marginRight', 'marginBottom' ]; |
| 31 | |
| 32 | return ( |
| 33 | <PanelArea |
| 34 | title={ __( 'Spacing', 'generateblocks' ) } |
| 35 | initialOpen={ false } |
| 36 | icon={ getIcon( 'spacing' ) } |
| 37 | className="gblocks-panel-label" |
| 38 | id={ `${ id }Spacing` } |
| 39 | > |
| 40 | { spacing.padding && |
| 41 | <DimensionsControl |
| 42 | label={ __( 'Padding', 'generateblocks' ) } |
| 43 | attributeNames={ paddingAttributes } |
| 44 | values={ deviceAttributes.spacing } |
| 45 | placeholders={ paddingAttributes.reduce( ( o, key ) => ( |
| 46 | { ...o, [ key ]: getResponsivePlaceholder( key, attributes.spacing, device, '' ) } |
| 47 | ), {} ) } |
| 48 | onChange={ ( values ) => setDeviceAttributes( values, 'spacing' ) } |
| 49 | /> |
| 50 | } |
| 51 | |
| 52 | { spacing.margin && |
| 53 | <DimensionsControl |
| 54 | label={ __( 'Margin', 'generateblocks' ) } |
| 55 | attributeNames={ marginAttributes } |
| 56 | values={ deviceAttributes.spacing } |
| 57 | placeholders={ marginAttributes.reduce( ( o, key ) => ( |
| 58 | { ...o, [ key ]: getResponsivePlaceholder( key, attributes.spacing, device, computedStyles[ key ] ) } |
| 59 | ), {} ) } |
| 60 | onChange={ ( values ) => setDeviceAttributes( values, 'spacing' ) } |
| 61 | /> |
| 62 | } |
| 63 | |
| 64 | { 'Desktop' === device && |
| 65 | <> |
| 66 | <DeviceControls |
| 67 | inlineWidth={ !! inlineWidth } |
| 68 | onChangeInlineWidth={ ( checked ) => setAttributes( { inlineWidth: checked } ) } |
| 69 | stack={ !! stack } |
| 70 | onChangeStack={ ( value ) => { |
| 71 | setAttributes( { |
| 72 | stack: value, |
| 73 | stackTablet: !! value && ! stackTablet ? value : stackTablet, |
| 74 | stackMobile: !! value && ! stackMobile ? value : stackMobile, |
| 75 | } ); |
| 76 | } } |
| 77 | fill={ !! fillHorizontalSpace } |
| 78 | onFillChange={ ( value ) => { |
| 79 | setAttributes( { |
| 80 | fillHorizontalSpace: value, |
| 81 | fillHorizontalSpaceTablet: !! value && ! fillHorizontalSpaceTablet ? value : fillHorizontalSpaceTablet, |
| 82 | fillHorizontalSpaceMobile: !! value && ! fillHorizontalSpaceMobile ? value : fillHorizontalSpaceMobile, |
| 83 | } ); |
| 84 | } } |
| 85 | /> |
| 86 | </> |
| 87 | } |
| 88 | |
| 89 | { 'Tablet' === device && |
| 90 | <> |
| 91 | <DeviceControls |
| 92 | inlineWidth={ !! inlineWidthTablet } |
| 93 | onChangeInlineWidth={ ( checked ) => setAttributes( { inlineWidthTablet: checked } ) } |
| 94 | stack={ !! stackTablet } |
| 95 | onChangeStack={ ( value ) => { |
| 96 | setAttributes( { |
| 97 | stackTablet: value, |
| 98 | stackMobile: !! value && ! stackMobile ? value : stackMobile, |
| 99 | } ); |
| 100 | } } |
| 101 | fill={ !! fillHorizontalSpaceTablet } |
| 102 | onFillChange={ ( value ) => { |
| 103 | setAttributes( { |
| 104 | fillHorizontalSpaceTablet: value, |
| 105 | fillHorizontalSpaceMobile: !! value && ! fillHorizontalSpaceMobile ? value : fillHorizontalSpaceMobile, |
| 106 | } ); |
| 107 | } } |
| 108 | /> |
| 109 | </> |
| 110 | } |
| 111 | |
| 112 | { 'Mobile' === device && |
| 113 | <> |
| 114 | <DeviceControls |
| 115 | inlineWidth={ !! inlineWidthMobile } |
| 116 | onChangeInlineWidth={ ( checked ) => setAttributes( { inlineWidthMobile: checked } ) } |
| 117 | stack={ !! stackMobile } |
| 118 | onChangeStack={ ( value ) => setAttributes( { stackMobile: value } ) } |
| 119 | fill={ !! fillHorizontalSpaceMobile } |
| 120 | onFillChange={ ( value ) => setAttributes( { fillHorizontalSpaceMobile: value } ) } |
| 121 | /> |
| 122 | </> |
| 123 | } |
| 124 | </PanelArea> |
| 125 | ); |
| 126 | } |
| 127 |