How to implement show more/less button #2226
Replies: 2 comments
|
For anyone still looking for this: in read-only mode ( The three pieces are:
.content--clamped {
display: -webkit-box;
-webkit-line-clamp: 4; /* how many lines to show */
-webkit-box-orient: vertical;
overflow: hidden;
}
const ref = useRef<HTMLDivElement>(null);
const [expanded, setExpanded] = useState(false);
const [overflowing, setOverflowing] = useState(false);
useLayoutEffect(() => {
const el = ref.current;
if (el) setOverflowing(el.scrollHeight > el.clientHeight + 1);
}, []);
// ...
<div ref={ref} className={expanded ? 'content' : 'content content--clamped'}>
<RichTextPlugin contentEditable={<ContentEditable />} ErrorBoundary={LexicalErrorBoundary} />
</div>
{overflowing && (
<button onClick={() => setExpanded(v => !v)}>
{expanded ? 'Show less' : 'Show more'}
</button>
)}If you'd rather clamp by pixel height, swap I put together a minimal, runnable React + Vite example here: npm install && npm run dev |
Uh oh!
There was an error while loading. Please reload this page.
I'm trying to implement a "Show more/less" button when the editor is in
readOnlymode. I'm not exactly sure how to go about doing this since I'm relatively new to this library, any pointers would be appreciated.All reactions