push.js 928 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. const push = (name, action, customisation = null) => {
  2. if (action !== null && typeof(action) !== "function") {
  3. throw new TypeError("Action must be an function.");
  4. }
  5. if (typeof(name) !== "string") {
  6. throw new TypeError("Name of the push must be string.");
  7. }
  8. if (customisation !== null && typeof(customisation) !== "function") {
  9. throw new TypeError("Customisation must be null or function");
  10. }
  11. const target = document.createElement("button");
  12. target.type = "button";
  13. target.name = name;
  14. target.id = "push-" + name;
  15. target.innerText = name
  16. .toUpperCase()
  17. .replaceAll("-", " ")
  18. .replaceAll("_", " ");
  19. target.classList.add("push");
  20. if (action !== null) {
  21. target.addEventListener("click", action);
  22. }
  23. if (customisation) {
  24. customisation(target);
  25. }
  26. return target;
  27. };
  28. export { push };