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.
- Editor Area: Create a
contenteditable div that serves as the main text editing area.
- Formatting Toolbar: Implement a toolbar above the editor area with at least three formatting buttons: Bold, Italic, and <ins>Underline</ins>.
- Apply Formatting: When text is selected in the editor and a formatting button is clicked, apply the corresponding style to the selected text.
- 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.