Skip to content

Commit 2363220

Browse files
committed
fix(lexical): hide transparent color and alpha slider
1 parent 9275cf7 commit 2363220

File tree

1 file changed

+17
-34
lines changed

1 file changed

+17
-34
lines changed

packages/lexical-editor-actions/src/components/LexicalColorPicker/LexicalColorPicker.tsx

Lines changed: 17 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useCallback, useState, SyntheticEvent, useRef, useEffect, useMemo } from "react";
1+
import React, { useCallback, useState, SyntheticEvent, useEffect, useMemo } from "react";
22
import styled from "@emotion/styled";
33
import { css } from "emotion";
44
import { usePageElements } from "@webiny/app-page-builder-elements/hooks/usePageElements";
@@ -13,7 +13,7 @@ const ColorPickerStyle = styled("div")({
1313
position: "relative",
1414
display: "flex",
1515
flexWrap: "wrap",
16-
justifyContent: "space-between",
16+
justifyContent: "flex-start",
1717
width: 240,
1818
padding: 15,
1919
backgroundColor: "#fff"
@@ -58,15 +58,6 @@ const Color = styled("button")({
5858
}
5959
});
6060

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-
7061
const iconPaletteStyle = css({
7162
height: 20,
7263
width: "100%",
@@ -135,23 +126,23 @@ export const LexicalColorPicker = ({
135126
const [showPicker, setShowPicker] = useState(false);
136127
// Either a custom color or a color coming from the theme object.
137128
const [actualSelectedColor, setActualSelectedColor] = useState(value || "#fff");
138-
const themeColor = useRef(false);
129+
const [isThemeColor, setIsThemeColor] = useState(false);
139130

140131
useEffect(() => {
141132
if (value) {
142133
setActualSelectedColor(value);
143134
}
144135
}, [value]);
145136

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})`;
148139
}, []);
149140

150141
const onColorChange = useCallback(
151142
(color: ColorState, event: React.SyntheticEvent) => {
152143
event.preventDefault();
153144
// 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);
155146
setActualSelectedColor(customColor);
156147
if (typeof onChange === "function") {
157148
onChange(customColor);
@@ -163,9 +154,9 @@ export const LexicalColorPicker = ({
163154
const onColorChangeComplete = useCallback(
164155
({ rgb }: ColorState, event: React.SyntheticEvent) => {
165156
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);
169160
},
170161
[onChangeComplete]
171162
);
@@ -185,14 +176,16 @@ export const LexicalColorPicker = ({
185176
}, {});
186177
}, [pageElements.theme?.styles?.colors]);
187178

179+
useEffect(() => {
180+
const isThemeColor = Object.keys(themeColors).some(key => themeColors[key] === value);
181+
setIsThemeColor(isThemeColor);
182+
}, [themeColors, value]);
183+
188184
return (
189185
<ColorPickerStyle>
190186
{Object.keys(themeColors).map((key, index) => {
191187
const color = themeColors[key];
192188

193-
if (color === value || value === "transparent") {
194-
themeColor.current = true;
195-
}
196189
return (
197190
<ColorBox key={index} className={color === value ? styles.selectedColor : ""}>
198191
<Tooltip content={<span>{color}</span>} placement="bottom">
@@ -210,21 +203,10 @@ export const LexicalColorPicker = ({
210203
);
211204
})}
212205

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 : ""}>
225207
<Tooltip content={<span>Color picker</span>} placement="bottom">
226208
<Color
227-
style={{ backgroundColor: themeColor.current ? "#fff" : value }}
209+
style={{ backgroundColor: isThemeColor ? "#fff" : value }}
228210
onClick={togglePicker}
229211
>
230212
<IconPalette className={iconPaletteStyle} />
@@ -236,6 +218,7 @@ export const LexicalColorPicker = ({
236218
<ChromePicker
237219
className={chromePickerStyle}
238220
color={actualSelectedColor}
221+
disableAlpha={true}
239222
onChange={onColorChange as OnChangeHandler}
240223
onChangeComplete={onColorChangeComplete as OnChangeHandler}
241224
/>

0 commit comments

Comments
 (0)