hxf31891 / react-gradient-color-picker

An easy to use color/gradient picker for React.js
MIT License
146 stars 49 forks source link

Update useColorPicker.ts to use current selected value from picker #109

Closed khoshrou closed 2 weeks ago

khoshrou commented 2 weeks ago

I like to create a component that contains ColorPicker and I like to pass the gradient object to the upper component. getGradientObject not returning the correct object coz color not updated with value yet. this happens for value to hex too. like this

const [color, setColor] = useState('rgba(70,70,142,1)');
const { getGradientObject, valueToHex } = useColorPicker(color, setColor);

function onChange(value: string) {
  setColor(value);

  if (props.onChange) {
      const g = getGradientObject();
      props.onChange(value, g.isGradient ? g : undefined);
  }
}
<ColorPicker value={color} onChange={onChange} width={290} />