From e13b395676a5e5bdd17e0561bdb849faecae9c4d Mon Sep 17 00:00:00 2001 From: Mikel Parra Date: Wed, 16 Jun 2021 18:46:46 +0200 Subject: [PATCH] feat: add compound components --- README.md | 69 ++++++---- src/Example.js | 73 ++++------ src/lib/clamp.js | 173 ++++++++++++++---------- src/lib/showLess.js | 25 ++++ src/lib/showMore.js | 25 ++++ src/lib/text.js | 20 +++ src/lib/utils/cloneReactElement.js | 9 ++ src/lib/utils/orchestrateClickEvents.js | 7 + 8 files changed, 253 insertions(+), 148 deletions(-) create mode 100644 src/lib/showLess.js create mode 100644 src/lib/showMore.js create mode 100644 src/lib/text.js create mode 100644 src/lib/utils/cloneReactElement.js create mode 100644 src/lib/utils/orchestrateClickEvents.js diff --git a/README.md b/README.md index c0d3808..80fe446 100644 --- a/README.md +++ b/README.md @@ -10,15 +10,26 @@ React component that uses the css line clamping to truncate given text in specif - 😱 It works both with plain text and HTML/Components - 💥 Integrated show more/less behaviour - 👂 Listens to text and lines changes and responds accordingly -- ⚙️ Easy-to-use component API -- 🌳 Tiny size, only 1.4kb - +- ⚙️ Easy-to-use Composable component API +- 🌳 Tiny size ## Installation ```shell npm install react-multiline-clamp ``` +### The library gives you three composable components to use inside the Clamp component as children + +- Clamp.Text +- Clamp.ShowMore +- Clamp.ShowLess + +
+ +You can compose this three components inside the Clamp in the order you desire, this way you can put the show more/less elements above the text for example. + +
+ ### Basic example ```jsx @@ -27,7 +38,9 @@ import Clamp from 'react-multiline-clamp'; const MyComponent = () => { return ( -

Multiline text

+ +

Multiline text

+
); }; @@ -40,39 +53,35 @@ import Clamp from 'react-multiline-clamp'; const MyComponent = () => { return ( - ( - - )} - showLessElement={({ toggle }) => ( - - menossssss - - )} - > -

Multiline text

-
+ + + + ); }; ``` - ## API -| Name | Type | Default | Description | -| :-------------: | :---------------------: | :---------------------------------: | :---------------------------------------------------------------------------------------------------------------------------------------------: | -| children | Element | | The expected element to which the ellipsis would be applied. It could be plain text or any HTML/Component | -| lines | Number | 2 | The number of lines we want the text to be truncated to | -| maxLines | Number | 8 | The maximum number of lines we want to show after clicking on showMore button | -| withTooltip | Boolean | true | Indicates if we want the text to have a tooltip title | -| withToggle | Boolean | false | Indicates if we want to have the show more/less actions | -| showMoreElement | Element | | Element that triggers the show more action | -| showLessElement | Element | | Element that triggers the show less action | -| onShowMore | (isExpanded) => Boolean | () => {} | A callback function that gets calls every time we click on the show more/less buttons. It returns whether the text is expanded or not (Boolean) | +| Name | Type | Default | Description | +| :-------------: | :---------------------: | :---------------------------------: | :---------------------------------------------------------------------------------------------------------------------------------------------: | +| children | Element | | The expected element to which the ellipsis would be applied. It could be plain text or any HTML/Component | +| lines | Number | 2 | The number of lines we want the text to be truncated to | +| maxLines | Number | 8 | The maximum number of lines we want to show after clicking on showMore button | +| withTooltip | Boolean | true | Indicates if we want the text to have a tooltip title | +| withToggle | Boolean | false | Indicates if we want to have the show more/less actions | +| Clamp.Text | Element | | Component to put the text you want to apply the ellipsis to | +| Clamp.ShowMore | Element | button | Component render the show more element | +| Clamp.ShowLess | Element | button | Component render the show less element | +| onShowMore | (isExpanded) => Boolean | () => {} | A callback function that gets calls every time we click on the show more/less buttons. It returns whether the text is expanded or not (Boolean) | #### [See browser support](https://caniuse.com/#feat=mdn-css_properties_-webkit-line-clamp) diff --git a/src/Example.js b/src/Example.js index fd7965b..a6c7f71 100644 --- a/src/Example.js +++ b/src/Example.js @@ -1,5 +1,5 @@ -import React, { useState } from 'react'; -import Clamp from './lib'; +import React, { useState } from "react"; +import Clamp from "./lib"; const initialText = `Lorem ipsum dolor sit amet consectetur adipisicing elit. Illum perspiciatis ea consequuntur ipsum deleniti placeat @@ -11,49 +11,36 @@ dignissimos dolor facere officia delectus corrupti perferendis laboriosam deserunt nobis, suscipit autem atque?`; function Example() { - const [text, setText] = useState(initialText); - const [lines, setLines] = useState(2); + const [text, setText] = useState(initialText); + const [lines, setLines] = useState(2); - return ( -
-

Multiline clamp example

-
-