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:
- Line Numbers: Display sequential line numbers alongside the code content.
- Editable Content: The code area must be fully editable, allowing users to type, delete, and modify code.
- 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).
- Scroll Synchronization: Ensure that the line numbers and the syntax-highlighted code content scroll synchronously with the editable area.
- 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.