1
- import React , { useCallback , useState , SyntheticEvent } from "react" ;
1
+ import React , { useCallback , useState , SyntheticEvent , useRef , 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" ;
@@ -122,6 +122,9 @@ interface LexicalColorPickerProps {
122
122
handlerClassName ?: string ;
123
123
}
124
124
125
+ const showPickerStyle = { display : "block" } ;
126
+ const hidePickerStyle = { display : "none" } ;
127
+
125
128
export const LexicalColorPicker = ( {
126
129
value,
127
130
onChange,
@@ -130,7 +133,13 @@ export const LexicalColorPicker = ({
130
133
const [ showPicker , setShowPicker ] = useState ( false ) ;
131
134
// Either a custom color or a color coming from the theme object.
132
135
const [ actualSelectedColor , setActualSelectedColor ] = useState ( value || "#fff" ) ;
133
- let themeColor = false ;
136
+ const themeColor = useRef ( false ) ;
137
+
138
+ useEffect ( ( ) => {
139
+ if ( value ) {
140
+ setActualSelectedColor ( value ) ;
141
+ }
142
+ } , [ value ] ) ;
134
143
135
144
const getColorValue = useCallback ( ( rgb : RGBColor ) => {
136
145
return `rgba(${ rgb . r } , ${ rgb . g } , ${ rgb . b } , ${ rgb . a } )` ;
@@ -151,37 +160,36 @@ export const LexicalColorPicker = ({
151
160
152
161
const onColorChangeComplete = useCallback (
153
162
( { rgb } : ColorState , event : React . SyntheticEvent ) => {
154
- setActualSelectedColor ( value ) ;
155
- onChangeComplete ( getColorValue ( rgb ) ) ;
156
163
event . preventDefault ( ) ;
164
+ const value = getColorValue ( rgb ) ;
165
+ setActualSelectedColor ( value ) ;
166
+ onChangeComplete ( value ) ;
157
167
} ,
158
168
[ onChangeComplete ]
159
169
) ;
160
170
161
- const togglePicker = useCallback ( ( e : SyntheticEvent , showPicker : boolean ) => {
171
+ const togglePicker = useCallback ( ( e : SyntheticEvent ) => {
162
172
e . stopPropagation ( ) ;
163
- setShowPicker ( ! showPicker ) ;
173
+ setShowPicker ( state => ! state ) ;
164
174
} , [ ] ) ;
165
175
166
176
const pageElements = usePageElements ( ) ;
167
177
168
- const themeColors : Record < string , any > = { } ;
169
- const colors = pageElements . theme ?. styles ?. colors ;
170
- if ( colors ) {
171
- for ( const key in colors ) {
172
- if ( colors [ key ] ) {
173
- themeColors [ key ] = colors [ key ] ;
174
- }
175
- }
176
- }
178
+ const themeColors : Record < string , any > = useMemo ( ( ) => {
179
+ const colors = pageElements . theme ?. styles ?. colors ?? { } ;
180
+
181
+ return Object . keys ( colors ) . reduce ( ( acc , key ) => {
182
+ return { ...acc , [ key ] : colors [ key ] } ;
183
+ } , { } ) ;
184
+ } , [ pageElements . theme ?. styles ?. colors ] ) ;
177
185
178
186
return (
179
187
< ColorPickerStyle >
180
188
{ Object . keys ( themeColors ) . map ( ( key , index ) => {
181
189
const color = themeColors [ key ] ;
182
190
183
191
if ( color === value || value === "transparent" ) {
184
- themeColor = true ;
192
+ themeColor . current = true ;
185
193
}
186
194
return (
187
195
< ColorBox key = { index } >
@@ -217,26 +225,23 @@ export const LexicalColorPicker = ({
217
225
< ColorBox >
218
226
< Tooltip content = { < span > Color picker</ span > } placement = "bottom" >
219
227
< Color
220
- className = { value && ! themeColor ? styles . selectedColor : "" }
221
- style = { { backgroundColor : themeColor ? "#fff" : value } }
222
- onClick = { e => {
223
- togglePicker ( e , showPicker ) ;
224
- } }
228
+ className = { value && ! themeColor . current ? styles . selectedColor : "" }
229
+ style = { { backgroundColor : themeColor . current ? "#fff" : value } }
230
+ onClick = { togglePicker }
225
231
>
226
232
< IconPalette className = { iconPaletteStyle } />
227
233
</ Color >
228
234
</ Tooltip >
229
235
</ ColorBox >
230
236
231
- { showPicker && (
237
+ < div style = { showPicker ? showPickerStyle : hidePickerStyle } >
232
238
< ChromePicker
233
239
className = { chromePickerStyle }
234
240
color = { actualSelectedColor }
235
- // TODO figure out types for the props
236
241
onChange = { onColorChange as OnChangeHandler }
237
242
onChangeComplete = { onColorChangeComplete as OnChangeHandler }
238
243
/>
239
- ) }
244
+ </ div >
240
245
</ ColorPickerStyle >
241
246
) ;
242
247
} ;
0 commit comments