index.js
232 lines
| 1 | import { __ } from '@wordpress/i18n'; |
| 2 | import { useContext, useRef } from '@wordpress/element'; |
| 3 | import { Tooltip, Button } from '@wordpress/components'; |
| 4 | import { applyFilters } from '@wordpress/hooks'; |
| 5 | |
| 6 | import PanelArea from '../../../../components/panel-area'; |
| 7 | import getIcon from '../../../../utils/get-icon'; |
| 8 | import ControlsContext from '../../../../block-context'; |
| 9 | import MinHeight from './components/MinHeight'; |
| 10 | import getAttribute from '../../../../utils/get-attribute'; |
| 11 | import Width from './components/Width'; |
| 12 | import MaxWidth from './components/MaxWidth'; |
| 13 | import Height from './components/Height'; |
| 14 | import MinWidth from './components/MinWidth'; |
| 15 | import MaxHeight from './components/MaxHeight'; |
| 16 | import './editor.scss'; |
| 17 | import getDeviceType from '../../../../utils/get-device-type'; |
| 18 | import getResponsivePlaceholder from '../../../../utils/get-responsive-placeholder'; |
| 19 | import { useStyleIndicator, useDeviceAttributes } from '../../../../hooks'; |
| 20 | import { getContentAttribute } from '../../../../utils/get-content-attribute'; |
| 21 | |
| 22 | export default function Sizing( { attributes, setAttributes, computedStyles } ) { |
| 23 | const { id, blockName, supports: { sizingPanel } } = useContext( ControlsContext ); |
| 24 | const device = getDeviceType(); |
| 25 | const panelRef = useRef( null ); |
| 26 | const contentValue = getContentAttribute( attributes, blockName ); |
| 27 | const [ deviceAttributes ] = useDeviceAttributes( attributes, setAttributes ); |
| 28 | const panelControls = { |
| 29 | width: false, |
| 30 | height: false, |
| 31 | minWidth: false, |
| 32 | minHeight: false, |
| 33 | maxWidth: false, |
| 34 | maxHeight: false, |
| 35 | }; |
| 36 | const { |
| 37 | dispatchControlGlobalStyle, |
| 38 | styleSources, |
| 39 | hasGlobalStyle, |
| 40 | contentWasUpdated, |
| 41 | } = useStyleIndicator( computedStyles, panelControls, contentValue, deviceAttributes ); |
| 42 | |
| 43 | const { |
| 44 | useGlobalMaxWidth = false, |
| 45 | useInnerContainer = false, |
| 46 | isGrid = false, |
| 47 | sizing, |
| 48 | } = attributes; |
| 49 | |
| 50 | function getValue( name ) { |
| 51 | return sizing && sizing[ getAttribute( name, { attributes, deviceType: device }, true ) ] |
| 52 | ? sizing[ getAttribute( name, { attributes, deviceType: device }, true ) ] |
| 53 | : ''; |
| 54 | } |
| 55 | |
| 56 | function getLabel( defaultLabel, rules ) { |
| 57 | return applyFilters( |
| 58 | 'generateblocks.editor.control.label', |
| 59 | defaultLabel, |
| 60 | rules, |
| 61 | styleSources, |
| 62 | dispatchControlGlobalStyle, |
| 63 | contentWasUpdated, |
| 64 | ); |
| 65 | } |
| 66 | |
| 67 | const labels = { |
| 68 | width: getLabel( |
| 69 | __( 'Width', 'generateblocks' ), |
| 70 | { |
| 71 | width: getValue( 'width' ), |
| 72 | }, |
| 73 | ), |
| 74 | height: getLabel( |
| 75 | __( 'Height', 'generateblocks' ), |
| 76 | { |
| 77 | height: getValue( 'height' ), |
| 78 | }, |
| 79 | ), |
| 80 | minWidth: getLabel( |
| 81 | __( 'Min Width', 'generateblocks' ), |
| 82 | { |
| 83 | minWidth: getValue( 'minWidth' ), |
| 84 | }, |
| 85 | ), |
| 86 | minHeight: getLabel( |
| 87 | __( 'Min Height', 'generateblocks' ), |
| 88 | { |
| 89 | minHeight: getValue( 'minHeight' ), |
| 90 | }, |
| 91 | ), |
| 92 | maxWidth: getLabel( |
| 93 | __( 'Max Width', 'generateblocks' ), |
| 94 | { |
| 95 | maxWidth: getValue( 'maxWidth' ), |
| 96 | }, |
| 97 | ), |
| 98 | maxHeight: getLabel( |
| 99 | __( 'Max Height', 'generateblocks' ), |
| 100 | { |
| 101 | maxHeight: getValue( 'maxHeight' ), |
| 102 | }, |
| 103 | ), |
| 104 | }; |
| 105 | |
| 106 | return ( |
| 107 | <PanelArea |
| 108 | title={ __( 'Sizing', 'generateblocks' ) } |
| 109 | initialOpen={ false } |
| 110 | icon={ getIcon( 'sizing' ) } |
| 111 | className="gblocks-panel-label" |
| 112 | id={ `${ id }Sizing` } |
| 113 | ref={ panelRef } |
| 114 | hasGlobalStyle={ hasGlobalStyle } |
| 115 | > |
| 116 | <div className="gblocks-sizing-fields"> |
| 117 | { sizingPanel.width && |
| 118 | <Width |
| 119 | label={ labels.width } |
| 120 | value={ getValue( 'width' ) } |
| 121 | placeholder={ getResponsivePlaceholder( 'width', attributes.sizing, device ) } |
| 122 | onChange={ ( value ) => { |
| 123 | setAttributes( { |
| 124 | sizing: { |
| 125 | [ getAttribute( 'width', { attributes, deviceType: device }, true ) ]: value, |
| 126 | }, |
| 127 | } ); |
| 128 | } } |
| 129 | /> |
| 130 | } |
| 131 | |
| 132 | { sizingPanel.height && |
| 133 | <Height |
| 134 | label={ labels.height } |
| 135 | value={ getValue( 'height' ) } |
| 136 | placeholder={ getResponsivePlaceholder( 'height', attributes.sizing, device ) } |
| 137 | onChange={ ( value ) => { |
| 138 | setAttributes( { |
| 139 | sizing: { |
| 140 | [ getAttribute( 'height', { attributes, deviceType: device }, true ) ]: value, |
| 141 | }, |
| 142 | } ); |
| 143 | } } |
| 144 | /> |
| 145 | } |
| 146 | |
| 147 | { sizingPanel.minWidth && |
| 148 | <MinWidth |
| 149 | label={ labels.minWidth } |
| 150 | value={ getValue( 'minWidth' ) } |
| 151 | placeholder={ getResponsivePlaceholder( 'minWidth', attributes.sizing, device ) } |
| 152 | disabled={ isGrid } |
| 153 | onChange={ ( value ) => { |
| 154 | setAttributes( { |
| 155 | sizing: { |
| 156 | [ getAttribute( 'minWidth', { attributes, deviceType: device }, true ) ]: value, |
| 157 | }, |
| 158 | } ); |
| 159 | } } |
| 160 | /> |
| 161 | } |
| 162 | |
| 163 | { sizingPanel.minHeight && |
| 164 | <MinHeight |
| 165 | label={ labels.minHeight } |
| 166 | value={ getValue( 'minHeight' ) } |
| 167 | placeholder={ getResponsivePlaceholder( 'minHeight', attributes.sizing, device ) } |
| 168 | onChange={ ( value ) => { |
| 169 | setAttributes( { |
| 170 | sizing: { |
| 171 | [ getAttribute( 'minHeight', { attributes, deviceType: device }, true ) ]: value, |
| 172 | }, |
| 173 | } ); |
| 174 | } } |
| 175 | /> |
| 176 | } |
| 177 | |
| 178 | { sizingPanel.maxWidth && |
| 179 | <MaxWidth |
| 180 | label={ labels.maxWidth } |
| 181 | value={ getValue( 'maxWidth' ) } |
| 182 | placeholder={ getResponsivePlaceholder( 'maxWidth', attributes.sizing, device ) } |
| 183 | overrideValue={ !! useGlobalMaxWidth ? generateBlocksInfo.globalContainerWidth : null } |
| 184 | disabled={ useInnerContainer || isGrid || ( useGlobalMaxWidth && 'Desktop' === device ) } |
| 185 | onChange={ ( value ) => { |
| 186 | setAttributes( { |
| 187 | sizing: { |
| 188 | [ getAttribute( 'maxWidth', { attributes, deviceType: device }, true ) ]: value, |
| 189 | }, |
| 190 | } ); |
| 191 | } } |
| 192 | overrideAction={ () => { |
| 193 | if ( ! sizingPanel.useGlobalMaxWidth || useInnerContainer || isGrid || 'Desktop' !== device || getValue( 'maxWidth' ) ) { |
| 194 | return null; |
| 195 | } |
| 196 | |
| 197 | return ( |
| 198 | <Tooltip text={ __( 'Use global max-width', 'generateblocks' ) }> |
| 199 | <Button |
| 200 | icon={ getIcon( 'globe' ) } |
| 201 | variant={ !! useGlobalMaxWidth ? 'primary' : '' } |
| 202 | onClick={ () => { |
| 203 | setAttributes( { |
| 204 | useGlobalMaxWidth: useGlobalMaxWidth ? false : true, |
| 205 | } ); |
| 206 | } } |
| 207 | /> |
| 208 | </Tooltip> |
| 209 | ); |
| 210 | } } |
| 211 | /> |
| 212 | } |
| 213 | |
| 214 | { sizingPanel.maxHeight && |
| 215 | <MaxHeight |
| 216 | label={ labels.maxHeight } |
| 217 | value={ getValue( 'maxHeight' ) } |
| 218 | placeholder={ getResponsivePlaceholder( 'maxHeight', attributes.sizing, device ) } |
| 219 | onChange={ ( value ) => { |
| 220 | setAttributes( { |
| 221 | sizing: { |
| 222 | [ getAttribute( 'maxHeight', { attributes, deviceType: device }, true ) ]: value, |
| 223 | }, |
| 224 | } ); |
| 225 | } } |
| 226 | /> |
| 227 | } |
| 228 | </div> |
| 229 | </PanelArea> |
| 230 | ); |
| 231 | } |
| 232 |