Skip to content

Commit

Permalink
WRQ-32413: [POC] webOS ColorPicker - add responsiveness (#1668)
Browse files Browse the repository at this point in the history
* handle resize of canvas

* code cleanup

* sorted props

---------

Co-authored-by: Stanca <stanca.pop@lgepartner.com>
  • Loading branch information
adrian-cocoara-lgp and stanca-pop-lgp authored Aug 16, 2024
1 parent 834dd8f commit a221536
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
22 changes: 16 additions & 6 deletions ColorPickerPOC/ColorPickerSpectrum.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ import css from './ColorPickerSpectrum.module.less';
const SpectrumColorPicker = (props) => {
const {selectedColor, selectedColorHandler} = props;
const canvasRef = useRef(null);
const [canvasHeight, setCanvasHeight] = useState(ri.scale(660));
const [canvasWidth, setCanvasWidth] = useState(ri.scale(800));
const [indicatorBgColor, setIndicatorBgColor] = useState('transparent');
const [indicatorX, setIndicatorX] = useState(0);
const [indicatorY, setIndicatorY] = useState(0);
const [isDragging, setIsDragging] = useState(false);
const [indicatorBgColor, setIndicatorBgColor] = useState('transparent');
const [isIndicatorActive, setIsIndicatorActive] = useState(false);

useEffect(() => {
Expand Down Expand Up @@ -57,7 +59,15 @@ const SpectrumColorPicker = (props) => {
}
};
positionIndicator();
}, []); // eslint-disable-line react-hooks/exhaustive-deps

const handleResize = () => {
setCanvasHeight(canvas.parentElement.clientHeight);
setCanvasWidth(canvas.parentElement.clientWidth);
};

window.addEventListener('resize', handleResize);
handleResize();
}, [canvasHeight, canvasWidth]); // eslint-disable-line react-hooks/exhaustive-deps

const handleCanvasPointerDown = useCallback((e) => {
const canvas = canvasRef.current;
Expand Down Expand Up @@ -107,14 +117,14 @@ const SpectrumColorPicker = (props) => {
return (
<div className={css.colorPicker}>
<canvas
ref={canvasRef}
height={ri.scale(600)}
className={css.gradientCanvas}
height={canvasHeight}
onPointerDown={handleCanvasPointerDown}
onPointerLeave={handleCanvasPointerLeave}
onPointerMove={handleCanvasPointerMove}
onPointerUp={handleCanvasPointerUp}
style={{touchAction: 'none'}}
width={ri.scale(800)}
ref={canvasRef}
width={canvasWidth}
/>
<SpectrumIndicator
bgColor={indicatorBgColor}
Expand Down
6 changes: 6 additions & 0 deletions ColorPickerPOC/ColorPickerSpectrum.module.less
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@

.colorPicker {
position: relative;

.gradientCanvas {
height: 100%;
touch-action: none;
width: 100%;
}
}

.circleIndicator {
Expand Down

0 comments on commit a221536

Please sign in to comment.