index.js
231 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 | const { |
| 43 | useGlobalMaxWidth = false, |
| 44 | useInnerContainer = false, |
| 45 | isGrid = false, |
| 46 | sizing, |
| 47 | } = attributes; |
| 48 | |
| 49 | function getValue( name ) { |
| 50 | return sizing && sizing[ getAttribute( name, { attributes, deviceType: device }, true ) ] |
| 51 | ? sizing[ getAttribute( name, { attributes, deviceType: device }, true ) ] |
| 52 | : ''; |
| 53 | } |
| 54 | |
| 55 | function getLabel( defaultLabel, rules ) { |
| 56 | return applyFilters( |
| 57 | 'generateblocks.editor.control.label', |
| 58 | defaultLabel, |
| 59 | rules, |
| 60 | styleSources, |
| 61 | dispatchControlGlobalStyle, |
| 62 | contentWasUpdated, |
| 63 | ); |
| 64 | } |
| 65 | |
| 66 | const labels = { |
| 67 | width: getLabel( |
| 68 | __( 'Width', 'generateblocks' ), |
| 69 | { |
| 70 | width: getValue( 'width' ), |
| 71 | }, |
| 72 | ), |
| 73 | height: getLabel( |
| 74 | __( 'Height', 'generateblocks' ), |
| 75 | { |
| 76 | height: getValue( 'height' ), |
| 77 | }, |
| 78 | ), |
| 79 | minWidth: getLabel( |
| 80 | __( 'Min Width', 'generateblocks' ), |
| 81 | { |
| 82 | minWidth: getValue( 'minWidth' ), |
| 83 | }, |
| 84 | ), |
| 85 | minHeight: getLabel( |
| 86 | __( 'Min Height', 'generateblocks' ), |
| 87 | { |
| 88 | minHeight: getValue( 'minHeight' ), |
| 89 | }, |
| 90 | ), |
| 91 | maxWidth: getLabel( |
| 92 | __( 'Max Width', 'generateblocks' ), |
| 93 | { |
| 94 | maxWidth: getValue( 'maxWidth' ), |
| 95 | }, |
| 96 | ), |
| 97 | maxHeight: getLabel( |
| 98 | __( 'Max Height', 'generateblocks' ), |
| 99 | { |
| 100 | maxHeight: getValue( 'maxHeight' ), |
| 101 | }, |
| 102 | ), |
| 103 | }; |
| 104 | |
| 105 | return ( |
| 106 | <PanelArea |
| 107 | title={ __( 'Sizing', 'generateblocks' ) } |
| 108 | initialOpen={ false } |
| 109 | icon={ getIcon( 'sizing' ) } |
| 110 | className="gblocks-panel-label" |
| 111 | id={ `${ id }Sizing` } |
| 112 | ref={ panelRef } |
| 113 | hasGlobalStyle={ hasGlobalStyle } |
| 114 | > |
| 115 | <div className="gblocks-sizing-fields"> |
| 116 | { sizingPanel.width && |
| 117 | <Width |
| 118 | label={ labels.width } |
| 119 | value={ getValue( 'width' ) } |
| 120 | placeholder={ getResponsivePlaceholder( 'width', attributes.sizing, device ) } |
| 121 | onChange={ ( value ) => { |
| 122 | setAttributes( { |
| 123 | sizing: { |
| 124 | [ getAttribute( 'width', { attributes, deviceType: device }, true ) ]: value, |
| 125 | }, |
| 126 | } ); |
| 127 | } } |
| 128 | /> |
| 129 | } |
| 130 | |
| 131 | { sizingPanel.height && |
| 132 | <Height |
| 133 | label={ labels.height } |
| 134 | value={ getValue( 'height' ) } |
| 135 | placeholder={ getResponsivePlaceholder( 'height', attributes.sizing, device ) } |
| 136 | onChange={ ( value ) => { |
| 137 | setAttributes( { |
| 138 | sizing: { |
| 139 | [ getAttribute( 'height', { attributes, deviceType: device }, true ) ]: value, |
| 140 | }, |
| 141 | } ); |
| 142 | } } |
| 143 | /> |
| 144 | } |
| 145 | |
| 146 | { sizingPanel.minWidth && |
| 147 | <MinWidth |
| 148 | label={ labels.minWidth } |
| 149 | value={ getValue( 'minWidth' ) } |
| 150 | placeholder={ getResponsivePlaceholder( 'minWidth', attributes.sizing, device ) } |
| 151 | disabled={ isGrid } |
| 152 | onChange={ ( value ) => { |
| 153 | setAttributes( { |
| 154 | sizing: { |
| 155 | [ getAttribute( 'minWidth', { attributes, deviceType: device }, true ) ]: value, |
| 156 | }, |
| 157 | } ); |
| 158 | } } |
| 159 | /> |
| 160 | } |
| 161 | |
| 162 | { sizingPanel.minHeight && |
| 163 | <MinHeight |
| 164 | label={ labels.minHeight } |
| 165 | value={ getValue( 'minHeight' ) } |
| 166 | placeholder={ getResponsivePlaceholder( 'minHeight', attributes.sizing, device ) } |
| 167 | onChange={ ( value ) => { |
| 168 | setAttributes( { |
| 169 | sizing: { |
| 170 | [ getAttribute( 'minHeight', { attributes, deviceType: device }, true ) ]: value, |
| 171 | }, |
| 172 | } ); |
| 173 | } } |
| 174 | /> |
| 175 | } |
| 176 | |
| 177 | { sizingPanel.maxWidth && |
| 178 | <MaxWidth |
| 179 | label={ labels.maxWidth } |
| 180 | value={ getValue( 'maxWidth' ) } |
| 181 | placeholder={ getResponsivePlaceholder( 'maxWidth', attributes.sizing, device ) } |
| 182 | overrideValue={ !! useGlobalMaxWidth ? generateBlocksInfo.globalContainerWidth : null } |
| 183 | disabled={ useInnerContainer || isGrid || ( useGlobalMaxWidth && 'Desktop' === device ) } |
| 184 | onChange={ ( value ) => { |
| 185 | setAttributes( { |
| 186 | sizing: { |
| 187 | [ getAttribute( 'maxWidth', { attributes, deviceType: device }, true ) ]: value, |
| 188 | }, |
| 189 | } ); |
| 190 | } } |
| 191 | overrideAction={ () => { |
| 192 | if ( ! sizingPanel.useGlobalMaxWidth || useInnerContainer || isGrid || 'Desktop' !== device || getValue( 'maxWidth' ) ) { |
| 193 | return null; |
| 194 | } |
| 195 | |
| 196 | return ( |
| 197 | <Tooltip text={ __( 'Use global max-width', 'generateblocks' ) }> |
| 198 | <Button |
| 199 | icon={ getIcon( 'globe' ) } |
| 200 | variant={ !! useGlobalMaxWidth ? 'primary' : '' } |
| 201 | onClick={ () => { |
| 202 | setAttributes( { |
| 203 | useGlobalMaxWidth: useGlobalMaxWidth ? false : true, |
| 204 | } ); |
| 205 | } } |
| 206 | /> |
| 207 | </Tooltip> |
| 208 | ); |
| 209 | } } |
| 210 | /> |
| 211 | } |
| 212 | |
| 213 | { sizingPanel.maxHeight && |
| 214 | <MaxHeight |
| 215 | label={ labels.maxHeight } |
| 216 | value={ getValue( 'maxHeight' ) } |
| 217 | placeholder={ getResponsivePlaceholder( 'maxHeight', attributes.sizing, device ) } |
| 218 | onChange={ ( value ) => { |
| 219 | setAttributes( { |
| 220 | sizing: { |
| 221 | [ getAttribute( 'maxHeight', { attributes, deviceType: device }, true ) ]: value, |
| 222 | }, |
| 223 | } ); |
| 224 | } } |
| 225 | /> |
| 226 | } |
| 227 | </div> |
| 228 | </PanelArea> |
| 229 | ); |
| 230 | } |
| 231 |