Frontend School

Basic Code Editor with Syntax Highlighting

Machine Coding
medium

Basic Code Editor with Syntax Highlighting

Medium
Frontend
React
Component
Editor
Syntax Highlighting
DOM Manipulation

Overview

In many developer-centric applications, such as internal tools, custom script editors, or interactive consoles, having an embedded code editor is crucial. It enhances user experience by providing a familiar and readable interface for writing and reviewing code. This problem challenges you to build a foundational CodeEditor component that incorporates essential features like line numbers and basic syntax highlighting, ensuring a pleasant and functional editing environment.

What you need to do / Task

Your task is to implement a CodeEditor React component with the following capabilities:

  1. Line Numbers: Display sequential line numbers alongside the code content.
  2. Editable Content: The code area must be fully editable, allowing users to type, delete, and modify code.
  3. Basic Syntax Highlighting: Implement custom syntax highlighting for a predefined set of JavaScript keywords (e.g., const, let, var, function, return, if, else, for, while, class, import, export).
  4. Scroll Synchronization: Ensure that the line numbers and the syntax-highlighted code content scroll synchronously with the editable area.
  5. Controlled Component: The CodeEditor component should accept a value prop (string) for its content and an onChange prop (function) to notify the parent component of content changes.

UI/UX expectations

  • Layout: The editor should present two main vertical sections: line numbers on the left and the code editing/display area on the right.
  • Font: Use a fixed-width (monospace) font for the code content to ensure consistent character alignment.
  • Highlighting: Keywords should be visually distinct (e.g., a specific color) from regular code text.
  • Responsiveness: The editor should handle varying code lengths gracefully, allowing vertical and horizontal scrolling as needed.
  • Interactivity: Typing, backspacing, selecting text, and navigating with arrow keys should work as expected in a standard text editor.

Acceptance criteria

  • The CodeEditor component correctly renders the initial code provided via the value prop.
  • Line numbers are accurately displayed and update dynamically as new lines are added or removed.
  • The editor's content is fully editable, and changes are propagated via the onChange callback.
  • Specified JavaScript keywords are highlighted with a distinct style (e.g., color).
  • Vertical and horizontal scrolling of the code content correctly synchronizes the line numbers and the highlighted text overlay.
  • The component's styling ensures a clean, readable, and functional editor appearance.

Constraints

Constraints & assumptions

  • You must use React (functional components) and plain CSS for styling.
  • No external syntax highlighting libraries (e.g., Prism.js, Ace Editor, Monaco Editor) are allowed. The core challenge is to implement this logic yourself.
  • Focus on basic JavaScript keyword highlighting only. You do not need to implement complex parsing for strings, comments, numbers, or full language grammar.
  • Assume a maximum code length of approximately 500 lines for performance considerations within the given time budget.
  • The editor should operate as a controlled component, managing its value and onChange props.

Out of scope

  • Advanced editor features like auto-completion, linting, multi-cursor support, undo/redo history, or drag-and-drop functionality.
  • Persistence of code content (saving/loading from a backend).
  • Extensive accessibility features beyond basic keyboard input.

What interviewers evaluate

  • React Component Design: How well the CodeEditor component is structured, its state management, and prop handling.
  • Custom Logic Implementation: The ingenuity and correctness of the custom syntax highlighting and scroll synchronization logic.
  • Code Quality: Readability, maintainability, and adherence to best practices for React and JavaScript.
  • UI/UX Fidelity: How closely the implemented editor matches the specified visual and interactive requirements.

Loading editor...