项目作者: zoltantothcom

项目描述 :
Responsive clamping component with Read more/Read less buttons built for React.
高级语言: JavaScript
项目地址: git://github.com/zoltantothcom/react-clamp-lines.git
创建时间: 2017-07-16T22:00:37Z
项目社区:https://github.com/zoltantothcom/react-clamp-lines

开源协议:The Unlicense

下载


react-clamp-lines

npm Snyk Vulnerabilities for npm package

Responsive and accessible clamping component with «Read more»/«Read less» buttons built for React.

react-clamp-lines

Demo

React Clamp Lines

Install

npm i react-clamp-lines

Features

  • built for React with no dependencies
  • option for «Read more» and «Read less» buttons
  • customizable ellipsis text
  • customizable CSS classes
  • responsive
  • accessible
  • window resize event debounced — and why it’s important

    If your web app uses JavaScript to accomplish taxing tasks, a debounce function is essential to ensuring a given task doesn’t fire so often that it bricks browser performance.

Usage

  1. import React, { Component } from 'react';
  2. import ReactDOM from 'react-dom';
  3. import ClampLines from 'react-clamp-lines';
  4. class App extends Component {
  5. render() {
  6. return (
  7. <ClampLines
  8. text={text_to_clamp}
  9. id="really-unique-id"
  10. lines={4}
  11. ellipsis="..."
  12. moreText="Expand"
  13. lessText="Collapse"
  14. className="custom-class"
  15. innerElement="p"
  16. ></ClampLines>
  17. );
  18. }
  19. }
  20. ReactDOM.render(<App ></App>, document.getElementById('clamp'));

The component and the or «Read more» button always have the clamp-lines and clamp-lines__button CSS classes respectively. In the example above the custom-class will be added to clamp-lines, so the output will be:

  1. <div class="clamp-lines custom-class">
  2. <p
  3. id="clamped-content-really-unique-id"
  4. aria-hidden="true"
  5. >
  6. clamped text here...
  7. </p>
  8. <button
  9. class="clamp-lines__button"
  10. aria-controls="clamped-content-really-unique-id"
  11. aria-expanded="false"
  12. >
  13. Expand
  14. </button>
  15. </div>

API

List of all available props with their default values and description.

  1. <ClampLines
  2. text={String}
  3. id={String}
  4. lines={Number}
  5. ellipsis={String}
  6. buttons={Boolean}
  7. moreText={String}
  8. lessText={String}
  9. className={String}
  10. delay={Number}
  11. stopPropagation={Boolean}
  12. innerElement={String}
  13. ></ClampLines>
prop required type default value description
text * {String} Text you wish to clamp
id * {String} Unique id (used for ARIA props)
lines {Number} 3 Number of visible lines
ellipsis {String} ... Text content for the ellipsis - will appear after the clamped lines
buttons {Boolean} true The «Read more» and «Read less» buttons
moreText {String} Read more «Read more» button value
lessText {String} Read less «Read less» button value
className {String} CSS class names added to component
delay {Number} 300 Milliseconds, the function is waiting before being triggered, after it stops being called
stopPropagation {Boolean} false Prevents the event from bubbling up the DOM tree when clicked on the «Read more» button
innerElement {String} div Custom inner element for clamped text. This MUST be a block level element or styled as one.

License

Anyone is free to copy, modify, publish, use, compile, sell, or distribute this software, either in source code form or as a compiled binary, for any purpose, commercial or non-commercial, and by any means.

See Unlicense for full details.