Giter VIP home page Giter VIP logo

vue-line-clamp's Introduction

Vue Line Clamp

npm version npm downloads

A simple, fast and lightweight directive for truncating multi line texts using "cross-browser" CSS strategies.

Install

npm install --save vue-line-clamp
import Vue       from 'vue'
import lineClamp from 'vue-line-clamp'

Vue.use(lineClamp, {
  // plugin options
})

Usage

<p v-line-clamp:20="2">Some long text that needs to be truncated to a fixed number, which is 2 in this case. And if the browser doesn't support `-webkit-line-clamp`, then a line-height of 20px is going to be used in order to truncate this text, thus calculating its max-height.</p>

NOTE: the argument passed to the directive must be a number, and its used as the line-height value for non-webkit browsers, as part of the fallback method. In some upcoming version it may be able to detect this value automatically.

Plugin options

property type default description
importCss Boolean false Set to true in order to import styles into <head> automatically, element.style is used by default
textOverflow String ellipsis Set the value for text-overflow property in modern browsers
fallbackFunc Function defaultFallbackFunc Provide your own default method to handle the truncation strategy on unsupported browsers. Accepts all directive params: element (Node), bindings (Object), lines (Number)

Caveats

  1. Probably there may be problems when loading custom fonts. I've done some tests and couldn't detect any inconsistence so far, so feel free to open an issue and provide code to reproduce any bug or glitch you find.
  2. The fallback method for older browsers won't show up the ellipsis (...) since we can't control the part of the text node that may get "clamped".

Changelog

v1.2.4 - Implemented textOverflow option.

v1.2.1 - Implemented fallbackFunc options, fixed multiple elements clamping on same page.

v1.2 - Lines parameter passed to v-line-clamp is now reactive.

vue-line-clamp's People

Contributors

frondor avatar vitorluizc avatar zevdg 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  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

vue-line-clamp's Issues

IE11 - Object does not support property or method 'assign'

image

Setup a vue project initially using vue-cli

Screen is blank in IE11 and the above error is seen. This happens when importing vue-line-clamp into a component (.vue file) and calling Vue.use(lineClamp, {}) there. However, when importing it into my main.js file (and I'm guess any .js file), there is no issue. For some reason it seems that the polyfill for Object.assign does not get used unless importing into a js file. Could this be an issue with vue-loader or babel-loader?

allow customization of fallback behavior

So I wasn't totally happy with the default fallback behavior of this library. Specifically, I wanted ellipsis. I went ahead and made a local fork that simply replaced these lines

let lineHeight = parseInt(bindings.arg)
if (isNaN(lineHeight)) {
console.warn('line-height argument for vue-line-clamp must be a number (of pixels), falling back to 16px')
lineHeight = 16
}
let maxHeight = lineHeight * lines
el.style.maxHeight = maxHeight ? maxHeight+'px' : ''
el.style.overflowX = 'hidden'
el.style.lineHeight = lineHeight+'px' // to ensure consistency

with

lineClamp(el, { lineCount: lines });

from https://www.npmjs.com/package/line-clamp

This seemed like too disruptive of a change to push upstream. There are several different ways to do do line clamping on non-webkit browsers even without considering word splitting issues. Different projects may have different requirements and I don't want to force my preferences on everyone.

However, if we added a new "fallbackFunc" option, then we can let users override the default behavior with one of their own construction. I went ahead and modified my fork to do this.

Now, I can initialize this library like so

import Vue from 'vue'
import VueLineClamp from 'vue-line-clamp'
import lineClamp from 'line-clamp'

Vue.use(VueLineClamp, {
  fallbackFunc: (el, bindings, lines) => {
    lineClamp(el, { lineCount: lines })
  }
})

and get the behavior I'm looking for.

If you like the idea, I can submit my fork as a PR.

enhancement suggestion: do not modify div on certain inputs (probably undefined and null)

I've worked around this in my code-base, but i was slightly surprised by one last behavior of this lib. I had a div that needed to be clamped in some cases and not clamped in others. I expected that if I passed 0 (or at least null or undefined), that this lib would leave my div unchanged. However, to my surprise, there was no way to prevent this lib from modifying the element. AFAIK, vue doesn't have a way to conditionally add an attribute to an element in a template, so i had to conditionally render 2 different versions of the div. one with this attribute and one without.

TBH, there are tradeoffs either way and i'm not convinced that this suggestion is a good one, but i figured i'd let you know about the pain point.

RTL text displays incorrectly

The component works fine for LTR text, but shows many issues for RTL text, as shown in the examples below.

Example 1: text clamps correctly to 3 lines on LTR text
image

Example 2: RTL text not clamping to 3 lines
image

Example 3: another RTL text is overlapped
image

Do you have any ideas about what's going on? When I start disabling some of the CSS properties added by the component (or when I remove the component at all), the text looks good.
Any help is appreciated.

Problem with custom font Inter UI

Hello, I'm using a custom font called Inter UI and line clamping doesn't work properly.

Screenshot

  • I'm running Vue via webpacker gem with Ruby on Rails
  • I'm loading the font via @import url("https://rsms.me/inter/inter-ui.css"); in my SCSS in Rails asset pipeline
  • I'm using line clamp like this: <p class="post__data__content" v-line-clamp="{ lines: 1, text: post.content }"></p>

Am I doing something wrong, or is there any fix for this behaviour? Thanks

How to programmatically enable the clamping of lines when it reaches the maximum amount of lines

Hi! I just want to ask how to know if the content reaches the maximum line being set.
Let say for example I set the maximum to 5 lines and the text content has only 1 line. I wanted to enable clamping if it reaches more than 5 lines.

The content can be html or text.

  <div v-line-clamp="clampLines" style="word-break:normal!important;">
                        <span v-html="text"></span>
                    </div>
                    <v-divider v-if="clampLines"></v-divider>
                    <v-btn @click="toggleLines" text small block color="primary" class="font-weight-bold">
                        <template v-if="clampLines">Read More</template>
                        <template v-if="!clampLines">Read Less</template>
                    </v-btn>

a little help would be appreciated. Thanks!

Reactive line clamps

Hey man!
You have created an awesome tool, that's exactly what I was looking for, since none of other solutions worked nicely with Vue.js.
But is value of v-line-clamp reactive? If I change the value of variable, that I assign to v-line-clamp, would number of lines in p change accordingly?

Emit event on overflow

Thanks for the library!
I want to add read more button behavior that will only be shown if the overflow occurred.
in order to do that i need some simple indication that the paragraph is collapsed.

Long words don't break lines

Long words don't break lines.

Example

<template>
  <p class="text" v-line-clamp:16="7">asdadsadsadsadsadasdasdasdasdasdasdasadsadsadsadsadasdasdasdasadsadsadsadasdasdasdasdasadsadsadsadasdasdasadsadsadasdasadsadsadsadasdasdasadsadasdasadsadasdasdasadsadasdasadsadsadasdasdasadsadasdasdasadsadsadsasa</p>
</template>

Causes

captura de tela 2018-12-04 as 15 59 25

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.