Toast.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. import "./toast.css";
  2. interface ToastOptions {
  3. html: string;
  4. displayLength: number;
  5. inDuration: number;
  6. outDuration: number;
  7. classes: string;
  8. completeCallback?: (toast: Toast) => void;
  9. activationPercent: number;
  10. }
  11. const defaultOptions: ToastOptions = {
  12. html: "",
  13. displayLength: 4000,
  14. inDuration: 300,
  15. outDuration: 375,
  16. classes: "",
  17. activationPercent: 0.7,
  18. };
  19. export class Toast {
  20. static _container: HTMLElement | null = null;
  21. static _toasts: Array<Toast> = [];
  22. element: HTMLElement;
  23. options: ToastOptions;
  24. isMoving: boolean;
  25. startTime: number;
  26. startingPosX: number;
  27. get activationDistance(): number {
  28. return this.element.offsetWidth * this.options.activationPercent;
  29. }
  30. constructor(options: Partial<ToastOptions>) {
  31. this.options = { ...defaultOptions, ...options };
  32. if (Toast._container == null) {
  33. Toast._createContainer();
  34. }
  35. this.startTime = Date.now();
  36. this.startingPosX = 0;
  37. this.isMoving = false;
  38. this.element = document.createElement("div");
  39. this.element.className = "toast " + this.options.classes;
  40. this.element.innerHTML = this.options.html;
  41. const onDragStart = (e: MouseEvent | TouchEvent) => {
  42. e.stopPropagation();
  43. this.startingPosX = this._getposX(e);
  44. this.element.style.transition = "all 0s";
  45. this.isMoving = true;
  46. document.addEventListener("touchmove", onDragMove);
  47. document.addEventListener("touchend", onDragEnd);
  48. document.addEventListener("mousemove", onDragMove);
  49. document.addEventListener("mouseup", onDragEnd);
  50. };
  51. const onDragMove = (e: MouseEvent | TouchEvent) => {
  52. e.stopPropagation();
  53. const totalDeltaX = this._getposX(e) - this.startingPosX;
  54. this.element.style.transform = `translateX(${totalDeltaX}px)`;
  55. this.element.style.opacity = "" + (1 - Math.abs(totalDeltaX / this.activationDistance));
  56. };
  57. const onDragEnd = (e: MouseEvent | TouchEvent) => {
  58. const totalDeltaX = this.startingPosX - this._getposX(e);
  59. this.isMoving = false;
  60. // Remove toast
  61. if (Math.abs(totalDeltaX) > this.activationDistance) {
  62. this.dismiss();
  63. // Animate toast back to original position
  64. } else {
  65. this.element.style.transition = "transform .2s, opacity .2s";
  66. this.element.style.transform = "";
  67. this.element.style.opacity = "";
  68. }
  69. document.removeEventListener("touchmove", onDragMove);
  70. document.removeEventListener("touchend", onDragEnd);
  71. document.removeEventListener("mousemove", onDragMove);
  72. document.removeEventListener("mouseup", onDragEnd);
  73. };
  74. this.element.addEventListener("touchstart", onDragStart);
  75. this.element.addEventListener("mousedown", onDragStart);
  76. this.element.style.top = `30px`;
  77. this.element.style.opacity = `0`;
  78. this.element.style.transition = `top ${this.options.inDuration}ms, opacity ${this.options.inDuration}ms`;
  79. if (this.options.displayLength < Infinity) {
  80. this.update();
  81. }
  82. Toast._container?.appendChild(this.element);
  83. setTimeout(() => {
  84. this.element.style.top = `0px`;
  85. this.element.style.opacity = `1`;
  86. }, 10);
  87. Toast._toasts.push(this);
  88. }
  89. update(): void {
  90. const ellapsed = Date.now() - this.startTime;
  91. if (this.options && ellapsed < this.options.displayLength) {
  92. this.element.style.setProperty(
  93. "--completion",
  94. Math.round((ellapsed / this.options.displayLength) * 100) + "%"
  95. );
  96. setTimeout(() => this.update(), 20);
  97. } else {
  98. this.dismiss();
  99. }
  100. }
  101. _getposX(e: MouseEvent | TouchEvent): number {
  102. if (e instanceof MouseEvent) {
  103. return e.clientX;
  104. } else {
  105. return e.touches[0].clientX;
  106. }
  107. }
  108. dismiss(): void {
  109. this.element.style.transition = `all ${this.options.outDuration}ms`;
  110. this.element.style.marginTop = `-${this.element.offsetHeight}px`;
  111. this.element.style.opacity = "0";
  112. setTimeout(() => {
  113. Toast._container?.removeChild(this.element);
  114. Toast._toasts = Toast._toasts.filter((e) => e !== this);
  115. if (Toast._toasts.length === 0) {
  116. Toast._removeContainer();
  117. }
  118. if (this.options.completeCallback) this.options.completeCallback(this);
  119. }, this.options.outDuration);
  120. }
  121. static dismissAll(): void {
  122. for (const toastIndex in Toast._toasts) {
  123. Toast._toasts[toastIndex].dismiss();
  124. }
  125. }
  126. static _createContainer(): void {
  127. const container = document.createElement("div");
  128. container.setAttribute("id", "toast-container");
  129. document.body.appendChild(container);
  130. Toast._container = container;
  131. }
  132. static _removeContainer(): void {
  133. if (Toast._container) {
  134. document.body.removeChild(Toast._container);
  135. Toast._container = null;
  136. }
  137. }
  138. }
  139. export default function (options: Partial<ToastOptions>): Toast {
  140. return new Toast(options);
  141. }