-
Notifications
You must be signed in to change notification settings - Fork 25
/
dropdown-tabs.js
63 lines (53 loc) · 1.94 KB
/
dropdown-tabs.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import { css, LitElement } from 'lit';
import { DropdownContentMixin } from './dropdown-content-mixin.js';
import { dropdownContentStyles } from './dropdown-content-styles.js';
/**
* A container for a "d2l-tabs" component. It provides additional support on top of "d2l-dropdown-content" for automatic resizing when the tab changes.
* @slot - Anything inside of "d2l-dropdown-content" that isn't in the "header" or "footer" slots appears as regular content
* @slot header - Sticky container at the top of the dropdown
* @slot footer - Sticky container at the bottom of the dropdown
* @fires d2l-dropdown-open - Dispatched when the dropdown is opened
*/
class DropdownTabs extends DropdownContentMixin(LitElement) {
static get styles() {
return [ dropdownContentStyles, css`
::slotted(d2l-tabs) {
margin-bottom: 0;
}
`];
}
firstUpdated(changedProperties) {
super.firstUpdated(changedProperties);
this.addEventListener('d2l-dropdown-open', this._onOpen);
this.addEventListener('d2l-menu-resize', this._onMenuResize);
this.addEventListener('d2l-tab-panel-selected', this._onTabSelected);
}
render() {
return this._renderContent();
}
_getTabsElement() {
if (!this.shadowRoot) return undefined;
return this.shadowRoot.querySelector('.d2l-dropdown-content-container > slot')
.assignedNodes()
.filter(node => node.hasAttribute && node.tagName === 'D2L-TABS')[0];
}
_onMenuResize(e) {
const tabs = this._getTabsElement();
const tabListRect = tabs.getTabListRect();
// need to include height of tablist, dropdown padding, tab margins
const rect = {
height: e.detail.height + tabListRect.height + 52,
width: e.detail.width
};
this.__position(rect, { updateAboveBelow: this._initializingHeight });
this._initializingHeight = false;
}
_onOpen(e) {
if (e.target !== this) return;
this._initializingHeight = true;
}
_onTabSelected() {
this.__position();
}
}
customElements.define('d2l-dropdown-tabs', DropdownTabs);