Coder Social home page Coder Social logo

yuanqing / line-clamp Goto Github PK

View Code? Open in Web Editor NEW
75.0 2.0 12.0 205 KB

:clamp: Line clamp a DOM element in vanilla JavaScript

Home Page: https://codepen.io/lyuanqing/pen/VQQVry

License: MIT License

JavaScript 100.00%
line-clamp truncate ellipsis javascript

line-clamp's Introduction

line-clamp npm Version Build Status Bundle Size

Line clamp a DOM element in vanilla JavaScript

  • Truncates in pure JavaScript; does not rely on -webkit-line-clamp
  • Works even if the given element contains nested DOM nodes
  • Supports appending a custom string instead of an ellipsis ()

Usage

Editable demo (CodePen)

HTML:

<div class="line-clamp">
  Lorem ipsum dolor sit amet, <strong>consectetur adipiscing</strong> elit.
</div>

CSS:

.line-clamp {
  width: 100px;
  line-height: 20px;
}

JavaScript:

const element = document.querySelector('.line-clamp')
lineClamp(element, 3)

Boom:

<div class="line-clamp" style="overflow: hidden; overflow-wrap: break-word; word-wrap: break-word;">
  Lorem ipsum dolor sit amet, <strong>consectetur…</strong>
</div>

Limitations

API

const lineClamp = require('line-clamp')

lineClamp(element, lineCount [, options])

Returns true if text was truncated, else returns false.

options is an optional configuration object.

  • Set options.ellipsis to change the string to be appended to the truncated text. Defaults to .

See Usage.

Installation

$ yarn add line-clamp

Prior art

License

MIT

line-clamp's People

Contributors

dpren avatar yuanqing avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

line-clamp's Issues

Float line-height

For the elements which have line-height written as a float number there is an issue in calculation, due to truncation of decimal part.
e.g. for line-height: 18.5667px
parseInt('18.5667px', 10) // 18
parseFloat('18.5667px') // 18.5667

Suggestion is to update how maximumHeight is calculated:

module.exports = function (rootElement, lineCount, options) {
  ...
  var maximumHeight = Math.round(
      (lineCount || 1) *
      parseFloat(window.getComputedStyle(rootElement).lineHeight))

Use of `getComputedStyle` Results in `maximumHeight` Being NaN

For app frameworks like Angular, HTML elements can be generated and apply effects (like line-clamping) before those HTML elements are actually rendered into the DOM. This means that virtually all values from window.getComputedStyles are empty strings which makes this line of code produce NaN as a value.

In truncateTextNode, the comparison of the text node's scroll height to the maximum height now compares a number (zero) to NaN, which is always false. This never breaks out of the loop and every word in the text node gets deleted. This means that text node content that gets initialized before an HTML element is inserted into the DOM will be deleted completely.

Can lineHeight be added as an option to the exported function so that frameworks can inject expected line heights before components are added to the DOM? Some other alternative could be acceptable, so long as the case of a zero-height element with no computed line-height value can be handled.

module.exports = function (rootElement, lineCount, options) {
  ...
  var maximumHeight =
    (lineCount || 1) * parseInt(window.getComputedStyle(rootElement).lineHeight || options.lineHeight, 10);

Truncates more lines than intended on smaller viewports

I have an issue where any text that apply line-clamp to gets truncated to just one line in smaller (< 968px) viewports even though I've set it the option to three lines. Works fine for viewports > 968px. Any ideas what could be causing this?

I've confirmed that the line-clamp method correctly calculates the line-height of the text element.

Adding via CDN?

Sorry if this sounds kind of stupid, I should know the answer, but how do I go about adding something like this into a non Node application?

Suppose I just had raw HTML page... is there a way I can just download minified version or have some kind of CDN to reference?

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.