GithubHelp home page GithubHelp logo

ramirezcgn / react-lines-ellipsis-observer Goto Github PK

View Code? Open in Web Editor NEW

This project forked from xiaody/react-lines-ellipsis

0.0 0.0 0.0 1.89 MB

Simple multiline ellipsis component for React.JS

Home Page: https://xiaody.github.io/react-lines-ellipsis/

License: MIT License

JavaScript 86.21% CSS 6.19% HTML 7.60%

react-lines-ellipsis-observer's Introduction

Build Status npm version js-standard-style

react-lines-ellipsis-observer

Fork with some improvements of Xiaody multiline ellipsis component for React.JS https://github.com/xiaody/react-lines-ellipsis

Installation

To install the stable version:

npm install --save react-lines-ellipsis-observer

Usage

import LinesEllipsis from 'react-lines-ellipsis-observer'

<LinesEllipsis
  text='long long text'
  maxLine='3'
  ellipsis='...'
  trimRight
  basedOn='letters'
/>

Options

props.text {String}

The text you want to clamp.

props.maxLine {Number|String}

Max count of lines allowed. Default 1.

props.ellipsis {Node}

Text content of the ellipsis. Default โ€ฆ.

props.trimRight {Boolean}

Trim right the clamped text to avoid putting the ellipsis on an empty line. Default true.

props.basedOn {String}

Split by letters or words. By default it uses a guess based on your text.

props.component {String}

The tagName of the rendered node. Default div.

props.onReflow {Function} (version >= 0.13.0)

Callback function invoked when the reflow logic complete.

Type: ({ clamped: boolean, text: string }) => any

  handleReflow = (rleState) => {
    const {
      clamped,
      text,
    } = rleState
    // do sth...
  }

  render() {
    const text = 'lorem text'
    return (
      <LinesEllipsis
        text={text}
        onReflow={this.handleReflow}
        maxLine={3}
      />
    )
  }

Methods

isClamped() {Boolean}

Is the text content clamped.

Limitations

  • not clamps text on the server side or with JavaScript disabled
  • only accepts plain text by default. Use lib/html.js for experimental rich html support
  • can be fooled by some special styles: ::first-letter, ligatures, etc.
  • requires modern browsers env

Experimental html truncation

Instead of props.text, use props.unsafeHTML to pass your content.

Also, props.ellipsis here only supports plain text, use props.ellipsisHTML is to fully customize the ellipsis style.

The props.onReflow gives you html instead of text.

props.trimRight is not supported by HTMLEllipsis.

import HTMLEllipsis from 'react-lines-ellipsis-observer/lib/html'

<HTMLEllipsis
  unsafeHTML='simple html content'
  maxLine='5'
  ellipsis='...'
  basedOn='letters'
/>

Responsive to window resize and orientation change

import LinesEllipsis from 'react-lines-ellipsis-observer'
import responsiveHOC from 'react-lines-ellipsis-observer/lib/responsiveHOC'

const ResponsiveEllipsis = responsiveHOC()(LinesEllipsis)
// then just use ResponsiveEllipsis

Loose version

This is a non-standardized css-based solution for some webkit-based browsers. It may have better render performance but also can be fragile. Be sure to test your use case if you use it. See https://css-tricks.com/line-clampin/#article-header-id-0 for some introduction. Also, you may want to star and follow https://crbug.com/305376.

import LinesEllipsisLoose from 'react-lines-ellipsis-observer/lib/loose'

<LinesEllipsisLoose
  text='long long text'
  maxLine='2'
  lineHeight='16'
/>

react-lines-ellipsis-observer's People

Contributors

xiaody avatar dependabot[bot] avatar ramirezcgn avatar jm5967a avatar vikas-parashar avatar danicase avatar vuryss 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.