1
- import React , { useCallback , useState , SyntheticEvent , useRef , useEffect , useMemo } from "react" ;
1
+ import React , { useCallback , useState , SyntheticEvent , useEffect , useMemo } from "react" ;
2
2
import styled from "@emotion/styled" ;
3
3
import { css } from "emotion" ;
4
4
import { usePageElements } from "@webiny/app-page-builder-elements/hooks/usePageElements" ;
@@ -13,7 +13,7 @@ const ColorPickerStyle = styled("div")({
13
13
position : "relative" ,
14
14
display : "flex" ,
15
15
flexWrap : "wrap" ,
16
- justifyContent : "space-between " ,
16
+ justifyContent : "flex-start " ,
17
17
width : 240 ,
18
18
padding : 15 ,
19
19
backgroundColor : "#fff"
@@ -58,15 +58,6 @@ const Color = styled("button")({
58
58
}
59
59
} ) ;
60
60
61
- const transparent = css ( {
62
- backgroundSize : "10px 10px" ,
63
- backgroundImage :
64
- "linear-gradient( 45deg, #555 25%, transparent 25%, transparent)," +
65
- "linear-gradient(-45deg, #555 25%, transparent 25%, transparent)," +
66
- "linear-gradient( 45deg, transparent 75%, #555 75%)," +
67
- "linear-gradient(-45deg, transparent 75%, #555 75%)"
68
- } ) ;
69
-
70
61
const iconPaletteStyle = css ( {
71
62
height : 20 ,
72
63
width : "100%" ,
@@ -135,23 +126,23 @@ export const LexicalColorPicker = ({
135
126
const [ showPicker , setShowPicker ] = useState ( false ) ;
136
127
// Either a custom color or a color coming from the theme object.
137
128
const [ actualSelectedColor , setActualSelectedColor ] = useState ( value || "#fff" ) ;
138
- const themeColor = useRef ( false ) ;
129
+ const [ isThemeColor , setIsThemeColor ] = useState ( false ) ;
139
130
140
131
useEffect ( ( ) => {
141
132
if ( value ) {
142
133
setActualSelectedColor ( value ) ;
143
134
}
144
135
} , [ value ] ) ;
145
136
146
- const getColorValue = useCallback ( ( rgb : RGBColor ) => {
147
- return `rgba(${ rgb . r } , ${ rgb . g } , ${ rgb . b } , ${ rgb . a } )` ;
137
+ const getColorValue = useCallback ( ( rgb : RGBColor , alpha ?: number ) => {
138
+ return `rgba(${ rgb . r } , ${ rgb . g } , ${ rgb . b } , ${ alpha ?? rgb . a } )` ;
148
139
} , [ ] ) ;
149
140
150
141
const onColorChange = useCallback (
151
142
( color : ColorState , event : React . SyntheticEvent ) => {
152
143
event . preventDefault ( ) ;
153
144
// controls of the picker are updated as user moves the mouse
154
- const customColor = getColorValue ( color . rgb ) ;
145
+ const customColor = getColorValue ( color . rgb , color . rgb . a === 0 ? 1 : color . rgb . a ) ;
155
146
setActualSelectedColor ( customColor ) ;
156
147
if ( typeof onChange === "function" ) {
157
148
onChange ( customColor ) ;
@@ -163,9 +154,9 @@ export const LexicalColorPicker = ({
163
154
const onColorChangeComplete = useCallback (
164
155
( { rgb } : ColorState , event : React . SyntheticEvent ) => {
165
156
event . preventDefault ( ) ;
166
- const value = getColorValue ( rgb ) ;
167
- setActualSelectedColor ( value ) ;
168
- onChangeComplete ( value ) ;
157
+ const color = getColorValue ( rgb , rgb . a === 0 ? 1 : rgb . a ) ;
158
+ setActualSelectedColor ( color ) ;
159
+ onChangeComplete ( color ) ;
169
160
} ,
170
161
[ onChangeComplete ]
171
162
) ;
@@ -185,14 +176,16 @@ export const LexicalColorPicker = ({
185
176
} , { } ) ;
186
177
} , [ pageElements . theme ?. styles ?. colors ] ) ;
187
178
179
+ useEffect ( ( ) => {
180
+ const isThemeColor = Object . keys ( themeColors ) . some ( key => themeColors [ key ] === value ) ;
181
+ setIsThemeColor ( isThemeColor ) ;
182
+ } , [ themeColors , value ] ) ;
183
+
188
184
return (
189
185
< ColorPickerStyle >
190
186
{ Object . keys ( themeColors ) . map ( ( key , index ) => {
191
187
const color = themeColors [ key ] ;
192
188
193
- if ( color === value || value === "transparent" ) {
194
- themeColor . current = true ;
195
- }
196
189
return (
197
190
< ColorBox key = { index } className = { color === value ? styles . selectedColor : "" } >
198
191
< Tooltip content = { < span > { color } </ span > } placement = "bottom" >
@@ -210,21 +203,10 @@ export const LexicalColorPicker = ({
210
203
) ;
211
204
} ) }
212
205
213
- < ColorBox className = { value === "transparent" ? styles . selectedColor : "" } >
214
- < Tooltip content = { < span > Transparent</ span > } placement = "bottom" >
215
- < Color
216
- className = { transparent }
217
- onClick = { ( ) => {
218
- onChangeComplete ( "transparent" ) ;
219
- } }
220
- />
221
- </ Tooltip >
222
- </ ColorBox >
223
-
224
- < ColorBox className = { value && ! themeColor . current ? styles . selectedColor : "" } >
206
+ < ColorBox className = { value && ! isThemeColor ? styles . selectedColor : "" } >
225
207
< Tooltip content = { < span > Color picker</ span > } placement = "bottom" >
226
208
< Color
227
- style = { { backgroundColor : themeColor . current ? "#fff" : value } }
209
+ style = { { backgroundColor : isThemeColor ? "#fff" : value } }
228
210
onClick = { togglePicker }
229
211
>
230
212
< IconPalette className = { iconPaletteStyle } />
@@ -236,6 +218,7 @@ export const LexicalColorPicker = ({
236
218
< ChromePicker
237
219
className = { chromePickerStyle }
238
220
color = { actualSelectedColor }
221
+ disableAlpha = { true }
239
222
onChange = { onColorChange as OnChangeHandler }
240
223
onChangeComplete = { onColorChangeComplete as OnChangeHandler }
241
224
/>
0 commit comments