1
- import { ToasterNotification } from "./types" ;
1
+ import { NotificationButton , ToasterNotification } from "./types" ;
2
2
3
3
export class ToastComponent extends HTMLElement {
4
4
private settings : ToasterNotification ;
@@ -21,76 +21,39 @@ export class ToastComponent extends HTMLElement {
21
21
22
22
private render ( ) {
23
23
this . dataset . uid = this . settings . uid ;
24
-
25
24
for ( let i = 0 ; i < this . settings . classes . length ; i ++ ) {
26
25
this . classList . add ( this . settings . classes [ i ] ) ;
27
26
}
28
-
29
- if ( this . settings . icon ) {
30
- const icon = document . createElement ( "i" ) ;
31
- icon . innerHTML = this . settings . icon ;
32
- this . appendChild ( icon ) ;
33
- }
34
-
35
- const copyWrapper = document . createElement ( "copy-wrapper" ) ;
36
-
37
- const title = document . createElement ( "h3" ) ;
38
- title . innerText = this . settings . title ;
39
- if ( this . settings . closeable ) {
40
- title . setAttribute ( "role" , "alertdialog" ) ;
41
- } else {
42
- title . setAttribute ( "role" , "alert" ) ;
43
- }
44
- copyWrapper . appendChild ( title ) ;
45
-
46
- const message = document . createElement ( "p" ) ;
47
- message . innerText = this . settings . message ;
48
- copyWrapper . appendChild ( message ) ;
49
- this . appendChild ( copyWrapper ) ;
50
-
51
- if ( this . settings . buttons . length ) {
52
- const actionsWrapper = document . createElement ( "toast-actions" ) ;
53
-
54
- for ( let i = 0 ; i < this . settings . buttons . length ; i ++ ) {
55
- const button = document . createElement ( "button" ) ;
56
- button . innerText = this . settings . buttons [ i ] . label ;
57
- button . dataset . index = `${ i } ` ;
58
-
59
- for ( let k = 0 ; k < this . settings . buttons [ i ] ?. classes ?. length ; k ++ ) {
60
- button . classList . add ( this . settings . buttons [ i ] . classes [ k ] ) ;
61
- }
62
-
63
- if ( this . settings . buttons [ i ] ?. ariaLabel ) {
64
- button . setAttribute ( "aria-label" , this . settings . buttons [ i ] . ariaLabel ) ;
65
- }
66
-
67
- button . addEventListener ( "click" , this . handleActionButtonClick ) ;
68
-
69
- actionsWrapper . appendChild ( button ) ;
70
- }
71
-
72
- copyWrapper . appendChild ( actionsWrapper ) ;
73
- }
74
-
75
- if ( this . settings . closeable ) {
76
- const closeButton = document . createElement ( "button" ) ;
77
- closeButton . setAttribute ( "aria-label" , "close notification" ) ;
78
- closeButton . className = "close js-toast-close" ;
79
- closeButton . innerHTML =
80
- '<svg aria-hidden="true" focusable="false" data-prefix="far" data-icon="times" class="svg-inline--fa fa-times fa-w-10" role="img" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 320 512"><path fill="currentColor" d="M207.6 256l107.72-107.72c6.23-6.23 6.23-16.34 0-22.58l-25.03-25.03c-6.23-6.23-16.34-6.23-22.58 0L160 208.4 52.28 100.68c-6.23-6.23-16.34-6.23-22.58 0L4.68 125.7c-6.23 6.23-6.23 16.34 0 22.58L112.4 256 4.68 363.72c-6.23 6.23-6.23 16.34 0 22.58l25.03 25.03c6.23 6.23 16.34 6.23 22.58 0L160 303.6l107.72 107.72c6.23 6.23 16.34 6.23 22.58 0l25.03-25.03c6.23-6.23 6.23-16.34 0-22.58L207.6 256z"></path></svg>' ;
81
- closeButton . addEventListener ( "click" , this . handleCloseClickEvent ) ;
82
- this . appendChild ( closeButton ) ;
83
- }
84
-
85
- if ( this . settings . timer ) {
86
- const timer = document . createElement ( "toast-timer" ) ;
87
- timer . className = this . settings . timer ;
88
- if ( this . settings . timer === "horizontal" ) {
89
- timer . style . transform = "scaleX(1)" ;
90
- } else {
91
- timer . style . transform = "scaleY(1)" ;
92
- }
93
- this . append ( timer ) ;
27
+ this . innerHTML = `
28
+ ${ this . settings . icon ? `
29
+ <i>${ this . settings . icon } </i>
30
+ ` : "" }
31
+ <copy-wrapper>
32
+ <h3 role="${ this . settings . closeable ? "alertdialog" : "alert" } ">${ this . settings . title } </h3>
33
+ <p>${ this . settings . message } </p>
34
+ ${ this . settings . buttons . length ? `
35
+ <toast-actions>
36
+ ${ this . settings . buttons . map ( ( button :NotificationButton , index :number ) => {
37
+ return `<button class="${ button . classes ?. join ( " " ) } " data-index="${ index } " ${ button ?. ariaLabel ? `aria-label="${ button . ariaLabel } "` : "" } >${ button . label } </button>` ;
38
+ } ) }
39
+ </toast-actions>
40
+ ` : "" }
41
+ </copy-wrapper>
42
+ ${ this . settings . closeable ? `
43
+ <button aria-label="close notification" class="close js-toast-close">
44
+ <svg aria-hidden="true" focusable="false" data-prefix="far" data-icon="times" class="svg-inline--fa fa-times fa-w-10" role="img" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 320 512"><path fill="currentColor" d="M207.6 256l107.72-107.72c6.23-6.23 6.23-16.34 0-22.58l-25.03-25.03c-6.23-6.23-16.34-6.23-22.58 0L160 208.4 52.28 100.68c-6.23-6.23-16.34-6.23-22.58 0L4.68 125.7c-6.23 6.23-6.23 16.34 0 22.58L112.4 256 4.68 363.72c-6.23 6.23-6.23 16.34 0 22.58l25.03 25.03c6.23 6.23 16.34 6.23 22.58 0L160 303.6l107.72 107.72c6.23 6.23 16.34 6.23 22.58 0l25.03-25.03c6.23-6.23 6.23-16.34 0-22.58L207.6 256z"></path></svg>
45
+ </button>
46
+ ` : "" }
47
+ ${ this . settings . timer ? `
48
+ <toast-timer class="${ this . settings . timer } " style="transform: ${ this . settings . timer === "horizontal" ? "scaleX(1)" : "scaleY(1)" } ;"></toast-timer>
49
+ ` : "" }
50
+ ` ;
51
+ this . querySelectorAll ( "button[data-index]" ) . forEach ( button => {
52
+ button . addEventListener ( "click" , this . handleActionButtonClick ) ;
53
+ } ) ;
54
+ const closeBttn = this . querySelector ( ".js-toast-close" ) ;
55
+ if ( closeBttn ) {
56
+ closeBttn . addEventListener ( "click" , this . handleCloseClickEvent ) ;
94
57
}
95
58
}
96
59
@@ -105,7 +68,7 @@ export class ToastComponent extends HTMLElement {
105
68
}
106
69
if ( this . settings . buttons . length ) {
107
70
for ( let i = 0 ; i < this . settings . buttons . length ; i ++ ) {
108
- if ( this . settings . buttons [ i ] . autofocus ) {
71
+ if ( this . settings . buttons [ i ] ? .autofocus ) {
109
72
const button :HTMLButtonElement = this . querySelector ( `button[data-index="${ i } "]` ) ;
110
73
if ( button ) {
111
74
button . focus ( ) ;
0 commit comments