Open this example in the interactive library
Live preview
Source code
// components/TruncatedTextDisclosure.tsx
import { useState } from "react";
export default function TruncatedTextDisclosure({
text = "Studio notes from the harbour",
}: { text?: string } = {}) {
const [full, set] = useState(false);
return (
<p>
{full || text.length <= 20 ? text : text.slice(0, 20) + "…"}{" "}
{text.length > 20 && (
<button aria-expanded={full} onClick={() => set(!full)}>
{full ? "Less" : "More"}
</button>
)}
</p>
);
}
