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

superclamp

v0.2.3

Published

Cross-browser ellipsis for multi-line texts, optimized for performance. No dependencies.

Downloads

1,310

Readme

Superclamp

Cross-browser ellipsis/truncation on multi-line texts. Optimized for performance, and supports tags inside clamped elements.

It is similar to -webkit-line-clamp, but behaves more consistently and will respect your container dimensions without requiring a line count that you may not always know.

No dependencies. Comes with a jQuery function for ease of use in jQuery projects.

Supports all real browsers and IE11+. We are using this in real-world projects.

Installation

Grab superclamp.js or superclamp.min.js from the dist directory and put it into your project.

For NPM projects, add superclamp to your package.json and import Superclamp from 'superclamp' when needed.

Usage

You want to truncate text to fit inside an element, so define your width/height restrictions on the element, e.g. via CSS. Superclamp will respect an element's padding.

  1. Clamp them when the DOM is ready by calling Superclamp.register with a NodeList.

    Superclamp.register(document.querySelectorAll('.clamp-me'));
  2. Re-clamp when necessary (e.g. on resize)

    window.addEventListener('resize', Superclamp.reclampAll);

You may also re-clamp by triggering a custom event superclamp:update anywhere inside document.

Cleanup

You don't need to do any clean-up yourself when removing DOM nodes.

All relevant data is stored on the DOM nodes themselves. They will remove any superclamp-related data with them.

jQuery bindings

If your project uses jQuery, Superclamp will automatically provide a clamp() function to use on jQuery collections, like so:

$('.clamp-me').clamp();

Development

I am a Ruby developer, so here is my stack:

  1. bundle inside the project directory.
  2. Run guard to watch the CoffeeScript source for changes and compile to JavaScript.
  3. Open test/index.html in a browser. You may want to run ruby -run -ehttpd . -p8000 as a tiny web server.

The ugly parts

We optimized heavily for performance, so code may not be as straight-forward as for a simpler solution. Here are a few things we are doing which increase complexity:

  • Best fit is detected via binary search.
  • We avoid layout thrashing by processing layout-modifying and layout-querying tasks en bloc.
  • Some smart logic to (re-)clamp only when necessary (because it's still expensive).

Tests

  • Testing is currently done by opening test/index.html as a human. Sorry for that.

TO DO

  • Maybe support containers of flexible height (e.g. flexbox children) instead of requiring max-height or height.
  • Get rid of CoffeeScript source.
  • Get rid of Ruby/Guard build stack.
  • Have tests.

Known issues

  • While inline nodes work nicely, block nodes (<p>, <div>, ...) inside the clamped element will cause the ellipsis to be put underneath them. We could/should try putting the ellipsis inside.

Credits

Arne Hartherz from makandra.