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(mantine): EllipsisText with line clamp #3942

Closed
wants to merge 2 commits into from
Closed
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
@@ -1,8 +1,8 @@
.root {
.noWrap {
white-space: nowrap;
}

.text {
.ellipsis {
flex-basis: 100%;
overflow: hidden;
text-overflow: ellipsis;
Expand Down
27 changes: 18 additions & 9 deletions packages/mantine/src/components/ellipsis-text/EllipsisText.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@ import {
useStyles,
} from '@mantine/core';
import {ReactNode, useRef, useState} from 'react';
import clsx from 'clsx';
import classes from './EllipsisText.module.css';

export type EllipsisTextStylesNames = 'root' | 'tooltip' | 'text';

export interface EllipsisTextProps
extends BoxProps,
Pick<TextProps, 'variant'>,
Pick<TextProps, 'variant' | 'lineClamp'>,
Omit<StylesApiProps<EllipsisTextFactory>, 'variant'> {
children: ReactNode;
tooltipProps?: Partial<Omit<TooltipProps, 'label' | 'opened' | 'children'>>;
Expand All @@ -36,11 +37,9 @@ 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 {className, children, style, classNames, styles, unstyled, variant, lineClamp, tooltipProps, ...others} =
useProps('EllipsisText', defaultProps, props);

const getStyles = useStyles<EllipsisTextFactory>({
name: 'EllipsisText',
classes,
Expand All @@ -55,6 +54,9 @@ export const EllipsisText = polymorphicFactory<EllipsisTextFactory>((props, ref)
const [showTooltip, setShowTooltip] = useState(false);
const textRef = useRef<HTMLDivElement>();

const {className: rootClass, ...rootStyles} = getStyles('root');
const {className: textClass, ...textStyles} = getStyles('text');

return (
<Box
ref={ref}
Expand All @@ -66,16 +68,23 @@ export const EllipsisText = polymorphicFactory<EllipsisTextFactory>((props, ref)
onMouseLeave={() => setShowTooltip(false)}
display="flex"
w="100%"
{...getStyles('root')}
className={clsx(rootClass, {[classes.noWrap]: !lineClamp})}
{...rootStyles}
{...others}
>
<Tooltip label={children} opened={showTooltip} {...tooltipProps} {...getStyles('tooltip')}>
<Text variant={variant} ref={textRef} {...getStyles('text')}>
<Text
variant={variant}
ref={textRef}
className={clsx(textClass, {[classes.ellipsis]: !lineClamp})}
{...(!!lineClamp && {lineClamp: lineClamp})}
{...textStyles}
>
{children}
</Text>
</Tooltip>
</Box>
);
});

const isOverflowing = (h: HTMLDivElement) => h.scrollWidth > h.clientWidth;
const isOverflowing = (h: HTMLDivElement) => h.scrollWidth > h.clientWidth || h.scrollHeight > h.clientHeight;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be good to have several demos.
One for the default example without the lineClamp prop and one with the lineClamp prop.

What do you think?

Original file line number Diff line number Diff line change
@@ -1,11 +1,27 @@
import {EllipsisText} from '@coveord/plasma-mantine';
import {Chip, EllipsisText, Stack, Title} 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>
<Stack gap="xs">
<Title order={5}>Default</Title>
<EllipsisText maw={250}>This is a very long text that is truncated with an ellipsis.</EllipsisText>
<EllipsisText maw={250}>This short text is not truncated.</EllipsisText>
<Chip>
<EllipsisText maw={250}>
This is a very long text within a special container is truncated with an ellipsis.
</EllipsisText>
</Chip>
</Stack>
<Stack gap="xs">
<Title order={5}>Line clamp</Title>
<EllipsisText maw={250} lineClamp={2}>
This is a very long text that is truncated with an ellipsis since clamp limit is not enough to display
the full text.
</EllipsisText>
<EllipsisText maw={250} lineClamp={2}>
This is a very long text that is not truncated since clamp limit is not reached.
</EllipsisText>
</Stack>
</>
);
export default Demo;