-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: ClampedText component (#28664)
- Loading branch information
Showing
11 changed files
with
110 additions
and
72 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
29 changes: 29 additions & 0 deletions
29
frontend/src/lib/lemon-ui/ClampedText/ClampedText.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,29 @@ | ||
import { Meta, StoryFn, StoryObj } from '@storybook/react' | ||
|
||
import { ClampedText, ClampedTextProps } from './ClampedText' | ||
|
||
type Story = StoryObj<typeof ClampedText> | ||
const meta: Meta<typeof ClampedText> = { | ||
title: 'Lemon UI/ClampedText', | ||
component: ClampedText, | ||
parameters: { | ||
testOptions: { | ||
waitForLoadersToDisappear: false, | ||
}, | ||
}, | ||
tags: ['autodocs'], | ||
} | ||
export default meta | ||
|
||
const Template: StoryFn<typeof ClampedText> = (props: ClampedTextProps) => { | ||
return <ClampedText {...props} /> | ||
} | ||
|
||
export const SingleLine: Story = Template.bind({}) | ||
SingleLine.args = { lines: 2, text: 'One line of text' } | ||
|
||
export const MultiLine: Story = Template.bind({}) | ||
MultiLine.args = { | ||
lines: 2, | ||
text: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.', | ||
} |
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,78 @@ | ||
import React, { useEffect, useState } from 'react' | ||
|
||
import { Link } from '../Link' | ||
|
||
export interface ClampedTextProps { | ||
lines: number | ||
text: string | ||
} | ||
|
||
const isCssEllipsisApplied = (elem: HTMLDivElement): boolean => elem.scrollHeight > elem.clientHeight | ||
|
||
export const ClampedText = React.forwardRef<HTMLDivElement, ClampedTextProps>(function ClampedText( | ||
{ lines, text }, | ||
ref | ||
) { | ||
const [localLines, setLocalLines] = useState<number | undefined>(lines) | ||
const [isExpanded, setIsExpanded] = useState<boolean>(false) | ||
const [showMore, setShowMore] = useState<boolean>(false) | ||
|
||
const handleToggleShowMore = (show: boolean): void => { | ||
setShowMore(!showMore) | ||
setIsExpanded(!isExpanded) | ||
setLocalLines(show ? undefined : lines) | ||
} | ||
|
||
const handleConfigElement = (elem: HTMLDivElement): void => { | ||
if (!elem) { | ||
return | ||
} | ||
|
||
if (isCssEllipsisApplied(elem)) { | ||
if (!showMore || !isExpanded) { | ||
setShowMore(true) | ||
} | ||
} else { | ||
setShowMore(false) | ||
} | ||
} | ||
|
||
useEffect(() => setLocalLines(lines), [lines]) | ||
|
||
return ( | ||
<div ref={ref}> | ||
<TruncatedElement lines={localLines} ref={handleConfigElement} text={text} /> | ||
<div> | ||
{isExpanded || showMore ? ( | ||
<Link onClick={() => handleToggleShowMore(!isExpanded)}> | ||
{isExpanded ? 'Show less' : 'Show more'} | ||
</Link> | ||
) : null} | ||
</div> | ||
</div> | ||
) | ||
}) | ||
|
||
const TruncatedElement = React.forwardRef<HTMLDivElement, { lines?: number; text: string }>(function TruncatedElement( | ||
{ text, lines }, | ||
ref | ||
) { | ||
return ( | ||
<span | ||
// eslint-disable-next-line react/forbid-dom-props | ||
style={ | ||
lines | ||
? { | ||
overflow: 'hidden', | ||
display: '-webkit-box', | ||
WebkitBoxOrient: 'vertical', | ||
WebkitLineClamp: lines, | ||
} | ||
: {} | ||
} | ||
ref={ref} | ||
> | ||
{text} | ||
</span> | ||
) | ||
}) |
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 { ClampedText } from './ClampedText' |
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
71 changes: 0 additions & 71 deletions
71
frontend/src/scenes/error-tracking/issue/panels/MetaPanel.tsx
This file was deleted.
Oops, something went wrong.