desktop.js
4 years ago
main.js
3 years ago
mobile.js
3 years ago
tablet-only.js
4 years ago
tablet.js
3 years ago
main.js
388 lines
| 1 | /* eslint-disable quotes */ |
| 2 | import buildCSS from '../../../utils/build-css'; |
| 3 | import shorthandCSS from '../../../utils/shorthand-css'; |
| 4 | import hexToRGBA from '../../../utils/hex-to-rgba'; |
| 5 | import valueWithUnit from '../../../utils/value-with-unit'; |
| 6 | import getBackgroundImageCSS from '../../../utils/get-background-image'; |
| 7 | import sizingValue from '../../../utils/sizingValue'; |
| 8 | import { |
| 9 | applyFilters, |
| 10 | } from '@wordpress/hooks'; |
| 11 | import SizingCSS from '../../../extend/inspector-control/controls/sizing/components/SizingCSS'; |
| 12 | import LayoutCSS from '../../../extend/inspector-control/controls/layout/components/LayoutCSS'; |
| 13 | import FlexChildCSS from '../../../extend/inspector-control/controls/flex-child-panel/components/FlexChildCSS'; |
| 14 | import isFlexItem from '../../../utils/is-flex-item'; |
| 15 | import SpacingCSS from '../../../extend/inspector-control/controls/spacing/components/SpacingCSS'; |
| 16 | |
| 17 | export default function MainCSS( props ) { |
| 18 | const attributes = applyFilters( 'generateblocks.editor.cssAttrs', props.attributes, props ); |
| 19 | |
| 20 | const { |
| 21 | clientId, |
| 22 | device, |
| 23 | } = props; |
| 24 | |
| 25 | const { |
| 26 | uniqueId, |
| 27 | isGrid, |
| 28 | flexGrow, |
| 29 | flexShrink, |
| 30 | flexBasis, |
| 31 | outerContainer, |
| 32 | innerContainer, |
| 33 | containerWidth, |
| 34 | paddingTop, |
| 35 | paddingRight, |
| 36 | paddingBottom, |
| 37 | paddingLeft, |
| 38 | paddingUnit, |
| 39 | borderSizeTop, |
| 40 | borderSizeRight, |
| 41 | borderSizeBottom, |
| 42 | borderSizeLeft, |
| 43 | borderRadiusTopRight, |
| 44 | borderRadiusBottomRight, |
| 45 | borderRadiusBottomLeft, |
| 46 | borderRadiusTopLeft, |
| 47 | borderRadiusUnit, |
| 48 | borderColor, |
| 49 | borderColorOpacity, |
| 50 | backgroundColor, |
| 51 | backgroundColorOpacity, |
| 52 | gradient, |
| 53 | gradientSelector, |
| 54 | textColor, |
| 55 | linkColor, |
| 56 | linkColorHover, |
| 57 | bgImage, |
| 58 | bgOptions, |
| 59 | verticalAlignment, |
| 60 | zindex, |
| 61 | innerZindex, |
| 62 | alignment, |
| 63 | fontFamily, |
| 64 | fontFamilyFallback, |
| 65 | fontWeight, |
| 66 | fontSize, |
| 67 | fontSizeUnit, |
| 68 | textTransform, |
| 69 | shapeDividers, |
| 70 | gridId, |
| 71 | useDynamicData, |
| 72 | dynamicContentType, |
| 73 | bgImageInline, |
| 74 | useInnerContainer, |
| 75 | sizing, |
| 76 | order, |
| 77 | display, |
| 78 | displayTablet, |
| 79 | displayMobile, |
| 80 | } = attributes; |
| 81 | |
| 82 | let containerWidthPreview = containerWidth; |
| 83 | |
| 84 | if ( ! containerWidthPreview ) { |
| 85 | containerWidthPreview = generateBlocksDefaults.container.containerWidth; |
| 86 | } |
| 87 | |
| 88 | let fontFamilyFallbackValue = ''; |
| 89 | |
| 90 | if ( fontFamily && fontFamilyFallback ) { |
| 91 | fontFamilyFallbackValue = ', ' + fontFamilyFallback; |
| 92 | } |
| 93 | |
| 94 | const hasBgImage = !! bgImage || ( useDynamicData && '' !== dynamicContentType ); |
| 95 | const backgroundImageValue = getBackgroundImageCSS( 'image', props ); |
| 96 | const gradientValue = getBackgroundImageCSS( 'gradient', props ); |
| 97 | |
| 98 | let cssObj = []; |
| 99 | cssObj[ '.editor-styles-wrapper .gb-container-' + uniqueId ] = [ { |
| 100 | 'background-color': hexToRGBA( backgroundColor, backgroundColorOpacity ), |
| 101 | 'color': textColor, // eslint-disable-line quote-props |
| 102 | 'border-radius': shorthandCSS( borderRadiusTopLeft, borderRadiusTopRight, borderRadiusBottomRight, borderRadiusBottomLeft, borderRadiusUnit ), |
| 103 | 'text-align': alignment, |
| 104 | 'font-family': fontFamily + fontFamilyFallbackValue, |
| 105 | 'font-weight': fontWeight, |
| 106 | 'text-transform': textTransform, |
| 107 | 'font-size': valueWithUnit( fontSize, fontSizeUnit ), |
| 108 | 'border-color': hexToRGBA( borderColor, borderColorOpacity ), |
| 109 | } ]; |
| 110 | |
| 111 | SpacingCSS( cssObj, '.editor-styles-wrapper .gb-container-' + uniqueId, attributes ); |
| 112 | SizingCSS( cssObj, '.editor-styles-wrapper .gb-container-' + uniqueId, attributes ); |
| 113 | LayoutCSS( cssObj, '.editor-styles-wrapper .gb-container-' + uniqueId, attributes ); |
| 114 | FlexChildCSS( cssObj, '.editor-styles-wrapper .gb-container-' + uniqueId, attributes ); |
| 115 | |
| 116 | if ( ! useInnerContainer ) { |
| 117 | cssObj[ '.editor-styles-wrapper .gb-container-' + uniqueId ].push( { |
| 118 | padding: shorthandCSS( paddingTop, paddingRight, paddingBottom, paddingLeft, paddingUnit ), |
| 119 | } ); |
| 120 | } |
| 121 | |
| 122 | if ( hasBgImage && 'element' === bgOptions.selector && backgroundImageValue ) { |
| 123 | cssObj[ '.editor-styles-wrapper .gb-container-' + uniqueId ].push( { |
| 124 | 'background-image': ! bgImageInline ? backgroundImageValue : null, |
| 125 | 'background-size': bgOptions.size, |
| 126 | 'background-position': bgOptions.position, |
| 127 | 'background-repeat': bgOptions.repeat, |
| 128 | 'background-attachment': bgOptions.attachment, |
| 129 | } ); |
| 130 | } else if ( gradient && 'element' === gradientSelector ) { |
| 131 | cssObj[ '.editor-styles-wrapper .gb-container-' + uniqueId ].push( { |
| 132 | 'background-image': gradientValue, |
| 133 | } ); |
| 134 | } |
| 135 | |
| 136 | if ( useInnerContainer ) { |
| 137 | if ( |
| 138 | ( hasBgImage && 'pseudo-element' === bgOptions.selector ) || |
| 139 | zindex || |
| 140 | ( gradient && 'pseudo-element' === gradientSelector ) |
| 141 | ) { |
| 142 | cssObj[ '.editor-styles-wrapper .gb-container-' + uniqueId ].push( { |
| 143 | 'position': 'relative', // eslint-disable-line quote-props |
| 144 | } ); |
| 145 | } |
| 146 | |
| 147 | if ( |
| 148 | ( hasBgImage && 'pseudo-element' === bgOptions.selector ) || |
| 149 | ( gradient && 'pseudo-element' === gradientSelector ) |
| 150 | ) { |
| 151 | cssObj[ '.editor-styles-wrapper .gb-container-' + uniqueId ].push( { |
| 152 | 'overflow': 'hidden', // eslint-disable-line quote-props |
| 153 | } ); |
| 154 | |
| 155 | cssObj[ '.gb-container-' + uniqueId + ' .block-list-appender' ] = [ { |
| 156 | 'z-index': 10, |
| 157 | } ]; |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | if ( |
| 162 | ( hasBgImage && 'pseudo-element' === bgOptions.selector ) || |
| 163 | ( gradient && 'pseudo-element' === gradientSelector ) |
| 164 | ) { |
| 165 | cssObj[ '.gb-container-' + uniqueId + ' .block-list-appender' ] = [ { |
| 166 | 'z-index': 10, |
| 167 | } ]; |
| 168 | } |
| 169 | |
| 170 | cssObj[ `.editor-styles-wrapper .gb-container-` + uniqueId + ` h1, |
| 171 | .editor-styles-wrapper .gb-container-` + uniqueId + ` h2, |
| 172 | .editor-styles-wrapper .gb-container-` + uniqueId + ` h3, |
| 173 | .editor-styles-wrapper .gb-container-` + uniqueId + ` h4, |
| 174 | .editor-styles-wrapper .gb-container-` + uniqueId + ` h5, |
| 175 | .editor-styles-wrapper .gb-container-` + uniqueId + ` h6` ] = [ { |
| 176 | 'color': textColor, // eslint-disable-line quote-props |
| 177 | } ]; |
| 178 | |
| 179 | if ( borderSizeTop || borderSizeRight || borderSizeBottom || borderSizeLeft ) { |
| 180 | cssObj[ '.editor-styles-wrapper .gb-container-' + uniqueId ].push( { |
| 181 | 'border-width': shorthandCSS( borderSizeTop, borderSizeRight, borderSizeBottom, borderSizeLeft, 'px' ), |
| 182 | 'border-style': 'solid', |
| 183 | } ); |
| 184 | } |
| 185 | |
| 186 | if ( hasBgImage && 'pseudo-element' === bgOptions.selector ) { |
| 187 | cssObj[ '.gb-container-' + uniqueId + ':before' ] = [ { |
| 188 | 'content': '""', // eslint-disable-line quote-props |
| 189 | 'background-image': backgroundImageValue, |
| 190 | 'background-repeat': bgOptions.repeat, |
| 191 | 'background-position': bgOptions.position, |
| 192 | 'background-size': bgOptions.size, |
| 193 | 'background-attachment': bgOptions.attachment, |
| 194 | 'z-index': '0', |
| 195 | 'position': 'absolute', // eslint-disable-line quote-props |
| 196 | 'top': '0', // eslint-disable-line quote-props |
| 197 | 'right': '0', // eslint-disable-line quote-props |
| 198 | 'bottom': '0', // eslint-disable-line quote-props |
| 199 | 'left': '0', // eslint-disable-line quote-props |
| 200 | 'border-radius': shorthandCSS( borderRadiusTopLeft, borderRadiusTopRight, borderRadiusBottomRight, borderRadiusBottomLeft, borderRadiusUnit ), |
| 201 | 'pointer-events': 'none', |
| 202 | } ]; |
| 203 | |
| 204 | if ( typeof bgOptions.opacity !== 'undefined' && 1 !== bgOptions.opacity ) { |
| 205 | cssObj[ '.gb-container-' + uniqueId + ':before' ].push( { |
| 206 | 'opacity': bgOptions.opacity, // eslint-disable-line quote-props |
| 207 | } ); |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | if ( gradient && 'pseudo-element' === gradientSelector ) { |
| 212 | cssObj[ '.gb-container-' + uniqueId + ':after' ] = [ { |
| 213 | 'content': '""', // eslint-disable-line quote-props |
| 214 | 'background-image': gradientValue, |
| 215 | 'z-index': '0 !important', |
| 216 | 'position': 'absolute', // eslint-disable-line quote-props |
| 217 | 'top': '0', // eslint-disable-line quote-props |
| 218 | 'right': '0', // eslint-disable-line quote-props |
| 219 | 'bottom': '0', // eslint-disable-line quote-props |
| 220 | 'left': '0', // eslint-disable-line quote-props |
| 221 | 'pointer-events': 'none', |
| 222 | } ]; |
| 223 | } |
| 224 | |
| 225 | cssObj[ '.editor-styles-wrapper .gb-container-' + uniqueId + ' a, .editor-styles-wrapper .gb-container-' + uniqueId + ' a:visited' ] = [ { |
| 226 | 'color': linkColor, // eslint-disable-line quote-props |
| 227 | } ]; |
| 228 | |
| 229 | cssObj[ '.editor-styles-wrapper .gb-container-' + uniqueId + ' a:hover' ] = [ { |
| 230 | 'color': linkColorHover, // eslint-disable-line quote-props |
| 231 | } ]; |
| 232 | |
| 233 | if ( useInnerContainer ) { |
| 234 | if ( sizingValue( 'minHeight', sizing ) && ! isGrid ) { |
| 235 | cssObj[ '.editor-styles-wrapper .gb-container-' + uniqueId ].push( { |
| 236 | 'display': 'flex', // eslint-disable-line quote-props |
| 237 | 'flex-direction': 'row', |
| 238 | 'align-items': verticalAlignment, |
| 239 | } ); |
| 240 | } |
| 241 | |
| 242 | cssObj[ '.gb-container-' + uniqueId + ' > .gb-inside-container' ] = [ { |
| 243 | 'padding': shorthandCSS( paddingTop, paddingRight, paddingBottom, paddingLeft, paddingUnit ), // eslint-disable-line quote-props |
| 244 | 'width': sizingValue( 'minHeight', sizing ) && ! isGrid ? '100%' : false, // eslint-disable-line quote-props |
| 245 | } ]; |
| 246 | |
| 247 | if ( innerZindex || 0 === innerZindex ) { |
| 248 | cssObj[ '.gb-container-' + uniqueId + ' > .gb-inside-container' ].push( { |
| 249 | 'z-index': innerZindex, |
| 250 | position: 'relative', |
| 251 | } ); |
| 252 | } |
| 253 | |
| 254 | if ( 'contained' === innerContainer && ! isGrid ) { |
| 255 | cssObj[ '.gb-container-' + uniqueId + ' > .gb-inside-container' ].push( { |
| 256 | 'max-width': valueWithUnit( containerWidthPreview, 'px' ), |
| 257 | 'margin-left': 'auto', |
| 258 | 'margin-right': 'auto', |
| 259 | } ); |
| 260 | } |
| 261 | |
| 262 | // We need use an ID for the contained block width so it overrides other |
| 263 | // .wp-block max-width selectors. |
| 264 | cssObj[ '#block-' + clientId ] = [ { |
| 265 | 'max-width': 'contained' === outerContainer && ! isGrid ? valueWithUnit( containerWidthPreview, 'px' ) : false, |
| 266 | 'margin-left': 'contained' === outerContainer && ! isGrid ? 'auto' : false, |
| 267 | 'margin-right': 'contained' === outerContainer && ! isGrid ? 'auto' : false, |
| 268 | } ]; |
| 269 | } |
| 270 | |
| 271 | if ( isGrid ) { |
| 272 | const gridColumnSelectors = [ |
| 273 | '.gb-post-template-' + gridId + ' > .gb-post-template-wrapper > .block-editor-inner-blocks', |
| 274 | '.gb-grid-wrapper > .block-editor-inner-blocks > .block-editor-block-list__layout > .gb-grid-column-' + uniqueId, |
| 275 | ]; |
| 276 | |
| 277 | cssObj[ gridColumnSelectors.join( ',' ) ] = [ { |
| 278 | width: sizingValue( 'width', sizing ), |
| 279 | 'flex-grow': flexGrow, |
| 280 | 'flex-shrink': flexShrink, |
| 281 | 'flex-basis': flexBasis, |
| 282 | order, |
| 283 | } ]; |
| 284 | |
| 285 | if ( useInnerContainer ) { |
| 286 | cssObj[ '.editor-styles-wrapper .gb-container-' + uniqueId ].push( { |
| 287 | display: 'flex', |
| 288 | 'flex-direction': 'column', |
| 289 | height: '100%', |
| 290 | 'justify-content': verticalAlignment, |
| 291 | } ); |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | if ( shapeDividers.length ) { |
| 296 | if ( useInnerContainer ) { |
| 297 | cssObj[ '.editor-styles-wrapper .gb-container-' + uniqueId ].push( { |
| 298 | position: 'relative', |
| 299 | } ); |
| 300 | } |
| 301 | |
| 302 | cssObj[ '.gb-container-' + uniqueId + ' .block-list-appender' ] = [ { |
| 303 | position: 'relative', |
| 304 | 'z-index': 100, |
| 305 | } ]; |
| 306 | |
| 307 | shapeDividers.forEach( ( location, index ) => { |
| 308 | const shapeTransforms = []; |
| 309 | const shapeNumber = index + 1; |
| 310 | |
| 311 | if ( 'top' === shapeDividers[ index ].location ) { |
| 312 | shapeTransforms.push( 'scaleY(-1)' ); |
| 313 | } |
| 314 | |
| 315 | if ( shapeDividers[ index ].flipHorizontally ) { |
| 316 | shapeTransforms.push( 'scaleX(-1)' ); |
| 317 | |
| 318 | cssObj[ '.gblocks-shape-container > .gblocks-shape-toggle-preview-' + shapeNumber + ' .gblocks-shape-divider-preview' ] = [ { |
| 319 | transform: 'scaleX(-1)', |
| 320 | } ]; |
| 321 | } |
| 322 | |
| 323 | cssObj[ '.gb-container-' + uniqueId + ' > .gb-shapes .gb-shape-' + shapeNumber ] = [ { |
| 324 | color: hexToRGBA( shapeDividers[ index ].color, shapeDividers[ index ].colorOpacity ), |
| 325 | 'z-index': shapeDividers[ index ].zindex, |
| 326 | } ]; |
| 327 | |
| 328 | if ( 'top' === shapeDividers[ index ].location || 'bottom' === shapeDividers[ index ].location ) { |
| 329 | cssObj[ '.gb-container-' + uniqueId + ' > .gb-shapes .gb-shape-' + shapeNumber ].push( { |
| 330 | left: '0', |
| 331 | right: '0', |
| 332 | } ); |
| 333 | } |
| 334 | |
| 335 | if ( 'bottom' === shapeDividers[ index ].location ) { |
| 336 | cssObj[ '.gb-container-' + uniqueId + ' > .gb-shapes .gb-shape-' + shapeNumber ].push( { |
| 337 | bottom: '-1px', |
| 338 | } ); |
| 339 | } |
| 340 | |
| 341 | if ( 'top' === shapeDividers[ index ].location ) { |
| 342 | cssObj[ '.gb-container-' + uniqueId + ' > .gb-shapes .gb-shape-' + shapeNumber ].push( { |
| 343 | top: '-1px', |
| 344 | } ); |
| 345 | } |
| 346 | |
| 347 | if ( shapeTransforms.length ) { |
| 348 | cssObj[ '.gb-container-' + uniqueId + ' > .gb-shapes .gb-shape-' + shapeNumber ].push( { |
| 349 | transform: shapeTransforms.join( ' ' ), |
| 350 | } ); |
| 351 | } |
| 352 | |
| 353 | let shapeWidth = shapeDividers[ index ].width + '%'; |
| 354 | |
| 355 | if ( 100 === shapeDividers[ index ].width ) { |
| 356 | shapeWidth = 'calc(' + shapeWidth + ' + 1.3px)'; |
| 357 | } |
| 358 | |
| 359 | cssObj[ '.gb-container-' + uniqueId + ' > .gb-shapes .gb-shape-' + shapeNumber + ' svg' ] = [ { |
| 360 | height: valueWithUnit( shapeDividers[ index ].height, 'px' ), |
| 361 | width: shapeWidth, |
| 362 | } ]; |
| 363 | |
| 364 | if ( 'top' === shapeDividers[ index ].location || 'bottom' === shapeDividers[ index ].location ) { |
| 365 | cssObj[ '.gb-container-' + uniqueId + ' > .gb-shapes .gb-shape-' + shapeNumber + ' svg' ].push( { |
| 366 | position: 'relative', |
| 367 | left: '50%', |
| 368 | transform: 'translateX(-50%)', |
| 369 | 'min-width': '100%', |
| 370 | } ); |
| 371 | } |
| 372 | } ); |
| 373 | } |
| 374 | |
| 375 | if ( isFlexItem( { device, display, displayTablet, displayMobile } ) ) { |
| 376 | cssObj[ '.gb-container-' + uniqueId + '.block-editor-block-list__block > .block-list-appender' ] = [ { |
| 377 | 'margin-top': 0, |
| 378 | } ]; |
| 379 | } |
| 380 | |
| 381 | cssObj = applyFilters( 'generateblocks.editor.mainCSS', cssObj, props, 'container' ); |
| 382 | |
| 383 | return ( |
| 384 | <style>{ buildCSS( cssObj ) }</style> |
| 385 | ); |
| 386 | } |
| 387 | /* eslint-enable quotes */ |
| 388 |