|
18 | 18 | 'font-style': 'italic', |
19 | 19 | }, |
20 | 20 | shared: { |
21 | | - display: 'inline-block', |
22 | 21 | maxWidth: '320px', |
23 | 22 | padding: '5px 8px', |
24 | 23 | borderRadius: '3px', |
|
33 | 32 | background: '#2980b9', |
34 | 33 | }, |
35 | 34 | button: { |
| 35 | + height: '20px', |
| 36 | + margin: '-3px 0 0 3px', |
| 37 | + padding: '0 5px', |
| 38 | + verticalAlign: 'middle', |
| 39 | + whiteSpace: 'nowrap', |
| 40 | + border: '1px solid rgba(27,31,35,0.2)', |
| 41 | + borderRadius: '10px', |
| 42 | + fontSize: '11px', |
36 | 43 | textShadow: '#173646 0px 0px 3px', |
37 | 44 | background: '#2c9fea', |
| 45 | + mouseOver: { |
| 46 | + 'border-color': 'rgba(27,31,35,0.35)', |
| 47 | + background: '#149FFF', |
| 48 | + }, |
38 | 49 | }, |
39 | 50 | }; |
40 | 51 |
|
41 | 52 | function applyCSS(element, css = {}) { |
| 53 | + const old = {}; |
42 | 54 | Object.keys(css).forEach((key) => { |
| 55 | + const val = css[key]; |
| 56 | + if (typeof val === 'object') return; |
| 57 | + old[key] = element.style[key]; |
43 | 58 | element.style[key] = css[key]; |
44 | 59 | }); |
| 60 | + return old; |
45 | 61 | } |
46 | 62 |
|
47 | 63 | const toasts = new Map(); |
|
75 | 91 | })(); |
76 | 92 | let count = 0; |
77 | 93 |
|
78 | | - function Toast({title, text, css, buttons, timeout}) { |
| 94 | + function Toast({title, text, css = {}, buttons, timeout}) { |
79 | 95 | if (typeof arguments[0] === 'string') { |
80 | 96 | text = arguments[0]; |
81 | 97 | } |
|
85 | 101 | el.setAttribute('id', `AlertToast-${id}`); |
86 | 102 | applyCSS(el, style.shared); |
87 | 103 | applyCSS(el, style.toast); |
88 | | - applyCSS(el, css); |
| 104 | + applyCSS(el, css.toast || css); |
89 | 105 |
|
90 | 106 | // Add title, body |
91 | 107 | if (title) { |
|
108 | 124 | toast.timeout = Date.now() + timeout; |
109 | 125 | } |
110 | 126 |
|
111 | | - if (buttons) { |
112 | | - // TODO: Add buttons |
| 127 | + if (typeof buttons === 'object') { |
| 128 | + if (!Array.isArray(buttons)) { |
| 129 | + buttons = [buttons]; |
| 130 | + } |
| 131 | + buttons.forEach((button) => { |
| 132 | + if (!button.text) return; |
| 133 | + const elb = document.createElement('button'); |
| 134 | + elb.innerHTML = button.text; |
| 135 | + applyCSS(elb, style.button); |
| 136 | + applyCSS(elb, css.button); |
| 137 | + applyCSS(elb, button.css); |
| 138 | + if (typeof button.onclick === 'function') { |
| 139 | + elb.onclick = button.onclick; |
| 140 | + } |
| 141 | + let prev = {}; |
| 142 | + elb.onmouseover = () => { |
| 143 | + // Apply default style |
| 144 | + const original = applyCSS(elb, style.button.mouseOver); |
| 145 | + // Apply CSS style |
| 146 | + const custom = applyCSS(elb, css.button && css.button.mouseOver); |
| 147 | + // Apply button style |
| 148 | + const custom2 = applyCSS(elb, button.css && button.css.mouseOver); |
| 149 | + // Remember the original styles, do this in reverse |
| 150 | + Object.assign(prev, custom2, custom, original); |
| 151 | + }; |
| 152 | + elb.onmouseout = () => { |
| 153 | + applyCSS(elb, prev); |
| 154 | + prev = {}; |
| 155 | + }; |
| 156 | + el.appendChild(elb); |
| 157 | + }); |
113 | 158 | } |
114 | 159 | el.addEventListener('click', toast.close); |
115 | 160 |
|
|
0 commit comments