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 🙏

© 2024 – Pkg Stats / Ryan Hefner

text-truncation

v1.0.5

Published

Truncates the text of multiple elements based on the height of their container

Downloads

15

Readme

text-truncation

text-truncation truncates the copy of multiple elements in a container with a fixed height.

If you, for example, have a teaser element with a headline and some copy, both having a variable length, you usually want to show as much text as possible to avoid whitespace at the bottom of the teaser. text-truncation allows you to do so by always only truncating as few characters as possible. It also works with HTML elements in your truncated copy. That means you can style your copy with, for example, <b> or <i> or use links.

NOTE: text-truncation truncates all children (or elements with a given class name) from last to first. That means that in the given example, first the copy gets truncated and afterwards the headline (if truncating the copy is not enough).

Demo

https://mgrsskls.github.io/text-truncation/

What about CSS?

CSS is only able to truncate text for a single line or recently also for multiple lines, but only for one container element. The given use case is not possible with CSS.

Installation

npm install text-truncation

Options:

  • appendix (default ): The string that is appended at the end of the truncated copy.
  • className (default null): The class name that is used to select your elements that should be truncated. If you omit it, text-truncation will use the direct children of the element, which you passed as the first argument.
  • cutOffLength (default 4): Defines how many characters will be removed at the end to properly append the appendix.

Usage

HTML

<div class="Teaser">
  <div class="TextTruncation">
    <h2 class="TextTruncation-text">[…]</h2>
    <p class="TextTruncation-text">[…]</p>
  </div>
</div>

NOTE: .Teaser needs a fixed height.

CSS

.TextTruncation {
  height: 100%;
  overflow: hidden;
}

NOTE: The .TextTruncation wrapper is only necessary if your containing element (here: .Teaser) has padding. If it does not, you can also add text-overflow: hidden directly to your containing element.

JS

import TextTruncation from "text-truncation";

new TextTruncation(document.querySelector(".TextTruncation"), {
  className: "TextTruncation-text"
});

Troubleshooting

  • Make sure that you initialize TextTruncation after your elements have been rendered completely. Otherwise text-truncation might use wrong dimensions and therefore not work.