Skip to content

Commit 30a1e9a

Browse files
committed
Button support
Lots of settings here...
1 parent 076ea77 commit 30a1e9a

File tree

1 file changed

+50
-5
lines changed

1 file changed

+50
-5
lines changed

simpletoast.js

Lines changed: 50 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
'font-style': 'italic',
1919
},
2020
shared: {
21-
display: 'inline-block',
2221
maxWidth: '320px',
2322
padding: '5px 8px',
2423
borderRadius: '3px',
@@ -33,15 +32,32 @@
3332
background: '#2980b9',
3433
},
3534
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',
3643
textShadow: '#173646 0px 0px 3px',
3744
background: '#2c9fea',
45+
mouseOver: {
46+
'border-color': 'rgba(27,31,35,0.35)',
47+
background: '#149FFF',
48+
},
3849
},
3950
};
4051

4152
function applyCSS(element, css = {}) {
53+
const old = {};
4254
Object.keys(css).forEach((key) => {
55+
const val = css[key];
56+
if (typeof val === 'object') return;
57+
old[key] = element.style[key];
4358
element.style[key] = css[key];
4459
});
60+
return old;
4561
}
4662

4763
const toasts = new Map();
@@ -75,7 +91,7 @@
7591
})();
7692
let count = 0;
7793

78-
function Toast({title, text, css, buttons, timeout}) {
94+
function Toast({title, text, css = {}, buttons, timeout}) {
7995
if (typeof arguments[0] === 'string') {
8096
text = arguments[0];
8197
}
@@ -85,7 +101,7 @@
85101
el.setAttribute('id', `AlertToast-${id}`);
86102
applyCSS(el, style.shared);
87103
applyCSS(el, style.toast);
88-
applyCSS(el, css);
104+
applyCSS(el, css.toast || css);
89105

90106
// Add title, body
91107
if (title) {
@@ -108,8 +124,37 @@
108124
toast.timeout = Date.now() + timeout;
109125
}
110126

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+
});
113158
}
114159
el.addEventListener('click', toast.close);
115160

0 commit comments

Comments
 (0)