-
Notifications
You must be signed in to change notification settings - Fork 45
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
Tag
- Text truncation for overflow fix
#2655
Open
dchyun
wants to merge
17
commits into
main
Choose a base branch
from
dchyun/tag-text-truncation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 16 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
df74c25
HDS-4317 tag text truncation
dchyun d99d48a
Fix: Refactor to use `line-clamp`
dchyun a100715
Feat: Tag tooltip shown based on text overflow
dchyun b718928
Fix: Tag ResizeObserver error message
dchyun 3c17985
Fix: component linting
dchyun 9ff1e3c
Fix: Tag tooltip being removed on resize
dchyun 4adba30
Add changeset
dchyun 5a2c2bd
Fix: Refactor tag observer to custom modifier, add tests for overflow
dchyun 3271f2a
Feat: Set `fit-content` width
dchyun 0c0ce6e
Fix: Set max width to avoid overflows
dchyun f5ebe3b
Fix: Support copying text when tag overflows
dchyun 237f8d1
Fix: Tag overflow test hardening
dchyun 83d2e63
Fix: Test linting
dchyun cff1ef0
Feat: Tag tooltipPlacement prop, max width for text
dchyun 5713af2
Fix: Component linting
dchyun b6350b4
Chore: Update changeset and showcase to remove widths
dchyun dfa2324
Fix: Update max-width to 160px
dchyun File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
"@hashicorp/design-system-components": minor | ||
--- | ||
|
||
`Tag` - Truncate any text that is longer than 150px and add a tooltip with the full text when truncation occurs | ||
|
||
`Tag` - Added `@tooltipPlacement` argument |
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 |
---|---|---|
|
@@ -4,27 +4,73 @@ | |
*/ | ||
|
||
import Component from '@glimmer/component'; | ||
import { tracked } from '@glimmer/tracking'; | ||
import { assert } from '@ember/debug'; | ||
import { modifier } from 'ember-modifier'; | ||
|
||
import { HdsTagColorValues } from './types.ts'; | ||
import type { HdsTagColors } from './types.ts'; | ||
import { HdsTagTooltipPlacementValues } from './types.ts'; | ||
import type { HdsTagTooltipPlacements } from './types.ts'; | ||
import type { HdsInteractiveSignature } from '../interactive/'; | ||
|
||
export const COLORS: string[] = Object.values(HdsTagColorValues); | ||
export const DEFAULT_COLOR = HdsTagColorValues.Primary; | ||
export const TOOLTIP_PLACEMENTS: string[] = Object.values( | ||
HdsTagTooltipPlacementValues | ||
); | ||
export const DEFAULT_TOOLTIP_PLACEMENT = HdsTagTooltipPlacementValues.Top; | ||
|
||
export interface HdsTagSignature { | ||
Args: HdsInteractiveSignature['Args'] & { | ||
color?: HdsTagColors; | ||
text: string; | ||
ariaLabel?: string; | ||
tooltipPlacement: HdsTagTooltipPlacements; | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
onDismiss?: (event: MouseEvent, ...args: any[]) => void; | ||
}; | ||
Element: HTMLSpanElement; | ||
} | ||
|
||
export default class HdsTag extends Component<HdsTagSignature> { | ||
@tracked private _isTextOverflow!: boolean; | ||
private _observer!: ResizeObserver; | ||
|
||
private _setUpObserver = modifier((element: HTMLElement) => { | ||
// Used to detect when text is clipped to one line, and tooltip should be added | ||
this._observer = new ResizeObserver((entries) => { | ||
entries.forEach((entry) => { | ||
this._isTextOverflow = this._isOverflow( | ||
entry.target.querySelector('.hds-tag__text-container')! | ||
); | ||
}); | ||
}); | ||
this._observer.observe(element); | ||
|
||
return () => { | ||
this._observer.disconnect(); | ||
}; | ||
}); | ||
|
||
/** | ||
* @param tooltioPlacement | ||
* @type {string} | ||
* @default top | ||
* @description The placement property of the tooltip attached to the tag text. | ||
*/ | ||
get tooltipPlacement(): HdsTagTooltipPlacements { | ||
const { tooltipPlacement = DEFAULT_TOOLTIP_PLACEMENT } = this.args; | ||
|
||
assert( | ||
'@tooltipPlacement for "Hds::Tag" must have a valid value', | ||
tooltipPlacement == undefined || | ||
TOOLTIP_PLACEMENTS.includes(tooltipPlacement) | ||
); | ||
|
||
return tooltipPlacement; | ||
} | ||
|
||
/** | ||
* @param onDismiss | ||
* @type {function} | ||
|
@@ -104,4 +150,8 @@ export default class HdsTag extends Component<HdsTagSignature> { | |
|
||
return classes.join(' '); | ||
} | ||
|
||
private _isOverflow(el: Element): boolean { | ||
return el.scrollHeight > el.clientHeight; | ||
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. π |
||
} | ||
} |
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
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like how this aligns with the AdvancedTable observer!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
all credit to dylan, I followed his pattern :)