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

docs(react-color-picker): Added RGB fields to stories. #33469

Open
wants to merge 3 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
@@ -1,6 +1,16 @@
import * as React from 'react';
import { tinycolor } from '@ctrl/tinycolor';
import { makeStyles, useId, Input, type InputProps, Label } from '@fluentui/react-components';
import {
makeStyles,
useId,
Input,
type InputProps,
Label,
SpinButton,
type SpinButtonProps,
type SpinButtonOnChangeData,
type SpinButtonChangeEvent,
} from '@fluentui/react-components';
import {
ColorPicker,
ColorSlider,
Expand Down Expand Up @@ -36,21 +46,46 @@ const useStyles = makeStyles({
input: {
width: '80px',
},
spinButton: {
width: '50px',
},
});

const HEX_COLOR_REGEX = /^#?([0-9A-Fa-f]{0,6})$/;
const NUMBER_REGEX = /^\d+$/;
const DEFAULT_COLOR_HSV = tinycolor('#2be700').toHsv();

type RgbKey = 'r' | 'g' | 'b';

export const Default = () => {
const hexId = useId('hex-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 handleChange: ColorPickerProps['onColorChange'] = (_, data) => {
setColor({ ...data.color, a: data.color.a ?? 1 });
setHex(tinycolor(data.color).toHexString());
setRgb(tinycolor(data.color).toRgb());
};

const onRgbChange = (event: SpinButtonChangeEvent, data: SpinButtonOnChangeData) => {
const value = data.value ?? (data.displayValue !== undefined ? parseFloat(data.displayValue) : null);

if (value === null || Number.isNaN(value) || !NUMBER_REGEX.test(value.toString())) {
return;
}

const colorKey = (event.target as HTMLInputElement).name as RgbKey;

const newColor = tinycolor({ ...rgb, [colorKey]: value });
if (newColor.isValid) {
setColor(newColor.toHsv());
setHex(newColor.toHex());
setRgb(newColor.toRgb());
}
};

return (
Expand All @@ -74,7 +109,11 @@ export const Default = () => {
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} />
</div>
<div className={styles.previewColor} style={{ backgroundColor: tinycolor(color).toHexString() }} />
</div>
);
};
Expand All @@ -99,6 +138,36 @@ const InputHexField = ({
);
};

const InputRgbField = ({
value,
onChange,
label,
name,
}: {
value: number;
label: string;
name: RgbKey;
onChange?: SpinButtonProps['onChange'];
}) => {
const id = useId(`${label.toLowerCase()}-input`);
const styles = useStyles();

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

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