Skip to content

Commit 70237f9

Browse files
authored
feat(Truncate): improve truncate (#2305)
1 parent 924160d commit 70237f9

File tree

13 files changed

+84
-4
lines changed

13 files changed

+84
-4
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { Truncate } from "@mittwald/flow-react-components";
2+
3+
<Truncate offset={9} ellipsis="[...]">
4+
https://mittwald.github.io/flow/04-components/lorem/ipsum/dolor/sit/amet/consetetur/sadipscing/elitr/sed/diam/nonumy/eirmod/tempor/invidunt/ut/labore/et/dolore/content/truncate/overview
5+
</Truncate>;

apps/docs/src/content/04-components/content/truncate/overview.mdx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,15 @@ darzustellen.
1616

1717
<LiveCodeEditor />
1818

19+
# Mit Offset
20+
21+
Um bei Pfaden sowohl den Anfang als auch das Ende sichtbar zu halten, kann über
22+
die Property `offset` festgelegt werden, wie viele Zeichen vom Ende angezeigt
23+
werden sollen. Zusätzlich lässt sich mit `ellipsis` ein individueller
24+
Platzhalter definieren.
25+
26+
<LiveCodeEditor example="offset" editorCollapsed />
27+
1928
---
2029

2130
# Kombiniere mit ...

packages/components/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@
9191
"react-markdown": "^10.1.0",
9292
"react-stately": "^3.43.0",
9393
"react-syntax-highlighter": "^16.1.0",
94+
"react-truncate-inside": "^1.0.3",
9495
"recharts": "3.7.0",
9596
"remark-gfm": "^4.0.1",
9697
"remeda": "^2.33.4",
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
.truncate {
22
display: block;
33
overflow: hidden;
4-
text-overflow: ellipsis;
54
white-space: nowrap;
5+
6+
&.ellipsis {
7+
text-overflow: ellipsis;
8+
}
69
}

packages/components/src/components/Truncate/Truncate.tsx

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,39 @@ import type { FC, PropsWithChildren } from "react";
22
import type { PropsWithClassName } from "@/lib/types/props";
33
import clsx from "clsx";
44
import styles from "./Truncate.module.scss";
5+
import type { TruncateProps as ReactTruncateProps } from "react-truncate-inside";
6+
import ReactTruncate from "react-truncate-inside";
7+
import { extractTextFromFirstChild } from "@/lib/react/remote";
8+
import { useIsSSR } from "react-aria";
59

6-
export interface TruncateProps extends PropsWithChildren, PropsWithClassName {
10+
export interface TruncateProps
11+
extends
12+
PropsWithChildren,
13+
PropsWithClassName,
14+
Omit<ReactTruncateProps, "text" | "width"> {
715
title?: string;
816
}
917

1018
/** @flr-generate all */
1119
export const Truncate: FC<TruncateProps> = (props) => {
12-
const { children, className, title } = props;
20+
const { children, className, title, offset, ellipsis } = props;
1321

1422
const rootClassName = clsx(styles.truncate, className);
1523

24+
const isSsr = useIsSSR();
25+
26+
if (!isSsr && (offset || ellipsis)) {
27+
const text = extractTextFromFirstChild(children) ?? "";
28+
29+
return (
30+
<span title={title} className={rootClassName}>
31+
<ReactTruncate offset={offset} ellipsis={ellipsis} text={text} />
32+
</span>
33+
);
34+
}
35+
1636
return (
17-
<span title={title} className={rootClassName}>
37+
<span title={title} className={clsx(rootClassName, styles.ellipsis)}>
1838
{children}
1939
</span>
2040
);

packages/components/src/components/Truncate/stories/Default.stories.tsx

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import React from "react";
44
import { dummyText } from "@/lib/dev/dummyText";
55
import { Heading } from "@/components/Heading";
66
import { Text } from "@/components/Text";
7+
import { ColumnLayout } from "@/components/ColumnLayout";
78

89
const meta: Meta<typeof Truncate> = {
910
title: "Content/Truncate",
@@ -30,3 +31,21 @@ export const InHeading: Story = {
3031
</Heading>
3132
),
3233
};
34+
35+
export const Offset: Story = {
36+
args: { offset: 10 },
37+
render: (props) => (
38+
<Text>
39+
<Truncate {...props}>{dummyText.long}</Truncate>
40+
</Text>
41+
),
42+
};
43+
44+
export const CustomEllipsis: Story = {
45+
args: { ellipsis: "[...]" },
46+
render: (props) => (
47+
<ColumnLayout>
48+
<Truncate {...props}>{dummyText.long}</Truncate>
49+
</ColumnLayout>
50+
),
51+
};

packages/remote-elements/src/auto-generated/RemoteTruncateElement.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ export class RemoteTruncateElement extends FlowRemoteElement<RemoteTruncateEleme
1212
static override get remoteProperties() {
1313
return {
1414
className: {},
15+
ellipsis: {},
16+
offset: {},
1517
title: {},
1618
};
1719
}

packages/remote-react-components/src/tests/visual/Truncate.browser.test.tsx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,15 @@ test.each(testEnvironments)(
3535
Lorem ipsum dolor sit amet.
3636
</Truncate>
3737
</Heading>
38+
<Text>
39+
<Truncate offset={10} ellipsis="[...]">
40+
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam
41+
nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam
42+
erat, sed diam voluptua. At vero eos et accusam et justo duo dolores
43+
et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est
44+
Lorem ipsum dolor sit amet.
45+
</Truncate>
46+
</Text>
3847
</Flex>,
3948
);
4049

23.3 KB
Loading
19.6 KB
Loading

0 commit comments

Comments
 (0)