Skip to content

Commit 9662a7c

Browse files
authored
Merge pull request #8 from CGWebDev2003/feature/add-toast-class
Add toast class
2 parents 152cb06 + c99c5e9 commit 9662a7c

File tree

2 files changed

+76
-1
lines changed

2 files changed

+76
-1
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1313

1414
- Badge class
1515
- Button class
16-
- Link class
16+
- Link class
17+
- Spinner class
18+
- Toast class

neptune.js

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,4 +237,77 @@ export class Spinner {
237237
document.body.appendChild(newSpinner);
238238
}
239239
}
240+
}
241+
242+
/**
243+
* @class Toast
244+
* @description Create a new Neptune Toast
245+
*
246+
* @param {any} config Add your configuration
247+
*
248+
* parent -> class or id of your target element, when null its document.body
249+
*
250+
* icon -> add your icon
251+
*
252+
* text -> add your message text
253+
*
254+
* style -> primary, cta, information, success, warning, error
255+
*
256+
* position -> left-top, left-bottom, right-top, right-bottom
257+
*
258+
* @example
259+
* const myToast = new Toast({
260+
* icon: "YOUR ICON",
261+
* text: "Test Toast",
262+
* style: "primary",
263+
* position: "top-right"
264+
* });
265+
*/
266+
export class Toast {
267+
constructor(config) {
268+
// Create Toast
269+
const newToast = document.createElement('div');
270+
newToast.classList.add('toast');
271+
272+
// Add Icon
273+
if (config.icon != null) {
274+
// Create Icon
275+
const newIcon = document.createElement('span');
276+
newIcon.classList.add('toast-icon');
277+
newIcon.innerHTML = config.icon;
278+
279+
// Append icon
280+
newToast.appendChild(newIcon);
281+
}
282+
283+
// Add Message
284+
if (config.text != null) {
285+
// Create Message
286+
const newMessage = document.createElement('p');
287+
newMessage.classList.add('toast-text', 'text-l');
288+
newMessage.innerText = config.text;
289+
290+
// Append Message
291+
newToast.appendChild(newMessage);
292+
} else {
293+
console.error('Error! Please ad a Message to your Toast!');
294+
}
295+
296+
// Setup Style
297+
if (config.style != null) {
298+
newToast.classList.add('toast-' + config.style);
299+
}
300+
301+
// Setup Position
302+
if (config.position != null) {
303+
newToast.classList.add('toast-' + config.position);
304+
}
305+
306+
// Append to parent element
307+
if (config.parent != null) {
308+
document.querySelector(config.parent).appendChild(newToast);
309+
} else {
310+
document.body.appendChild(newToast);
311+
}
312+
}
240313
}

0 commit comments

Comments
 (0)