toast.html 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. <style>
  2. /* Inter Font */
  3. @import url('https://fonts.googleapis.com/css2?family=Inter:wght@100..900&display=swap');
  4. #dx-toast-template {
  5. display: none;
  6. visibility: hidden;
  7. }
  8. .dx-toast {
  9. position: absolute;
  10. top: 10px;
  11. right: 0;
  12. padding-right: 10px;
  13. user-select: none;
  14. transition: transform 0.2s ease;
  15. z-index: 2147483647;
  16. }
  17. .dx-toast .dx-toast-inner {
  18. transition: right 0.2s ease-out;
  19. position: fixed;
  20. background-color: #181B20;
  21. color: #ffffff;
  22. font-family: "Inter", sans-serif;
  23. display: grid;
  24. grid-template-columns: auto auto;
  25. min-width: 280px;
  26. min-height: 92px;
  27. width: min-content;
  28. border-radius: 5px;
  29. }
  30. .dx-toast:hover {
  31. cursor: pointer;
  32. transform: translateX(-5px);
  33. }
  34. .dx-toast .dx-toast-level-bar-container {
  35. height: 100%;
  36. width: 6px;
  37. }
  38. .dx-toast .dx-toast-level-bar-container .dx-toast-level-bar {
  39. width: 100%;
  40. height: 100%;
  41. border-radius: 5px 0px 0px 5px;
  42. }
  43. .dx-toast .dx-toast-content {
  44. padding: 13px;
  45. }
  46. .dx-toast .dx-toast-header {
  47. display: flex;
  48. flex-direction: row;
  49. justify-content: start;
  50. align-items: end;
  51. margin-bottom: 13px;
  52. }
  53. .dx-toast .dx-toast-header>svg {
  54. height: 22px;
  55. margin-right: 5px;
  56. }
  57. .dx-toast .dx-toast-header .dx-toast-header-text {
  58. font-size: 16px;
  59. font-weight: 700;
  60. padding: 0;
  61. margin: 0;
  62. }
  63. .dx-toast .dx-toast-msg {
  64. font-size: 14px;
  65. font-weight: 400;
  66. padding: 0;
  67. margin: 0;
  68. }
  69. .dx-toast-level-bar.info {
  70. background-color: #428EFF;
  71. }
  72. .dx-toast-level-bar.success {
  73. background-color: #42FF65;
  74. }
  75. .dx-toast-level-bar.error {
  76. background-color: #FF4242;
  77. }
  78. </style>
  79. <div id="dx-toast-template" class="dx-toast">
  80. <div class="dx-toast-inner" style="right:-300px;">
  81. <!-- Level/Color decor -->
  82. <div class="dx-toast-level-bar-container">
  83. <div class="dx-toast-level-bar info"></div>
  84. </div>
  85. <!-- Content -->
  86. <div class="dx-toast-content">
  87. <!-- Header -->
  88. <div class="dx-toast-header">
  89. <!-- Dioxus Logo -->
  90. <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 32 32" preserveAspectRatio="none">
  91. <path
  92. d="M22.158 1.783c0 3.077-.851 5.482-2.215 7.377s-3.32 3.557-5.447 5.33-4.425 3.657-6.252 6.195-3.102 5.515-3.102 9.532h4.699c0-3.077.853-5.377 2.217-7.272s3.32-3.557 5.447-5.33 4.425-3.657 6.252-6.195 3.102-5.62 3.102-9.637z"
  93. fill="#e96020" />
  94. <path
  95. d="M9.531 25.927c-.635 0-1.021.515-1.02 1.15s.385 1.151 1.02 1.15H22.47a1.151 1.151 0 1 0 0-2.301zm1.361-4.076c-.608 0-.954.558-.953 1.166s.346 1.035.953 1.035h10.217a1.101 1.101 0 1 0 0-2.201zm0-13.594a1.101 1.101 0 1 0 0 2.201h10.217c.607 0 .953-.598.953-1.205s-.345-.996-.953-.996zM9.531 4.021A1.15 1.15 0 0 0 8.38 5.17a1.15 1.15 0 0 0 1.15 1.15h12.94c.635 0 1.021-.498 1.02-1.133s-.386-1.166-1.02-1.166z"
  96. fill="#2d323b" />
  97. <path
  98. d="M5.142 1.783c0 4.016 1.275 7.099 3.102 9.637s4.125 4.422 6.252 6.195 4.083 3.656 5.447 5.551 2.215 3.974 2.215 7.051h4.701c0-4.016-1.275-7.038-3.102-9.576s-4.125-4.422-6.252-6.195-4.083-3.435-5.447-5.33S9.841 4.86 9.841 1.783z"
  99. fill="#00a8d6" />
  100. </svg>
  101. <!-- Toast Title Text -->
  102. <h3 class="dx-toast-header-text">Your app is being rebuilt.</h3>
  103. </div>
  104. <!-- Message -->
  105. <p class="dx-toast-msg">A non-hot-reloadable change occurred and we must rebuild.</p>
  106. </div>
  107. </div>
  108. </div>
  109. <script>
  110. const STORAGE_KEY = "SCHEDULED-DX-TOAST";
  111. let currentToast = null;
  112. let currentTimeout = null;
  113. // Show a toast, removing the previous one.
  114. function showDXToast(headerText, message, progressLevel, durationMs) {
  115. // Close current toast if exists.
  116. closeDXToast();
  117. // Clone template and add unique id.
  118. let toastTemplate = document.getElementById("dx-toast-template");
  119. let cloned = toastTemplate.cloneNode(true);
  120. let toastId = `dx-toast`;
  121. cloned.id = toastId;
  122. currentToast = cloned;
  123. let innerElem = currentToast.querySelector(`#${toastId} .dx-toast-inner`);
  124. // Set the progress level
  125. let progressBarElem = innerElem.querySelector(".dx-toast-inner .dx-toast-level-bar-container .dx-toast-level-bar");
  126. progressBarElem.className = `dx-toast-level-bar ${progressLevel}`;
  127. // Set header text
  128. let headerTextElem = innerElem.querySelector(".dx-toast-inner .dx-toast-header .dx-toast-header-text");
  129. headerTextElem.innerText = headerText;
  130. // Set message
  131. let messageElem = innerElem.querySelector(".dx-toast-inner .dx-toast-msg");
  132. messageElem.innerText = message;
  133. document.body.appendChild(currentToast);
  134. // Add listener to close toasts when clicked.
  135. // Safety: Calling `closeToast` removes the element and all event listeners with it.
  136. currentToast.addEventListener("click", closeDXToast);
  137. // Wait a bit of time so animation plays correctly.
  138. setTimeout(() => {
  139. innerElem.style.right = "0";
  140. currentTimeout = setTimeout(() => {
  141. closeDXToast();
  142. }, durationMs);
  143. }, 100);
  144. }
  145. // Schedule a toast to be displayed after reload.
  146. function scheduleDXToast(headerText, message, level, durationMs) {
  147. let data = {
  148. headerText,
  149. message,
  150. level,
  151. durationMs,
  152. };
  153. let jsonData = JSON.stringify(data);
  154. sessionStorage.setItem(STORAGE_KEY, jsonData);
  155. }
  156. // Close the current toast.
  157. function closeDXToast() {
  158. if (currentToast) {
  159. currentToast.remove();
  160. }
  161. clearTimeout(currentTimeout);
  162. }
  163. // Handle any scheduled toasts after reload.
  164. let potentialData = sessionStorage.getItem(STORAGE_KEY);
  165. if (potentialData) {
  166. sessionStorage.removeItem(STORAGE_KEY);
  167. let data = JSON.parse(potentialData);
  168. showDXToast(data.headerText, data.message, data.level, data.durationMs);
  169. }
  170. </script>