npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

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

  1. Installation
  2. Component Overview
  3. Basic Usage
  4. Advanced Usage
  5. Styling and Customization
  6. Troubleshooting

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:

  1. 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';
  1. 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
    },
  },
];
  1. 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:

  1. Popup not appearing: Ensure the text is actually selectable and that there's enough space for the popup to render.
  2. Styling issues: Check that your CSS is being applied correctly and that it's not being overridden by other styles.
  3. Performance problems: Try reducing the amount of text wrapped by the component, or optimize your action handlers.
  4. 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.