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

@gabrielceh/react-truncate

v1.0.5

Published

A lightweight, typed React component for truncating text by number of lines, with expand/collapse support.

Readme

react-truncate

A lightweight, typed React component for truncating text by number of lines, with support for expanding and collapsing content.

Zero external dependencies. Compatible with React 18+.

Installation

npm i @gabrielceh/react-truncate

Basic Usage

import { TextTruncate } from "@gabrielceh/react-truncate";
import "@gabrielceh/react-truncate/style.css";

function App() {
  return (
    <TextTruncate
      text="Lorem ipsum dolor sit amet consectetur adipisicing elit."
      lines={3}
      showMoreLabel="Show more"
    />
  );
}

Props

| Prop | Type | Default | Description | | ------------------- | ------------------------------------ | ----------- | -------------------------------------------------- | | text | string | — | Text to render (required) | | lines | number | 1 | Max lines before truncation | | as | TextElement | "p" | HTML element that renders the text | | className | string | undefined | Custom CSS class | | showMoreLabel | string | undefined | Button text to expand content | | showLessLabel | string | undefined | Button text to collapse content | | labelPosition | ShowPosition | "center" | Alignment of the show more/less button | | labelhasUnderline | boolean | false | Adds underline decoration to the button | | labelColor | 'inherit' \| string | "inherit" | Custom color for the button label | | labelClassName | string | undefined | Custom CSS class for the button | | textTruncateChild | React.ReactNode | undefined | React element rendered at the end of the component |

TextElement

type TextElement =
  | "p" | "span" | "a" | "label" | "strong"
  | "small" | "em" | "b" | "i"
  | "h1" | "h2" | "h3" | "h4" | "h5" | "h6";

ShowPosition

type ShowPosition = 'center' | 'right' | 'left';

Used by the labelPosition prop to control the alignment of the expand/collapse button.

Behavior

Without showMoreLabel

The text is permanently truncated with no indicator.

Lorem ipsum dolor sit...

showMoreLabel only

A button appears with the label text. On click, the text expands and the button disappears. It cannot be collapsed again.

Lorem ipsum dolor sit...
Show more

showMoreLabel + showLessLabel

The component toggles between expanded and collapsed states on click. The button label changes according to the state.

Collapsed:

Lorem ipsum dolor sit...
Show more

Expanded:

Lorem ipsum dolor sit amet consectetur...
Show less

Label Customization

The expand/collapse button supports several customization options for position, style, and appearance.

Position

By default the button is centered. Use labelPosition to align it to the left, center, or right.

<TextTruncate
  text="Long text to truncate..."
  lines={2}
  showMoreLabel="Show more"
  showLessLabel="Show less"
  labelPosition="right"
/>

Underline

Add labelhasUnderline to underline the button text.

<TextTruncate
  text="Long text to truncate..."
  lines={2}
  showMoreLabel="Show more"
  showLessLabel="Show less"
  labelhasUnderline
/>

Custom Color

Use labelColor to change the button text color. Accepts any CSS color string or 'inherit'.

<TextTruncate
  text="Long text to truncate..."
  lines={2}
  showMoreLabel="Show more"
  showLessLabel="Show less"
  labelColor="#e63946"
/>

Custom Class

Use labelClassName to apply your own CSS class to the button.

<TextTruncate
  text="Long text to truncate..."
  lines={2}
  showMoreLabel="Show more"
  showLessLabel="Show less"
  labelClassName="my-custom-button"
/>

Combined Example

All label props can be used together.

<TextTruncate
  text="Lorem ipsum dolor sit amet consectetur adipisicing elit. Quisquam voluptatum, quibusdam, quae, quos quas voluptates quod quia voluptatem exercitationem."
  lines={3}
  showMoreLabel="Show more"
  showLessLabel="Show less"
  labelPosition="right"
  labelhasUnderline
  labelColor="#e63946"
  labelClassName="read-more"
/>

Overflow Detection

The component automatically detects whether the text actually overflows its container by comparing scrollHeight vs clientHeight. The button only appears when there is real truncation, regardless of text length.

Responsive

The component reacts to container resize via ResizeObserver. If the text stops truncating on resize, the button disappears.

Accessibility

The expand and collapse controls are native <button> elements, keyboard accessible (Enter and Space) and compatible with screen readers.

TypeScript Examples

import { TextTruncate } from "@gabrielceh/react-truncate";
import type { TextElement, TextTruncateProps } from "@gabrielceh/react-truncate";
import "@gabrielceh/react-truncate/style.css";

function App() {
  return (
    <>
      <TextTruncate text="Text truncated to 1 line." />

      <TextTruncate text="Text truncated to 3 lines." lines={3} />

      <TextTruncate
        text="Expand once."
        showMoreLabel="Show more"
      />

      <TextTruncate
        text="Toggle between expanded and collapsed."
        showMoreLabel="Show more"
        showLessLabel="Show less"
      />

      <TextTruncate
        as="h3"
        text="Truncated heading."
        lines={2}
        className="custom-title"
        showMoreLabel="Read more"
        showLessLabel="Show less"
        textTruncateChild={<span> — 5 min read</span>}
      />

      <TextTruncate
        text="Customized label with position, underline, and color."
        lines={2}
        showMoreLabel="Show more"
        showLessLabel="Show less"
        labelPosition="right"
        labelhasUnderline
        labelColor="#2563eb"
        labelClassName="custom-label"
      />
    </>
  );
}

Bundle

  • Zero external dependencies
  • Full TypeScript typings
  • Tree-shaking optimized