Frontend School

Build a Code Editor Component with Syntax Highlighting

Machine Coding
medium

Build a Code Editor Component with Syntax Highlighting

Medium
React
TypeScript
Frontend
UI

Overview

You are tasked with building a core component for an internal developer tool: a custom Code Editor. This editor will be used by engineers to view and modify code snippets directly within the application, enhancing code review workflows and quick edits. A robust and user-friendly code editor is critical for improving developer productivity and ensuring a smooth experience.

What you need to do / Task

Your goal is to create a CodeEditor React component that displays code, provides line numbers, and applies basic syntax highlighting.

  1. Render Code Content: The component should accept code (string) as a prop and display it in an editable area.
  2. Line Numbers: Display dynamic line numbers alongside the code. These numbers should correspond to the current lines of code and update automatically as the code changes.
  3. Syntax Highlighting: Integrate a basic syntax highlighting solution to visually distinguish keywords, comments, strings, etc. You can use the provided react-syntax-highlighter library for this purpose.
  4. User Input: Allow users to type and paste content into the code area. The displayed code, line numbers, and highlighting should update in real-time.
  5. Scroll Synchronization: Ensure that scrolling in the code display area also scrolls the line number area, and vice-versa, maintaining alignment.

UI/UX expectations

  • The editor should have two main vertical sections: line numbers on the left and the code editing area on the right.
  • Line numbers should be clearly visible and aligned with their corresponding code lines.
  • The code editing area should behave like a standard text editor, allowing text input and selection.
  • Both sections should scroll together horizontally and vertically.
  • A monospace font should be used for the code and line numbers.

Acceptance criteria

  • The CodeEditor component renders with initial code content.
  • Line numbers are correctly displayed and update dynamically based on the number of lines in the code.
  • Typing or pasting new content into the editor updates the displayed code and its line numbers.
  • Syntax highlighting is visibly applied to the code content.
  • Scrolling the code area vertically or horizontally also scrolls the line number area, keeping them aligned.
  • The component is responsive to changes in its code prop.

Constraints

Constraints & assumptions

  • You must use React and TypeScript.
  • Focus on functional correctness and a clean component structure.
  • The input code will always be a plain string.
  • Assume a modern browser environment.
  • You are encouraged to use the react-syntax-highlighter library for syntax highlighting. prismjs is also available as a dependency.

Out of scope

  • Implementing advanced IDE features like undo/redo, multiple cursors, autocompletion, code folding, or linting.
  • Optimizing for extremely large files (e.g., millions of lines).
  • Handling different programming languages beyond a basic generic highlight (e.g., JavaScript/TypeScript).
  • Saving or loading code content from external sources.

What interviewers evaluate

  • Component Design: How well the CodeEditor component is structured, its props, and internal state management.
  • React Proficiency: Effective use of React hooks, event handling, and DOM manipulation for scroll synchronization.
  • Correctness: Accurate display of line numbers, proper handling of user input, and correct integration of syntax highlighting.
  • Code Quality: Readability, maintainability, and appropriate use of TypeScript for type safety.

Loading editor...