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