1+ import { HXElement } from './HXElement' ;
2+ import shadowHtml from './HXToastElement.html' ;
3+ import shadowStyles from './HXToastElement.less' ;
4+
5+ const tagName = 'hx-toast' ;
6+ const template = document . createElement ( 'template' ) ;
7+ const ICONS = {
8+ 'error' : 'exclamation-circle' ,
9+ 'info' : 'info-circle' ,
10+ 'success' : 'checkmark' ,
11+ } ;
12+
13+ template . innerHTML = `
14+ <style>${ shadowStyles } </style>
15+ ${ shadowHtml }
16+ ` ;
17+
18+ export class HXToastElement extends HXElement {
19+ static get is ( ) {
20+ return tagName ;
21+ }
22+
23+ constructor ( ) {
24+ super ( tagName , template ) ;
25+ this . _onDismiss = this . _onDismiss . bind ( this ) ;
26+ this . _onSubmit = this . _onSubmit . bind ( this ) ;
27+ }
28+
29+ connectedCallback ( ) {
30+ this . $upgradeProperty ( 'cta' ) ;
31+ this . $upgradeProperty ( 'type' ) ;
32+
33+ this . _btnCta . addEventListener ( 'click' , this . _onSubmit ) ;
34+ this . _btnDismiss . addEventListener ( 'click' , this . _onDismiss ) ;
35+ }
36+
37+ disconnectedCallback ( ) {
38+ this . _btnCta . removeEventListener ( 'click' , this . _onSubmit ) ;
39+ this . _btnDismiss . removeEventListener ( 'click' , this . _onDismiss ) ;
40+ }
41+
42+ static get observedAttributes ( ) {
43+ return [
44+ 'cta' ,
45+ 'type' ,
46+ ] ;
47+ } //observedAttributes
48+
49+ attributeChangedCallback ( attr , oldVal , newVal ) {
50+ let hasValue = ( newVal !== null ) ;
51+ switch ( attr ) {
52+ case 'cta' :
53+ this . _btnCta . textContent = ( hasValue ? newVal : '' ) ;
54+ break ;
55+
56+ case 'type' :
57+ if ( hasValue ) {
58+ this . _elIcon . type = ( ICONS [ newVal ] || ICONS [ 'info' ] ) ;
59+ } else {
60+ this . _elIcon . type = ICONS [ 'info' ] ;
61+ }
62+ break ;
63+ }
64+ } //attributeChangedCallback()
65+
66+ // GETTERS
67+ get cta ( ) {
68+ return this . getAttribute ( 'cta' ) ;
69+ }
70+
71+ get type ( ) {
72+ return this . getAttribute ( 'type' ) ;
73+ }
74+
75+ // SETTERS
76+ set cta ( value ) {
77+ if ( value ) {
78+ this . setAttribute ( 'cta' , value ) ;
79+ } else {
80+ this . removeAttribute ( 'cta' ) ;
81+ }
82+ }
83+
84+ set type ( value ) {
85+ if ( value ) {
86+ this . setAttribute ( 'type' , value ) ;
87+ } else {
88+ this . removeAttribute ( 'type' ) ;
89+ }
90+ }
91+
92+ // PUBLIC METHODS
93+ dismiss ( ) {
94+ this . remove ( ) ;
95+ }
96+
97+ // PRIVATE METHODS
98+ _onDismiss ( evt ) {
99+ evt . preventDefault ( ) ;
100+
101+ if ( this . $emit ( 'dismiss' ) ) {
102+ // only if event was not canceled by consumer
103+ this . dismiss ( ) ;
104+ }
105+ }
106+
107+ _onSubmit ( evt ) {
108+ evt . preventDefault ( ) ;
109+ this . $emit ( 'submit' ) ;
110+ }
111+
112+ // PRIVATE GETTERS
113+ get _elIcon ( ) {
114+ return this . shadowRoot . getElementById ( 'icon' ) ;
115+ }
116+
117+ get _btnCta ( ) {
118+ return this . shadowRoot . getElementById ( 'cta' ) ;
119+ }
120+
121+ get _btnDismiss ( ) {
122+ return this . shadowRoot . getElementById ( 'dismiss' ) ;
123+ }
124+ }
0 commit comments