Skip to content

Commit d0ace0c

Browse files
committed
fix(Colorful): center circle around cursor in Colorful picker. #169
1 parent 96c6181 commit d0ace0c

File tree

3 files changed

+36
-14
lines changed

3 files changed

+36
-14
lines changed

packages/color-colorful/src/index.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ const Pointer = ({ style, color, ...props }: React.HTMLAttributes<HTMLDivElement
2626
height: 28,
2727
width: 28,
2828
position: 'absolute',
29-
transform: 'translate(-16px, -10px)',
29+
transform: 'translate(-16px, -16px)',
3030
boxShadow: '0 2px 4px rgb(0 0 0 / 20%)',
3131
borderRadius: '50%',
3232
background: `url(${BACKGROUND_IMG})`,
@@ -78,15 +78,17 @@ const Colorful = React.forwardRef<HTMLDivElement, ColorfulProps>((props, ref) =>
7878
radius={disableAlpha ? '0 0 8px 8px' : 0}
7979
className={prefixCls}
8080
onChange={(newHue) => handleChange({ ...hsva, ...newHue })}
81-
pointer={({ left }) => <Pointer style={{ left }} color={`hsl(${hsva.h || 0}deg 100% 50%)`} />}
81+
pointer={({ left }) => (
82+
<Pointer style={{ left, transform: 'translate(-16px, -5px)' }} color={`hsl(${hsva.h || 0}deg 100% 50%)`} />
83+
)}
8284
/>
8385
{!disableAlpha && (
8486
<Alpha
8587
hsva={hsva}
8688
height={24}
8789
className={prefixCls}
8890
radius="0 0 8px 8px"
89-
pointer={({ left }) => <Pointer style={{ left }} color={hsvaToRgbaString(hsva)} />}
91+
pointer={({ left }) => <Pointer style={{ left, transform: 'translate(-16px, -5px)' }} color={hsvaToRgbaString(hsva)} />}
9092
onChange={(newAlpha) => handleChange({ ...hsva, ...newAlpha })}
9193
/>
9294
)}

packages/color-saturation/src/index.tsx

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useMemo } from 'react';
1+
import React, { useMemo, useState } from 'react';
22
import { HsvaColor, hsvaToHslaString } from '@uiw/color-convert';
33
import Interactive, { Interaction } from '@uiw/react-drag-event-interactive';
44
import { Pointer, PointerProps } from './Pointer';
@@ -24,7 +24,9 @@ const Saturation = React.forwardRef<HTMLDivElement, SaturationProps>((props, ref
2424
position: 'relative',
2525
};
2626

27+
const [interaction, setInteraction] = useState<Interaction>({ left: 0, top: 0, x: 0, y: 0, width: 0, height: 0 });
2728
const handleChange = (interaction: Interaction, event: MouseEvent | TouchEvent) => {
29+
setInteraction(interaction);
2830
onChange &&
2931
hsva &&
3032
onChange({
@@ -39,15 +41,15 @@ const Saturation = React.forwardRef<HTMLDivElement, SaturationProps>((props, ref
3941
const pointerElement = useMemo(() => {
4042
if (!hsva) return null;
4143
const comProps = {
42-
top: `${100 - hsva.v}%`,
43-
left: `${hsva.s}%`,
44+
top: `${interaction.y}px`,
45+
left: `${interaction.x}px`,
4446
color: hsvaToHslaString(hsva),
4547
};
4648
if (pointer && typeof pointer === 'function') {
4749
return pointer({ prefixCls, ...comProps });
4850
}
4951
return <Pointer prefixCls={prefixCls} {...comProps} />;
50-
}, [hsva, pointer, prefixCls]);
52+
}, [hsva, interaction, pointer, prefixCls]);
5153

5254
return (
5355
<Interactive

packages/drag-event-interactive/src/utils.ts

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useRef, useEffect, useCallback, useLayoutEffect } from 'react';
1+
import { useRef, useEffect, useCallback } from 'react';
22

33
// Saves incoming handler to the ref in order to avoid "useCallback hell"
44
export function useEventCallback<T, K>(handler?: (value: T, event: K) => void): (value: T, event: K) => void {
@@ -36,19 +36,37 @@ export interface Interaction {
3636
y: number;
3737
}
3838

39+
// // Returns a relative position of the pointer inside the node's bounding box
40+
// export const getRelativePosition = (node: HTMLDivElement, event: MouseEvent | TouchEvent): Interaction => {
41+
// const rect = node.getBoundingClientRect();
42+
43+
// // Get user's pointer position from `touches` array if it's a `TouchEvent`
44+
// const pointer = isTouch(event) ? event.touches[0] : (event as MouseEvent);
45+
46+
// return {
47+
// left: clamp((pointer.pageX - (rect.left + window.pageXOffset)) / rect.width),
48+
// top: clamp((pointer.pageY - (rect.top + window.pageYOffset)) / rect.height),
49+
// width: rect.width,
50+
// height: rect.height,
51+
// x: pointer.pageX - (rect.left + window.pageXOffset),
52+
// y: pointer.pageY - (rect.top + window.pageYOffset),
53+
// };
54+
// };
55+
3956
// Returns a relative position of the pointer inside the node's bounding box
4057
export const getRelativePosition = (node: HTMLDivElement, event: MouseEvent | TouchEvent): Interaction => {
4158
const rect = node.getBoundingClientRect();
42-
43-
// Get user's pointer position from `touches` array if it's a `TouchEvent`
4459
const pointer = isTouch(event) ? event.touches[0] : (event as MouseEvent);
4560

61+
const x = clamp(pointer.pageX - (rect.left + window.pageXOffset), 0, rect.width);
62+
const y = clamp(pointer.pageY - (rect.top + window.pageYOffset), 0, rect.height);
63+
4664
return {
47-
left: clamp((pointer.pageX - (rect.left + window.pageXOffset)) / rect.width),
48-
top: clamp((pointer.pageY - (rect.top + window.pageYOffset)) / rect.height),
65+
left: x / rect.width,
66+
top: y / rect.height,
4967
width: rect.width,
5068
height: rect.height,
51-
x: pointer.pageX - (rect.left + window.pageXOffset),
52-
y: pointer.pageY - (rect.top + window.pageYOffset),
69+
x,
70+
y,
5371
};
5472
};

0 commit comments

Comments
 (0)