origin/apps/web/src/components/common/container/CollapsibleContent.tsx

72 lines
1.9 KiB
TypeScript
Raw Normal View History

2025-02-23 22:51:19 +08:00
import React, { useEffect, useRef, useState } from "react";
import Quill from "quill";
import "quill/dist/quill.snow.css"; // 引入默认样式
2025-02-21 17:32:25 +08:00
interface CollapsibleContentProps {
content: string;
maxHeight?: number;
}
const CollapsibleContent: React.FC<CollapsibleContentProps> = ({
content,
maxHeight = 150,
}) => {
2025-02-23 22:51:19 +08:00
const contentWrapperRef = useRef(null);
2025-02-21 17:32:25 +08:00
const [isExpanded, setIsExpanded] = useState(false);
2025-02-23 22:51:19 +08:00
const [shouldCollapse, setShouldCollapse] = useState(false);
2025-02-21 17:32:25 +08:00
2025-02-23 22:51:19 +08:00
useEffect(() => {
if (contentWrapperRef.current) {
const shouldCollapse =
contentWrapperRef.current.scrollHeight > maxHeight;
setShouldCollapse(shouldCollapse);
}
}, [content]);
2025-02-21 17:32:25 +08:00
return (
2025-02-23 22:51:19 +08:00
<div className=" text-base ">
<div className=" flex flex-col gap-4 border border-white hover:ring-1 ring-white transition-all duration-300 ease-in-out rounded-xl p-6 ">
{/* 包装整个内容区域的容器 */}
2025-02-21 17:32:25 +08:00
<div
2025-02-23 22:51:19 +08:00
ref={contentWrapperRef}
2025-02-26 10:19:29 +08:00
style={{
maxHeight:
shouldCollapse && !isExpanded
? maxHeight
: undefined,
}}
2025-02-23 22:51:19 +08:00
className={`duration-300 ${
shouldCollapse && !isExpanded
2025-02-26 10:19:29 +08:00
? ` overflow-hidden relative`
2025-02-23 22:51:19 +08:00
: ""
}`}>
{/* 内容区域 */}
<div
className="ql-editor p-0 space-y-1 leading-relaxed"
dangerouslySetInnerHTML={{
__html: content || "",
}}
/>
{/* 渐变遮罩 */}
{shouldCollapse && !isExpanded && (
<div className="absolute bottom-0 left-0 right-0 h-20 bg-gradient-to-t from-white to-transparent" />
)}
</div>
2025-02-21 17:32:25 +08:00
2025-02-23 22:51:19 +08:00
{/* 展开/收起按钮 */}
{shouldCollapse && (
<button
onClick={() => setIsExpanded(!isExpanded)}
className="mt-2 text-blue-500 hover:text-blue-700">
{isExpanded ? "收起" : "展开"}
</button>
2025-02-21 17:32:25 +08:00
)}
2025-02-23 22:51:19 +08:00
{/* PostResources 组件 */}
{/* <PostResources post={post} /> */}
2025-02-21 17:32:25 +08:00
</div>
</div>
);
};
export default CollapsibleContent;