Overview
Modern web applications often require robust and intuitive user interfaces for customization. A well-designed color picker is a fundamental component in design tools, administrative dashboards, and user personalization settings, directly impacting user experience and application versatility. This problem challenges you to build a sophisticated color picker component, focusing on accurate color manipulation, responsive interactions, and accessibility.
What you need to do / Task
Your task is to implement a ColorPicker React component with the following features:
- Core Color Selection: Allow users to select a color using:
- A hue slider to control the hue (0-360 degrees).
- A saturation/lightness square to pick saturation (horizontal) and lightness (vertical) values for the current hue.
- Color Input/Output:
- Display the currently selected color in a swatch.
- Provide input fields for manual entry and display of the color in HEX (e.g.,
#RRGGBB) and RGB (e.g., rgb(R, G, B)) formats. Changes in these inputs should update the picker state.
- Predefined Color Palette: Include a small set of clickable predefined colors. Clicking a palette color should update the main picker.
- Component API: The
ColorPicker component should accept an initialColor (HEX string) and an onChange callback that provides the selected color as a HEX string.
UI/UX expectations
- Layout: The component should have a clear, organized layout. A suggested layout includes:
- A main color swatch displaying the currently selected color.
- Below or beside it, the hue slider and the saturation/lightness square.
- Input fields for HEX and RGB values.
- A small palette of predefined colors.
- Interactions:
- Dragging the hue slider should smoothly update the hue and the saturation/lightness square's background.
- Dragging a point within the saturation/lightness square should update the saturation and lightness, reflecting in the main swatch and input fields.
- Typing valid HEX or RGB values into their respective input fields should update the color picker state and the main swatch.
- Clicking a predefined palette color should immediately set the picker to that color.
- Accessibility:
- All interactive elements (sliders, input fields, palette items) must be navigable and operable via keyboard (e.g., Tab key for focus, Arrow keys for sliders).
- Appropriate ARIA attributes (e.g.,
aria-label, role, aria-valuenow, aria-valuemin, aria-valuemax) should be used to convey element purpose and state to assistive technologies.
Acceptance criteria
- The component renders without errors and displays an initial color.
- Dragging the hue slider accurately changes the hue of the displayed color.
- Dragging within the saturation/lightness square accurately changes saturation and lightness.
- The main color swatch consistently reflects the selected color from all interaction points (sliders, inputs, palette).
- Typing a valid HEX color string into the HEX input updates the picker state.
- Typing valid RGB values into the RGB input fields updates the picker state.
- Clicking any predefined palette color updates the picker state to that color.
- The
onChange callback is triggered with the correct HEX color string when the color changes.
- All interactive elements are keyboard navigable and operable.
- Relevant ARIA attributes are present on interactive elements for accessibility.