diff --git a/src/ViewMoreText/README.mdx b/src/ViewMoreText/README.mdx new file mode 100644 index 00000000..bb4fe15d --- /dev/null +++ b/src/ViewMoreText/README.mdx @@ -0,0 +1,36 @@ +--- +name: ViewMoreText +menu: Components +--- + +import {Playground, Props} from 'docz'; + +import InlineList from '../InlineList'; +import ViewMoreText from './'; + +# ViewMoreText + +Truncates a piece of text by a specified amount and displays a "View more" button at the end of it, which will reveal the rest of the text when clicked. + +The labels of the "View more" and "View less" buttons can be customised using the props `viewMoreLabel` and `viewLessLabel`. + +## Examples + + + +
+
+ +
+ +## Props + + diff --git a/src/ViewMoreText/index.js b/src/ViewMoreText/index.js new file mode 100644 index 00000000..842c5380 --- /dev/null +++ b/src/ViewMoreText/index.js @@ -0,0 +1,42 @@ +import React, {Fragment, useState} from 'react'; +import PropTypes from 'prop-types'; + +import truncateText from '../utils/truncateText'; +import TextLink from '../TextLink'; + +function ViewMoreText(props) { + const { + content, + truncateBy = 110, + viewMoreLabel = 'View more', + viewLessLabel = 'View less', + } = props; + + const [showMore, setShowmore] = useState(false); + + function toggleShowMore() { + setShowmore(prevState => !prevState); + } + + const shouldTextBeTruncated = content && content.length > truncateBy; + + return ( + + {truncateText(content, showMore ? null : truncateBy)}{' '} + {shouldTextBeTruncated && ( + + {showMore ? viewLessLabel : viewMoreLabel} + + )} + + ); +} + +ViewMoreText.propTypes = { + content: PropTypes.string.isRequired, + truncateBy: PropTypes.number, + viewMoreLabel: PropTypes.string, + viewLessLabel: PropTypes.string, +}; + +export default ViewMoreText;