diff --git a/packages/module/patternfly-docs/content/examples/DeleteModal.md b/packages/module/patternfly-docs/content/examples/DeleteModal/DeleteModal.md similarity index 100% rename from packages/module/patternfly-docs/content/examples/DeleteModal.md rename to packages/module/patternfly-docs/content/examples/DeleteModal/DeleteModal.md diff --git a/packages/module/patternfly-docs/content/examples/DeleteModalBasic.tsx b/packages/module/patternfly-docs/content/examples/DeleteModal/DeleteModalBasic.tsx similarity index 100% rename from packages/module/patternfly-docs/content/examples/DeleteModalBasic.tsx rename to packages/module/patternfly-docs/content/examples/DeleteModal/DeleteModalBasic.tsx diff --git a/packages/module/patternfly-docs/content/examples/TruncatedText/TruncatedText.md b/packages/module/patternfly-docs/content/examples/TruncatedText/TruncatedText.md new file mode 100644 index 0000000..c236fe6 --- /dev/null +++ b/packages/module/patternfly-docs/content/examples/TruncatedText/TruncatedText.md @@ -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" + +``` diff --git a/packages/module/patternfly-docs/content/examples/TruncatedText/TruncatedTextBasic.tsx b/packages/module/patternfly-docs/content/examples/TruncatedText/TruncatedTextBasic.tsx new file mode 100644 index 0000000..939e9b4 --- /dev/null +++ b/packages/module/patternfly-docs/content/examples/TruncatedText/TruncatedTextBasic.tsx @@ -0,0 +1,26 @@ +import React from 'react'; + +import { TruncatedText } from "@patternfly/ai-infra-ui-components"; + +export const TruncatedTextBasic: React.FunctionComponent = () => ( + <> +
+ +
+
+ +
+ + ) diff --git a/packages/module/src/TruncatedText/TruncatedText.tsx b/packages/module/src/TruncatedText/TruncatedText.tsx new file mode 100644 index 0000000..aaffbad --- /dev/null +++ b/packages/module/src/TruncatedText/TruncatedText.tsx @@ -0,0 +1,57 @@ +import React from 'react'; +import { Tooltip } from '@patternfly/react-core'; + +export type TruncatedTextProps = React.HTMLProps & { + /** Maximum number of lines before the text gets truncated */ + maxLines: number; + /** Content to be conditionally truncated */ + content: string; +}; + +export const TruncatedText: React.FunctionComponent = ({ + maxLines, + content, + ...props +}: TruncatedTextProps) => { + const outerElementRef = React.useRef(null); + const textElementRef = React.useRef(null); + const [isTruncated, setIsTruncated] = React.useState(false); + + const updateTruncation = React.useCallback(() => { + if (textElementRef.current && outerElementRef.current) { + setIsTruncated(textElementRef.current.offsetHeight > outerElementRef.current.offsetHeight); + } + }, []); + + const truncateBody = ( + { + props.onMouseEnter?.(e); + updateTruncation(); + }} + onFocus={(e) => { + props.onFocus?.(e); + updateTruncation(); + }} + > + {content} + + ); + + return ( + + ); +}; + diff --git a/packages/module/src/TruncatedText/index.ts b/packages/module/src/TruncatedText/index.ts new file mode 100644 index 0000000..499f12b --- /dev/null +++ b/packages/module/src/TruncatedText/index.ts @@ -0,0 +1 @@ +export * from './TruncatedText'; diff --git a/packages/module/src/index.ts b/packages/module/src/index.ts index 2d52cdf..c1a5b4e 100644 --- a/packages/module/src/index.ts +++ b/packages/module/src/index.ts @@ -1 +1,2 @@ export * from './DeleteModal'; +export * from './TruncatedText';