diff --git a/ColorPickerPOC/ColorPickerPOC.js b/ColorPickerPOC/ColorPickerPOC.js index ca73776a59..d8b6d5931c 100644 --- a/ColorPickerPOC/ColorPickerPOC.js +++ b/ColorPickerPOC/ColorPickerPOC.js @@ -20,24 +20,22 @@ import componentsCss from './ColorPickerPOC.module.less'; const SpottableButton = Spottable(ButtonBase); -const FavoriteColors = ({colorHandler, colors = [], selectedColor = '#3455eb', selectedColorHandler}) => { +const FavoriteColors = ({favoriteColors = [], favoriteColorsHandler, selectedColor = '#3455eb', selectedColorHandler}) => { const [clickEnabled, setClickEnabled] = useState(true); const [editEnabled, setEditEnabled] = useState(false); - const [favoriteColors, setFavoriteColors] = useState(colors); const shakeEffectRef = useRef(null); const timerRef = useRef(null); const addNewFavoriteColor = useCallback(() => { if (favoriteColors.includes(selectedColor)) return; - setFavoriteColors(prevState => { - const colorsState = [...prevState, selectedColor]; + favoriteColorsHandler(() => { + const colorsState = [...favoriteColors, selectedColor]; if (colorsState.length > 8) colorsState.shift(); - colorHandler({selectedColor, favoriteColors: colorsState}); return colorsState; }); - }, [colorHandler, favoriteColors, selectedColor]); + }, [favoriteColors, favoriteColorsHandler, selectedColor]); const onAddNewFavoriteColor = useCallback(() => { if (editEnabled) { @@ -51,32 +49,40 @@ const FavoriteColors = ({colorHandler, colors = [], selectedColor = '#3455eb', s if (!clickEnabled) return; const targetId = ev.target.offsetParent.id || ev.target.id; const [buttonColor, buttonIndex] = targetId.split('-'); + if (editEnabled && clickEnabled) { - setFavoriteColors(prevState => - prevState.filter((stateColor, index) => { - return !(stateColor === buttonColor && index === Number(buttonIndex)); - })); + const filteredColors = favoriteColors.filter((color, index) => { + return !(color === buttonColor && index === Number(buttonIndex)); + }); + + favoriteColorsHandler(filteredColors); + selectedColorHandler(selectedColor); return; } + + favoriteColorsHandler(favoriteColors); selectedColorHandler(buttonColor); - colorHandler({currentColor: buttonColor, favoriteColors}); - }, [clickEnabled, colorHandler, editEnabled, favoriteColors, selectedColorHandler]); + }, [clickEnabled, editEnabled, favoriteColors, favoriteColorsHandler, selectedColor, selectedColorHandler]); - const onMouseDown = useCallback((ev) => { + const onPressHandler = useCallback((ev) => { if (editEnabled) return; const target = ev.target.id ? ev.target : ev.target.offsetParent; + shakeEffectRef.current = setTimeout(() => { target.classList.add(componentsCss.shakeFavoriteColor); }, 300); + timerRef.current = setTimeout(() => { setEditEnabled(true); setClickEnabled(false); + target.classList.remove(componentsCss.shakeFavoriteColor); }, 1000); }, [editEnabled]); - const onMouseUp = useCallback((ev) => { + const onReleaseHandler = useCallback((ev) => { const target = ev.target.id ? ev.target : ev.target.offsetParent; target.classList.remove(componentsCss.shakeFavoriteColor); + clearTimeout(shakeEffectRef.current); clearTimeout(timerRef.current); setTimeout(() => { @@ -87,7 +93,7 @@ const FavoriteColors = ({colorHandler, colors = [], selectedColor = '#3455eb', s return (
- + {favoriteColors.slice(4, 8).map((color, index) => { return ( - + {favoriteColors.slice(0, 4).map((color, index) => { return ( - + - {editEnabled ? 'check' : 'plus'} + {editEnabled ? 'check' : 'plus'}
@@ -156,19 +166,22 @@ FavoriteColors.propTypes = { colorHandler: PropTypes.func, colors: PropTypes.array, css: PropTypes.object, + favoriteColors: PropTypes.array, + favoriteColorsHandler: PropTypes.func, selectedColor: PropTypes.string, selectedColorHandler: PropTypes.func }; const ColorPickerPOCBase = ({color = '#eb4034', colors = [], css, onChangeColor, open, ...rest}) => { + const [favoriteColors, setFavoriteColors] = useState(colors); const [selectedColor, setSelectedColor] = useState(color); useEffect(() => { - if (selectedColor) { - onChangeColor({currentColor: selectedColor}); + if (selectedColor || favoriteColors) { + onChangeColor({selectedColor, favoriteColors}); } - }, [onChangeColor, selectedColor]); + }, [favoriteColors, onChangeColor, selectedColor]); return ( @@ -198,6 +211,8 @@ const ColorPickerPOCBase = ({color = '#eb4034', colors = [], css, onChangeColor, colorHandler={onChangeColor} colors={colors} css={css} + favoriteColors={favoriteColors} + favoriteColorsHandler={setFavoriteColors} selectedColor={selectedColor} selectedColorHandler={setSelectedColor} /> diff --git a/ColorPickerPOC/ColorPickerPOC.module.less b/ColorPickerPOC/ColorPickerPOC.module.less index 9a3923adcf..0dcb981d00 100644 --- a/ColorPickerPOC/ColorPickerPOC.module.less +++ b/ColorPickerPOC/ColorPickerPOC.module.less @@ -7,24 +7,29 @@ min-width: 800px; } -.currentColor { - border-radius: 24px; - border: 9px solid; - height: 240px; - transition: transform .2s; - width: 100%; - - .currentColorIcon { - transition: transform .2s; - } +.selectedColorColumn { + margin-top: 15px; - .focus({ - box-shadow: 0 0 12px 6px inset; + .selectedColor { + border-radius: 24px; + border: 9px solid; + height: 240px; + margin-inline: 0; + transition: transform .2s; + width: 95%; - .currentColorIcon { - transform: scale(1.2); + .selectedColorIcon { + transition: transform .2s; } - }); + + .focus({ + box-shadow: 0 0 12px 6px inset; + + .selectedColorIcon { + transform: scale(1.2); + } + }); + } } .favoriteColorsRow {