|
1 | 1 | import Linkify from 'linkify-react'
|
2 |
| -import { FC, memo } from 'react' |
| 2 | +import { FC, memo, useEffect, useRef, useState } from 'react' |
3 | 3 |
|
4 |
| -export const Paragraphs: FC<{ content?: string; linkify?: boolean }> = memo( |
5 |
| - ({ content, linkify }) => { |
6 |
| - const paragraphs = content?.split('\n').map((el) => el.trim()) |
7 |
| - const child = ( |
8 |
| - <> |
| 4 | +export const Paragraphs: FC<{ |
| 5 | + content?: string |
| 6 | + linkify?: boolean |
| 7 | + limitHeight?: number |
| 8 | +}> = memo(({ content, linkify, limitHeight }) => { |
| 9 | + const paragraphElementRef = useRef<HTMLDivElement>(null) |
| 10 | + const paragraphs = content?.split('\n').map((el) => el.trim()) |
| 11 | + |
| 12 | + // exceededLimitHeight |
| 13 | + const [exceededLimitHeight, setExceededLimitHeight] = useState(false) |
| 14 | + |
| 15 | + useEffect(() => { |
| 16 | + if (!paragraphElementRef.current || !limitHeight) { |
| 17 | + return |
| 18 | + } |
| 19 | + |
| 20 | + const { height } = paragraphElementRef.current.getBoundingClientRect() |
| 21 | + |
| 22 | + setExceededLimitHeight(height > limitHeight) |
| 23 | + }, [paragraphElementRef.current, limitHeight]) |
| 24 | + |
| 25 | + const mask = exceededLimitHeight |
| 26 | + ? 'linear-gradient(to bottom, rgba(0,0,0,1) 0%, rgba(0,0,0,1) calc(100% - 2rem), rgba(0,0,0,0) 100%)' |
| 27 | + : 'none' |
| 28 | + |
| 29 | + const child = ( |
| 30 | + <div |
| 31 | + className="text-gray-700 leading-normal" |
| 32 | + style={{ |
| 33 | + maxHeight: limitHeight, |
| 34 | + overflow: 'hidden', |
| 35 | + mask, |
| 36 | + WebkitMask: mask, |
| 37 | + background: exceededLimitHeight |
| 38 | + ? 'linear-gradient(to bottom, rgba(0,0,0,0) 0%, rgba(0,0,0,0) calc(100% - 2rem), rgba(0,0,0,0.1) 100%)' |
| 39 | + : 'none', |
| 40 | + }} |
| 41 | + > |
| 42 | + <div ref={paragraphElementRef}> |
9 | 43 | {paragraphs?.map((paragraph, index) => (
|
10 | 44 | <p key={index}>{paragraph}</p>
|
11 | 45 | ))}
|
12 |
| - </> |
13 |
| - ) |
14 |
| - |
15 |
| - return linkify ? ( |
16 |
| - <Linkify |
17 |
| - options={{ |
18 |
| - attributes: { |
19 |
| - target: '_blank', |
20 |
| - rel: 'noopener noreferrer', |
21 |
| - }, |
22 |
| - className: 'break-all', |
23 |
| - format: { |
24 |
| - url: (value) => |
25 |
| - value.length > 50 ? value.slice(0, 50) + '…' : value, |
26 |
| - }, |
27 |
| - }} |
28 |
| - > |
29 |
| - {child} |
30 |
| - </Linkify> |
31 |
| - ) : ( |
32 |
| - child |
33 |
| - ) |
34 |
| - }, |
35 |
| -) |
| 46 | + </div> |
| 47 | + </div> |
| 48 | + ) |
| 49 | + |
| 50 | + return linkify ? ( |
| 51 | + <Linkify |
| 52 | + options={{ |
| 53 | + attributes: { |
| 54 | + target: '_blank', |
| 55 | + rel: 'noopener noreferrer', |
| 56 | + }, |
| 57 | + className: 'break-all', |
| 58 | + format: { |
| 59 | + url: (value) => |
| 60 | + value.length > 50 ? value.slice(0, 50) + '…' : value, |
| 61 | + }, |
| 62 | + }} |
| 63 | + > |
| 64 | + {child} |
| 65 | + </Linkify> |
| 66 | + ) : ( |
| 67 | + child |
| 68 | + ) |
| 69 | +}) |
36 | 70 |
|
37 | 71 | Paragraphs.displayName = 'Paragraphs'
|
0 commit comments