PluginProbe ʕ •ᴥ•ʔ
GenerateBlocks / 2.2.0
GenerateBlocks v2.2.0
trunk 1.0 1.0.1 1.0.2 1.1.0 1.1.1 1.1.2 1.2.0 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.4.0 1.4.1 1.4.2 1.4.3 1.4.4 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.6.0 1.7.0 1.7.1 1.7.2 1.7.3 1.8.0 1.8.1 1.8.2 1.8.3 1.9.0 1.9.1 2.0.0 2.0.1 2.0.2 2.1.0 2.1.1 2.1.2 2.2.0 2.2.1 2.3.0
generateblocks / src / blocks / element / components / InlineBackgroundImage.jsx
generateblocks / src / blocks / element / components Last commit date
BlockSettings.jsx 1 year ago InlineBackgroundImage.jsx 1 year ago
InlineBackgroundImage.jsx
121 lines
1 import { useEffect, useMemo } from '@wordpress/element';
2
3 import { ImageUpload } from '@components/index.js';
4
5 export function InlineBackgroundImage( { htmlAttributes, setAttributes, styles, onStyleChange, context, label } ) {
6 const inlineBackgroundURL = useMemo( () => {
7 const { style = '' } = htmlAttributes;
8
9 const styleParts = style.split( ';' );
10
11 if ( 0 === styleParts.length ) {
12 return '';
13 }
14
15 const inlineBackgroundPart = styleParts.find( ( part ) => (
16 part.trim().startsWith( '--inline-bg-image' )
17 ) );
18
19 if ( ! inlineBackgroundPart ) {
20 return '';
21 }
22
23 return inlineBackgroundPart.split( 'url(' )[ 1 ].split( ')' )[ 0 ];
24 }, [ htmlAttributes?.style ] );
25
26 function onChange( value ) {
27 const { style = '' } = htmlAttributes;
28
29 const styleParts = style
30 .split( ';' )
31 .filter( ( part ) => (
32 '' !== part && ! part.trim().startsWith( '--inline-bg-image' )
33 ) );
34
35 if ( value ) {
36 styleParts.push( '--inline-bg-image: url(' + value + ')' );
37 }
38
39 setAttributes( {
40 htmlAttributes: {
41 ...htmlAttributes,
42 style: styleParts.join( ';' ),
43 },
44 } );
45 }
46
47 useEffect( () => {
48 const backgroundParts = styles?.backgroundImage?.split( ',' ) ?? [];
49 const hasInlineBackgroundVariable = backgroundParts.length > 0 &&
50 backgroundParts.includes( 'var(--inline-bg-image)' );
51 const hasInlineBackgroundAttribute = '' !== inlineBackgroundURL;
52
53 if ( hasInlineBackgroundVariable && ! hasInlineBackgroundAttribute ) {
54 // We have the CSS, but no background image.
55 // Since we can't guess the background image, let's remove the CSS.
56
57 const cleanedBackgroundParts = backgroundParts.filter( ( part ) => {
58 return part !== 'var(--inline-bg-image)';
59 } );
60
61 const cleanedBackgroundStyle = cleanedBackgroundParts.length > 0
62 ? cleanedBackgroundParts.join( ',' )
63 : '';
64
65 onStyleChange( 'backgroundImage', cleanedBackgroundStyle );
66
67 if ( ! cleanedBackgroundStyle ) {
68 onStyleChange( 'backgroundSize', '' );
69 onStyleChange( 'backgroundRepeat', '' );
70 onStyleChange( 'backgroundPosition', '' );
71 onStyleChange( 'backgroundBlendMode', '' );
72 }
73 }
74
75 if ( ! hasInlineBackgroundVariable && hasInlineBackgroundAttribute ) {
76 // We have the background image, but no CSS.
77 // Let's add the CSS.
78
79 backgroundParts.push( 'var(--inline-bg-image)' );
80 const newBackgroundStyle = backgroundParts.join( ',' );
81 onStyleChange( 'backgroundImage', newBackgroundStyle );
82
83 if ( ! styles?.backgroundSize ) {
84 onStyleChange( 'backgroundSize', 'cover' );
85 }
86
87 if ( ! styles?.backgroundRepeat ) {
88 onStyleChange( 'backgroundRepeat', 'no-repeat' );
89 }
90
91 if ( ! styles?.backgroundPosition ) {
92 onStyleChange( 'backgroundPosition', 'center' );
93 }
94
95 if ( ! styles?.backgroundBlendMode ) {
96 onStyleChange( 'backgroundBlendMode', 'normal' );
97 }
98 }
99 }, [ styles?.backgroundImage, inlineBackgroundURL ] );
100
101 return (
102 <ImageUpload
103 label={ label }
104 value={ inlineBackgroundURL }
105 onInsert={ ( value ) => {
106 onChange( value );
107 } }
108 onSelectImage={ ( image ) => {
109 if ( !! image ) {
110 onChange( image.url );
111 }
112 } }
113 allowDynamicTags={ true }
114 onInsertDynamicTag={ ( tag ) => {
115 onChange( tag );
116 } }
117 context={ context }
118 />
119 );
120 }
121