-
-
Notifications
You must be signed in to change notification settings - Fork 655
Truncated text component #1117
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
Truncated text component #1117
Changes from 32 commits
0ebcede
ca4de6e
95c2051
98e8cd4
d126560
7117a69
3ee3a64
c0dd7d4
1fe7197
1916577
269ff95
2aeffce
563902a
e4ad50d
8db1ffa
8041c12
ccb61a2
170f5b4
8cebc03
698c9a8
19ca19c
b1d9fad
4d36fd9
c56666b
7fafc90
1a57db6
cbae4b9
e5c1427
0e409b8
ae8344c
38808c6
79bb085
e431133
e8ebf83
464d980
39bf7bb
da5da20
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| import { render, screen } from 'wrappers/testUtil' | ||
| import { TruncatedText } from 'components/TruncatedText' | ||
|
|
||
| describe('TruncatedText Component', () => { | ||
| const longText = 'This is very long text that should be truncated for display.' | ||
|
|
||
| test('renders full text when it fits within container', () => { | ||
| render(<TruncatedText text="Short text" className="w-auto" />) | ||
| expect(screen.getByText('Short text')).toBeInTheDocument() | ||
| }) | ||
|
|
||
| test('truncates long text with ellipsis dynamically', () => { | ||
| render(<TruncatedText text={longText} className="w-20" />) | ||
| const textElement = screen.getByText(/This is very long text that should be/) | ||
| expect(textElement).toHaveClass('truncate') | ||
| }) | ||
|
|
||
| test('does not show tooltip when text is fully visible', () => { | ||
| render(<TruncatedText text={longText} className="w-full" disabledTooltip />) | ||
| expect(screen.getByText(longText)).toBeInTheDocument() | ||
| }) | ||
|
|
||
| test('renders correctly across different screen sizes', () => { | ||
| render(<TruncatedText text={longText} className="max-w-xs sm:max-w-md md:max-w-lg" />) | ||
| const textElement = screen.getByText(/This is very long text that should be/) | ||
| expect(textElement).toHaveClass('truncate') | ||
| }) | ||
| }) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| import { useRef, useEffect, useCallback } from 'react' | ||
|
|
||
| export const TruncatedText = ({ text, className = '' }: { text: string; className?: string }) => { | ||
| const textRef = useRef<HTMLSpanElement>(null) | ||
|
|
||
| const checkTruncation = useCallback(() => { | ||
| const element = textRef.current | ||
| if (element) { | ||
| element.title = text | ||
| } | ||
| }, [text]) | ||
|
|
||
| useEffect(() => { | ||
| checkTruncation() | ||
|
|
||
| const observer = new ResizeObserver(() => checkTruncation()) | ||
| if (textRef.current) { | ||
| observer.observe(textRef.current) | ||
| } | ||
|
|
||
| window.addEventListener('resize', checkTruncation) | ||
|
|
||
| return () => { | ||
| observer.disconnect() | ||
| window.removeEventListener('resize', checkTruncation) | ||
| } | ||
| }, [text, checkTruncation]) | ||
|
|
||
| return ( | ||
| <span | ||
| ref={textRef} | ||
| className={`block overflow-hidden truncate text-ellipsis whitespace-nowrap ${className}`} | ||
| > | ||
| {text} | ||
| </span> | ||
| ) | ||
| } |
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The issue description contains
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. all existing cases also includes project, issue and chapter card titles?
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes. We want a consistent look, not just a main page fix. |
Uh oh!
There was an error while loading. Please reload this page.