-
Notifications
You must be signed in to change notification settings - Fork 484
/
Copy pathopensearch-collapsible-panel.html
138 lines (128 loc) · 5.09 KB
/
opensearch-collapsible-panel.html
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<template id="opensearch_collapsible_panel_tpl">
<style>
:host {
background: inherit;
font-size: inherit;
font-weight: inherit;
display: block;
}
.collapsible-toggle {
background: inherit;
font-weight: inherit;
line-height: 1.3;
font-size: inherit;
display: flex;
min-height: 1.3em;
align-items: center;
justify-content: flex-start;
flex-direction: row;
height: auto;
width: auto;
}
:host([toggle-button-position="left"]) > .collapsible-toggle {
flex-direction: row-reverse;
}
.collapsible-content {
position: sticky;
top: 0;
width: 100%;
overflow-x: hidden;
height: 0;
transition-property: all;
transition-duration: 0.3s;
transition-delay: 0.1s;
transition-timing-function: ease-out;
opacity: 0.3;
}
:host([expanded]) .collapsible-content {
height: var(--expanded-height, fit-content);
opacity: 1;
}
</style>
<div class="collapsible-toggle">
<slot name="toggle-slot"></slot>
</div>
<div class="collapsible-content" aria-expanded="false">
<slot name="content-slot"></slot>
</div>
</template>
<script type="module">
class OpenSearchCollapsiblePanel extends HTMLElement {
static get observedAttributes() {
return [
'expanded-height',
'transition-delay',
'transition-duration',
'transition-timing-function',
'toggle-button-position',
'expanded'
];
}
constructor() {
super();
this.attachShadow({ mode: 'open' });
this.shadowRoot.appendChild(document.getElementById('opensearch_collapsible_panel_tpl').content.cloneNode(true));
this.toggle = this.toggle.bind(this);
}
fireExpandStateChangeEvent(panelHeight) {
const collapsibleContent = this.shadowRoot.querySelector('.collapsible-content');
const isExpanded = this.toggleAttribute('expanded');
collapsibleContent.setAttribute('aria-expanded', isExpanded);
this.dispatchEvent(new CustomEvent('expand-state-change', {
bubbles: true,
composed: true,
detail: {
isExpanded,
panelHeight,
},
}));
}
toggle(event) {
event.preventDefault();
const nestedCollapsiblePanels = this.findNestedCollapsiblePanels();
if (nestedCollapsiblePanels?.length > 0) {
const panelHeights = nestedCollapsiblePanels.map(
panel => panel.hasAttribute('expanded') && panel.getAttribute('expanded') !== 'false' ?
parseInt(panel.getAttribute('expanded-height'), 10) :
0
);
const sumOfNestedCollapsiblePanels = panelHeights.reduce((carry, current) => carry + current, 0);
this.fireExpandStateChangeEvent(sumOfNestedCollapsiblePanels);
} else {
const expandedHeight = this.getAttribute('expanded-height');
this.style.setProperty('--expanded-height', expandedHeight);
this.fireExpandStateChangeEvent(expandedHeight);
}
}
connectedCallback() {
const expandedHeight = this.getAttribute('expanded-height');
if (expandedHeight) {
this.style.setProperty('--expanded-height', expandedHeight);
} else {
const collapsibleContent = this.shadowRoot.querySelector('.collapsible-content');
const contentHeight = collapsibleContent.scrollHeight;
if (Number(contentHeight) > 0) {
const heightProp = `${contentHeight}px`;
this.style.setProperty('--expanded-height', heightProp);
this.setAttribute('expanded-height', heightProp);
}
}
this.shadowRoot.querySelector('.collapsible-toggle').addEventListener('click', this.toggle);
}
disconnectedCallback() {
this.shadowRoot.querySelector('.collapsible-toggle').removeEventListener('click', this.toggle);
}
findNestedCollapsiblePanels() {
const nestedCollapsiblePanels = this.querySelector(
'slot'
)?.assignedElements?.()?.map?.(
element => element.shadowRoot.querySelectorAll(
'opensearch-collapsible-panel'
)
)?.filter?.(panels => (panels?.length ?? 0) > 0)
?.reduce?.((carry, current) => [...carry, ...current], []);
return nestedCollapsiblePanels;
}
}
customElements.define('opensearch-collapsible-panel', OpenSearchCollapsiblePanel);
</script>