ExpandableText Components

import { Typography } from “@mui/material”;
import { useEffect, useRef, useState } from “react”;

interface ExpandableTextProps {
text: string;
extendedText: string;
numberOfLine: number;
}

const ExpandableText: React.FC<ExpandableText…


This content originally appeared on DEV Community and was authored by Akash Yadav

import { Typography } from "@mui/material";
import { useEffect, useRef, useState } from "react";

interface ExpandableTextProps {
    text: string;
    extendedText: string;
    numberOfLine: number;
}

const ExpandableText: React.FC<ExpandableTextProps> = ({ text, extendedText = "Description", numberOfLine = 3 }) => {
    const [expanded, setExpanded] = useState(false);
    const [maxHeight, setMaxHeight] = useState<string | number>('none');
    const contentRef = useRef<HTMLDivElement>(null);

    useEffect(() => {
        if (contentRef.current) {
            setMaxHeight(expanded ? contentRef.current.scrollHeight : `${numberOfLine * 1.5}em`);
        }
    }, [expanded, numberOfLine])

    const toggleExpand = () => {
        setExpanded((prev) => !prev);
    };

    return (
        <>
            <div
                ref={contentRef}
                style={{
                    overflow: 'hidden',
                    maxHeight: maxHeight,
                    transition: 'max-height 0.3s ease-in-out',
                }}
            >
                <Typography
                    component="p"
                    sx={{
                        fontSize: 12,
                        fontWeight: 700,
                        letterSpacing: 0.5,
                        display: '-webkit-box',
                        WebkitBoxOrient: 'vertical',
                        WebkitLineClamp: expanded ? 'unset' : numberOfLine,
                        textOverflow: 'ellipsis',
                        mt: 1,
                    }}
                >
                    {text}
                </Typography>
            </div>
            <span style={{ color: "blue", cursor: "pointer" }} onClick={toggleExpand}>
                {expanded ? `Collapse ${extendedText}` : `Expand ${extendedText}`}
            </span>
        </>
    );
};


export default ExpandableText;


This content originally appeared on DEV Community and was authored by Akash Yadav


Print Share Comment Cite Upload Translate Updates
APA

Akash Yadav | Sciencx (2024-07-18T07:53:19+00:00) ExpandableText Components. Retrieved from https://www.scien.cx/2024/07/18/expandabletext-components/

MLA
" » ExpandableText Components." Akash Yadav | Sciencx - Thursday July 18, 2024, https://www.scien.cx/2024/07/18/expandabletext-components/
HARVARD
Akash Yadav | Sciencx Thursday July 18, 2024 » ExpandableText Components., viewed ,<https://www.scien.cx/2024/07/18/expandabletext-components/>
VANCOUVER
Akash Yadav | Sciencx - » ExpandableText Components. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/07/18/expandabletext-components/
CHICAGO
" » ExpandableText Components." Akash Yadav | Sciencx - Accessed . https://www.scien.cx/2024/07/18/expandabletext-components/
IEEE
" » ExpandableText Components." Akash Yadav | Sciencx [Online]. Available: https://www.scien.cx/2024/07/18/expandabletext-components/. [Accessed: ]
rf:citation
» ExpandableText Components | Akash Yadav | Sciencx | https://www.scien.cx/2024/07/18/expandabletext-components/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.