Skip to content

pangrr/react-truncate-middle

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

react hook to truncate text in the middle

I prefer hooks over components because components limit my styling.

Screenshot 2025-06-16 at 9 27 35 AM

use

npm install @pangrr/react-truncate-middle

import useTruncateMiddle from "@pangrr/react-truncate-middle"
import { Tooltip } from "react-tooltip"

function App() {
  const text =
    "this is a very looooooooong text this is a very looooooooong text this is a very looooooooong text"
  const { handleContainer, truncatedText } = useTruncateMiddle(text)

  return (
    <>
      /* use "noWrap" to prevent temporary text wrap when container width
      quickly descreases */
      <div ref={handleContainer} style={{ textWrap: "noWrap" }}>
        {truncatedText}
      </div>
      {truncatedText !== text && <Tooltip>{text}</Tooltip>}
    </>
  )
}

About

react hook to truncate text in the middle

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published