Skip to content

Commit

Permalink
fix: Typography symbol height exceed line should also support ellipsis (
Browse files Browse the repository at this point in the history
ant-design#47889)

* fix: Typography with ellipsis of symbol height

* test: update snapshot
  • Loading branch information
zombieJ authored and tanzhenyun committed Mar 29, 2024
1 parent 81d574f commit b8aa920
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 15 deletions.
64 changes: 52 additions & 12 deletions components/typography/Base/Ellipsis.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,10 @@ export default function EllipsisMeasure(props: EllipsisProps) {
// ========================= NeedEllipsis =========================
const needEllipsisRef = React.useRef<MeasureTextRef>(null);

// Measure for `rows-1` height, to avoid operation exceed the line height
const descRowsEllipsisRef = React.useRef<MeasureTextRef>(null);
const symbolRowEllipsisRef = React.useRef<MeasureTextRef>(null);

const [needEllipsis, setNeedEllipsis] = React.useState(STATUS_MEASURE_NONE);
const [ellipsisHeight, setEllipsisHeight] = React.useState(0);

Expand All @@ -166,8 +170,17 @@ export default function EllipsisMeasure(props: EllipsisProps) {
setNeedEllipsis(isOverflow ? STATUS_MEASURE_NEED_ELLIPSIS : STATUS_MEASURE_NO_NEED_ELLIPSIS);
setEllipsisCutIndex(isOverflow ? [0, nodeLen] : null);

// For the accuracy issue, we add 1px to the height
setEllipsisHeight((needEllipsisRef.current?.getHeight() || 0) + 1);
// Get the basic height of ellipsis rows
const baseRowsEllipsisHeight = needEllipsisRef.current?.getHeight() || 0;

// Get the height of `rows - 1` + symbol height
const descRowsEllipsisHeight = rows === 1 ? 0 : descRowsEllipsisRef.current?.getHeight() || 0;
const symbolRowEllipsisHeight = symbolRowEllipsisRef.current?.getHeight() || 0;
const rowsWithEllipsisHeight = descRowsEllipsisHeight + symbolRowEllipsisHeight;

const maxRowsHeight = Math.max(baseRowsEllipsisHeight, rowsWithEllipsisHeight);

setEllipsisHeight(maxRowsHeight + 1);

onEllipsis(isOverflow);
}
Expand Down Expand Up @@ -246,16 +259,43 @@ export default function EllipsisMeasure(props: EllipsisProps) {

{/* Measure if current content is exceed the rows */}
{needEllipsis === STATUS_MEASURE_START && (
<MeasureText
style={{
...measureStyle,
...lineClipStyle,
WebkitLineClamp: rows,
}}
ref={needEllipsisRef}
>
{fullContent}
</MeasureText>
<>
{/** With `rows` */}
<MeasureText
style={{
...measureStyle,
...lineClipStyle,
WebkitLineClamp: rows,
}}
ref={needEllipsisRef}
>
{fullContent}
</MeasureText>

{/** With `rows - 1` */}
<MeasureText
style={{
...measureStyle,
...lineClipStyle,
WebkitLineClamp: rows - 1,
}}
ref={descRowsEllipsisRef}
>
{fullContent}
</MeasureText>

{/** With `rows - 1` */}
<MeasureText
style={{
...measureStyle,
...lineClipStyle,
WebkitLineClamp: 1,
}}
ref={symbolRowEllipsisRef}
>
{children([], true, true)}
</MeasureText>
</>
)}

{/* Real size overflow measure */}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1919,7 +1919,7 @@ Array [
</div>
</div>,
<div
class="ant-typography ant-typography-ellipsis ant-typography-ellipsis-multiple-line"
class="ant-typography ant-typography-ellipsis"
style="width: 300px;"
>
In the process of internal desktop applications development,
Expand Down
11 changes: 9 additions & 2 deletions components/typography/demo/ellipsis-debug.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { useState } from 'react';
import { Slider, Switch, Typography } from 'antd';
import { Button, Slider, Switch, Typography } from 'antd';

const { Text, Paragraph } = Typography;

Expand Down Expand Up @@ -78,7 +78,14 @@ const App: React.FC = () => {
</Text>
</div>

<Typography.Paragraph style={{ width: 300 }} ellipsis={{ rows: 3 }}>
<Typography.Paragraph
style={{ width: 300 }}
ellipsis={{
rows: 3,
expandable: true,
symbol: <Button>Open</Button>,
}}
>
{templateStr.slice(0, 60)}
<span style={{ fontSize: '5em' }}>ANTD</span>
{templateStr.slice(60)}
Expand Down

0 comments on commit b8aa920

Please sign in to comment.