Frontend School

Build a Custom Single-Thumb Range Slider Component

Machine Coding
easy

Build a Custom Single-Thumb Range Slider Component

Easy
Frontend
React
UI
Range Slider
Component

Overview

Sharechat's content discovery heavily relies on robust filtering options to help users find relevant posts. A common requirement for numerical filters, such as age ranges or video durations, is an intuitive range slider. Your task is to implement a basic, custom single-thumb range slider component from scratch in React.

What you need to do / Task

  1. Create a reusable RangeSlider React component that accepts min, max, step, and an initial value as props.
  2. The component should display a single draggable thumb that controls the selected value.
  3. As the thumb is dragged, the selected value should update and be visually reflected (e.g., displaying the current value).
  4. The slider should respect min, max, and step constraints.
  5. Implement an onChange prop that is called with the new value when the slider's value changes.

UI/UX expectations

  • The slider should be a horizontal bar (track).
  • The thumb should be visually distinct and draggable along the track.
  • The current selected value should be displayed clearly, perhaps above or next to the thumb.
  • The track should visually indicate the progress up to the thumb's position (e.g., a different color for the filled portion).
  • Basic styling for clarity and usability is expected.

Acceptance criteria

  • The RangeSlider component renders correctly with initial min, max, step, and value.
  • Dragging the thumb updates the displayed value in real-time.
  • The thumb cannot be dragged beyond the min or max bounds.
  • The onChange callback is triggered with the correct numerical value when the slider's value changes.
  • The slider respects the step property, meaning the value only changes in increments of step.

Constraints

Constraints & assumptions

  • The min, max, step props will always be positive integers, with min < max and step > 0.
  • The initial value will always be between min and max (inclusive).
  • You are expected to build the slider component from scratch using standard browser APIs (e.g., MouseEvent for drag, getBoundingClientRect()) without relying on third-party slider libraries.
  • Focus on mouse/touch interactions for dragging.

Out of scope

  • Multi-thumb sliders.
  • Vertical sliders.
  • Complex accessibility features (e.g., full ARIA roles for screen readers, advanced keyboard navigation beyond basic tab focus).
  • Extensive visual polish beyond basic functional styling.

What interviewers evaluate

  • Correct implementation of component state and prop handling.
  • Effective use of DOM event listeners for drag interactions (mousedown, mousemove, mouseup).
  • Accurate calculation of thumb position and corresponding value based on min, max, and step.
  • Clear and maintainable component structure and styling.

Loading editor...