[resumes][feat] add see more/less for overflow comments

pull/363/head
Terence Ho 3 years ago
parent 3c573110f9
commit 701ccce5ae

@ -4,6 +4,8 @@ import {
} from '@heroicons/react/20/solid'; } from '@heroicons/react/20/solid';
import { FaceSmileIcon } from '@heroicons/react/24/outline'; import { FaceSmileIcon } from '@heroicons/react/24/outline';
import CommentDescription from './CommentDescription';
import type { ResumeComment } from '~/types/resume-comments'; import type { ResumeComment } from '~/types/resume-comments';
type CommentBodyProps = { type CommentBodyProps = {
@ -49,7 +51,7 @@ export default function CommentBody({
</div> </div>
{/* Description */} {/* Description */}
<div className="whitespace-pre-wrap text-sm">{comment.description}</div> <CommentDescription>{comment.description}</CommentDescription>
{/* Upvote and edit */} {/* Upvote and edit */}
<div className="flex flex-row space-x-1 pt-1 align-middle"> <div className="flex flex-row space-x-1 pt-1 align-middle">

@ -0,0 +1,48 @@
import clsx from 'clsx';
import type { ReactNode } from 'react';
import { useLayoutEffect, useRef, useState } from 'react';
type CommentDescriptionProps = Readonly<{
children: ReactNode;
}>;
export default function CommentDescription({
children,
}: CommentDescriptionProps) {
const ref = useRef<HTMLSpanElement>(null);
const [descriptionExpanded, setDescriptionExpanded] = useState(false);
const [descriptionOverflow, setDescriptionOverflow] = useState(false);
useLayoutEffect(() => {
if (ref.current && ref.current.clientHeight < ref.current.scrollHeight) {
setDescriptionOverflow(true);
}
}, [ref]);
const onSeeActionClicked = () => {
setDescriptionExpanded(!descriptionExpanded);
};
return (
<>
<span
ref={ref}
className={clsx(
'whitespace-pre-wrap text-sm',
'line-clamp-3',
descriptionExpanded ? 'line-clamp-none' : '',
)}>
{children}
</span>
{descriptionOverflow && (
<div className="flex flex-row">
<div
className="text-xs text-indigo-500 hover:text-indigo-300"
onClick={onSeeActionClicked}>
{descriptionExpanded ? 'See Less' : 'See More'}
</div>
</div>
)}
</>
);
}
Loading…
Cancel
Save