desktop.js
242 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 | const { Component } = wp.element; |
| 9 | const { applyFilters } = wp.hooks; |
| 10 | |
| 11 | export default class DesktopCSS extends Component { |
| 12 | render() { |
| 13 | const { |
| 14 | attributes, |
| 15 | clientId, |
| 16 | } = this.props; |
| 17 | |
| 18 | const { |
| 19 | uniqueId, |
| 20 | isGrid, |
| 21 | width, |
| 22 | outerContainer, |
| 23 | innerContainer, |
| 24 | containerWidth, |
| 25 | minHeight, |
| 26 | minHeightUnit, |
| 27 | paddingTop, |
| 28 | paddingRight, |
| 29 | paddingBottom, |
| 30 | paddingLeft, |
| 31 | paddingUnit, |
| 32 | marginTop, |
| 33 | marginRight, |
| 34 | marginBottom, |
| 35 | marginLeft, |
| 36 | marginUnit, |
| 37 | borderSizeTop, |
| 38 | borderSizeRight, |
| 39 | borderSizeBottom, |
| 40 | borderSizeLeft, |
| 41 | borderRadiusTopRight, |
| 42 | borderRadiusBottomRight, |
| 43 | borderRadiusBottomLeft, |
| 44 | borderRadiusTopLeft, |
| 45 | borderRadiusUnit, |
| 46 | borderColor, |
| 47 | borderColorOpacity, |
| 48 | backgroundColor, |
| 49 | backgroundColorOpacity, |
| 50 | gradient, |
| 51 | textColor, |
| 52 | linkColor, |
| 53 | linkColorHover, |
| 54 | bgImage, |
| 55 | bgOptions, |
| 56 | verticalAlignment, |
| 57 | zindex, |
| 58 | removeVerticalGap, |
| 59 | alignment, |
| 60 | fontFamily, |
| 61 | fontFamilyFallback, |
| 62 | fontWeight, |
| 63 | fontSize, |
| 64 | fontSizeUnit, |
| 65 | textTransform, |
| 66 | } = attributes; |
| 67 | |
| 68 | const backgroundImageValue = getBackgroundImageCSS( attributes ); |
| 69 | |
| 70 | let containerWidthPreview = containerWidth; |
| 71 | |
| 72 | if ( ! containerWidthPreview ) { |
| 73 | containerWidthPreview = generateBlocksDefaults.container.containerWidth; |
| 74 | } |
| 75 | |
| 76 | let fontFamilyFallbackValue = ''; |
| 77 | |
| 78 | if ( fontFamily && fontFamilyFallback ) { |
| 79 | fontFamilyFallbackValue = ', ' + fontFamilyFallback; |
| 80 | } |
| 81 | |
| 82 | let cssObj = []; |
| 83 | cssObj[ '.gb-container-' + uniqueId ] = [ { |
| 84 | 'background-color': hexToRGBA( backgroundColor, backgroundColorOpacity ), |
| 85 | 'color': textColor, // eslint-disable-line quote-props |
| 86 | 'border-radius': shorthandCSS( borderRadiusTopLeft, borderRadiusTopRight, borderRadiusBottomRight, borderRadiusBottomLeft, borderRadiusUnit ), |
| 87 | 'margin': shorthandCSS( marginTop, marginRight, marginBottom, marginLeft, marginUnit ), // eslint-disable-line quote-props |
| 88 | 'z-index': zindex, |
| 89 | 'text-align': alignment, |
| 90 | 'font-family': fontFamily + fontFamilyFallbackValue, |
| 91 | 'font-weight': fontWeight, |
| 92 | 'text-transform': textTransform, |
| 93 | 'font-size': valueWithUnit( fontSize, fontSizeUnit ), |
| 94 | 'min-height': valueWithUnit( minHeight, minHeightUnit ), |
| 95 | } ]; |
| 96 | |
| 97 | if ( bgImage && 'element' === bgOptions.selector && backgroundImageValue ) { |
| 98 | cssObj[ '.gb-container-' + uniqueId ].push( { |
| 99 | 'background-image': backgroundImageValue, |
| 100 | 'background-size': bgOptions.size, |
| 101 | 'background-position': bgOptions.position, |
| 102 | 'background-repeat': bgOptions.repeat, |
| 103 | 'background-attachment': bgOptions.attachment, |
| 104 | } ); |
| 105 | } else if ( gradient && backgroundImageValue ) { |
| 106 | cssObj[ '.gb-container-' + uniqueId ].push( { |
| 107 | 'background-image': backgroundImageValue, |
| 108 | } ); |
| 109 | } |
| 110 | |
| 111 | if ( ( bgImage && 'pseudo-element' === bgOptions.selector ) || zindex ) { |
| 112 | cssObj[ '.gb-container-' + uniqueId ].push( { |
| 113 | 'position': 'relative', // eslint-disable-line quote-props |
| 114 | } ); |
| 115 | } |
| 116 | |
| 117 | if ( bgImage && 'pseudo-element' === bgOptions.selector ) { |
| 118 | cssObj[ '.gb-container-' + uniqueId ].push( { |
| 119 | 'overflow': 'hidden', // eslint-disable-line quote-props |
| 120 | } ); |
| 121 | } |
| 122 | |
| 123 | cssObj[ `.editor-styles-wrapper .gb-container-` + uniqueId + ` h1, |
| 124 | .editor-styles-wrapper .gb-container-` + uniqueId + ` h2, |
| 125 | .editor-styles-wrapper .gb-container-` + uniqueId + ` h3, |
| 126 | .editor-styles-wrapper .gb-container-` + uniqueId + ` h4, |
| 127 | .editor-styles-wrapper .gb-container-` + uniqueId + ` h5, |
| 128 | .editor-styles-wrapper .gb-container-` + uniqueId + ` h6` ] = [ { |
| 129 | 'color': textColor, // eslint-disable-line quote-props |
| 130 | } ]; |
| 131 | |
| 132 | if ( borderSizeTop || borderSizeRight || borderSizeBottom || borderSizeLeft ) { |
| 133 | cssObj[ '.gb-container-' + uniqueId ].push( { |
| 134 | 'border-width': shorthandCSS( borderSizeTop, borderSizeRight, borderSizeBottom, borderSizeLeft, 'px' ), |
| 135 | 'border-style': 'solid', |
| 136 | 'border-color': hexToRGBA( borderColor, borderColorOpacity ), |
| 137 | } ); |
| 138 | } |
| 139 | |
| 140 | if ( minHeight && ! isGrid ) { |
| 141 | cssObj[ '.gb-container-' + uniqueId ].push( { |
| 142 | 'display': 'flex', // eslint-disable-line quote-props |
| 143 | 'flex-direction': 'row', |
| 144 | 'align-items': verticalAlignment, |
| 145 | } ); |
| 146 | } |
| 147 | |
| 148 | if ( bgImage && 'pseudo-element' === bgOptions.selector ) { |
| 149 | cssObj[ '.gb-container-' + uniqueId + ':before' ] = [ { |
| 150 | 'content': '""', // eslint-disable-line quote-props |
| 151 | 'background-image': 'url(' + bgImage.image.url + ')', |
| 152 | 'background-repeat': bgOptions.repeat, |
| 153 | 'background-position': bgOptions.position, |
| 154 | 'background-size': bgOptions.size, |
| 155 | 'background-attachment': bgOptions.attachment, |
| 156 | 'z-index': '0', |
| 157 | 'position': 'absolute', // eslint-disable-line quote-props |
| 158 | 'top': '0', // eslint-disable-line quote-props |
| 159 | 'right': '0', // eslint-disable-line quote-props |
| 160 | 'bottom': '0', // eslint-disable-line quote-props |
| 161 | 'left': '0', // eslint-disable-line quote-props |
| 162 | } ]; |
| 163 | |
| 164 | if ( typeof bgOptions.opacity !== 'undefined' && 1 !== bgOptions.opacity ) { |
| 165 | cssObj[ '.gb-container-' + uniqueId + ':before' ].push( { |
| 166 | 'opacity': bgOptions.opacity, // eslint-disable-line quote-props |
| 167 | } ); |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | cssObj[ '.gb-container-' + uniqueId + ' a, .gb-container-' + uniqueId + ' a:visited' ] = [ { |
| 172 | 'color': linkColor, // eslint-disable-line quote-props |
| 173 | } ]; |
| 174 | |
| 175 | cssObj[ '.gb-container-' + uniqueId + ' a:hover' ] = [ { |
| 176 | 'color': linkColorHover, // eslint-disable-line quote-props |
| 177 | } ]; |
| 178 | |
| 179 | cssObj[ '.gb-container-' + uniqueId + ' > .gb-inside-container' ] = [ { |
| 180 | 'padding': shorthandCSS( paddingTop, paddingRight, paddingBottom, paddingLeft, paddingUnit ), // eslint-disable-line quote-props |
| 181 | 'width': minHeight && ! isGrid ? '100%' : false, // eslint-disable-line quote-props |
| 182 | } ]; |
| 183 | |
| 184 | if ( 'contained' === innerContainer && ! isGrid ) { |
| 185 | cssObj[ '.gb-container-' + uniqueId + ' > .gb-inside-container' ].push( { |
| 186 | 'max-width': valueWithUnit( containerWidthPreview, 'px' ), |
| 187 | 'margin-left': 'auto', |
| 188 | 'margin-right': 'auto', |
| 189 | } ); |
| 190 | } |
| 191 | |
| 192 | if ( bgImage && 'pseudo-element' === bgOptions.selector ) { |
| 193 | cssObj[ '.gb-container-' + uniqueId + ' > .gb-inside-container' ].push( { |
| 194 | 'z-index': '1', |
| 195 | 'position': 'relative', // eslint-disable-line quote-props |
| 196 | } ); |
| 197 | } |
| 198 | |
| 199 | cssObj[ '.gb-grid-wrapper > div > .block-editor-block-list__layout > #block-' + clientId ] = [ { |
| 200 | 'width': valueWithUnit( width, '%' ), // eslint-disable-line quote-props |
| 201 | 'display': 'flex', // eslint-disable-line quote-props |
| 202 | 'flex-direction': 'column', |
| 203 | 'margin-left': '0px', |
| 204 | 'margin-right': '0px', |
| 205 | } ]; |
| 206 | |
| 207 | cssObj[ '.gb-grid-wrapper > div > .block-editor-block-list__layout > #block-' + clientId + ' > .gb-grid-column' ] = [ { |
| 208 | 'height': '100%', // eslint-disable-line quote-props |
| 209 | } ]; |
| 210 | |
| 211 | cssObj[ '.block-editor-block-list__layout > #block-' + clientId ] = [ { |
| 212 | 'max-width': 'contained' === outerContainer && ! isGrid ? valueWithUnit( containerWidthPreview, 'px' ) : false, |
| 213 | 'margin-bottom': removeVerticalGap ? '0px !important' : false, |
| 214 | } ]; |
| 215 | |
| 216 | cssObj[ '.gb-grid-column > .gb-container-' + uniqueId ] = [ { |
| 217 | 'display': 'flex', // eslint-disable-line quote-props |
| 218 | 'flex-direction': 'column', |
| 219 | 'height': '100%', // eslint-disable-line quote-props |
| 220 | 'justify-content': verticalAlignment, |
| 221 | } ]; |
| 222 | |
| 223 | cssObj[ `.gb-grid-wrapper > div > .block-editor-block-list__layout > #block-` + clientId + ` > .block-editor-block-list__block-edit, |
| 224 | .gb-grid-wrapper > div > .block-editor-block-list__layout > #block-` + clientId + ` > .block-editor-block-list__block-edit > [data-block="` + clientId + `"], |
| 225 | .gb-grid-wrapper > div > .block-editor-block-list__layout > #block-` + clientId + ` > .block-editor-block-list__block-edit > [data-block="` + clientId + `"] > .gb-grid-column` ] = [ { |
| 226 | 'height': '100%', // eslint-disable-line quote-props |
| 227 | } ]; |
| 228 | |
| 229 | cssObj[ `#block-` + clientId + `:not(.has-child-selected):not(.is-selected) .block-list-appender:not(:first-child), |
| 230 | #block-` + clientId + `:not(.has-child-selected):not(.is-selected) .block-editor-block-list__layout > div:not(:first-child) > .block-list-appender` ] = [ { |
| 231 | 'display': 'none', // eslint-disable-line quote-props |
| 232 | } ]; |
| 233 | |
| 234 | cssObj = applyFilters( 'generateblocks.editor.desktopCSS', cssObj, this.props, 'container' ); |
| 235 | |
| 236 | return ( |
| 237 | <style>{ buildCSS( cssObj ) }</style> |
| 238 | ); |
| 239 | } |
| 240 | } |
| 241 | /* eslint-enable quotes */ |
| 242 |