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

react-truncate-line-clamp

v0.1.3

Published

A React component for clamping any content to a specified number of lines with expand/collapse functionality

Downloads

10

Readme

React Line Clamp

A flexible and performant React component for clamping content to a specified number of lines with expand/collapse functionality.

Features

  • 🎯 Precise line clamping - Uses CSS line-clamp for optimal performance
  • 🔄 Expand/Collapse functionality - Built-in toggle with customizable text
  • 🎨 Highly customizable - Custom styles, classes, and button components
  • 📱 Responsive - Automatically recalculates on window resize
  • 🧩 Flexible content - Supports text, JSX, and any React content
  • 🔧 TypeScript support - Full type definitions included
  • Lightweight - Zero dependencies (except React)
  • 🧩 Flexible API - Controlled and uncontrolled modes

Installation

npm install react-truncate-line-clamp

or

yarn add react-truncate-line-clamp

Usage

Basic Example

import LineClamp from 'react-truncate-line-clamp';

function App() {
  return (
    <LineClamp lines={3}>
      This is a very long text that will be clamped to 3 lines. Lorem ipsum dolor sit amet,
      consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
    </LineClamp>
  );
}

With Custom Expand/Collapse Text

<LineClamp
  lines={2}
  expandText="Read more"
  collapseText="Show less"
>
  Your long text here...
</LineClamp>

Without Toggle Button

<LineClamp lines={3} showButton={false}>
  Your long text here...
</LineClamp>

With Custom Styling

<LineClamp
  lines={3}
  className="my-custom-class"
  style={{
    fontSize: '16px',
    lineHeight: '1.5',
    color: '#333'
  }}
>
  Your long text here...
</LineClamp>

With Rich Content

<LineClamp lines={3}>
  <p>This component now supports <strong>rich content</strong>!</p>
  <p>You can include <em>multiple paragraphs</em>, <a href="#">links</a>, and other elements.</p>
  <p>Perfect for blog previews, product descriptions, and more complex layouts.</p>
</LineClamp>

Controlled Mode

import { useState } from 'react';
import LineClamp from 'react-truncate-line-clamp';

function ControlledExample() {
  const [expanded, setExpanded] = useState(false);

  return (
    <LineClamp
      lines={3}
      expanded={expanded}
      onToggle={setExpanded}
    >
      Your long content here...
    </LineClamp>
  );
}

Custom Button Component

const CustomButton = ({ onClick, children }) => (
  <button
    onClick={onClick}
    className="btn btn-primary"
  >
    {children}
  </button>
);

<LineClamp
  lines={3}
  buttonComponent={CustomButton}
>
  Your long content here...
</LineClamp>

API Reference

Props

| Prop | Type | Default | Description | |------|------|---------|-------------| | children | React.ReactNode | - | Required. The content to be clamped | | lines | number | - | Required. Maximum number of lines to display | | className | string | "" | Additional CSS classes | | style | React.CSSProperties | {} | Custom styles | | expandText | string | "Show more" | Text to show when content is clamped | | collapseText | string | "Show less" | Text to show when content is expanded | | showButton | boolean | true | Whether to show expand/collapse buttons | | buttonComponent | React.ComponentType | - | Custom button component | | onToggle | (expanded: boolean) => void | - | Callback when expand/collapse state changes | | expanded | boolean | - | Control the expanded state (controlled mode) |

TypeScript

The component is written in TypeScript and includes full type definitions. The main interface is:

interface LineClampProps {
  children: React.ReactNode;
  lines: number;
  className?: string;
  style?: React.CSSProperties;
  collapseText?: string;
  expandText?: string;
  showButton?: boolean;
  buttonComponent?: React.ComponentType<{
    onClick: () => void;
    children: React.ReactNode;
  }>;
  onToggle?: (expanded: boolean) => void;
  expanded?: boolean;
}

Browser Support

This component uses CSS line-clamp which is supported in:

  • Chrome 6+
  • Firefox 68+
  • Safari 5+
  • Edge 17+

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

MIT © [Your Name]