highlight-pop
v1.0.1
Published
The `TextHighlightPopup` component is a simple react component that enhances text interaction in your application. It allows users to select text and interact with it through a customizable popup menu.
Downloads
28
Readme
Text Highlight Popup
The TextHighlightPopup
component is a simple react component that enhances text interaction in your application. It allows users to select text and interact with it through a customizable popup menu.
Table of Contents
Installation
Before using the TextHighlightPopup
component, make sure you have the necessary dependencies installed in your React project:
npm install react @types/react
# or
yarn add react @types/react
Component Overview
The TextHighlightPopup
component is designed to wrap around any content where you want to enable text selection and interaction. It provides a customizable popup that appears when text is selected, offering actions that can be performed on the selected text.
Key features:
- Customizable actions
- Automatic positioning of the popup
- Support for custom payloads
- Accessibility considerations
Basic Usage
To use the TextHighlightPopup
component in its simplest form, follow these steps:
- Import the component and necessary types:
import React from 'react';
import { TextHighlightPopup } from './path-to-component/TextHighlightPopup';
import type { Action } from './path-to-types/types';
- Define your actions:
const actions: Action<string>[] = [
{
label: 'Copy',
icon: <Icon>,
action: (text) => {
navigator.clipboard.writeText(text);
console.log('Text copied:', text);
},
},
{
label: 'Share',
icon: <Icon>,
action: (text) => {
console.log('Sharing:', text);
// Implement your sharing logic here
},
},
];
- Use the component in your JSX:
function MyComponent() {
return (
<TextHighlightPopup actions={actions}>
<p>
This is an example paragraph. Users can select any part of this text,
and a popup will appear with options to copy or share the selected text.
</p>
</TextHighlightPopup>
);
}
In this basic setup, when a user selects text within the paragraph, a popup will appear with "Copy" and "Share" options. Clicking "Copy" will copy the selected text to the clipboard, while "Share" will log the text to the console (you would replace this with your actual sharing logic).
Advanced Usage
Custom Action Payloads
For more complex scenarios, you might want to pass additional data along with the selected text. You can achieve this using the getActionPayload
prop:
interface CustomPayload {
text: string;
selectionStart: number;
selectionEnd: number;
}
const getCustomPayload = (text: string): ActionPayload<CustomPayload> => {
const selection = window.getSelection();
return {
text,
selectionStart: selection?.anchorOffset || 0,
selectionEnd: selection?.focusOffset || text.length,
};
};
const customActions: Action<CustomPayload>[] = [
{
label: 'Analyze',
icon: <AnalyzeIcon />,
action: (payload) => {
console.log(`Analyzing text from position ${payload.selectionStart} to ${payload.selectionEnd}:`, payload.text);
// Implement your text analysis logic here
},
},
];
function AdvancedComponent() {
return (
<TextHighlightPopup
actions={customActions}
getActionPayload={getCustomPayload}
>
<p>This paragraph demonstrates advanced usage with custom payloads.</p>
</TextHighlightPopup>
);
}
Dynamic Action Generation
You can also generate actions dynamically based on the selected text:
const generateDynamicActions = (text: string): Action<string>[] => {
const actions: Action<string>[] = [
{
label: 'Copy',
icon: <CopyIcon />,
action: (text) => navigator.clipboard.writeText(text),
},
];
if (text.split(' ').length > 5) {
actions.push({
label: 'Summarize',
icon: <SummarizeIcon />,
action: (text) => console.log('Summarizing:', text),
});
}
return actions;
};
function DynamicActionsComponent() {
return (
<TextHighlightPopup actions={generateDynamicActions}>
<p>This paragraph will show different actions based on the length of the selected text.</p>
</TextHighlightPopup>
);
}
Styling and Customization
The TextHighlightPopup
component uses CSS classes for styling, which you can customize in your project's CSS file:
You can adjust these styles to match your application's design system.
Troubleshooting
If you encounter issues:
- Popup not appearing: Ensure the text is actually selectable and that there's enough space for the popup to render.
- Styling issues: Check that your CSS is being applied correctly and that it's not being overridden by other styles.
- Performance problems: Try reducing the amount of text wrapped by the component, or optimize your action handlers.
- TypeScript errors: Make sure you're using the correct types for actions and payloads.
Remember to wrap your content with TextHighlightPopup
only where text selection functionality is needed to avoid unnecessary event listeners and potential performance impacts.