Skip to content

vimtor/use-line-clamp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

use-line-clamp

React.js hook to detect if text will clamp in X number of lines. This allows you to use lines instead of character for implementing "show more" behaviour.

See live demo

Demo.mp4

Install

npm install use-line-clamp

pnpm install use-line-clamp

yarn add use-line-clamp

Usage

import { useRef, useState } from "react";
import useLineClamp from "use-line-clamp";

function Example() {
  const ref = useRef<HTMLParagraphElement>(null);
  const clamps = useLineClamp(ref, { lines: 4 });
  const [visible, setVisible] = useState(false);

  return (
    <div>
      <p ref={ref} className={visible ? "" : "line-clamp-4"}>
        Lorem, ipsum dolor sit amet consectetur adipisicing elit. In consectetur
        doloremque molestiae perspiciatis aliquam voluptatem natus quod quia
        repellendus? Vero veniam quidem quae veritatis voluptas ratione
        doloremque. Quo, saepe quidem. Inventore voluptatibus ipsam totam nobis
        magni molestiae ea culpa animi?
      </p>
      {clamps && (
        <button onClick={() => setVisible(!visible)}>
          Show {visible ? "less" : "more"}
        </button>
      )}
    </div>
  );
}

About

React.js hook to detect if text will clamp in X number of lines.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published