Skip to main content

Text extender

Big O

React JS​

import { useState } from "react";
import "./styles.css";

export default function App() {
return (
<div className="container">
<div className="box">
<TextExpander>
Space travel is the ultimate adventure! Imagine soaring past the stars
and exploring new worlds. It's the stuff of dreams and science
fiction, but believe it or not, space travel is a real thing. Humans
and robots are constantly venturing out into the cosmos to uncover its
secrets and push the boundaries of what's possible.
</TextExpander>
</div>

<div className="box">
<TextExpander
collapsedNumWords={20}
expandButtonText="Show text"
collapseButtonText="Collapse text"
buttonColor="#ff6622"
>
Space travel requires some seriously amazing technology and
collaboration between countries, private companies, and international
space organizations. And while it's not always easy (or cheap), the
results are out of this world. Think about the first time humans
stepped foot on the moon or when rovers were sent to roam around on
Mars.
</TextExpander>
</div>

<div className="box">
<TextExpander expanded={false}>
Space missions have given us incredible insights into our universe and
have inspired future generations to keep reaching for the stars. Space
travel is a pretty cool thing to think about. Who knows what we'll
discover next!
</TextExpander>
</div>
</div>
);
}

function TextExpander({
collapsedNumWords = 10,
expandButtonText = "Show more",
collapseButtonText = "Show less",
buttonColor = "#1f09cd",
expanded = false,
className,
children,
}) {
const [isExpanded, setIsExpanded] = useState(expanded);

const text =
typeof children === "string" ? children : children.props.children;

const displayText = isExpanded
? text
: text.split(" ").slice(0, collapsedNumWords).join(" ") + "...";

const buttonStyle = {
background: "none",
border: "none",
font: "inherit",
cursor: "pointer",
color: buttonColor,
display: "inline",
padding: "0",
margin: "0",
};

return (
<div className={className}>
<span>{displayText}</span>
<button onClick={() => setIsExpanded((exp) => !exp)} style={buttonStyle}>
{isExpanded ? collapseButtonText : expandButtonText}
</button>
</div>
);
}

React TS​

import { useState } from "react";
import "./styles.css";

export default function App() {
return (
<div className="container">
<div className="box">
<TextExpander>
Space travel is the ultimate adventure! Imagine soaring past the stars
and exploring new worlds. It's the stuff of dreams and science
fiction, but believe it or not, space travel is a real thing. Humans
and robots are constantly venturing out into the cosmos to uncover its
secrets and push the boundaries of what's possible.
</TextExpander>
</div>

<div className="box">
<TextExpander
collapsedNumWords={20}
expandButtonText="Show text"
collapseButtonText="Collapse text"
buttonColor="#ff6622"
>
Space travel requires some seriously amazing technology and
collaboration between countries, private companies, and international
space organizations. And while it's not always easy (or cheap), the
results are out of this world. Think about the first time humans
stepped foot on the moon or when rovers were sent to roam around on
Mars.
</TextExpander>
</div>

<div className="box">
<TextExpander expanded={false}>
Space missions have given us incredible insights into our universe and
have inspired future generations to keep reaching for the stars. Space
travel is a pretty cool thing to think about. Who knows what we'll
discover next!
</TextExpander>
</div>
</div>
);
}

import { useState, ReactNode } from "react";

interface TextExpanderProps {
collapsedNumWords?: number;
expandButtonText?: string;
collapseButtonText?: string;
buttonColor?: string;
expanded?: boolean;
className?: string;
children: ReactNode;
}

export default function TextExpander({
collapsedNumWords = 10,
expandButtonText = "Show more",
collapseButtonText = "Show less",
buttonColor = "#1f09cd",
expanded = false,
className,
children,
}: TextExpanderProps) {
const [isExpanded, setIsExpanded] = useState(expanded);

const text =
typeof children === "string" ? children : children?.toString() || "";

const displayText = isExpanded
? text
: text.split(" ").slice(0, collapsedNumWords).join(" ") + "...";

const buttonStyle = {
background: "none",
border: "none",
font: "inherit",
cursor: "pointer",
color: buttonColor,
display: "inline",
padding: "0",
margin: "0",
};

return (
<div className={className}>
<span>{displayText}</span>
<button onClick={() => setIsExpanded((exp) => !exp)} style={buttonStyle}>
{isExpanded ? collapseButtonText : expandButtonText}
</button>
</div>
);
}