Frontend School

Interactive Hex Color Picker

Machine Coding
medium

Interactive Hex Color Picker

Medium
React
TypeScript
UI
State Management
Event Handling
Color Picker
Frontend

Overview

Color pickers are fundamental UI components in many applications, from design tools to user profile customization. They allow users to select and apply colors to various elements. This problem challenges you to build a simplified interactive color picker component that allows users to select colors from a predefined palette or by entering a hexadecimal code.

What you need to do / Task

Your goal is to implement a ColorPicker React component with the following functionalities:

  1. Display Current Color: Show the currently selected color in a visually prominent area.
  2. Color Palette: Render a grid of predefined color swatches. Clicking on a swatch should select that color.
  3. Hex Input: Provide an input field where users can type a hexadecimal color code (e.g., #RRGGBB). Entering a valid hex code should update the selected color.
  4. Prop-based Control: The component should accept an initialColor prop to set its initial state and an onChange callback prop to communicate color changes to its parent.

UI/UX expectations

  • Layout: The component should clearly display the selected color, followed by the color palette, and then the hex input field. A simple vertical stack or a compact horizontal layout is acceptable.
  • Interactions:
    • Clicking a color swatch in the palette should immediately update the displayed color and the value in the hex input.
    • Typing a valid 6-digit hex code (with or without #) into the input field should update the displayed color and, if the color exists in the palette, highlight the corresponding swatch.
    • Typing an invalid hex code should not change the current color but should provide some visual feedback (e.g., input border color, temporary error message) and revert to the last valid color on blur or enter.
  • Accessibility: Ensure the color swatches are focusable and can be selected using the keyboard (e.g., Enter/Space key). Provide appropriate aria-label attributes for interactive elements.

Acceptance criteria

  • The ColorPicker component accepts initialColor: string and onChange: (color: string) => void props.
  • The displayed color box, palette, and hex input all reflect the initialColor on mount.
  • Clicking a color swatch in the palette updates the displayed color, the hex input field, and triggers the onChange callback with the new hex value.
  • Typing a valid 6-digit hex code (e.g., FFFFFF, #123456) into the input field updates the displayed color, highlights the matching palette swatch (if any), and triggers the onChange callback.
  • Typing an invalid hex code (e.g., ABC, GHIJKL) into the input field does not update the selected color; the input should visually indicate an invalid state or revert to the last valid hex value on blur.
  • Keyboard navigation (focus and selection) is functional for the color palette swatches.

Constraints

Constraints & assumptions

  • You must use React with TypeScript.
  • Focus on a functional component approach for ColorPicker.
  • You are not allowed to use any external color manipulation libraries (e.g., tinycolor2, color). Basic string parsing for hex codes is expected.
  • Assume all hex codes will be 6 digits (e.g., #RRGGBB). Support for 3-digit hex codes (e.g., #RGB) is out of scope.
  • The predefined color palette will be an array of hex strings.
  • The component should manage its own internal state for the selected color and input value, but communicate changes outwards via onChange.

Out of scope

  • Advanced color models (e.g., RGB, HSL sliders, alpha channel).
  • Color gradients or more complex color selection mechanisms.
  • Persistence of selected colors.
  • Full internationalization or complex error messaging.

What interviewers evaluate

  • Component Design: How well the ColorPicker component is structured, its state managed, and props utilized.
  • Event Handling: Correct implementation of click and input change events.
  • Input Validation: Robustness of hex code validation and user feedback.
  • UI Updates: Ensuring all parts of the UI (display, palette, input) consistently reflect the current color state.
  • Code Quality: Readability, maintainability, and adherence to React/TypeScript best practices.

Loading editor...