npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@miedward/ngx-line-truncation

v2.0.1

Published

Ngx Line Truncation is line truncation implementation for Angular that truncate text by user defined line number.

Downloads

195

Readme

Logo

NGX Line Truncation

Ngx Line Truncation is line truncation implementation for Angular that truncate text by user defined line number. (demo)

In addition to Line Truncation, this package has few performance optimizations not only improved usability but also reliability in Angular platform. It uses retry logic to guarantee we get Client Height text block all the time, which is an essential value of the truncation input. It also watches the dom changes,to catch the case when the text value get applied at a later time.

Feature

  • Tailor made for Angular platform
  • Smart, just declare how many lines you wish to truncate for, no need to provide max-height, line-height
  • Works with nest DOM element
  • Support rich text, maintain original HTML element tags and styles
  • Lightening fast and capable
  • Custom ellipsis character
  • Re-execute on window resize
  • Dynamic content truncation

Installation

To install, simply run

npm install ngx-line-truncation

And import to the module that use truncation

import { LineTruncationLibModule } from 'ngx-line-truncation';

  ...

@NgModule({
imports: [
  ...

LineTruncationLibModule
]
})
export class MyModule { }

if you import this package into a shared module, you need to export LineTruncationDirective

@NgModule({
  imports: [LineTruncationLibModule],
  declarations: [...components],
  exports: [...components, LineTruncationDirective],
  entryComponents: []
})
export class MySharedModule {

How to use

Declare [line-truncation] with div, p, and pass a number that indicates how many lines of text you are expected to truncate

<p [line-truncation]="2">
  Lorem ipsum dolor sit, amet consectetur adipisicing elit. Nesciunt consequatur
  ipsum unde doloremque aliquid hic vitae iure necessitatibus, maiores
  repellendus, quos dignissimos Quis necessitatibus quos voluptas nesciunt
  facere mollitia cupiditate.
</p>
<p [line-truncation]="2" [innerHTML]="myText"></p>
<div [line-truncation]="2">
  Lorem ipsum dolor sit, amet consectetur adipisicing elit. Nesciunt consequatur
  ipsum unde doloremque aliquid hic vitae iure necessitatibus, maiores
  repellendus, quos dignissimos? Quis necessitatibus quos voluptas nesciunt
  facere mollitia cupiditate.
</div>

Optionally, an output function can help to know if the text has been truncate

<p
  [line-truncation]="numOfLines"
  (hasTruncated)="handler($event)"
  [innerHTML]="myText"
></p>

in your component.ts file

export class myComponent implements OnInit {

  hasTruncated = false;
  numberOfLines = 2;

  myText=`Lorem ipsum dolor sit amet consectetur, adipisicing elit. Fuga itaque voluptatibus sequi laborum, consequatur aut nisi.
  Eaque nulla animi qui exercitationem suscipit voluptas cum est dicta, magnam odio et distinctio?`;

  //...

  handler(res: boolean){
    this.hasTruncated = res;
  }

By default, '...' will be added to the end of the truncated body, if you wish to use your desired ellipsis, you can pass an object like this

<p [line-truncation]="numOfLines" [options]="{ellipsis: "🚀"}" (hasTruncated)="handler(booleanValue)" [innerHTML]="myText" [disabled]="disabled"></p>

Known issue:

  • When you specify emoji as ellipsis 🚀, or use rich text( <p [innerHTML]>), the truncation result might ended up with less lines than you desired(e.g. desire 3, but only have 1). I will be looking into this issue in the future, current work around for this issue is say you realize you get 1 line instead 3, you could declare with 5, it will be truncated to 3.

List of Input & Output

@Input("line-truncation") lines = 1; -- Lines that you desire, default to 1

@Input() options: Options = { ellipsis: "\u2026" }; -- Ellipsis Character, default to ...

@Input() set disabled(val: boolean) { this._disabled$.next(val); -- To disable the truncation, default to false }

@Input() watchChanges = false; -- To watch the text change and truncate, default to false

@Output() hasTruncated = new EventEmitter(); -- $event to true if truncation happen (every time)

Update

2022

  • 06-06 For lib version v1.9.1 now support peer dependency Angular ~14
  • 05-25 For lib version v1.8.1 now support peer dependency Angular ~13

2021

2020

2019

  • 12-02 add input watchChanges to provide truncation on dynamic text content

  • 10-27 add input disabled fix an issue when not truncating, hasTruncated is not emitting value

Contact me

If you have more idea about improving this package, feel free to reach me at [email protected]

License

The repository code is open-sourced software licensed under the MIT license.