Frontend School

Advanced Color Picker Component

Machine Coding
hard

Advanced Color Picker Component

Hard
React
TypeScript
UI/UX
Color Picker
Accessibility
State Management
Event Handling

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:

  1. 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.
  2. 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.
  3. Predefined Color Palette: Include a small set of clickable predefined colors. Clicking a palette color should update the main picker.
  4. 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.

Constraints

Constraints & assumptions

  • You must implement the core color conversion logic (e.g., HSL to RGB, RGB to HEX, HEX to HSL) yourself. Do not use external color manipulation libraries (e.g., colord, tinycolor2).
  • Focus on functional React components and hooks.
  • The application will run in a standard browser environment.
  • Assume valid initial HEX color string format is provided.
  • For RGB input, you can use three separate number inputs (R, G, B) or a single text input that parses rgb(R, G, B) format. For simplicity, three separate inputs are recommended.
  • No alpha (opacity) channel is required; focus only on opaque colors.

Out of scope

  • Saving custom colors or color history.
  • Complex input validation beyond basic format checks (e.g., ensuring numbers are within 0-255 or 0-100%).
  • Internationalization (i18n).
  • Server-side rendering considerations.

What interviewers evaluate

  • Correctness and Robustness: Accuracy of color conversion algorithms and state management across different interaction types.
  • Component Design: Clean, modular, and maintainable React component structure, effective use of hooks, and clear separation of concerns.
  • UI Interactions & Responsiveness: Smooth and intuitive user experience for dragging, typing, and clicking interactions.
  • Accessibility: Thorough implementation of keyboard navigation and ARIA attributes for an inclusive user interface.

Loading editor...