• This repository has been archived on 28/Oct/2020
  • Stars
    star
    2,110
  • Rank 21,744 (Top 0.5 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created almost 8 years ago
  • Updated almost 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

💈 Shave is a 0 dep JS plugin that truncates text to fit within an element based on a set max-height ✁

⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️

This software is maintained under a new repository located at yowainwright/shave

⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️


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('selector', maxheight, { 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.

More Repositories

1

stickybits

Stickybits is a lightweight alternative to `position: sticky` polyfills 🍬
JavaScript
2,188
star
2

postmate

📭 A powerful, simple, promise-based postMessage library.
JavaScript
1,842
star
3

reframe.js

🖼 Reframe unresponsive elements responsively.
JavaScript
1,598
star
4

scrolldir

0 dependency JS plugin to leverage scroll direction with CSS ⬆⬇ 🔌💉
JavaScript
663
star
5

cloudworker

Run Cloudflare Worker scripts locally
JavaScript
518
star
6

es-check

Checks the version of ES in JavaScript files with simple shell commands 🏆
JavaScript
460
star
7

ImageButter

Makes dealing with images buttery smooth.
C
394
star
8

furan

Scale out Docker builds
Go
344
star
9

study

A simple, progressive, client/server AB testing library 📚
JavaScript
312
star
10

polymerase

A tool for populating templates with environment variables and Vault values
Go
85
star
11

react-passage

Link and Redirect to routes safely in your react applications 🌄
JavaScript
58
star
12

package-diff

Diffs the packages used between two node_modules folders
JavaScript
57
star
13

ex_cluster

Clustered Elixir OTP application on Kubernetes with Horde and LibCluster
Elixir
52
star
14

ember-responds-to

Simple mixins for browser event handling.
JavaScript
41
star
15

fastboot-docker

[DEPRECATED] Ember FastBoot App Server in a box.
JavaScript
34
star
16

e2e

Make End-to-End Testing Great For Once
JavaScript
32
star
17

line

An easy to use golang package for stylizing terminal output
Go
27
star
18

ember-route-layers

Wire up your cancel buttons in easy mode.
JavaScript
26
star
19

ember-uni-form

Powerful forms without the confusion.
JavaScript
23
star
20

dynamo-drift

Go
23
star
21

terraform-provider-nrs

A Terraform provider for New Relic Synthetics
Go
22
star
22

monitor

A remote uptime monitoring framework for running monitors as a CRON job
JavaScript
21
star
23

vault-dev-docker

Vault docker image for local development
Shell
16
star
24

s3-uploader

Concurrent streaming upload to Amazon S3
Go
16
star
25

guardian

Go
13
star
26

ember-cli-anybar

A non-intrusive build notification system built atop AnyBar.
JavaScript
12
star
27

golang-protobuf-base-docker

Shell
9
star
28

psst

A secret sharing tool
Go
9
star
29

runtype

Runtype converts Typescript type aliases, interfaces, and enums to Javascript that can be used during runtime
JavaScript
9
star
30

vaultenvporter-go

A tool for turning a set of Vault secrets into environment variables
Go
8
star
31

talcum

Talcum allows members of a distributed system to auto-configure themselves 👥
Go
8
star
32

harmless-changes

Ignore unnecessary build steps if changes are harmless to make builds faster 🏎 💨
Shell
7
star
33

node-auto-repair-operator

A Kubernetes operator that can repair problematic nodes (under development)
Go
6
star
34

eslint-config-dollarshaveclub

Base eslint configs for Dollar Shave Club.
JavaScript
5
star
35

dependents

Shows package dependency versions in specified repositories
JavaScript
5
star
36

new-relic-synthetics-go

A New Relic Synthetics API client for Go
Go
5
star
37

pvc

Applications secrets access library
Go
4
star
38

Swift-WebP

Easy WebP usage in your iOS app!
C
4
star
39

ember-link-after-build

Symlink a folder in lieu of copying files for faster build times
JavaScript
4
star
40

fastboot-cluster-node-cache

A FastBoot app server cache built atop cluster-node-cache
JavaScript
3
star
41

jobmanager

Go
3
star
42

crudite

Go
3
star
43

go-productionize

A set of libraries that will help Go services be more production ready
Go
3
star
44

thermite

Removes old Amazon Elastic Container Registry images that are not deployed in a Kubernetes cluster
Go
2
star
45

go-lib

Go
2
star
46

sysctl-write-docker

Go
2
star
47

ember-shave

A simple wrapper over DSC's super fast and simple text truncation library called shave.
JavaScript
2
star
48

redis-resp

Go
2
star
49

ember-qualtrics

Ember Qualtrics Site Intercept addon.
JavaScript
1
star
50

eslint-plugin-dollarshaveclub

Linting code to shave the world.
JavaScript
1
star
51

ember-preapp-adapter

Request a payload before your Ember app has loaded.
JavaScript
1
star
52

go-for-newbs

Sample applications for people learning Go
Go
1
star
53

vault-shared-users

Vault Shared Users for securely allowing access to robot accounts across the organization
Go
1
star