Skip to content

Commit d0eb9dd

Browse files
authored
refactor: remove iron-resizable-behavior from tabs (#3197)
1 parent 1631431 commit d0eb9dd

6 files changed

Lines changed: 119 additions & 11 deletions

File tree

‎dev/tabs.html‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
7+
<title>Tabs</title>
8+
9+
<script type="module">
10+
import '@vaadin/tabs';
11+
</script>
12+
</head>
13+
14+
<body>
15+
<vaadin-tabs style="max-width: 100%; width: 400px;">
16+
<vaadin-tab>Analytics</vaadin-tab>
17+
<vaadin-tab>Customers</vaadin-tab>
18+
<vaadin-tab>Dashboards</vaadin-tab>
19+
<vaadin-tab>Documents</vaadin-tab>
20+
<vaadin-tab>Orders</vaadin-tab>
21+
</vaadin-tabs>
22+
</body>
23+
</html>

‎packages/tabs/package.json‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
"polymer"
3333
],
3434
"dependencies": {
35-
"@polymer/iron-resizable-behavior": "^3.0.0",
3635
"@polymer/polymer": "^3.0.0",
3736
"@vaadin/component-base": "23.0.0-alpha1",
3837
"@vaadin/item": "23.0.0-alpha1",

‎packages/tabs/src/vaadin-tabs.js‎

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
* This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
55
*/
66
import './vaadin-tab.js';
7-
import { IronResizableBehavior } from '@polymer/iron-resizable-behavior/iron-resizable-behavior.js';
8-
import { mixinBehaviors } from '@polymer/polymer/lib/legacy/class.js';
97
import { afterNextRender } from '@polymer/polymer/lib/utils/render-status.js';
108
import { html, PolymerElement } from '@polymer/polymer/polymer-element.js';
119
import { ElementMixin } from '@vaadin/component-base/src/element-mixin.js';
@@ -51,7 +49,7 @@ import { ThemableMixin } from '@vaadin/vaadin-themable-mixin/vaadin-themable-mix
5149
* @mixes ListMixin
5250
* @mixes ThemableMixin
5351
*/
54-
class Tabs extends ElementMixin(ListMixin(ThemableMixin(mixinBehaviors([IronResizableBehavior], PolymerElement)))) {
52+
class Tabs extends ElementMixin(ListMixin(ThemableMixin(PolymerElement))) {
5553
static get template() {
5654
return html`
5755
<style>
@@ -167,12 +165,19 @@ class Tabs extends ElementMixin(ListMixin(ThemableMixin(mixinBehaviors([IronResi
167165
}
168166

169167
static get observers() {
170-
return ['_updateOverflow(items.*)'];
168+
return ['__tabsItemsChanged(items, items.*)'];
169+
}
170+
171+
constructor() {
172+
super();
173+
174+
this.__resizeObserver = new ResizeObserver(() => {
175+
requestAnimationFrame(() => this._updateOverflow());
176+
});
171177
}
172178

173179
ready() {
174180
super.ready();
175-
this.addEventListener('iron-resize', () => this._updateOverflow());
176181
this._scrollerElement.addEventListener('scroll', () => this._updateOverflow());
177182
this.setAttribute('role', 'tablist');
178183

@@ -182,6 +187,20 @@ class Tabs extends ElementMixin(ListMixin(ThemableMixin(mixinBehaviors([IronResi
182187
});
183188
}
184189

190+
/** @private */
191+
__tabsItemsChanged(items) {
192+
// Disconnected to unobserve any removed items
193+
this.__resizeObserver.disconnect();
194+
this.__resizeObserver.observe(this);
195+
196+
// Observe current items
197+
(items || []).forEach((item) => {
198+
this.__resizeObserver.observe(item);
199+
});
200+
201+
this._updateOverflow();
202+
}
203+
185204
/** @private */
186205
_scrollForward() {
187206
this._scroll(-this.__direction * this._scrollOffset);

‎packages/tabs/test/tabs.test.js‎

Lines changed: 64 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,28 @@
11
import { expect } from '@esm-bundle/chai';
2-
import { arrowRight, enter, fixtureSync, listenOnce, nextRender, space } from '@vaadin/testing-helpers';
2+
import { arrowRight, aTimeout, enter, fixtureSync, listenOnce, nextFrame, space } from '@vaadin/testing-helpers';
33
import sinon from 'sinon';
44
import '../vaadin-tabs.js';
55

6+
/**
7+
* Resolves once the function is invoked on the given object.
8+
*/
9+
function onceInvoked(object, functionName) {
10+
return new Promise((resolve) => {
11+
sinon.replace(object, functionName, (...args) => {
12+
sinon.restore();
13+
object[functionName](...args);
14+
resolve();
15+
});
16+
});
17+
}
18+
19+
/**
20+
* Resolves once the ResizeObserver has processed a resize.
21+
*/
22+
async function onceResized(tabs) {
23+
await onceInvoked(tabs, '_updateOverflow');
24+
}
25+
626
describe('tabs', () => {
727
let tabs;
828

@@ -44,6 +64,23 @@ describe('tabs', () => {
4464
expect(item.tagName.toLowerCase()).to.equal('vaadin-tab');
4565
});
4666
});
67+
68+
it('should not resize on detached item resize', async () => {
69+
// Remove a tab
70+
const item = tabs.items[0];
71+
document.body.append(item);
72+
await aTimeout(100);
73+
74+
// Resize the removed tab
75+
const stub = sinon.stub(tabs, '_updateOverflow');
76+
item.style.width = '100px';
77+
await aTimeout(100);
78+
79+
item.remove();
80+
// Expect the resizeobserver not to have been invoked on the
81+
// removed tab resize
82+
expect(stub.called).to.be.false;
83+
});
4784
});
4885

4986
['horizontal', 'vertical'].forEach((orientation) => {
@@ -73,8 +110,8 @@ describe('tabs', () => {
73110
} else {
74111
tabs.style.height = '100px';
75112
}
76-
tabs._updateOverflow();
77-
await nextRender(tabs);
113+
await onceResized(tabs);
114+
await nextFrame();
78115
});
79116

80117
it(`when orientation=${orientation} should have overflow="end" if scroll is at the beginning`, () => {
@@ -121,6 +158,30 @@ describe('tabs', () => {
121158

122159
expect(tabs.getAttribute('overflow')).to.be.equal('end');
123160
});
161+
162+
it('should update overflow on resize', async () => {
163+
tabs.style.width = 'auto';
164+
tabs.style.height = 'auto';
165+
await onceResized(tabs);
166+
expect(tabs.hasAttribute('overflow')).to.be.false;
167+
});
168+
169+
it('should update overflow on item resize', async () => {
170+
tabs.items.forEach((item) => {
171+
item.style.height = '1px';
172+
item.style.width = '1px';
173+
item.style.minHeight = '1px';
174+
item.style.minWidth = '1px';
175+
});
176+
await onceResized(tabs);
177+
expect(tabs.hasAttribute('overflow')).to.be.false;
178+
});
179+
180+
it('should update overflow on items change', async () => {
181+
tabs.items.forEach((item) => item.remove());
182+
await nextFrame();
183+
expect(tabs.hasAttribute('overflow')).to.be.false;
184+
});
124185
});
125186
});
126187
});

‎packages/tabs/test/visual/lumo/tabs.test.js‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { nextFrame } from '@vaadin/testing-helpers';
12
import { fixtureSync } from '@vaadin/testing-helpers/dist/fixture.js';
23
import { visualDiff } from '@web/test-runner-visual-regression';
34
import '../../../theme/lumo/vaadin-tabs.js';
@@ -107,7 +108,7 @@ describe('tabs', () => {
107108
describe('scroll', () => {
108109
['horizontal', 'vertical'].forEach((orientation) => {
109110
describe(orientation, () => {
110-
beforeEach(() => {
111+
beforeEach(async () => {
111112
if (orientation === 'vertical') {
112113
div.style.height = '150px';
113114
div.style.display = 'inline-flex';
@@ -138,6 +139,8 @@ describe('tabs', () => {
138139
`,
139140
div
140141
);
142+
143+
await nextFrame();
141144
});
142145

143146
['ltr', 'rtl'].forEach((dir) => {

‎packages/tabs/test/visual/material/tabs.test.js‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { nextFrame } from '@vaadin/testing-helpers';
12
import { fixtureSync } from '@vaadin/testing-helpers/dist/fixture.js';
23
import { visualDiff } from '@web/test-runner-visual-regression';
34
import '../../../theme/material/vaadin-tabs.js';
@@ -107,7 +108,7 @@ describe('tabs', () => {
107108
describe('scroll', () => {
108109
['horizontal', 'vertical'].forEach((orientation) => {
109110
describe(orientation, () => {
110-
beforeEach(() => {
111+
beforeEach(async () => {
111112
if (orientation === 'vertical') {
112113
div.style.height = '150px';
113114
div.style.display = 'inline-flex';
@@ -138,6 +139,8 @@ describe('tabs', () => {
138139
`,
139140
div
140141
);
142+
143+
await nextFrame();
141144
});
142145

143146
['ltr', 'rtl'].forEach((dir) => {

0 commit comments

Comments
 (0)