index.js
222 lines
| 1 | import { __ } from '@wordpress/i18n'; |
| 2 | import { useContext, useRef, useMemo } from '@wordpress/element'; |
| 3 | import { applyFilters } from '@wordpress/hooks'; |
| 4 | |
| 5 | import PanelArea from '../../../../components/panel-area'; |
| 6 | import getIcon from '../../../../utils/get-icon'; |
| 7 | import ControlsContext from '../../../../block-context'; |
| 8 | import DeviceControls from './components/device-controls'; |
| 9 | import getDeviceType from '../../../../utils/get-device-type'; |
| 10 | import useDeviceAttributes from '../../../../hooks/useDeviceAttributes'; |
| 11 | import getResponsivePlaceholder from '../../../../utils/get-responsive-placeholder'; |
| 12 | import DimensionsControl from '../../../../components/dimensions'; |
| 13 | import { useStyleIndicator } from '../../../../hooks'; |
| 14 | import { getContentAttribute } from '../../../../utils/get-content-attribute'; |
| 15 | |
| 16 | export default function Spacing( { attributes, setAttributes, computedStyles } ) { |
| 17 | const device = getDeviceType(); |
| 18 | const { id, blockName, supports: { spacing } } = useContext( ControlsContext ); |
| 19 | const contentValue = getContentAttribute( attributes, blockName ); |
| 20 | const [ deviceAttributes, setDeviceAttributes ] = useDeviceAttributes( attributes, setAttributes ); |
| 21 | const panelRef = useRef( null ); |
| 22 | const panelControls = { |
| 23 | marginTop: false, |
| 24 | marginBottom: false, |
| 25 | marginRight: false, |
| 26 | marginLeft: false, |
| 27 | paddingTop: false, |
| 28 | paddingBottom: false, |
| 29 | paddingRight: false, |
| 30 | paddingLeft: false, |
| 31 | }; |
| 32 | const { |
| 33 | dispatchControlGlobalStyle, |
| 34 | styleSources, |
| 35 | hasGlobalStyle, |
| 36 | contentWasUpdated, |
| 37 | } = useStyleIndicator( computedStyles, panelControls, contentValue, deviceAttributes ); |
| 38 | |
| 39 | const { |
| 40 | inlineWidth, |
| 41 | inlineWidthTablet, |
| 42 | inlineWidthMobile, |
| 43 | stack, |
| 44 | stackTablet, |
| 45 | stackMobile, |
| 46 | fillHorizontalSpace, |
| 47 | fillHorizontalSpaceTablet, |
| 48 | fillHorizontalSpaceMobile, |
| 49 | } = attributes; |
| 50 | |
| 51 | const paddingAttributes = [ 'paddingTop', 'paddingLeft', 'paddingRight', 'paddingBottom' ]; |
| 52 | const marginAttributes = [ 'marginTop', 'marginLeft', 'marginRight', 'marginBottom' ]; |
| 53 | |
| 54 | function getLabel( defaultLabel, rules ) { |
| 55 | return applyFilters( |
| 56 | 'generateblocks.editor.control.label', |
| 57 | defaultLabel, |
| 58 | rules, |
| 59 | styleSources, |
| 60 | dispatchControlGlobalStyle, |
| 61 | contentWasUpdated, |
| 62 | ); |
| 63 | } |
| 64 | |
| 65 | const { |
| 66 | marginTop, |
| 67 | marginBottom, |
| 68 | marginLeft, |
| 69 | marginRight, |
| 70 | paddingTop, |
| 71 | paddingBottom, |
| 72 | paddingLeft, |
| 73 | paddingRight, |
| 74 | } = deviceAttributes.spacing; |
| 75 | |
| 76 | const paddingRules = useMemo( () => { |
| 77 | const rules = { |
| 78 | paddingTop, |
| 79 | paddingRight, |
| 80 | paddingBottom, |
| 81 | paddingLeft, |
| 82 | }; |
| 83 | |
| 84 | const allValuesSet = Object.values( rules ).every( ( value ) => { |
| 85 | return '' !== value && undefined !== value; |
| 86 | } ); |
| 87 | |
| 88 | return allValuesSet ? { |
| 89 | padding: Object.values( rules ).join( ' ' ), |
| 90 | } : rules; |
| 91 | }, [ deviceAttributes.spacing ] ); |
| 92 | |
| 93 | const marginRules = useMemo( () => { |
| 94 | const rules = { |
| 95 | marginTop, |
| 96 | marginRight, |
| 97 | marginBottom, |
| 98 | marginLeft, |
| 99 | }; |
| 100 | |
| 101 | const allValuesSet = Object.values( rules ).every( ( value ) => { |
| 102 | return '' !== value && undefined !== value; |
| 103 | } ); |
| 104 | |
| 105 | return allValuesSet ? { |
| 106 | margin: Object.values( rules ).join( ' ' ), |
| 107 | } : rules; |
| 108 | }, [ deviceAttributes.spacing ] ); |
| 109 | |
| 110 | const labels = { |
| 111 | padding: getLabel( |
| 112 | __( 'Padding', 'generateblocks' ), |
| 113 | paddingRules, |
| 114 | ), |
| 115 | margin: getLabel( |
| 116 | __( 'Margin', 'generateblocks' ), |
| 117 | marginRules, |
| 118 | ), |
| 119 | }; |
| 120 | |
| 121 | return ( |
| 122 | <PanelArea |
| 123 | title={ __( 'Spacing', 'generateblocks' ) } |
| 124 | initialOpen={ false } |
| 125 | icon={ getIcon( 'spacing' ) } |
| 126 | className="gblocks-panel-label" |
| 127 | id={ `${ id }Spacing` } |
| 128 | ref={ panelRef } |
| 129 | hasGlobalStyle={ hasGlobalStyle } |
| 130 | > |
| 131 | { spacing.padding && |
| 132 | <DimensionsControl |
| 133 | label={ labels.padding } |
| 134 | attributeNames={ paddingAttributes } |
| 135 | values={ deviceAttributes.spacing } |
| 136 | placeholders={ paddingAttributes.reduce( ( o, key ) => ( |
| 137 | { ...o, [ key ]: getResponsivePlaceholder( key, attributes.spacing, device, '' ) } |
| 138 | ), {} ) } |
| 139 | onChange={ ( values ) => setDeviceAttributes( values, 'spacing' ) } |
| 140 | /> |
| 141 | } |
| 142 | { spacing.margin && |
| 143 | <DimensionsControl |
| 144 | label={ __( 'Margin', 'generateblocks' ) } |
| 145 | attributeNames={ marginAttributes } |
| 146 | values={ deviceAttributes.spacing } |
| 147 | placeholders={ marginAttributes.reduce( ( o, key ) => ( |
| 148 | { ...o, [ key ]: getResponsivePlaceholder( |
| 149 | key, |
| 150 | attributes.spacing, |
| 151 | device, |
| 152 | parseInt( computedStyles[ key ] ) || '' |
| 153 | ) } |
| 154 | ), {} ) } |
| 155 | onChange={ ( values ) => setDeviceAttributes( values, 'spacing' ) } |
| 156 | /> |
| 157 | } |
| 158 | |
| 159 | { 'Desktop' === device && |
| 160 | <> |
| 161 | <DeviceControls |
| 162 | inlineWidth={ !! inlineWidth } |
| 163 | onChangeInlineWidth={ ( checked ) => setAttributes( { inlineWidth: checked } ) } |
| 164 | stack={ !! stack } |
| 165 | onChangeStack={ ( value ) => { |
| 166 | setAttributes( { |
| 167 | stack: value, |
| 168 | stackTablet: !! value && ! stackTablet ? value : stackTablet, |
| 169 | stackMobile: !! value && ! stackMobile ? value : stackMobile, |
| 170 | } ); |
| 171 | } } |
| 172 | fill={ !! fillHorizontalSpace } |
| 173 | onFillChange={ ( value ) => { |
| 174 | setAttributes( { |
| 175 | fillHorizontalSpace: value, |
| 176 | fillHorizontalSpaceTablet: !! value && ! fillHorizontalSpaceTablet ? value : fillHorizontalSpaceTablet, |
| 177 | fillHorizontalSpaceMobile: !! value && ! fillHorizontalSpaceMobile ? value : fillHorizontalSpaceMobile, |
| 178 | } ); |
| 179 | } } |
| 180 | /> |
| 181 | </> |
| 182 | } |
| 183 | |
| 184 | { 'Tablet' === device && |
| 185 | <> |
| 186 | <DeviceControls |
| 187 | inlineWidth={ !! inlineWidthTablet } |
| 188 | onChangeInlineWidth={ ( checked ) => setAttributes( { inlineWidthTablet: checked } ) } |
| 189 | stack={ !! stackTablet } |
| 190 | onChangeStack={ ( value ) => { |
| 191 | setAttributes( { |
| 192 | stackTablet: value, |
| 193 | stackMobile: !! value && ! stackMobile ? value : stackMobile, |
| 194 | } ); |
| 195 | } } |
| 196 | fill={ !! fillHorizontalSpaceTablet } |
| 197 | onFillChange={ ( value ) => { |
| 198 | setAttributes( { |
| 199 | fillHorizontalSpaceTablet: value, |
| 200 | fillHorizontalSpaceMobile: !! value && ! fillHorizontalSpaceMobile ? value : fillHorizontalSpaceMobile, |
| 201 | } ); |
| 202 | } } |
| 203 | /> |
| 204 | </> |
| 205 | } |
| 206 | |
| 207 | { 'Mobile' === device && |
| 208 | <> |
| 209 | <DeviceControls |
| 210 | inlineWidth={ !! inlineWidthMobile } |
| 211 | onChangeInlineWidth={ ( checked ) => setAttributes( { inlineWidthMobile: checked } ) } |
| 212 | stack={ !! stackMobile } |
| 213 | onChangeStack={ ( value ) => setAttributes( { stackMobile: value } ) } |
| 214 | fill={ !! fillHorizontalSpaceMobile } |
| 215 | onFillChange={ ( value ) => setAttributes( { fillHorizontalSpaceMobile: value } ) } |
| 216 | /> |
| 217 | </> |
| 218 | } |
| 219 | </PanelArea> |
| 220 | ); |
| 221 | } |
| 222 |