Frontend School

Simple Rich Text Editor with Formatting Toolbar

Machine Coding
medium

Simple Rich Text Editor with Formatting Toolbar

Medium
React
TypeScript
Frontend
UI
Rich Text Editor

Overview

Many modern web applications require rich text editing capabilities, from composing emails to writing blog posts. A fundamental part of any rich text editor is its toolbar, which provides users with intuitive controls to format their text. This problem challenges you to build a simplified rich text editor focusing on the interaction between user selection, text formatting, and dynamic toolbar button states.

What you need to do / Task

Your task is to implement a basic rich text editor component that allows users to type and format text.

  1. Editor Area: Create a contenteditable div that serves as the main text editing area.
  2. Formatting Toolbar: Implement a toolbar above the editor area with at least three formatting buttons: Bold, Italic, and <ins>Underline</ins>.
  3. Apply Formatting: When text is selected in the editor and a formatting button is clicked, apply the corresponding style to the selected text.
  4. Toolbar State: Dynamically update the visual state of the toolbar buttons (e.g., active/pressed) to reflect the formatting applied to the current text selection. If the selection spans text with mixed formatting, the button should reflect the dominant or first encountered style, or simply not be active if mixed. For simplicity, assume the button should be active if any part of the selection has that formatting.

UI/UX expectations

  • The toolbar should be positioned clearly above the editor area.
  • Formatting buttons should have distinct visual cues (e.g., text labels like "B", "I", "U" or simple icons).
  • When a formatting button is active (i.e., the selected text has that style applied), it should visually indicate its active state (e.g., a different background color, a "pressed" look).
  • The editor area should have a visible border and some padding for a better editing experience.
  • Basic usability: users should be able to type, select, and apply/remove formatting.

Acceptance criteria

  • The editor area allows free-form text input and editing.
  • Clicking the "Bold" button applies/removes bold formatting to the selected text.
  • Clicking the "Italic" button applies/removes italic formatting to the selected text.
  • Clicking the "Underline" button applies/removes underline formatting to the selected text.
  • Multiple formatting styles (e.g., bold and italic) can be applied to the same text selection.
  • Toolbar buttons visually reflect the formatting of the text currently selected in the editor.
  • The component is responsive to user interactions, with no noticeable lag.

Constraints

Constraints & assumptions

  • You should use a contenteditable div for the editor area.
  • For applying and checking formatting, leverage the browser's native document.execCommand API (e.g., document.execCommand('bold', false, null) and document.queryCommandState('bold')).
  • Focus on the core functionality for Bold, Italic, and Underline.
  • The solution should be implemented using React and TypeScript.
  • Assume a modern browser environment (e.g., Chrome, Firefox, Safari).
  • No need for complex undo/redo stacks or advanced rich text features.

Out of scope

  • Saving or loading editor content.
  • Implementing custom parsing or rendering of rich text (e.g., converting to HTML, Markdown, or custom AST).
  • Advanced toolbar features like font selection, text alignment, lists, links, or images.
  • Extensive cross-browser compatibility testing for older browsers.
  • Full accessibility implementation beyond basic button states.

What interviewers evaluate

  • Component Structure: How well the React components are organized, managing state, and handling references.
  • Event Handling: Correct implementation of event listeners for user interactions (button clicks, selection changes).
  • DOM Manipulation: Effective use of document.execCommand and document.queryCommandState to interact with the contenteditable area.
  • UI Responsiveness: The toolbar buttons accurately reflect the selection's formatting in real-time.
  • Code Quality: Readability, maintainability, and adherence to TypeScript best practices.

Loading editor...