deprecated
2 months ago
GenerateBgImage.js
2 months ago
block.json
1 month ago
edit.js
1 week ago
icon.svg
2 months ago
index.js
2 months ago
index.php
2 months ago
save.js
2 months ago
style.scss
2 months ago
GenerateBgImage.js
113 lines
| 1 | const GenerateBgImage = (props) => { |
| 2 | const { attributes, prefix } = props; |
| 3 | const { bgImageMobile, bgImageTablet, bgImage, bgSize, blockId } = |
| 4 | attributes; |
| 5 | |
| 6 | const mobileViewport = 'max-width: 575.98px'; |
| 7 | const tabletViewport = 'min-width: 576px'; |
| 8 | const pcViewport = 'min-width: 992px'; |
| 9 | const underPcViewport = 'max-width: 992.98px'; |
| 10 | |
| 11 | let backgroundStyle; |
| 12 | const backgroundPosition = 'background-position:center!important;'; |
| 13 | if ('cover' === bgSize) { |
| 14 | backgroundStyle = `background-size:${bgSize}!important; ${backgroundPosition}`; |
| 15 | } else if ('repeat' === bgSize) { |
| 16 | backgroundStyle = `background-repeat:${bgSize}!important; background-size: auto; ${backgroundPosition}`; |
| 17 | } else { |
| 18 | backgroundStyle = ``; |
| 19 | } |
| 20 | |
| 21 | const createBgCss = (imageUrl) => ` |
| 22 | --vk-slider-item-bg-image: url(${imageUrl}); |
| 23 | background-image: url(${imageUrl}); |
| 24 | background-image: var(--vk-slider-item-bg-image); |
| 25 | ${backgroundStyle} |
| 26 | `; |
| 27 | |
| 28 | //mobile only |
| 29 | if (bgImageMobile && !bgImageTablet && !bgImage) { |
| 30 | return ( |
| 31 | <style>{`.${prefix}-${blockId}{${createBgCss(bgImageMobile)}}`}</style> |
| 32 | ); |
| 33 | } |
| 34 | //tablet only |
| 35 | if (!bgImageMobile && bgImageTablet && !bgImage) { |
| 36 | return ( |
| 37 | <style>{`.${prefix}-${blockId}{${createBgCss(bgImageTablet)}}`}</style> |
| 38 | ); |
| 39 | } |
| 40 | //pc only |
| 41 | if (!bgImageMobile && !bgImageTablet && bgImage) { |
| 42 | return ( |
| 43 | <style>{`.${prefix}-${blockId}{${createBgCss(bgImage)}}`}</style> |
| 44 | ); |
| 45 | } |
| 46 | //pc -mobile |
| 47 | if (bgImageMobile && !bgImageTablet && bgImage) { |
| 48 | return ( |
| 49 | <style> |
| 50 | {` |
| 51 | @media screen and (${underPcViewport}) { |
| 52 | .${prefix}-${blockId}{${createBgCss(bgImageMobile)}} |
| 53 | } |
| 54 | @media screen and (${pcViewport}) { |
| 55 | .${prefix}-${blockId}{${createBgCss(bgImage)}} |
| 56 | } |
| 57 | `} |
| 58 | </style> |
| 59 | ); |
| 60 | } |
| 61 | //pc -tablet |
| 62 | if (!bgImageMobile && bgImageTablet && bgImage) { |
| 63 | return ( |
| 64 | <style> |
| 65 | {` |
| 66 | @media screen and (${underPcViewport}) { |
| 67 | .${prefix}-${blockId}{${createBgCss(bgImageTablet)}} |
| 68 | } |
| 69 | @media screen and (${pcViewport}) { |
| 70 | .${prefix}-${blockId}{${createBgCss(bgImage)}} |
| 71 | } |
| 72 | `} |
| 73 | </style> |
| 74 | ); |
| 75 | } |
| 76 | //tablet - mobile |
| 77 | if (bgImageMobile && bgImageTablet && !bgImage) { |
| 78 | return ( |
| 79 | <style> |
| 80 | {` |
| 81 | @media screen and (${mobileViewport}) { |
| 82 | .${prefix}-${blockId}{${createBgCss(bgImageMobile)}} |
| 83 | } |
| 84 | @media screen and (${tabletViewport}) { |
| 85 | .${prefix}-${blockId}{${createBgCss(bgImageTablet)}} |
| 86 | } |
| 87 | `} |
| 88 | </style> |
| 89 | ); |
| 90 | } |
| 91 | //pc -tablet - mobile |
| 92 | if (bgImageMobile && bgImageTablet && bgImage) { |
| 93 | return ( |
| 94 | <style> |
| 95 | {` |
| 96 | @media screen and (${mobileViewport}) { |
| 97 | .${prefix}-${blockId}{${createBgCss(bgImageMobile)}} |
| 98 | } |
| 99 | @media screen and (${tabletViewport}) { |
| 100 | .${prefix}-${blockId}{${createBgCss(bgImageTablet)}} |
| 101 | } |
| 102 | @media screen and (${pcViewport}) { |
| 103 | .${prefix}-${blockId}{${createBgCss(bgImage)}} |
| 104 | } |
| 105 | `} |
| 106 | </style> |
| 107 | ); |
| 108 | } |
| 109 | |
| 110 | return null; |
| 111 | }; |
| 112 | export default GenerateBgImage; |
| 113 |