-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(mantine,website): create the EllipsisText component (#3933)
* feat(mantine,website): create the EllipsisText component * refactor(mantine): group EllipsisText tooltipProps
- Loading branch information
Showing
10 changed files
with
130 additions
and
0 deletions.
There are no files selected for viewing
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
9 changes: 9 additions & 0 deletions
9
packages/mantine/src/components/ellipsis-text/EllipsisText.module.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,9 @@ | ||
.root { | ||
white-space: nowrap; | ||
} | ||
|
||
.text { | ||
flex-basis: 100%; | ||
overflow: hidden; | ||
text-overflow: ellipsis; | ||
} |
79 changes: 79 additions & 0 deletions
79
packages/mantine/src/components/ellipsis-text/EllipsisText.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,79 @@ | ||
import { | ||
Box, | ||
type BoxProps, | ||
type Factory, | ||
polymorphicFactory, | ||
type StylesApiProps, | ||
Text, | ||
type TextProps, | ||
Tooltip, | ||
type TooltipProps, | ||
useProps, | ||
useStyles, | ||
} from '@mantine/core'; | ||
import {ReactNode, useRef, useState} from 'react'; | ||
import classes from './EllipsisText.module.css'; | ||
|
||
export type EllipsisTextStylesNames = 'root' | 'tooltip' | 'text'; | ||
|
||
export interface EllipsisTextProps | ||
extends BoxProps, | ||
Pick<TextProps, 'variant'>, | ||
Omit<StylesApiProps<EllipsisTextFactory>, 'variant'> { | ||
children: ReactNode; | ||
tooltipProps: Omit<TooltipProps, 'label' | 'opened'>; | ||
} | ||
|
||
type EllipsisTextFactory = Factory<{ | ||
props: EllipsisTextProps; | ||
defaultRef: HTMLDivElement; | ||
defaultComponent: 'div'; | ||
stylesNames: EllipsisTextStylesNames; | ||
}>; | ||
|
||
const defaultProps: Partial<EllipsisTextProps> = {}; | ||
|
||
export const EllipsisText = polymorphicFactory<EllipsisTextFactory>((props, ref) => { | ||
const {className, children, style, classNames, styles, unstyled, variant, tooltipProps, ...others} = useProps( | ||
'EllipsisText', | ||
defaultProps, | ||
props, | ||
); | ||
const getStyles = useStyles<EllipsisTextFactory>({ | ||
name: 'EllipsisText', | ||
classes, | ||
props, | ||
className, | ||
style, | ||
classNames, | ||
styles, | ||
unstyled, | ||
}); | ||
|
||
const [showTooltip, setShowTooltip] = useState(false); | ||
const textRef = useRef<HTMLDivElement>(); | ||
|
||
return ( | ||
<Box | ||
ref={ref} | ||
onMouseEnter={() => { | ||
if (textRef.current && isOverflowing(textRef.current)) { | ||
setShowTooltip(true); | ||
} | ||
}} | ||
onMouseLeave={() => setShowTooltip(false)} | ||
display="flex" | ||
w="100%" | ||
{...getStyles('root')} | ||
{...others} | ||
> | ||
<Tooltip label={children} opened={showTooltip} {...tooltipProps} {...getStyles('tooltip')}> | ||
<Text variant={variant} ref={textRef} {...getStyles('text')}> | ||
{children} | ||
</Text> | ||
</Tooltip> | ||
</Box> | ||
); | ||
}); | ||
|
||
const isOverflowing = (h: HTMLDivElement) => h.scrollWidth > h.clientWidth; |
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 * from './EllipsisText'; |
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,3 @@ | ||
.tooltip { | ||
word-break: break-word; | ||
} |
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
11 changes: 11 additions & 0 deletions
11
packages/website/src/examples/layout/EllipsisText/EllipsisText.demo.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,11 @@ | ||
import {EllipsisText} from '@coveord/plasma-mantine'; | ||
|
||
const Demo = () => ( | ||
<> | ||
<EllipsisText maw={250}> | ||
This is a very long text that is truncated with an ellipsis. The rest is shown on hover. | ||
</EllipsisText> | ||
<EllipsisText maw={250}>This short text is not truncated.</EllipsisText> | ||
</> | ||
); | ||
export default Demo; |
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,18 @@ | ||
import {EllipsisTextMetadata} from '@coveord/plasma-components-props-analyzer'; | ||
import EllipsisTextDemo from '@examples/layout/EllipsisText/EllipsisText.demo?demo'; | ||
|
||
import {PageLayout} from '../../building-blocs/PageLayout'; | ||
|
||
const Page = () => ( | ||
<PageLayout | ||
section="Layout" | ||
title="EllipsisText" | ||
sourcePath="/packages/mantine/src/components/ellipsis-text/EllipsisText.tsx" | ||
description="Allows to display text that will automatically truncate and shows a tooltip on hover if it overflows the max width." | ||
id="EllipsisText" | ||
propsMetadata={EllipsisTextMetadata} | ||
demo={<EllipsisTextDemo />} | ||
/> | ||
); | ||
|
||
export default Page; |