Selection Toolbar
Relevant source files
The following files were used as context for generating this wiki page:- src/entrypoints/selection.content/index.tsx
- src/entrypoints/selection.content/selection-toolbar/index.tsx
- src/entrypoints/selection.content/selection-toolbar/translate-button.tsx
- src/entrypoints/selection.content/selection-toolbar/tests/selection-toolbar.test.tsx
- src/entrypoints/popup/components/text-selection-tooltip.tsx
- src/entrypoints/options/pages/floating-button-and-toolbar/selection-toolbar-disabled-sites.tsx
- src/utils/config/tests/example/v022.ts
The Selection Toolbar is a contextual user interface element that appears when a user selects text on a webpage. It provides quick access to Read Frog's core functionalities, such as translation, AI analysis, and text-to-speech, directly within the user's reading flow. This feature is designed to be unobtrusive yet readily available, enhancing the immersive learning experience.
The toolbar's visibility, position, and functionality are managed by a dedicated content script. Its behavior is highly configurable, allowing users to enable or disable it globally or on a per-site basis.
Sources: README.md, src/locales/en.yml:105-106, src/entrypoints/selection.content/selection-toolbar/index.tsx
Architecture
The Selection Toolbar is loaded into web pages via the selection.content entrypoint. The root of this entrypoint renders the <SelectionToolbar /> component, which serves as the orchestrator for the entire feature.
Sources: src/entrypoints/selection.content/index.tsx
Component Structure
The SelectionToolbar component is responsible for managing its state, position, and visibility. It is composed of several child components, each providing a specific action.
This diagram shows the main SelectionToolbar component and its child components.
Sources: src/entrypoints/selection.content/selection-toolbar/index.tsx:179-186
State Management
The toolbar's state is managed using Jotai atoms. This allows for a decoupled state that can be accessed and updated by different components.
| Atom | Description | Source File |
|---|---|---|
isSelectionToolbarVisibleAtom | A boolean atom that controls the visibility of the toolbar. | src/entrypoints/selection.content/selection-toolbar/index.tsx:15 |
selectionContentAtom | A string atom that stores the currently selected text. | src/entrypoints/selection.content/selection-toolbar/index.tsx:16 |
selectionRangeAtom | An atom that stores the Range object of the current selection. | src/entrypoints/selection.content/selection-toolbar/index.tsx:17 |
configFieldsAtomMap.selectionToolbar | An atom that holds the user's configuration for the toolbar. | src/entrypoints/selection.content/selection-toolbar/index.tsx:18 |
Visibility Logic
The toolbar's visibility is controlled by a set of event listeners attached to the document and window. The core logic determines when to show or hide the toolbar based on user interactions like mouse clicks and text selection.
Sources: src/entrypoints/selection.content/selection-toolbar/index.tsx:58-170
Event Handling Flow
The following diagram illustrates the logic flow for showing the toolbar, primarily triggered by the mouseup event.
This flow ensures the toolbar appears only when appropriate, handling edge cases like interactions with input fields.
Sources: src/entrypoints/selection.content/selection-toolbar/index.tsx:58-97, src/entrypoints/selection.content/selection-toolbar/tests/selection-toolbar.test.tsx
Hiding the Toolbar
The toolbar is hidden under the following conditions:
- A
mousedownevent occurs anywhere on the page, unless the drag originates from the toolbar itself. - The
selectionchangeevent fires and the selection is empty. This does not happen if a dropdown menu from the toolbar is open, to prevent accidental dismissal (e.g., on Firefox).
Sources: src/entrypoints/selection.content/selection-toolbar/index.tsx:99-124
Positioning
The toolbar's position is calculated to appear near the user's text selection while remaining within the viewport boundaries.
The updatePosition function is responsible for this logic. It is called by useLayoutEffect for the initial placement and is also attached to the window's scroll event (throttled with requestAnimationFrame) to ensure the toolbar stays in view as the user scrolls.
The position is calculated based on the mouseup event's coordinates and is clamped to stay within a defined MARGIN from the viewport edges. To optimize performance, the position is applied by directly manipulating the component's DOM element style, avoiding unnecessary React re-renders.
Sources: src/entrypoints/selection.content/selection-toolbar/index.tsx:23-55
Configuration
The Selection Toolbar's behavior can be customized through the extension's popup and options page.
Configuration Options
| Option | Type | Description | Source File |
|---|---|---|---|
enabled | boolean | Globally enables or disables the Selection Toolbar. | src/entrypoints/popup/components/text-selection-tooltip.tsx:14-17 |
disabledSelectionToolbarPatterns | string[] | An array of URL patterns for websites where the toolbar should be disabled. | src/entrypoints/options/pages/floating-button-and-toolbar/selection-toolbar-disabled-sites.tsx:9 |
The configuration is stored in the selectionToolbar object within the main application config.
Example configuration from tests:
{
"selectionToolbar": {
"enabled": true
}
}
Sources: src/utils/config/tests/example/v022.ts:241-243
Enabling/Disabling the Toolbar
- Globally: A switch in the extension popup toggles the
enabledflag.
Sources: src/entrypoints/popup/components/text-selection-tooltip.tsx - Per-Site: Users can add URL patterns to a blocklist on the options page. The
SelectionToolbarcomponent checks the current page's URL against these patterns before rendering.
Sources: src/entrypoints/options/pages/floating-button-and-toolbar/selection-toolbar-disabled-sites.tsx, src/entrypoints/selection.content/selection-toolbar/index.tsx:173-176
Toolbar Components
The toolbar itself is a container for several action buttons.
| Component | Description | Source File |
|---|---|---|
AiButton | A button that likely triggers AI-related actions, paired with an AiPopover. | src/entrypoints/selection.content/selection-toolbar/index.tsx:180 |
TranslateButton | A button that initiates translation for the selected text. It is associated with a TranslatePopover. | src/entrypoints/selection.content/selection-toolbar/translate-button.tsx |
SpeakButton | A button to trigger text-to-speech for the selected text. | src/entrypoints/selection.content/selection-toolbar/index.tsx:182 |
CloseButton | A button to manually dismiss the toolbar. | src/entrypoints/selection.content/selection-toolbar/index.tsx:184 |