Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add TruncateText component #60

Merged
merged 2 commits into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
# Sidenav top-level section
# should be the same for all markdown files
section: AI-infra-ui-components
# Sidenav secondary level section
# should be the same for all markdown files
id: TruncatedText
# Tab (react | react-demos | html | html-demos | design-guidelines | accessibility)
source: react
# If you use typescript, the name of the interface to display props for
# These are found through the sourceProps function provided in patternfly-docs.source.js
propComponents: ['TruncatedText']
---

import { TruncatedText } from "@patternfly/ai-infra-ui-components";

Note: this component documents the API and enhances the [existing TruncatedText](https://github.com/opendatahub-io/odh-dashboard/blob/main/frontend/src/components/TruncatedText.tsx) component from odh-dashboard. It can be imported from [@patternfly/ai-infra-ui-components](https://www.npmjs.com/package/@patternfly/AI-infra-ui-components). Alternatively, it can be used within the odh-dashboard via the import: `~/components/TruncatedText`

### Example

```js file="./TruncatedTextBasic.tsx"

```
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import React from 'react';

import { TruncatedText } from "@patternfly/ai-infra-ui-components";

export const TruncatedTextBasic: React.FunctionComponent = () => (
<>
<div style={{
width: '320px',
height: '100px',
}}>
<TruncatedText
maxLines={2}
content="Vestibulum interdum risus et enim faucibus, sit amet molestie est accumsan. Vestibulum interdum risus et enim faucibus, sit amet molestie est accumsan."
/>
</div>
<div style={{
width: '320px',
height: '100px',
}}>
<TruncatedText
maxLines={3}
content="Vestibulum interdum risus et enim faucibus, sit amet molestie est accumsan. Vestibulum interdum risus et enim faucibus, sit amet molestie est accumsan."
/>
</div>
</>
)
57 changes: 57 additions & 0 deletions packages/module/src/TruncatedText/TruncatedText.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import React from 'react';
import { Tooltip } from '@patternfly/react-core';

export type TruncatedTextProps = React.HTMLProps<HTMLSpanElement> & {
/** Maximum number of lines before the text gets truncated */
maxLines: number;
/** Content to be conditionally truncated */
content: string;
};

export const TruncatedText: React.FunctionComponent<TruncatedTextProps> = ({
maxLines,
content,
...props
}: TruncatedTextProps) => {
const outerElementRef = React.useRef<HTMLElement>(null);
const textElementRef = React.useRef<HTMLElement>(null);
const [isTruncated, setIsTruncated] = React.useState<boolean>(false);

const updateTruncation = React.useCallback(() => {
if (textElementRef.current && outerElementRef.current) {
setIsTruncated(textElementRef.current.offsetHeight > outerElementRef.current.offsetHeight);
}
}, []);

const truncateBody = (
<span
{...props}
style={{
display: '-webkit-box',
WebkitBoxOrient: 'vertical',
overflowWrap: 'anywhere',
overflow: 'hidden',
WebkitLineClamp: maxLines,
...(props.style || {}),
}}
ref={outerElementRef}
onMouseEnter={(e) => {
props.onMouseEnter?.(e);
updateTruncation();
}}
onFocus={(e) => {
props.onFocus?.(e);
updateTruncation();
}}
>
<span ref={textElementRef}>{content}</span>
</span>
);

return (
<Tooltip hidden={!isTruncated ? true : undefined} content={content}>
{truncateBody}
</Tooltip>
);
};

1 change: 1 addition & 0 deletions packages/module/src/TruncatedText/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './TruncatedText';
1 change: 1 addition & 0 deletions packages/module/src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './DeleteModal';
export * from './TruncatedText';