Skip to content

Commit f8c7183

Browse files
committed
feat: support null/undefined & add hue props (#128).
1 parent 959d2ca commit f8c7183

File tree

2 files changed

+37
-15
lines changed

2 files changed

+37
-15
lines changed

packages/color-saturation/README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,22 @@ export default function Demo() {
3434
}
3535
```
3636

37+
The value of `hsva` does not exist
38+
39+
```jsx mdx:preview
40+
import React from 'react';
41+
import Saturation from '@uiw/react-color-saturation';
42+
43+
export default function Demo() {
44+
return (
45+
<div style={{ display: 'flex', gap: 10 }}>
46+
<Saturation hue={122} />
47+
<Saturation hue={210} />
48+
<Saturation hue={23} />
49+
</div>
50+
);
51+
}
52+
```
3753
## Props
3854

3955
```ts

packages/color-saturation/src/index.tsx

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
1-
import React from 'react';
1+
import React, { useMemo } 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';
55

66
export interface SaturationProps extends Omit<React.HTMLAttributes<HTMLDivElement>, 'onChange'> {
77
prefixCls?: string;
88
/** hsva => `{ h: 0, s: 75, v: 82, a: 1 }` */
9-
hsva: HsvaColor;
9+
hsva?: HsvaColor;
10+
hue?: number;
1011
radius?: React.CSSProperties['borderRadius'];
1112
/** React Component, Custom pointer component */
1213
pointer?: ({ prefixCls, left, top, color }: PointerProps) => JSX.Element;
1314
onChange?: (newColor: HsvaColor) => void;
1415
}
1516

1617
const Saturation = React.forwardRef<HTMLDivElement, SaturationProps>((props, ref) => {
17-
const { prefixCls = 'w-color-saturation', radius = 0, pointer, className, style, hsva, onChange, ...other } = props;
18+
const { prefixCls = 'w-color-saturation', radius = 0, pointer, className, hue = 0, style, hsva, onChange, ...other } = props;
1819
const containerStyle: React.CSSProperties = {
1920
width: 200,
2021
height: 200,
@@ -25,6 +26,7 @@ const Saturation = React.forwardRef<HTMLDivElement, SaturationProps>((props, ref
2526

2627
const handleChange = (interaction: Interaction, event: MouseEvent | TouchEvent) => {
2728
onChange &&
29+
hsva &&
2830
onChange({
2931
h: hsva.h,
3032
s: interaction.left * 100,
@@ -34,17 +36,19 @@ const Saturation = React.forwardRef<HTMLDivElement, SaturationProps>((props, ref
3436
});
3537
};
3638

37-
const comProps = {
38-
top: `${100 - hsva.v}%`,
39-
left: `${hsva.s}%`,
40-
color: hsvaToHslaString(hsva),
41-
};
42-
const pointerElement =
43-
pointer && typeof pointer === 'function' ? (
44-
pointer({ prefixCls, ...comProps })
45-
) : (
46-
<Pointer prefixCls={prefixCls} {...comProps} />
47-
);
39+
const pointerElement = useMemo(() => {
40+
if (!hsva) return null;
41+
const comProps = {
42+
top: `${100 - hsva.v}%`,
43+
left: `${hsva.s}%`,
44+
color: hsvaToHslaString(hsva),
45+
};
46+
if (pointer && typeof pointer === 'function') {
47+
return pointer({ prefixCls, ...comProps });
48+
}
49+
return <Pointer prefixCls={prefixCls} {...comProps} />;
50+
}, [hsva, pointer, prefixCls]);
51+
4852
return (
4953
<Interactive
5054
className={[prefixCls, className || ''].filter(Boolean).join(' ')}
@@ -53,7 +57,9 @@ const Saturation = React.forwardRef<HTMLDivElement, SaturationProps>((props, ref
5357
position: 'absolute',
5458
inset: 0,
5559
cursor: 'crosshair',
56-
backgroundImage: `linear-gradient(0deg, #000, transparent), linear-gradient(90deg, #fff, hsl(${hsva.h}, 100%, 50%))`,
60+
backgroundImage: `linear-gradient(0deg, #000, transparent), linear-gradient(90deg, #fff, hsl(${
61+
hsva?.h ?? hue
62+
}, 100%, 50%))`,
5763
...containerStyle,
5864
}}
5965
ref={ref}

0 commit comments

Comments
 (0)