Skip to content

Commit 9755be1

Browse files
Elliott Marquezcopybara-github
authored andcommitted
fix(tab): activate() works before first render
PiperOrigin-RevId: 345494326
1 parent cbe9fc1 commit 9755be1

File tree

3 files changed

+51
-3
lines changed

3 files changed

+51
-3
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
3636
- List items removal from DOM initiates an async layout in the managing list
3737
- Added `itemsReady` promise to the list's updateComplete
3838

39+
- `tab`
40+
- Calling `activate()` before first render does not throw an error.
41+
3942
## [v0.19.1] - 2020-10-08
4043

4144
### Fixed

packages/tab/mwc-tab-base.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,8 +208,18 @@ export class TabBase extends BaseElement {
208208
if (!clientRect) {
209209
this.initFocus = true;
210210
}
211-
this.mdcFoundation.activate(clientRect);
212-
this.setActive(this.mdcFoundation.isActive());
211+
212+
if (this.mdcFoundation) {
213+
this.mdcFoundation.activate(clientRect);
214+
this.setActive(this.mdcFoundation.isActive());
215+
} else {
216+
// happens if this is called by tab-bar on initialization, but tab has not
217+
// finished rendering.
218+
this.updateComplete.then(() => {
219+
this.mdcFoundation.activate(clientRect);
220+
this.setActive(this.mdcFoundation.isActive());
221+
});
222+
}
213223
}
214224

215225
deactivate() {

packages/tab/test/mwc-tab.test.ts

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
import {Tab} from '@material/mwc-tab';
1919
import * as hanbi from 'hanbi';
20-
import {html} from 'lit-html';
20+
import {html, render} from 'lit-html';
2121

2222
import {fixture, rafPromise, TestFixture} from '../../../test/src/util/helpers';
2323

@@ -129,4 +129,39 @@ suite('mwc-tab', () => {
129129
assert.equal(content.textContent!.trim(), 'add');
130130
});
131131
});
132+
133+
suite('activate', () => {
134+
teardown(() => {
135+
const tabs = document.body.querySelectorAll('mwc-tab');
136+
137+
for (const tab of tabs) {
138+
document.body.removeChild(tab);
139+
}
140+
});
141+
142+
test('don\'t throw if active is called before firstRender', async () => {
143+
const dummyClientRect = document.body.getBoundingClientRect();
144+
render(html`<mwc-tab></mwc-tab>`, document.body);
145+
const tab = document.body.querySelector('mwc-tab')!;
146+
assert.isFalse(
147+
!!(tab as unknown as {mdcFoundation: boolean}).mdcFoundation);
148+
149+
let didThrow = false;
150+
151+
try {
152+
tab.activate(dummyClientRect);
153+
} catch (e) {
154+
didThrow = true;
155+
}
156+
157+
assert.isFalse(didThrow);
158+
159+
await tab.updateComplete;
160+
await tab.updateComplete;
161+
162+
assert.isTrue(
163+
!!(tab as unknown as {mdcFoundation: boolean}).mdcFoundation);
164+
assert.isTrue(tab.active);
165+
});
166+
});
132167
});

0 commit comments

Comments
 (0)