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

@forbesmedia/multiclamp

v2.1.0

Published

A small, performant utility for truncating multiple lines of text in a single DOM element.

Downloads

21

Readme

multiclamp

A small, performant utility for truncating multiple lines of text in a single DOM element.

Usage

Install via npm: npm install --save @forbesmedia/multiclamp

Import the multiclamp function and pass the element that contains the text that should be truncated. Passing in a nested element may result in undesired behavior, the element passed should contain only text. If no element is provided the function will do nothing. The second argument is the number of lines to clamp the text to. If no number of lines is provided it will default to 2. The third argument is boolean for whether or not to recalculate the clampped text when the element resizes. This may cause the page to crash when a large number of elements are resized. The default value for the resize parameter is false if not provided.

import multiclamp from '@forbesmedia/multiclamp';

window.addEventListener('load', () => {
    const el = document.querySelector('#clamp-this');
    multiclamp(el, 3, true);
});

If you don't have access to import you can include a minified version of the package as a script tag which will attach the multiclamp function to the window making it globally accessable. Ensure this script tag is included before any other javascript that will use the multiclamp function.

<script type="text/javascript" src="node_modules/@forbesmedia/multiclamp/dist/multiclamp.min.js"></script>

Requirements

The element provided must be the DOM node containing the text to be truncated (no support for nested elements). Also, the styling for the element must include the following:

  • display - either block or inline-block
  • line-height - provide a value in pixels
  • max-height OR height - should equal the value for line-height * number of lines
  • overflow: hidden;

Dev Requirements

Development

To install dependencies: npm install

To watch code changes: npm run start

Open the index.html file in browser. Making changes to the source files will trigger a build. Reload the page to see changes.

Additional Information

The function will attempt to use the css property -webkit-line-clamp if supported by the browser. If not it will fall back to a javascript implementation that relies on the HTML canvas api for calculating text widths and requestAnimationFrame() to recalculate when the element resizes, both of which are widely supported by most browsers and relatively performant compared to other javascript implementations that rely on multiple re-layouts of the page. For details on browser support see Canvas browser support and requestAnimationFrame() browser support.

Contributors