Skip to content

Commit 5a2c2bd

Browse files
committed
Fix: Refactor tag observer to custom modifier, add tests for overflow
1 parent 4adba30 commit 5a2c2bd

File tree

3 files changed

+42
-29
lines changed

3 files changed

+42
-29
lines changed

packages/components/src/components/hds/tag/index.hbs

+1-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@
88
@size="100"
99
@weight="medium"
1010
@color="primary"
11-
{{did-insert this.didInsert}}
12-
{{will-destroy this.willDestroyNode}}
11+
{{this._setUpObserver}}
1312
...attributes
1413
>
1514
{{#if this.onDismiss}}

packages/components/src/components/hds/tag/index.ts

+18-27
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55

66
import Component from '@glimmer/component';
77
import { tracked } from '@glimmer/tracking';
8-
import { action } from '@ember/object';
98
import { assert } from '@ember/debug';
9+
import { modifier } from 'ember-modifier';
1010

1111
import { HdsTagColorValues } from './types.ts';
1212
import type { HdsTagColors } from './types.ts';
@@ -30,6 +30,22 @@ export default class HdsTag extends Component<HdsTagSignature> {
3030
@tracked private _isTextOverflow!: boolean;
3131
private _observer!: ResizeObserver;
3232

33+
private _setUpObserver = modifier((element: HTMLElement) => {
34+
// Used to detect when text is clipped to one line, and tooltip should be added
35+
this._observer = new ResizeObserver((entries) => {
36+
entries.forEach((entry) => {
37+
this._isTextOverflow = this._isOverflow(
38+
entry.target.querySelector('.hds-tag__text-container')!
39+
);
40+
});
41+
});
42+
this._observer.observe(element);
43+
44+
return () => {
45+
this._observer.disconnect();
46+
};
47+
});
48+
3349
/**
3450
* @param onDismiss
3551
* @type {function}
@@ -110,32 +126,7 @@ export default class HdsTag extends Component<HdsTagSignature> {
110126
return classes.join(' ');
111127
}
112128

113-
@action
114-
didInsert(element: HTMLElement): void {
115-
// Used to detect when text is clipped to one line, and tooltip should be added
116-
this._observer = new ResizeObserver((entries) => {
117-
requestAnimationFrame(() => {
118-
entries.forEach((entry) => {
119-
this._isTextOverflow = this._isOverflow(
120-
entry.target.querySelector('.hds-tag__text-container')!
121-
);
122-
});
123-
});
124-
});
125-
this._observer.observe(element);
126-
}
127-
128-
@action
129-
willDestroyNode(): void {
130-
super.willDestroy();
131-
this._observer.disconnect();
132-
}
133-
134129
private _isOverflow(el: Element): boolean {
135-
if (el.scrollHeight > el.clientHeight) {
136-
return true;
137-
} else {
138-
return false;
139-
}
130+
return el.scrollHeight > el.clientHeight;
140131
}
141132
}

showcase/tests/integration/components/hds/tag/index-test.js

+23
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ module('Integration | Component | hds/tag/index', function (hooks) {
2525
await render(hbs`<Hds::Tag @text="My tag" />`);
2626
assert.dom('button.hds-tag__dismiss').doesNotExist();
2727
});
28+
2829
test('it should render the "dismiss" button if a callback function is passed to the @onDismiss argument', async function (assert) {
2930
this.set('NOOP', () => {});
3031
await render(hbs`<Hds::Tag @text="My tag" @onDismiss={{this.NOOP}} />`);
@@ -44,6 +45,7 @@ module('Integration | Component | hds/tag/index', function (hooks) {
4445
.dom('button.hds-tag__dismiss')
4546
.hasAttribute('aria-label', 'Please dismiss My tag');
4647
});
48+
4749
// COLOR
4850

4951
test('it should render the primary color as the default if no @color prop is declared when the text is a link', async function (assert) {
@@ -52,12 +54,14 @@ module('Integration | Component | hds/tag/index', function (hooks) {
5254
);
5355
assert.dom('#test-link-tag').hasClass('hds-tag--color-primary');
5456
});
57+
5558
test('it should render the correct CSS color class if the @color prop is declared when the text is a link', async function (assert) {
5659
await render(
5760
hbs`<Hds::Tag @text="My text tag" @href="/" @color="secondary" id="test-link-tag"/>`
5861
);
5962
assert.dom('#test-link-tag').hasClass('hds-tag--color-secondary');
6063
});
64+
6165
test('it should throw an assertion if an incorrect value for @color is provided when the text is a link', async function (assert) {
6266
const errorMessage =
6367
'@color for "Hds::Tag" must be one of the following: primary, secondary; received: foo';
@@ -70,6 +74,7 @@ module('Integration | Component | hds/tag/index', function (hooks) {
7074
throw new Error(errorMessage);
7175
});
7276
});
77+
7378
test('it should throw an assertion if @color is provided without @href or @route', async function (assert) {
7479
const errorMessage =
7580
'@color can only be applied to "Hds::Tag" along with either @href or @route';
@@ -82,4 +87,22 @@ module('Integration | Component | hds/tag/index', function (hooks) {
8287
throw new Error(errorMessage);
8388
});
8489
});
90+
91+
// OVERFLOW
92+
93+
test('it should not render a tooltip if the text does not overflow', async function (assert) {
94+
await render(hbs`
95+
<Hds::Tag @text="My text tag" id="test-tag"/>
96+
`);
97+
assert.dom('.hds-tooltip-button').doesNotExist();
98+
});
99+
100+
test('it should render a tooltip if the text overflows', async function (assert) {
101+
await render(hbs`
102+
<div style="width: 50px;">
103+
<Hds::Tag @text="This is a very long text that should go on multiple lines" id="test-tag"/>
104+
</div>
105+
`);
106+
assert.dom('.hds-tooltip-button').exists();
107+
});
85108
});

0 commit comments

Comments
 (0)