-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(OverflowTooltipText): setup new component
- Loading branch information
1 parent
8736e71
commit fe04d25
Showing
12 changed files
with
222 additions
and
1 deletion.
There are no files selected for viewing
30 changes: 30 additions & 0 deletions
30
...ges/react-components/src/components/OverflowTooltipText/OverflowTooltipText.mdx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { Canvas, ArgTypes, Meta, Title } from '@storybook/blocks'; | ||
|
||
import * as OverflowTooltipTextStories from './OverflowTooltipText.stories'; | ||
|
||
<Meta of={OverflowTooltipTextStories} /> | ||
|
||
<Title>OverflowTooltipText</Title> | ||
|
||
[Intro](#Intro) | [Component API](#ComponentAPI) | ||
|
||
## Intro <a id="Intro" /> | ||
|
||
The OverflowTooltipText component displays a text element with a tooltip that appears when the text overflows its container. | ||
It is typically used to ensure that long or truncated text can still be fully viewed by the user, enhancing readability and providing additional context in space-constrained layouts. | ||
|
||
<Canvas of={OverflowTooltipTextStories.Default} sourceState="none" /> | ||
|
||
#### Example implementation | ||
|
||
```jsx | ||
<Text> | ||
<OverflowTooltipText | ||
text="This is a long text that will be truncated and displayed as a tooltip" | ||
/> | ||
</Text> | ||
``` | ||
|
||
## Component API <a id="ComponentAPI" /> | ||
|
||
<ArgTypes of={OverflowTooltipTextStories.Default} sort="requiredFirst" /> |
9 changes: 9 additions & 0 deletions
9
packages/react-components/src/components/OverflowTooltipText/OverflowTooltipText.module.scss
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
.tooltipContent { | ||
word-break: break-word; | ||
} | ||
|
||
.text { | ||
overflow: hidden; | ||
text-overflow: ellipsis; | ||
white-space: nowrap; | ||
} |
3 changes: 3 additions & 0 deletions
3
packages/react-components/src/components/OverflowTooltipText/OverflowTooltipText.stories.css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.text-container { | ||
width: 200px; | ||
} |
67 changes: 67 additions & 0 deletions
67
packages/react-components/src/components/OverflowTooltipText/OverflowTooltipText.stories.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { Meta } from '@storybook/react'; | ||
|
||
import { Heading, Text } from '../Typography'; | ||
|
||
import { OverflowTooltipText } from './OverflowTooltipText'; | ||
import './OverflowTooltipText.stories.css'; | ||
import { OverflowTooltipTextProps } from './types'; | ||
|
||
export default { | ||
title: 'Components/OverflowTooltipText', | ||
component: OverflowTooltipText, | ||
render: (args: OverflowTooltipTextProps) => ( | ||
<div className="text-container"> | ||
<OverflowTooltipText {...args} /> | ||
</div> | ||
), | ||
} as Meta<typeof OverflowTooltipText>; | ||
|
||
export const Default = { | ||
args: { | ||
text: 'This is a text with a tooltip.', | ||
}, | ||
}; | ||
|
||
export const ShortText = { | ||
args: { | ||
text: 'No tooltip.', | ||
}, | ||
}; | ||
|
||
export const VeryLongText = { | ||
args: { | ||
text: "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.", | ||
}, | ||
}; | ||
|
||
export const WithTextElement = { | ||
render: (args: OverflowTooltipTextProps) => ( | ||
<div className="text-container"> | ||
<Text> | ||
<OverflowTooltipText {...args} /> | ||
</Text> | ||
</div> | ||
), | ||
args: { | ||
text: 'This is a text with a Text element and a tooltip.', | ||
}, | ||
}; | ||
|
||
export const WithHeadingElement = { | ||
render: (args: OverflowTooltipTextProps) => ( | ||
<div className="text-container"> | ||
<Heading> | ||
<OverflowTooltipText {...args} /> | ||
</Heading> | ||
</div> | ||
), | ||
args: { | ||
text: 'This is a text with a Heading element and a tooltip.', | ||
}, | ||
}; | ||
|
||
export const NoWhiteSpacesText = { | ||
args: { | ||
text: 'john.doensky.lorem.ipsum.lorem.ipsum.lorem.dolorem@gmail.com', | ||
}, | ||
}; |
38 changes: 38 additions & 0 deletions
38
packages/react-components/src/components/OverflowTooltipText/OverflowTooltipText.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { type FC } from 'react'; | ||
import { useRef } from 'react'; | ||
|
||
import { useIsOverflow, useOnHover } from '../../hooks'; | ||
import { Tooltip } from '../Tooltip'; | ||
|
||
import { type OverflowTooltipTextProps } from './types'; | ||
|
||
import styles from './OverflowTooltipText.module.scss'; | ||
|
||
export const OverflowTooltipText: FC<OverflowTooltipTextProps> = ({ text }) => { | ||
const wrapperRef = useRef<HTMLDivElement>(null); | ||
const isOverflow = useIsOverflow(wrapperRef); | ||
const { isHovered, handleMouseOut, handleMouseOver } = useOnHover(isOverflow); | ||
|
||
const renderChildren = () => { | ||
return ( | ||
<div | ||
onMouseEnter={handleMouseOver} | ||
onMouseLeave={handleMouseOut} | ||
ref={wrapperRef} | ||
className={styles.text} | ||
> | ||
{text} | ||
</div> | ||
); | ||
}; | ||
|
||
return ( | ||
<Tooltip | ||
kind="invert" | ||
isVisible={isOverflow && isHovered} | ||
triggerRenderer={renderChildren} | ||
> | ||
<div className={styles.tooltipContent}>{text}</div> | ||
</Tooltip> | ||
); | ||
}; |
1 change: 1 addition & 0 deletions
1
packages/react-components/src/components/OverflowTooltipText/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { OverflowTooltipText } from './OverflowTooltipText'; |
3 changes: 3 additions & 0 deletions
3
packages/react-components/src/components/OverflowTooltipText/types.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export interface OverflowTooltipTextProps { | ||
text: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { useState, useLayoutEffect, useCallback, RefObject } from 'react'; | ||
|
||
import debounce from 'lodash.debounce'; | ||
|
||
import { resizeCallback } from './helpers'; | ||
|
||
export const useIsOverflow = <T extends HTMLElement>( | ||
ref: RefObject<T> | ||
): boolean => { | ||
const [isOverflow, setIsOverflow] = useState(false); | ||
|
||
const checkOverflow = useCallback(() => { | ||
if (ref.current) { | ||
const isOverflowing = | ||
ref.current.scrollHeight > ref.current.offsetHeight || | ||
ref.current.scrollWidth > ref.current.offsetWidth; | ||
setIsOverflow(isOverflowing); | ||
} | ||
}, [ref]); | ||
|
||
const checkOverflowDebounced = useCallback( | ||
() => | ||
debounce(() => { | ||
checkOverflow(); | ||
}, 100), | ||
[checkOverflow] | ||
); | ||
|
||
useLayoutEffect(() => { | ||
checkOverflow(); | ||
|
||
const node = ref.current; | ||
if (node) { | ||
resizeCallback(node, () => { | ||
checkOverflowDebounced(); | ||
}); | ||
} | ||
|
||
return () => { | ||
resizeCallback(null, () => {}); | ||
}; | ||
}, [ref, checkOverflowDebounced]); | ||
|
||
return isOverflow; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { useState } from 'react'; | ||
|
||
import { IUseOnHover } from './types'; | ||
|
||
export const useOnHover = (initialState = false): IUseOnHover => { | ||
const [isHovered, setIsHovered] = useState(initialState); | ||
|
||
const handleMouseOver = (): void => setIsHovered(true); | ||
const handleMouseOut = (): void => setIsHovered(false); | ||
|
||
return { | ||
isHovered, | ||
handleMouseOver, | ||
handleMouseOut, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters