JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 12
  • Score
    100M100P100Q41287F
  • License MIT

Universal library that makes a lot of text truncated in a small parent container.

Package Exports

  • text-truncator

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (text-truncator) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Text-truncator 1.2.5v

Universal library that makes a lot of text truncated in a small parent container.

text-truncator listened by resize event for applied every time when page is resized. You can turn this option off by provide in parameter options boolean property once is true.

MIT License

Attention!

Version before 1.0.11 didn't work correctly. Please update it.

Installation

npm install text-truncator

Usage

import truncator from 'text-truncator'

...

const stopTruncator = truncator({
  sourceNode: "h3", // Required! Use CSS selectors (string) or HTML element
  sourceAncestor: ".parentDiv", // Use CSS selectors (string) or HTML element. By default it uses "body"
  ending: "read mode...",  // Use CSS selectors (string) or HTML element. By default it uses ...
  options: {
    maxLength: 700, // The maximum number of letters that we want to be shown before truncate. By default it uses Infinity
    minCutLength: 100, // The maximum number of letters after which the text completely disappears. By default it uses 0
    delay: 250, // Time (in millisec) to delay animation of truncate. Inside itself truncator uses the throttling function. By default it uses 100
    once: true // Needs to call truncator just one time without adding listener to resize event. By default it equals false
  }
});

To use in SPA it returns closing function which can be called before a component will be removed from the DOM.

...
useEffect(() => {
  ...
  return () => {
    stopTruncator()
    ...
  }
})
...

Ending

If you use a string as the ending character don't use special chars like these < \ ( ) ^ * $ as this can lead to incorrect results.