-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathindex.js
161 lines (142 loc) · 4.92 KB
/
index.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
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import { LitElement, html, css, unsafeCSS } from 'lit';
import { unsafeHTML } from 'lit/directives/unsafe-html.js';
import expandLess from '@axa-ch-webhub-cloud/materials/icons/material-design/expand_less.svg';
import expandMore from '@axa-ch-webhub-cloud/materials/icons/material-design/expand_more.svg';
import { classMap } from 'lit/directives/class-map.js';
import { defineVersioned } from '../../../utils/component-versioning';
import { sanitizeSVG } from '../../../utils/sanitize';
import applyDefaults from '../../../utils/apply-defaults';
import fireCustomEvent from '../../../utils/custom-event';
import createRefId from '../../../utils/create-ref-id';
import styles from './index.scss';
const collapseContent = el => {
// get the height of the element's inner content, regardless of its actual size
const contentHeight = el.scrollHeight;
// temporarily disable all css transitions
const savedTransition = el.style.transition;
el.style.transition = '';
// on the next frame (as soon as the previous style change has taken effect),
// explicitly set the element's height to its current pixel height, so we
// aren't transitioning out of 'height: auto'.
requestAnimationFrame(() => {
el.style.height = `${contentHeight}px`;
el.style.transition = savedTransition;
// on the next frame (as soon as the previous style change has taken effect),
// have the element transition to height: 0
requestAnimationFrame(() => {
el.style.height = 0;
});
});
};
const expandContent = el => {
// get the height of the element's inner content, regardless of its actual size
const contentHeight = el.scrollHeight;
// have the element transition to the height of its inner content
el.style.height = `${contentHeight}px`;
const expandAnimation = () => {
// remove "height" from the element's inline styles, so it can return to its initial value
el.style.removeProperty('height');
};
// when the next CSS transition finishes (which should be the one we just triggered)
el.addEventListener('transitionend', expandAnimation, { once: true });
};
class AXAAccordion extends LitElement {
static get tagName() {
return 'axa-accordion';
}
static get styles() {
return css`
${unsafeCSS(styles)}
`;
}
static get properties() {
return {
refId: { type: String, defaultValue: `accordion-${createRefId()}` },
small: { type: Boolean },
ariaLevel: { type: String },
open: { type: Boolean },
disabled: { type: Boolean },
icon: { type: String },
title: { type: String },
onStateChange: { type: Function, attribute: false },
};
}
constructor() {
super();
applyDefaults(this);
this.toggleAccordion = this.toggleAccordion.bind(this);
}
toggleAccordion() {
const accordionContent = this.shadowRoot.querySelector(
'.m-accordion__content'
);
if (!accordionContent || this.disabled) {
return;
}
if (this.open) {
collapseContent(accordionContent);
} else {
expandContent(accordionContent);
}
this.open = !this.open;
this.onStateChange(this.open);
fireCustomEvent('axa-state-change', this.open, this);
}
render() {
const { refId, open, title, small, disabled, icon, ariaLevel = '' } = this;
const titleContainerClasses = {
'm-accordion__header': true,
'm-accordion__header--disabled': disabled,
};
const titleClasses = {
'm-accordion__title': true,
'm-accordion__title--small': small,
};
const contentClasses = {
'm-accordion__content': true,
'm-accordion__content--open': open,
};
return html`
<div class="m-accordion">
<div
class="${classMap(titleContainerClasses)}"
aria-role="${ariaLevel ? 'heading' : ''}"
aria-level="${ariaLevel}"
@click="${this.toggleAccordion}"
>
<button
id="${refId}"
class="m-accordion__button"
aria-expanded="${open}"
aria-controls="${refId}-content"
?disabled="${disabled}"
>
<div class="m-accordion__button-flex-wrapper">
<div class="m-accordion__button-title-wrapper">
${icon
? html`<div class="m-accordion__icon">
${unsafeHTML(sanitizeSVG(icon))}
</div>`
: html``}
<span class="${classMap(titleClasses)}">${title}</span>
</div>
<div class="m-accordion__icon-expand">
${unsafeHTML(open ? expandLess : expandMore)}
</div>
</div>
</button>
</div>
<div
id="${refId}-content"
role="region"
aria-labelledby="${refId}"
class="${classMap(contentClasses)}"
>
<slot></slot>
</div>
</div>
`;
}
}
defineVersioned([AXAAccordion], __VERSION_INFO__);
export default AXAAccordion;