Overview
Color pickers are fundamental components in many applications, from design tools to user profile customization. They empower users to personalize their experience and express creativity. In this problem, you will build a reusable ColorPicker component that allows users to select colors from a predefined palette and input custom hexadecimal values, reflecting Thoughtworks' emphasis on robust and intuitive user interfaces.
What you need to do / Task
Your primary task is to develop a React TypeScript component named ColorPicker. This component should:
- Display a Palette: Render a grid or list of predefined colors provided via props.
- Select from Palette: Allow users to click on any predefined color to select it.
- Display Current Color: Visually indicate the currently selected color (e.g., a larger swatch or a border around the selected color).
- Custom Hex Input: Provide an input field where users can type a custom hexadecimal color code (e.g.,
#RRGGBB or #RGB).
- Validate Hex Input: Implement basic validation for the custom hex input. If the input is a valid hex code, it should update the selected color; otherwise, it should show an error or prevent selection.
- Propagate Changes: The
ColorPicker component should accept an onSelect prop (a callback function) that is invoked with the newly selected color (as a hex string) whenever the color changes, whether by palette selection or custom input.
UI/UX expectations
- Layout: The component should clearly display the predefined color swatches. A separate area should show the currently selected color, and an input field for custom hex codes. You are free to choose a suitable layout (e.g., CSS Grid or Flexbox) for the color palette.
- Interactions:
- Clicking a color swatch should immediately update the selected color and trigger the
onSelect callback.
- Typing a valid hex code into the custom input field should update the selected color and trigger
onSelect.
- Invalid hex input should not change the selected color and ideally provide visual feedback (e.g., a red border on the input, or an error message).
- Visual Feedback: The currently selected color should be prominently displayed. When a color from the palette is selected, it should have a clear visual indicator (e.g., a border, a checkmark).
Acceptance criteria
- The
ColorPicker component renders with a predefined color palette.
- Clicking a color swatch from the palette updates the active selection and calls the
onSelect callback with the new color.
- A visual indicator distinguishes the currently selected color swatch.
- A text input field for custom hex codes is present.
- Entering a valid 3-digit or 6-digit hex code (e.g.,
#FFF, #FF00FF) into the input field updates the selected color and calls onSelect.
- Entering an invalid hex code does not update the selected color and provides visual feedback (e.g., an error state for the input).
- The component correctly displays the currently selected color in a separate swatch/display area.