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

feat(react-color-picker): added alpha input field #33536

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ const useStyles = makeStyles({
width: '80px',
},
spinButton: {
width: '50px',
minWidth: '60px',
},
});

Expand All @@ -62,16 +62,19 @@ type RgbKey = 'r' | 'g' | 'b';

export const Default = () => {
const hexId = useId('hex-input');
const alphaId = useId('alpha-input');

const styles = useStyles();
const [color, setColor] = React.useState(DEFAULT_COLOR_HSV);
const [hex, setHex] = React.useState(tinycolor(color).toHexString());
const [rgb, setRgb] = React.useState(tinycolor(color).toRgb());
const [alpha, setAlpha] = React.useState(color.a);

const handleChange: ColorPickerProps['onColorChange'] = (_, data) => {
setColor({ ...data.color, a: data.color.a ?? 1 });
setHex(tinycolor(data.color).toHexString());
setRgb(tinycolor(data.color).toRgb());
setAlpha(data.color.a ?? 1);
};

const onRgbChange = (event: SpinButtonChangeEvent, data: SpinButtonOnChangeData) => {
Expand All @@ -91,6 +94,27 @@ export const Default = () => {
}
};

const onAlphaChange: SpinButtonProps['onChange'] = React.useCallback(
(_ev, data) => {
const value = data.value ?? parseFloat(data.displayValue ??
'');

if (Number.isNaN(value) || value < 0 || value > 1) {
return;
}

const newColor = tinycolor({ ...color, a: value });

if (newColor.isValid) {
setColor(newColor.toHsv());
setHex(newColor.toHex());
setRgb(newColor.toRgb());
}
setAlpha(newColor.a);
Copy link
Contributor

@dmytrokirpa dmytrokirpa Jan 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be called only when the newColor is valid, similar to all other setters?

},
[setAlpha, setRgb, setHex, setColor, color],
);

return (
<div className={styles.example}>
<ColorPicker color={color} onColorChange={handleChange}>
Expand All @@ -99,7 +123,6 @@ export const Default = () => {
<AlphaSlider />
</ColorPicker>
<div className={styles.inputFields}>
<div className={styles.previewColor} style={{ backgroundColor: tinycolor(color).toRgbString() }} />
<InputHexField
id={hexId}
value={hex}
Expand All @@ -108,15 +131,17 @@ export const Default = () => {
const newColor = tinycolor(value);
if (newColor.isValid) {
setColor(newColor.toHsv());
setRgb(newColor.toRgb());
}
setHex(oldValue => (HEX_COLOR_REGEX.test(value) ? value : oldValue));
}}
/>
<InputRgbField label="Red" value={rgb.r} name="r" onChange={onRgbChange} />
<InputRgbField label="Green" value={rgb.g} name="g" onChange={onRgbChange} />
<InputRgbField label="Blue" value={rgb.b} name="b" onChange={onRgbChange} />
<InputAlphaField id={alphaId} value={alpha} onChange={onAlphaChange} />
</div>
<div className={styles.previewColor} style={{ backgroundColor: tinycolor(color).toHexString() }} />
<div className={styles.previewColor} style={{ backgroundColor: tinycolor(color).toRgbString() }} />
</div>
);
};
Expand Down Expand Up @@ -171,6 +196,27 @@ const InputRgbField = ({
);
};

const InputAlphaField = ({
label = 'Alpha',
value,
onChange,
id,
}: {
value: number;
label?: string;
onChange?: SpinButtonProps['onChange'];
id: string;
}) => {
const styles = useStyles();

return (
<div className={styles.colorFieldWrapper}>
<Label htmlFor={id}>{label}</Label>
<SpinButton min={0} max={1} className={styles.spinButton} value={value} step={0.01} onChange={onChange} id={id} />
</div>
);
};

const handleOnBlur = (e: React.FocusEvent<HTMLInputElement>) => {
const value = tinycolor(e.target.value);
if (!value.isValid) {
Expand Down
Loading