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

Tag - Text truncation for overflow fix #2655

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
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
74 changes: 57 additions & 17 deletions packages/components/src/components/hds/tag/index.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,69 @@
Copyright (c) HashiCorp, Inc.
SPDX-License-Identifier: MPL-2.0
}}
<Hds::Text::Body class={{this.classNames}} @tag="span" @size="100" @weight="medium" @color="primary" ...attributes>
<Hds::Text::Body
class={{this.classNames}}
@tag="span"
@size="100"
@weight="medium"
@color="primary"
{{did-insert this.didInsert}}
{{will-destroy this.willDestroyNode}}
...attributes
>
{{#if this.onDismiss}}
<button class="hds-tag__dismiss" type="button" aria-label={{this.ariaLabel}} {{on "click" this.onDismiss}}>
<Hds::Icon class="hds-tag__dismiss-icon" @name="x" @size="16" />
</button>
{{/if}}
{{#if (or @href @route)}}
<Hds::Interactive
class="hds-tag__link"
@current-when={{@current-when}}
@models={{hds-link-to-models @model @models}}
@query={{hds-link-to-query @query}}
@replace={{@replace}}
@route={{@route}}
@isRouteExternal={{@isRouteExternal}}
@href={{@href}}
@isHrefExternal={{@isHrefExternal}}
>
{{this.text}}
</Hds::Interactive>
{{#if this._isTextOverflow}}
<Hds::Interactive
class="hds-tag__link"
@current-when={{@current-when}}
@models={{hds-link-to-models @model @models}}
@query={{hds-link-to-query @query}}
@replace={{@replace}}
@route={{@route}}
@isRouteExternal={{@isRouteExternal}}
@href={{@href}}
@isHrefExternal={{@isHrefExternal}}
{{hds-tooltip this.text options=(hash placement="right")}}
>
<div class="hds-tag__text-container">
{{this.text}}
</div>
</Hds::Interactive>
{{else}}
<Hds::Interactive
class="hds-tag__link"
@current-when={{@current-when}}
@models={{hds-link-to-models @model @models}}
@query={{hds-link-to-query @query}}
@replace={{@replace}}
@route={{@route}}
@isRouteExternal={{@isRouteExternal}}
@href={{@href}}
@isHrefExternal={{@isHrefExternal}}
>
<div class="hds-tag__text-container">
{{this.text}}
</div>
</Hds::Interactive>
{{/if}}
{{else}}
<span class="hds-tag__text">
{{this.text}}
</span>
{{#if this._isTextOverflow}}
<Hds::TooltipButton class="hds-tag__text" @text={{this.text}} @placement="right">
<div class="hds-tag__text-container">
{{this.text}}
</div>
</Hds::TooltipButton>
{{else}}
<span class="hds-tag__text">
<div class="hds-tag__text-container">
{{this.text}}
</div>
</span>
{{/if}}
{{/if}}
</Hds::Text::Body>
34 changes: 34 additions & 0 deletions packages/components/src/components/hds/tag/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
*/

import Component from '@glimmer/component';
import { tracked } from '@glimmer/tracking';
import { action } from '@ember/object';
import { assert } from '@ember/debug';

import { HdsTagColorValues } from './types.ts';
Expand All @@ -25,6 +27,9 @@ export interface HdsTagSignature {
}

export default class HdsTag extends Component<HdsTagSignature> {
@tracked private _isTextOverflow!: boolean;
private _observer!: ResizeObserver;

/**
* @param onDismiss
* @type {function}
Expand Down Expand Up @@ -104,4 +109,33 @@ export default class HdsTag extends Component<HdsTagSignature> {

return classes.join(' ');
}

@action
didInsert(element: HTMLElement): void {
// Used to detect when text is clipped to one line, and tooltip should be added
this._observer = new ResizeObserver((entries) => {
requestAnimationFrame(() => {
entries.forEach((entry) => {
this._isTextOverflow = this._isOverflow(
entry.target.querySelector('.hds-tag__text-container')!
);
});
});
});
this._observer.observe(element);
}

@action
willDestroyNode(): void {
super.willDestroy();
this._observer.disconnect();
}

private _isOverflow(el: Element): boolean {
if (el.scrollHeight > el.clientHeight) {
return true;
} else {
return false;
}
}
}
20 changes: 20 additions & 0 deletions packages/components/src/styles/components/tag.scss
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ $hds-tag-border-radius: 50px;
border-radius: inherit;
}

.hds-tag__text-container {
display: -webkit-box;
overflow: hidden;
-webkit-box-orient: vertical;
-webkit-line-clamp: 1;
line-clamp: 1;
}

.hds-tag__dismiss ~ .hds-tag__text,
.hds-tag__dismiss ~ .hds-tag__link {
padding: 3px 8px 5px 6px;
Expand Down Expand Up @@ -76,6 +84,18 @@ $hds-tag-border-radius: 50px;
}
}

.hds-tooltip-button.hds-tag__text {
&:focus,
&.mock-focus {
@include hds-focus-ring-basic();
z-index: 1; // ensures focus is not obscured by adjacent elements
}

&:focus-visible::before {
box-shadow: none; // override default tooltip button focus styles
}
}

// COLORS (FOR LINK)

.hds-tag--color-primary {
Expand Down
10 changes: 10 additions & 0 deletions showcase/app/templates/components/tag.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@
<SF.Item {{style width="200px"}}>
<Hds::Tag @text="This is a very long text that should go on multiple lines" />
</SF.Item>
<SF.Item {{style width="200px"}}>
<Hds::Tag
@text="This is a very long text that should go on multiple lines"
@onDismiss={{this.noop}}
@route="components.tag"
/>
</SF.Item>
<SF.Item {{style width="200px"}}>
<Hds::Tag @text="This is a very long text that should go on multiple lines" @route="components.tag" />
</SF.Item>
</Shw::Flex>

<Shw::Divider @level={{2}} />
Expand Down
Loading