πŸ’ˆ Shave is a 0 dep JS plugin that truncates text to fit within an element based on a set max-height ✁
Clone or download
greenkeeper[bot] and yowainwright Update rollup to the latest version πŸš€ (#124)
* chore(package): update rollup to version 0.66.0

* chore(package): update lockfile yarn.lock
Latest commit 5af9e24 Sep 16, 2018
Permalink
Failed to load latest commit information.
.circleci Update eslint to the latest version πŸš€ (#97) Jun 24, 2018
.github [version][bug] feature version bump => new config; @setheid fix (#106) Aug 4, 2018
dist [uupdate] adds description in README (#121) Sep 6, 2018
scripts Update eslint to the latest version πŸš€ (#97) Jun 24, 2018
src Fixes bug when shaving the same element multiple times (#105) Aug 4, 2018
tests Fixes bug when shaving the same element multiple times (#105) Aug 4, 2018
types Add types/shave (#111) Aug 10, 2018
.babelrc Update eslint to the latest version πŸš€ (#97) Jun 24, 2018
.editorconfig remove git history Sep 13, 2016
.eslintignore [init] adds circleci; removes travis (#90) Apr 17, 2018
.eslintrc.json Update eslint to the latest version πŸš€ (#97) Jun 24, 2018
.gitignore [update] changes ternary order for `spaces` option (#73) Sep 14, 2017
.npmignore adds src to allow for treeshaking jquery plugin code Nov 18, 2016
.nvmrc Update eslint to the latest version πŸš€ (#97) Jun 24, 2018
CHANGELOG.md [chore] updates changelog (#120) Sep 6, 2018
CODEOWNERS [enhancement] adds CODEOWNERS (#64) Jul 19, 2017
CODE_OF_CONDUCT.md [version][bug] feature version bump => new config; @setheid fix (#106) Aug 4, 2018
LICENSE remove git history Sep 13, 2016
README.md [uupdate] adds description in README (#121) Sep 6, 2018
package.json Update rollup to the latest version πŸš€ (#124) Sep 16, 2018
rollup.config.js Update eslint to the latest version πŸš€ (#97) Jun 24, 2018
tsconfig.json Adds type support (#112) Aug 10, 2018
yarn.lock Update rollup to the latest version πŸš€ (#124) Sep 16, 2018

README.md

Shave


npm Bower Travis Greenkeeper CDNJS Twitter share


Shave ✁

Shave is a zero dependency javascript plugin that truncates multi-line text to fit within an html element based on a set pixel number max-height. It then stores the diff of the original text string in a hidden <span> element following the visible text. This means the original text remains intact!

Shave, compared to other truncation plugins:

  • maintains the original text after truncation.
  • does not require other libraries
  • only requires a selector and a max height
  • is very lightweight; ~1.5kb unminified
  • allows for custom ellipsis strings and class names but doesn't over complicate
  • is fast and capable of truncating text within lots of elements quickly
  • is additive. It will play nice with other javascript libraries and more truncation features can easily be built with it.
  • supports non-spaced languages (Non-ascii).

Installing from a package manager

npm

npm install shave --save

bower

bower install shave --save

yarn

yarn add shave

Usage

Add dist/shave.js to your html

  • Or, dist/jquery.shave.js for jQuery/Zepto as of Shave >= v2.

Or as a module

import shave from 'shave';

Syntax

Basic setup

shave('selector', maxheight);
// shave('.shave-selector', 0) for example

Shave also provided options only to overwrite what it uses.

If you'd like have custom class names and not use .js-shave:

shave('selector', maxheight, {classname: 'classname'});

Or if you'd like to have custom characters (instead of the standard ellipsis):

shave('selector', maxheight, {character: '✁'});

Or both:

shave('selector', maxheight, {classname: 'classname', character: '✁' });

Without spaces:

shave({ spaces: false });

You can also use shave as a jQuery or Zepto plugin. As of Shave >= v2, use dist/jquery.shave.js for jQuery/Zepto.

$('selector').shave(maxheight);

And here's a jQuery/Zepto example with custom options:

$('selector').shave(maxheight, { classname: 'your-css-class', character: '✁'  });

If you're using a non-spaced language, you can support shave by setting an option spaces to false.

$('selector').shave(maxheight, { classname: 'your-css-class', character: '✁', spaces: false });

Examples

Codepen example with plain javascript.

Codepen example with jQuery.

Codepen example with a non-spaced language.

Notes

text-overflow: ellipsis is the way to go when truncating text to a single line. Shave does something very similar to text-overflow: ellipsis but for multiple lines when line-clamp is not supported. Shave bypasses being a line-clamp polyfill by only accepting a max-height number. This keeps shave a fast and light weight utility.

Shave implements a binary search to truncate text in the most optimal way possible.

Shave is meant to truncate text within a selected html element. This means it will overwrite html within an html element with just the text within the selected element.

Here are some super basic examples of shave with window resize and click events. πŸ™Œ

Shave works in all modern browsers and was tested in some not so modern browsers (like Internet Explorer 8) - it works there too. 🍻


Created and maintained by Jeff Wainwright with Dollar Shave Club Engineering.