forked from axa-ch-webhub-cloud/pattern-library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
204 lines (177 loc) · 5.48 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import { css, html, unsafeCSS } from 'lit';
import { classMap } from 'lit/directives/class-map.js';
/* eslint-disable import/no-extraneous-dependencies */
import AXAContainer from '@axa-ch/container';
import AXAButton from '@axa-ch/button';
import AXAButtonLink from '@axa-ch/button-link';
import AXAIcon from '@axa-ch/icon';
import InlineStyles from '../../../utils/inline-styles';
import childStyles from './child.scss';
import {
defineVersioned,
versionedHtml,
} from '../../../utils/component-versioning';
import applyDefaults from '../../../utils/apply-defaults';
import styles from './index.scss';
import fireCustomEvent from '../../../utils/custom-event';
/**
* We need InlineStyles to give children some margins.
*/
class AXATopContentBar extends InlineStyles {
static get tagName() {
return 'axa-top-content-bar';
}
static get styles() {
return css`
${unsafeCSS(styles)}
`;
}
static get properties() {
// Define properties and types
return {
ctatext: { type: String },
href: { type: String },
variant: { type: String },
icon: { type: String },
stickymobile: { type: Boolean },
closable: { type: Boolean },
closed: { type: Boolean },
onClick: { type: Function, attribute: false },
};
}
static get childStyles() {
return childStyles;
}
constructor() {
super();
applyDefaults(this);
defineVersioned(
[AXAButton, AXAButtonLink, AXAContainer, AXAIcon],
__VERSION_INFO__,
this
);
}
onClose() {
this.closed = true;
// The sessionStorage item 'top-content-bar-closed' needs to be set to make
// sure the bar will not be opened during the current browser session.
// sessionStorage can be used only in the core module, so the event will be
// fired here and handled in core module by adding the 'top-content-bar-closed'
// item to sessionStorage.
fireCustomEvent('axa-top-content-bar-close', null, this);
}
firstUpdated() {
this.inlineStyles('childStyles');
const links = [...this.querySelectorAll('axa-link')];
links.forEach(link => {
link.setAttribute('variant', 'hyperlink-white-underline');
});
}
/* eslint-disable indent */
getButtonHtml() {
const { ctatext, href } = this;
if (href && ctatext) {
return versionedHtml(this)`
<axa-button-link
class="m-top-content-bar__button-link js-button-link"
href="${href}"
@click="${ev => {
if (typeof this.onClick === 'function') {
ev.preventDefault();
this.onClick();
}
}}"
variant="inverted"
size="small"
>
${ctatext}
</axa-button-link>
`;
}
if (ctatext) {
return versionedHtml(this)`
<axa-button
class="m-top-content-bar__button js-button"
@click="${ev => {
if (typeof this.onClick === 'function') {
ev.preventDefault();
this.onClick();
}
}}"
variant="inverted"
size="small"
>
${ctatext}
</axa-button>
`;
}
return '';
}
render() {
const { variant, icon, stickymobile, closable, closed } = this;
if (closed) return html``;
const btnHtml = this.getButtonHtml();
const variantClasses = {
'm-top-content-bar__container--warning': variant === 'warning',
'm-top-content-bar__container--success': variant === 'success',
'm-top-content-bar__container--attention': variant === 'attention',
};
const rootClasses = {
'm-top-content-bar__bottom-mobile': stickymobile,
};
const iconClasses = {
'hide-icon': !icon,
};
const closeClasses = {
'hide-close': !closable,
};
const buttonClasses = {
'hide-button': !btnHtml,
};
return versionedHtml(this)`
<article class="m-top-content-bar ${classMap(rootClasses)}">
<div class="m-top-content-bar__container ${classMap(variantClasses)}">
<div class="m-top-content-bar__container-flex">
<div class="m-top-content-bar-empty"></div>
<div class="m-top-content-bar__container-component">
<div class="m-top-content-bar__children">
<div class="m-top-content-bar__icon ${classMap(iconClasses)}">
<axa-icon size="large" icon="${icon}"></axa-icon>
</div>
<div class="m-top-content-bar__content-text">
<slot></slot>
</div>
<div class="m-top-content-bar__content-button ${classMap(
buttonClasses
)}">
${btnHtml}
</div>
</div>
</div>
<div class="m-top-content-bar__close-button ${classMap(
closeClasses
)}" @click="${this.onClose}">
<axa-icon icon="close"></axa-icon>
</div>
${
closable
? html``
: html` <div class="m-top-content-bar-empty"></div> `
}
</div>
${
btnHtml
? html`
<div class="m-top-content-bar__content-button-mobile">
${btnHtml}
</div>
`
: html``
}
</div>
</article>
`;
}
}
defineVersioned([AXATopContentBar], __VERSION_INFO__);
export default AXATopContentBar;