Skip to content

Commit f366bb2

Browse files
committed
feat(withEllipsisTypography): add watchResize props
1 parent d964b85 commit f366bb2

File tree

4 files changed

+69
-5
lines changed

4 files changed

+69
-5
lines changed

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,15 @@
22

33
# Changelog
44

5+
## 1.7.8
6+
7+
2026-3-18
8+
9+
### Features
10+
11+
- **EllipsisText**, **EllipsisParagraph**, **EllipsisTitle**, **EllipsisLink**
12+
- ✨ add `watchResize` prop to monitor component size and adjust ellipsis accordingly.
13+
514
## 1.7.7
615

716
2026-3-9

package-lock.json

Lines changed: 25 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@tiny-codes/react-easy",
3-
"version": "1.7.7",
3+
"version": "1.7.8",
44
"description": "Simplify React and AntDesign development with practical components and hooks",
55
"keywords": [
66
"react",
@@ -46,6 +46,7 @@
4646
"crypto-js": "^4.2.0",
4747
"lexical": "^0.33.1",
4848
"react-contexify": "^6.0.0",
49+
"react-resize-detector": "^12.3.0",
4950
"sockjs-client": "^1.6.1"
5051
},
5152
"devDependencies": {

src/components/EllipsisTypography/withEllipsisTypography.tsx

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,17 @@ import type { EllipsisConfig } from 'antd/es/typography/Base';
66
import type { ParagraphProps } from 'antd/es/typography/Paragraph';
77
import type { TextProps } from 'antd/es/typography/Text';
88
import type { TitleProps } from 'antd/es/typography/Title';
9+
import { useResizeDetector } from 'react-resize-detector';
10+
import { useRefFunction, useRefValue } from '../../hooks';
911

1012
function withEllipsisTypography<T extends TextProps | ParagraphProps | TitleProps>(
1113
Component: ComponentType<MakeEllipsisTypographyProps<T>>
1214
) {
1315
return function EllipsisText(props: MakeEllipsisTypographyProps<T>) {
14-
const { ellipsis = true, children, text = children, ...rest } = props;
16+
const { ellipsis = true, children, text = children, watchResize = true, ...rest } = props;
1517
const [isEllipsis, setIsEllipsis] = useState(false);
1618
const [dom, setDom] = useState<HTMLElement | null>(null);
19+
const domRef = useRefValue(dom);
1720
const isAutoEllipsis = useMemo(() => ellipsis === true, [ellipsis]);
1821
const isAutoTooltip = useMemo(() => typeof ellipsis === 'object' && ellipsis.tooltip === true, [ellipsis]);
1922
const isAutoTooltipTitle = useMemo(
@@ -31,10 +34,22 @@ function withEllipsisTypography<T extends TextProps | ParagraphProps | TitleProp
3134
);
3235
const tooltipTitle = useMemo(() => (isEllipsis ? text : undefined), [isEllipsis, text]);
3336

37+
const detectEllipsis = useRefFunction(
38+
() => dom && setIsEllipsis(dom.scrollWidth > dom.clientWidth || dom.scrollHeight > dom.clientHeight)
39+
);
40+
useResizeDetector({
41+
targetRef: watchResize ? domRef : undefined,
42+
onResize: detectEllipsis,
43+
refreshRate: 10,
44+
refreshOptions: {
45+
leading: false,
46+
},
47+
});
48+
3449
useEffect(() => {
3550
if (dom && isAuto) {
3651
Promise.resolve().then(() => {
37-
setIsEllipsis(dom.scrollWidth > dom.clientWidth || dom.scrollHeight > dom.clientHeight);
52+
detectEllipsis();
3853
});
3954
}
4055
}, [text, isAuto, dom]);
@@ -72,8 +87,24 @@ function withEllipsisTypography<T extends TextProps | ParagraphProps | TitleProp
7287
}
7388

7489
export type MakeEllipsisTypographyProps<T> = Omit<T, 'children'> & {
90+
/**
91+
* - **EN:** The text content to display. If not provided, the children will be used.
92+
* - **CN:** 要显示的文本内容。如果未提供,将使用子元素。
93+
*/
7594
text?: string | undefined;
95+
/**
96+
* - **EN:** The children content to display. The `text` prop will take precedence if both are
97+
* provided.
98+
* - **CN:** 要显示的子元素内容。如果同时提供了 `text` 属性,将优先使用 `text`。
99+
*/
76100
children?: string | undefined | null;
101+
/**
102+
* - **EN:** Whether to monitor the component's size and adjust the ellipsis accordingly.
103+
* - **CN:** 是否监控组件的大小并相应地调整省略号。
104+
*
105+
* @default true
106+
*/
107+
watchResize?: boolean;
77108
};
78109

79110
export default withEllipsisTypography;

0 commit comments

Comments
 (0)