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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@blacksheepcode/react-text-highlight

v0.4.0

Published

Storybook here: [https://dwjohnston.github.io/react-text-highlight](https://react-text-highlight.netlify.app/)

Downloads

50

Readme

react-text-highlight

Storybook here: https://dwjohnston.github.io/react-text-highlight

This is a lightweight component that will allow display highlighted spans in your main content, with linked comments/footnotes in the page margin.

The component includes for having multiple components in close proximity stack nicely.

For mobile view, the comments are not displayed until they highlight is clicked.

How to use

Near the top of your application you will need:

  • A gutter container, with a ref attached, where the comments will be inserted.

    • This container needs to be a column flexbox for desktop views
    • Do not use the gap property as the position calculations will not detect them. If you need spacing, add margin to your comment components
    • For mobile, if you want the comments to display on click as a toast, implementation is up to you, in my implementation it is a position:fixed set at the bottom of the screen.
  • The TextHighlightProvider - pass the ref to the comments container to it.


function App() {
	const ref = useRef<HTMLDivElement>(null);
	const toastContainerRef = useRef<HTMLDivElement>(null);
	return (

                {/* This is just layout stuff to get a main content and side bar layout*/
		<div style={{
			display: "flex",
			flexFlow: "row nowrap",
		}}>
			<main
				style={{
					flex: "1 1 auto",
				}}>

				<TextHighlightProvider gutterRef={ref}>
					{props.children}
				</TextHighlightProvider >
			</main>

			{/* 👇 This is the important part
				The container for the comments to sit in, needs to be a column flex box.
			*/}
			<div ref={ref} style={{
				display: "flex",
				flexFlow: "column nowrap",
				flex: "0 0 200px",
			}}></div>
		</div>
	);

}

Now use the highlights anywhere you need:


function MyComponent() {
    <p>I am some text. <TextHighlight comment={
        <p>I am the associated comment.</p>}>
            This text is highlighted
        </TextHighlight>
    </p>
}

Styling

Approach 1 - Use the default components and style with vanilla CSS.

You can use the default components, and style them with the existing stylesheet or copy it and modify it for your tastes.

Include the stylesheet with

import "react-text-highlight/dist/main.css";

Approach 2 - Provide your own components

You can provide your own components by passing them into the context provider:

<TextHighlightProvider
    Highlight={MyHighlightComponent}
    Comment={MyCommentComponent}
>
    
</TextHighlightProvider>

These are the requisite typings:

export type CommentProps = PropsWithChildren<{
    /**
     * Attach this to the `id` attribute of your main element. 
     * 
     * This for a11y purposes so the highlight can be linked with the comment.
     */
    id: string;
    hasHover: boolean;

    isSelected: boolean;
    /**
     * Attach this to click handlers for your component
     * @param hasHover 
     * @returns 
     */
    setSelectedStatus: (isSelected: boolean) => void;
    /**
     * Attach this to onMouseEnter and onMouseLeave event handlers
     * @param hasHover 
     * @returns 
     */
    setHoverStatus: (hasHover: boolean) => void;

    /**
     * This ref needs to be attached to the main element 
     * 
     */
    ref: RefObject<HTMLDivElement | null>;

}>

export type HighlightProps = PropsWithChildren<{
    /**
     * Attach this to the `id` attribute of your main element. 
     * 
     * This for a11y purposes so the highlight can be linked with the comment.
     */
    commentId: string;
    isSelected: boolean;
    hasHover: boolean;
    setSelectedStatus: (isSelected: boolean) => void;
    /**
     * Attach this to onMouseEnter and onMouseLeave event handlers
     * @param hasHover 
     * @returns 
     */
    setHoverStatus: (hasHover: boolean) => void;

    /**
     * This ref needs to be attached to the main element 
     * 
     */
    ref: RefObject<HTMLSpanElement | null>;
}>

Requesting repositioning

By default the comments are repositioned when:

  • A highlight is added or removed
  • The highlights container is resized
  • The comments container is resized

It's possible that your application will contain scenarios where additional repositioning is required.

You can imperatively request repositioning via the hook:

const highlightContext = useTextHighlight(); 
highlightContext.requestRecalculatePositions();

SSR/RSC support

The highlighted span itself will be included in the server side rendering. The comment will not appear until rendering client side.

The reason for this is because this solution uses portals, which don't have particularly good support for server side rendering. Well, actually the problem is more with the ref. Essentially the issue is that we need access to the element that holds the comments, but with regular use of a ref, the element won't be attached to the ref until second render. This is the best discussion I could find of this topic: https://stackoverflow.com/a/68659960