A11y: Improve accessibility of ColorPickerInput (#60923)

This commit is contained in:
Joao Silva 2023-01-04 13:19:41 +01:00 committed by GitHub
parent c68603a573
commit 220175cab9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 9 deletions

View File

@ -12,10 +12,11 @@ import { ColorPickerProps } from './ColorPickerPopover';
interface ColorInputProps extends ColorPickerProps, Omit<InputProps, 'color' | 'onChange'> {
isClearable?: boolean;
buttonAriaLabel?: string;
}
const ColorInput = forwardRef<HTMLInputElement, ColorInputProps>(
({ color, onChange, isClearable = false, ...inputProps }, ref) => {
({ color, onChange, isClearable = false, onClick, onBlur, disabled, buttonAriaLabel, ...inputProps }, ref) => {
const [value, setValue] = useState(color);
const [previousColor, setPreviousColor] = useState(color);
// eslint-disable-next-line react-hooks/exhaustive-deps
@ -44,12 +45,14 @@ const ColorInput = forwardRef<HTMLInputElement, ColorInputProps>(
}
};
const onBlur = () => {
const onBlurInput = (event: React.FocusEvent<HTMLInputElement>) => {
const newColor = tinycolor(value);
if (!newColor.isValid()) {
setValue(color);
}
onBlur?.(event);
};
return (
@ -57,8 +60,10 @@ const ColorInput = forwardRef<HTMLInputElement, ColorInputProps>(
{...inputProps}
value={value}
onChange={onChangeColor}
onBlur={onBlur}
addonBefore={<ColorPreview color={color} />}
disabled={disabled}
onClick={onClick}
onBlur={onBlurInput}
addonBefore={<ColorPreview onClick={onClick} ariaLabel={buttonAriaLabel} disabled={disabled} color={color} />}
ref={ref}
/>
);
@ -71,13 +76,20 @@ export default ColorInput;
interface ColorPreviewProps {
color: string;
onClick?: React.MouseEventHandler<HTMLElement>;
disabled?: boolean;
ariaLabel?: string;
}
const ColorPreview = ({ color }: ColorPreviewProps) => {
const ColorPreview = ({ color, onClick, disabled, ariaLabel }: ColorPreviewProps) => {
const styles = useStyles2(getColorPreviewStyles);
return (
<div
<button
type="button"
onClick={onClick}
aria-label={ariaLabel}
disabled={disabled || !onClick}
className={cx(
styles,
css`

View File

@ -59,9 +59,17 @@ export const ColorPickerInput = forwardRef<HTMLInputElement, ColorPickerInputPro
className={cx(paletteStyles.root, styles.picker)}
/>
)}
<div onClick={() => setIsOpen(true)}>
<ColorInput {...inputProps} theme={theme} color={currentColor} onChange={setColor} ref={ref} isClearable />
</div>
<ColorInput
{...inputProps}
theme={theme}
color={currentColor}
onChange={setColor}
buttonAriaLabel="Open color picker"
onClick={() => setIsOpen(true)}
onBlur={() => setIsOpen(false)}
ref={ref}
isClearable
/>
</div>
</ClickOutsideWrapper>
);