Skip to content

Commit

Permalink
feat(color-picker): colorpicker saturation
Browse files Browse the repository at this point in the history
  • Loading branch information
insekkei committed Apr 14, 2022
1 parent 295c8c1 commit 920263a
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 6 deletions.
10 changes: 4 additions & 6 deletions src/color-picker/components/panel/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -117,18 +117,16 @@ const Panel = (props: ColorPickerProps) => {

// 饱和度变化
const handleSaturationChange = ({ saturation, value }: TdColorSaturationData) => {
const colorInstance = colorInstanceRef.current;
const { saturation: sat, value: val } = colorInstance;
const { saturation: sat, value: val } = colorInstanceRef.current;
let changeTrigger: ColorPickerChangeTrigger = 'palette-saturation-brightness';
colorInstanceRef.current.saturation = saturation;
colorInstanceRef.current.value = value;

if (value !== val && saturation !== sat) {
colorInstance.saturation = saturation;
colorInstance.value = value;
changeTrigger = 'palette-saturation-brightness';
} else if (saturation !== sat) {
colorInstance.saturation = saturation;
changeTrigger = 'palette-saturation';
} else if (value !== val) {
colorInstance.value = value;
changeTrigger = 'palette-brightness';
} else {
return;
Expand Down
35 changes: 35 additions & 0 deletions src/color-picker/components/panel/saturation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,17 @@ const Saturation = (props: TdColorBaseProps) => {
};
};

const isDragging = useRef<Boolean>(false);

const handleDrag = useCallback(
(coordinate: Coordinate, isEnded?: boolean) => {
if (disabled) {
return;
}
if (coordinate.x < 0 || coordinate.y < 0) {
return;
}

const { saturation, value } = getSaturationAndValueByCoordinate(coordinate);
onChange({
saturation: saturation / 100,
Expand All @@ -56,11 +62,39 @@ const Saturation = (props: TdColorBaseProps) => {
if (disabled) {
return;
}

isDragging.current = false;
handleDrag(coordinate, true);
},
[disabled, handleDrag],
);

// 移动中
const handleMove = useCallback(
(e: MouseEvent) => {
if (!isDragging.current || disabled) {
return;
}

const panelRect = panelRef.current.getBoundingClientRect();
handleDrag({ x: e.clientX - panelRect.x, y: e.clientY - panelRect.y }, false);
},
[disabled, handleDrag],
);

const handleEnd = useCallback(() => {
isDragging.current = false;
}, []);

useEffect(() => {
window.addEventListener('mousemove', handleMove, false);
window.addEventListener('mouseup', handleEnd, false);
return () => {
window.removeEventListener('mousemove', handleMove, false);
window.removeEventListener('mouseup', handleEnd, false);
};
}, [handleMove, handleEnd]);

useEffect(() => {
panelRectRef.current.width = panelRef.current.offsetWidth || SATURATION_PANEL_DEFAULT_WIDTH;
panelRectRef.current.height = panelRef.current.offsetHeight || SATURATION_PANEL_DEFAULT_HEIGHT;
Expand All @@ -70,6 +104,7 @@ const Saturation = (props: TdColorBaseProps) => {
panelRectRef.current.height = panelRef.current.offsetHeight;
},
drag: (coordinate: Coordinate) => {
isDragging.current = true;
handleDrag(coordinate);
},
end: handleDragEnd,
Expand Down

0 comments on commit 920263a

Please sign in to comment.