Coder Social home page Coder Social logo

delktsai / shave Goto Github PK

View Code? Open in Web Editor NEW

This project forked from dollarshaveclub/shave

0.0 2.0 0.0 30.34 MB

๐Ÿ’ˆ Shave is a 0 dep JS plugin that truncates text to fit within an element based on a set max-height โœ

Home Page: https://dollarshaveclub.github.io/shave/

License: MIT License

JavaScript 83.69% HTML 16.31%

shave's Introduction

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 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 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 but for multiple lines.

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.

shave's People

Contributors

yowainwright avatar pwfisher avatar greenkeeper[bot] avatar briangonzalez avatar jakiestfu avatar awcross avatar demiazz avatar pvnr0082t avatar garygreen avatar jackrugile avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.