2
2
* WordPress dependencies
3
3
*/
4
4
import { __ } from '@wordpress/i18n' ;
5
- import { InspectorControls , InnerBlocks , RichText , useBlockProps } from '@wordpress/block-editor' ;
5
+ import {
6
+ __experimentalColorGradientSettingsDropdown as ColorGradientSettingsDropdown ,
7
+ __experimentalUseMultipleOriginColorsAndGradients as useMultipleOriginColorsAndGradients ,
8
+ InspectorControls ,
9
+ InnerBlocks ,
10
+ RichText ,
11
+ useBlockProps ,
12
+ withColors ,
13
+ } from '@wordpress/block-editor' ;
6
14
import { PanelBody , TextControl , ToggleControl } from '@wordpress/components' ;
7
15
import { registerBlockType } from '@wordpress/blocks' ;
8
16
import { useState } from 'react' ;
@@ -11,18 +19,104 @@ import { useState } from 'react';
11
19
* Internal dependencies
12
20
*/
13
21
import metadata from './block.json' ;
14
- import './editor.scss' ;
15
22
import './style.scss' ;
16
23
17
- function Edit ( { attributes, setAttributes } ) {
24
+ function Edit ( {
25
+ attributes,
26
+ setAttributes,
27
+ backgroundColor,
28
+ setBackgroundColor,
29
+ textColor,
30
+ setTextColor,
31
+ closeButtonColor,
32
+ setCloseButtonColor,
33
+ overlayColor,
34
+ setOverlayColor,
35
+ clientId,
36
+ } ) {
18
37
const [ isModalPreview , setIsModalPreview ] = useState ( false ) ;
38
+ const { customBackgroundColor, customCloseButtonColor, customTextColor, customOverlayColor } = attributes ;
39
+ const colorGradientSettings = useMultipleOriginColorsAndGradients ( ) ;
19
40
const classes = [ ] ;
20
41
if ( isModalPreview ) {
21
42
classes . push ( 'is-modal-open' ) ;
22
43
}
44
+
45
+ const style = {
46
+ '--wp--custom--wporg-modal--color--background' : backgroundColor . slug
47
+ ? `var( --wp--preset--color--${ backgroundColor . slug } )`
48
+ : customBackgroundColor ,
49
+ '--wp--custom--wporg-modal--color--text' : textColor . slug
50
+ ? `var( --wp--preset--color--${ textColor . slug } )`
51
+ : customTextColor ,
52
+ '--wp--custom--wporg-modal--color--close-button' : closeButtonColor . slug
53
+ ? `var( --wp--preset--color--${ closeButtonColor . slug } )`
54
+ : customCloseButtonColor ,
55
+ '--wp--custom--wporg-modal--color--overlay' : overlayColor . slug
56
+ ? `var( --wp--preset--color--${ overlayColor . slug } )`
57
+ : customOverlayColor ,
58
+ } ;
59
+ console . log ( style ) ;
60
+ const blockProps = useBlockProps ( {
61
+ className : classes ,
62
+ style,
63
+ } ) ;
64
+
23
65
return (
24
66
< >
25
- < InspectorControls key = "modal-preview" >
67
+ < InspectorControls group = "color" >
68
+ < ColorGradientSettingsDropdown
69
+ settings = { [
70
+ {
71
+ label : __ ( 'Modal background' , 'wporg' ) ,
72
+ colorValue : backgroundColor . color || customBackgroundColor ,
73
+ onColorChange : ( value ) => {
74
+ setBackgroundColor ( value ) ;
75
+ setAttributes ( {
76
+ customBackgroundColor : value ,
77
+ } ) ;
78
+ } ,
79
+ } ,
80
+ {
81
+ label : __ ( 'Modal text' , 'wporg' ) ,
82
+ colorValue : textColor . color || customTextColor ,
83
+ onColorChange : ( value ) => {
84
+ setTextColor ( value ) ;
85
+ setAttributes ( {
86
+ customTextColor : value ,
87
+ } ) ;
88
+ } ,
89
+ } ,
90
+ {
91
+ label : __ ( 'Close button' , 'wporg' ) ,
92
+ colorValue : closeButtonColor . color || customCloseButtonColor ,
93
+ onColorChange : ( value ) => {
94
+ setCloseButtonColor ( value ) ;
95
+ setAttributes ( {
96
+ customCloseButtonColor : value ,
97
+ } ) ;
98
+ } ,
99
+ } ,
100
+ {
101
+ label : __ ( 'Overlay' , 'wporg' ) ,
102
+ colorValue : overlayColor . color || customOverlayColor ,
103
+ onColorChange : ( value ) => {
104
+ setOverlayColor ( value ) ;
105
+ setAttributes ( {
106
+ customOverlayColor : value ,
107
+ } ) ;
108
+ } ,
109
+ enableAlpha : true ,
110
+ } ,
111
+ ] }
112
+ panelId = { clientId }
113
+ hasColorsOrGradients = { false }
114
+ disableCustomColors = { false }
115
+ __experimentalIsRenderedInSidebar
116
+ { ...colorGradientSettings }
117
+ />
118
+ </ InspectorControls >
119
+ < InspectorControls group = "default" >
26
120
< PanelBody title = { __ ( 'Settings' , 'wporg' ) } initialOpen = { true } >
27
121
< ToggleControl
28
122
__nextHasNoMarginBottom
@@ -46,7 +140,7 @@ function Edit( { attributes, setAttributes } ) {
46
140
/>
47
141
</ PanelBody >
48
142
</ InspectorControls >
49
- < div { ...useBlockProps ( { className : classes } ) } >
143
+ < div { ...blockProps } >
50
144
< div className = "wp-block-buttons" >
51
145
< div className = "wp-block-button" >
52
146
< RichText
@@ -60,6 +154,11 @@ function Edit( { attributes, setAttributes } ) {
60
154
</ div >
61
155
< div className = "wporg-modal__modal-backdrop" hidden = { ! isModalPreview } >
62
156
< div className = "wporg-modal__modal" >
157
+ < button
158
+ className = "wporg-modal__modal-close"
159
+ onClick = { ( ) => setIsModalPreview ( false ) }
160
+ aria-label = { __ ( 'Close modal' , 'wporg' ) }
161
+ > </ button >
63
162
< InnerBlocks
64
163
template = { [
65
164
[
@@ -75,7 +174,6 @@ function Edit( { attributes, setAttributes } ) {
75
174
} ,
76
175
} ,
77
176
} ,
78
- layout : { type : 'constrained' } ,
79
177
} ,
80
178
[ [ 'core/paragraph' ] ] ,
81
179
] ,
@@ -89,7 +187,12 @@ function Edit( { attributes, setAttributes } ) {
89
187
}
90
188
91
189
registerBlockType ( metadata . name , {
92
- edit : Edit ,
190
+ edit : withColors ( {
191
+ backgroundColor : 'background-color' ,
192
+ textColor : 'text-color' ,
193
+ closeButtonColor : 'close-button-color' ,
194
+ overlayColor : 'overlay-color' ,
195
+ } ) ( Edit ) ,
93
196
save : ( ) => {
94
197
return < InnerBlocks . Content /> ;
95
198
} ,
0 commit comments