index.js
78 lines
| 1 | import hexToRGBA from '../hex-to-rgba'; |
| 2 | import getBackgroundImageUrl from '../get-background-image-url'; |
| 3 | |
| 4 | import { |
| 5 | applyFilters, |
| 6 | } from '@wordpress/hooks'; |
| 7 | |
| 8 | export default function getBackgroundImageCSS( type, props ) { |
| 9 | const attributes = applyFilters( 'generateblocks.editor.cssAttrs', props.attributes, props ); |
| 10 | |
| 11 | const { |
| 12 | backgroundColor, |
| 13 | backgroundColorOpacity, |
| 14 | bgImage, |
| 15 | bgImageInline, |
| 16 | gradient, |
| 17 | bgOptions, |
| 18 | gradientColorOne, |
| 19 | gradientColorOneOpacity, |
| 20 | gradientColorTwo, |
| 21 | gradientColorTwoOpacity, |
| 22 | gradientColorStopOne, |
| 23 | gradientColorStopTwo, |
| 24 | gradientDirection, |
| 25 | useDynamicData, |
| 26 | dynamicContentType, |
| 27 | } = attributes; |
| 28 | |
| 29 | let gradientValue = ''; |
| 30 | |
| 31 | if ( gradient ) { |
| 32 | let gradientColorStopOneValue = '', |
| 33 | gradientColorStopTwoValue = ''; |
| 34 | |
| 35 | const gradientColorOneValue = hexToRGBA( gradientColorOne, gradientColorOneOpacity ); |
| 36 | const gradientColorTwoValue = hexToRGBA( gradientColorTwo, gradientColorTwoOpacity ); |
| 37 | |
| 38 | if ( gradientColorOne && '' !== gradientColorStopOne ) { |
| 39 | gradientColorStopOneValue = ' ' + gradientColorStopOne + '%'; |
| 40 | } |
| 41 | |
| 42 | if ( gradientColorTwo && '' !== gradientColorStopTwo ) { |
| 43 | gradientColorStopTwoValue = ' ' + gradientColorStopTwo + '%'; |
| 44 | } |
| 45 | |
| 46 | gradientValue = 'linear-gradient(' + gradientDirection + 'deg, ' + gradientColorOneValue + gradientColorStopOneValue + ', ' + gradientColorTwoValue + gradientColorStopTwoValue + ')'; |
| 47 | } |
| 48 | |
| 49 | if ( 'gradient' === type ) { |
| 50 | return gradientValue; |
| 51 | } |
| 52 | |
| 53 | let backgroundImage = false; |
| 54 | |
| 55 | const backgroundColorValue = hexToRGBA( backgroundColor, backgroundColorOpacity ); |
| 56 | |
| 57 | if ( !! bgImage || ( useDynamicData && '' !== dynamicContentType ) ) { |
| 58 | const url = getBackgroundImageUrl( props ); |
| 59 | |
| 60 | if ( 'element' === bgOptions.selector && ( backgroundColorValue || gradient ) && 'undefined' !== typeof bgOptions.overlay && bgOptions.overlay ) { |
| 61 | // Old background image overlays mixed with our gradients. |
| 62 | if ( gradient ) { |
| 63 | backgroundImage = gradientValue + ', url(' + url + ')'; |
| 64 | } else if ( backgroundColorValue ) { |
| 65 | backgroundImage = 'linear-gradient(0deg, ' + backgroundColorValue + ', ' + backgroundColorValue + '), url(' + url + ')'; |
| 66 | } |
| 67 | } else { |
| 68 | backgroundImage = 'url(' + url + ')'; |
| 69 | |
| 70 | if ( bgImageInline && 'element' !== bgOptions.selector ) { |
| 71 | backgroundImage = 'var(--background-image)'; |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | return backgroundImage; |
| 77 | } |
| 78 |