Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: bump fast-color version #259

Merged
merged 2 commits into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion docs/example/basic.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@ export default () => {

return (
<>
<ColorPicker color={value} onChange={setValue} />
<ColorPicker
color={value}
onChange={nextValue => {
console.log('onChange', nextValue.toHsbString(), nextValue);
setValue(nextValue);
}}
/>
<br />
<div
style={{
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
]
},
"dependencies": {
"@ant-design/fast-color": "^1.2.0",
"@ant-design/fast-color": "^2.0.1",
"@babel/runtime": "^7.23.6",
"classnames": "^2.2.6",
"rc-util": "^5.38.1"
Expand Down
27 changes: 9 additions & 18 deletions src/ColorPicker.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { CSSProperties } from 'react';
import React, { forwardRef, useMemo } from 'react';
import { ColorPickerPrefixCls, defaultColor, generateColor } from './util';
import { ColorPickerPrefixCls, defaultColor } from './util';

import classNames from 'classnames';
import { Color } from './color';
Expand Down Expand Up @@ -76,12 +76,10 @@ export default forwardRef<HTMLDivElement, ColorPickerProps>((props, ref) => {
value,
defaultValue,
});
const alphaColor = useMemo(() => {
const rgb = generateColor(colorValue.toRgbString());
// alpha color need equal 1 for base color
rgb.setAlpha(1);
return rgb.toRgbString();
}, [colorValue]);
const alphaColor = useMemo(
() => colorValue.setA(1).toRgbString(),
[colorValue],
);

// ============================ Events ============================
const handleChange: BaseColorPickerProps['onChange'] = (data, type) => {
Expand All @@ -92,17 +90,10 @@ export default forwardRef<HTMLDivElement, ColorPickerProps>((props, ref) => {
};

// Convert
const getHueColor = (hue: number) => {
const hsb = colorValue.toHsb();
hsb.h = hue;
return new Color(hsb);
};
const getHueColor = (hue: number) => new Color(colorValue.setHue(hue));

const getAlphaColor = (alpha: number) => {
const hsb = colorValue.toHsb();
hsb.a = Math.round(alpha) / 100;
return new Color(hsb);
};
const getAlphaColor = (alpha: number) =>
new Color(colorValue.setA(alpha / 100));

// Slider change
const onHueChange = (hue: number) => {
Expand Down Expand Up @@ -170,7 +161,7 @@ export default forwardRef<HTMLDivElement, ColorPickerProps>((props, ref) => {
]}
min={0}
max={100}
value={colorValue.getAlpha() * 100}
value={colorValue.a * 100}
onChange={onAlphaChange}
onChangeComplete={onAlphaChangeComplete}
/>
Expand Down
6 changes: 3 additions & 3 deletions src/components/Gradient.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { FC } from 'react';
import React, { useMemo } from 'react';
import type { Color } from '../color';
import { Color } from '../color';
import type { HsbaColorType } from '../interface';
import { generateColor } from '../util';

Expand All @@ -15,9 +15,9 @@ const Gradient: FC<{
() =>
colors
.map((color, idx) => {
const result = generateColor(color);
let result = generateColor(color);
if (type === 'alpha' && idx === colors.length - 1) {
result.setAlpha(1);
result = new Color(result.setA(1));
}
return result.toRgbString();
})
Expand Down
2 changes: 1 addition & 1 deletion src/components/Slider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ const Slider: FC<BaseSliderProps> = props => {
const colorRef = useRef(color);

const getValue = (c: Color) => {
return type === 'hue' ? c.getHue() : c.getAlpha() * 100;
return type === 'hue' ? c.getHue() : c.a * 100;
};

const onDragChange = useEvent((offsetValue: TransformOffset) => {
Expand Down
6 changes: 4 additions & 2 deletions tests/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -421,11 +421,13 @@ describe('ColorPicker', () => {
color={value}
onChange={(color, type) => {
onChange(color, type);

let passedColor = color;
if (type !== 'alpha') {
// bad case, color should be immutable
color.setAlpha(1);
passedColor = new Color(color.setA(1));
}
setValue(color);
setValue(passedColor);
}}
/>
</>
Expand Down
Loading