
Security News
Attackers Are Hunting High-Impact Node.js Maintainers in a Coordinated Social Engineering Campaign
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.
react-shorten
Advanced tools
Quickstart | Examples | Demo | Native Demo
ReadMore component that can be used both in React DOM (Web) and in React Native to truncate text (it also allows showing more or less text).ReadMoreWeb that can be used directly in react-dom, only requiring the length to truncate and the text itself, removing the need to handle the expanded state of the base component.IMPORTANT
The truncation is done traversing the children of the ReadMore component from top to bottom, from start to end, traversing the children of those components until reaching the leaves, that can be either strings or components without children.
This means that the text can be scattered across several components, but must be in the children property of those components (and not in a custom property like text or something else).
All descendants that generate text to be displayed should be able to be reached traversing only the children props of the descendants of the ReadMore component.
The descendants of the ReadMore component can have other props other than children as long as they don't display text.
npm install react-shorten
The next examples will use the following styled ReadMore web component:
import { ReadMoreWeb } from 'react-shorten';
import React from 'react';
const StyledReadMore: React.FC<{
truncate?: number;
children: React.ReactNode;
}> = ({ truncate, children }) => (
<ReadMoreWeb
truncate={truncate}
showMoreText="Show more"
showLessText="Show less"
className="read-more-btn"
>
{children}
</ReadMoreWeb>
);
and the following css:
.read-more-btn {
display: inline;
padding: 0;
margin: 0;
font-size: 1rem;
color: rgb(80, 154, 223);
background: none;
border: none;
cursor: pointer;
}
.link {
color: rgb(26, 210, 243);
}
const SmallText = () => (
<StyledReadMore truncate={110}>Lorem ipsum dolor sit amet.</StyledReadMore>
);
ReadMoreWeb component, but can be done differently if using the ReadMore base component directly), and a Show more button styled as a text that expands the text when clicked. After the text is expanded, it shows a Show less button to collapse the text again.const LargeText = () => (
<StyledReadMore truncate={110}>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed
ullamcorper, odio eu aliquam ultricies, enim sapien aliquet arcu, quis
aliquam diam massa eu nisl. Sed vitae nunc eget nunc ullamcorper
aliquet. Sed euismod, nisl eget aliquam ultricies, justo nisl aliquet
nunc, quis aliquam diam massa eu nisl. Sed vitae nunc eget nunc
ullamcorper aliquet. Sed euismod, nisl eget aliquam ultricies, justo
nisl aliquet nunc, quis aliquam diam massa eu nisl. Sed vitae nunc eget
nunc ullamcorper aliquet. Sed euismod, nisl eget aliquam ultricies,
justo nisl aliquet nunc, quis aliquam diam massa eu nisl. Sed vitae nunc
eget nunc ullamcorper aliquet. Sed euismod, nisl eget aliquam ultricies,
justo nisl aliquet nunc, quis aliquam diam massa eu nisl. Sed vitae nunc
eget nunc ullamcorper aliquet. Sed euismod, nisl eget aliquam ultricies,
justo nisl aliquet nunc, quis aliquam diam massa eu nisl. Sed vitae nunc
eget nunc ullamcorper aliquet. Sed euismod, nisl eget aliquam ultricies,
justo nisl aliquet nunc, quis aliquam diam massa eu nisl. Sed vitae nunc
eget nunc ullamcorper aliquet. Sed euismod, nisl eget aliquam ultricies,
justo nisl aliquet nunc, quis aliquam diam massa eu nisl. Sed vitae nunc
eget nunc ullamcorper aliquet. Sed euismod, nisl eget aliquam ultricies,
justo nisl aliquet nunc, quis aliquam diam massa eu nisl. Sed vitae nunc
eget nunc ullamcorper aliquet. Sed euismod, nisl eget aliquam ultricies,
justo nisl aliquet nunc, quis aliquam diam massa eu nisl.
</StyledReadMore>
);
ReadMore component (as long as the entire text can be reached through the children property of the descendants of the ReadMore component).const HtmlText = () => (
<StyledReadMore truncate={110}>
Lorem ipsum dolor sit amet,{' '}
<i>
<u>
consectetur adipiscing elit. Sed ullamcorper, odio eu aliquam
ultricies, enim sapien aliquet arcu, quis aliquam diam massa eu
nisl. Sed vitae nunc eget nunc ullamcorper aliquet.
</u>
</i>{' '}
Sed euismod, nisl eget aliquam ultricies, justo nisl aliquet nunc, quis
aliquam diam massa eu nisl. Sed vitae nunc eget nunc ullamcorper
aliquet.
</StyledReadMore>
);
const LinkText = () => (
<StyledReadMore truncate={110}>
Lorem ipsum dolor sit amet,{' '}
<a href="http://localhost:3000" className="link">
consectetur adipiscing elit. Sed ullamcorper, odio eu aliquam
ultricies, enim sapien aliquet arcu, quis aliquam diam massa eu
nisl. Sed vitae nunc eget nunc ullamcorper aliquet.
</a>{' '}
Sed euismod, nisl eget aliquam ultricies, justo nisl aliquet nunc, quis
aliquam diam massa eu nisl. Sed vitae nunc eget nunc ullamcorper
aliquet.
</StyledReadMore>
);
You can create a custom web component, instead of using the one provided by this library, if you want more customization.
You only need to make sure to handle the expanded state (this can be done in a simple useState) and (optionally, but recommended) buttons to show more and less content.
The following code is the implementation of the ReadMoreWeb component of this library. It can be used as a reference when implementing a custom component:
import { ReadMore } from 'react-shorten';
import React, { CSSProperties } from 'react';
export interface ReadMoreWebProps {
truncate: number | undefined;
showMoreText?: React.ReactNode;
showLessText?: React.ReactNode;
className?: string;
style?: CSSProperties;
children: React.ReactNode;
}
export const ReadMoreWeb: React.FC<ReadMoreWebProps> = ({
truncate,
showMoreText,
showLessText,
className,
style,
children,
}) => {
const [expanded, setExpanded] = React.useState(false);
const onShowMore = React.useCallback(() => setExpanded(true), []);
const onShowLess = React.useCallback(() => setExpanded(false), []);
return (
<ReadMore
truncate={truncate}
expanded={expanded}
showMore={
<>
{'... '}
<button
onClick={onShowMore}
className={className}
style={style}
>
{showMoreText ?? 'Show more'}
</button>
</>
}
showLess={
<>
{' '}
<button
onClick={onShowLess}
className={className}
style={style}
>
{showLessText ?? 'Show less'}
</button>
</>
}
>
{children}
</ReadMore>
);
};
export default ReadMoreWeb;
This library does not provides a native component (so as to not have react-native as a dependency), but it can be easily created just like the web/html example above, just changing the buttons to native components like Pressable (or even Text) with onPress or another press/tap event handler of your preference.
The following code is the implementation of the ReadMoreNative component in the react-native demo. It can be used as a reference when implementing a native component:
import React from 'react';
import { ReadMore } from 'react-shorten';
import type { StyleProp, TextStyle } from 'react-native';
import { Text } from 'react-native';
interface ReadMoreNativeProps {
truncate: number | undefined;
showMoreText?: React.ReactNode;
showLessText?: React.ReactNode;
style?: StyleProp<TextStyle>;
btnStyle?: StyleProp<TextStyle>;
children: React.ReactNode;
}
const ReadMoreNative: React.FC<ReadMoreNativeProps> = ({
truncate,
showMoreText,
showLessText,
style,
btnStyle,
children,
}) => {
const [expanded, setExpanded] = React.useState(false);
const onShowMore = React.useCallback(() => setExpanded(true), []);
const onShowLess = React.useCallback(() => setExpanded(false), []);
return (
<ReadMore
truncate={truncate}
expanded={expanded}
showMore={
<Text style={style}>
{'... '}
<Text onPress={onShowMore} style={btnStyle}>
{showMoreText ?? 'Show more'}
</Text>
</Text>
}
showLess={
<Text style={style}>
{' '}
<Text onPress={onShowLess} style={btnStyle}>
{showLessText ?? 'Show less'}
</Text>
</Text>
}
>
{children}
</ReadMore>
);
};
export default ReadMoreNative;
You can define the text to show after a truncated text inside the last tag (instead of outside it) with the endTruncate property.
For example, if the truncated text ends with a link, and you define endTruncate="...", the truncated link will end with ... as part of the link, instead of outside it.
See section Truncated text with content inside the last tag in the demo.
You can see a live web (react-dom) demo here and a react-native demo here.
FAQs
React Read More base and web (HTML) component
The npm package react-shorten receives a total of 866 weekly downloads. As such, react-shorten popularity was classified as not popular.
We found that react-shorten demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.