You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

439 lines
12 KiB

7 months ago
  1. # Toastify
  2. ![Built with JavaScript](https://img.shields.io/badge/Built%20with-JavaScript-red?style=for-the-badge&logo=javascript)
  3. [![toastify-js](https://img.shields.io/badge/toastify--js-1.12.0-brightgreen.svg)](https://www.npmjs.com/package/toastify-js)
  4. ![MIT License](https://img.shields.io/npm/l/toastify-js)
  5. Toastify is a lightweight, vanilla JS toast notification library.
  6. ## Demo
  7. [Click here](https://apvarun.github.io/toastify-js/)
  8. ## Features
  9. * Multiple stacked notifications
  10. * Customizable
  11. * No blocking of execution thread
  12. ### Customization options
  13. * Notification Text
  14. * Duration
  15. * Toast background color
  16. * Close icon display
  17. * Display position
  18. * Offset position
  19. ## Installation
  20. #### Toastify now supports installation via NPM
  21. * Run the below command to add toastify-js to your exisitng or new project.
  22. ```
  23. npm install --save toastify-js
  24. ```
  25. or
  26. ```
  27. yarn add toastify-js -S
  28. ```
  29. * Import toastify-js into your module to start using it.
  30. ```
  31. import Toastify from 'toastify-js'
  32. ```
  33. You can use the default CSS from Toastify as below and later override it or choose to write your own CSS.
  34. ```
  35. import "toastify-js/src/toastify.css"
  36. ```
  37. #### Adding ToastifyJs to HTML page using the traditional method
  38. To start using **Toastify**, add the following CSS on to your page.
  39. ```html
  40. <link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/toastify-js/src/toastify.min.css">
  41. ```
  42. And the script at the bottom of the page
  43. ```html
  44. <script type="text/javascript" src="https://cdn.jsdelivr.net/npm/toastify-js"></script>
  45. ```
  46. > Files are delivered via the CDN service provided by [jsdeliver](https://www.jsdelivr.com/)
  47. ## Documentation
  48. ```javascript
  49. Toastify({
  50. text: "This is a toast",
  51. duration: 3000,
  52. destination: "https://github.com/apvarun/toastify-js",
  53. newWindow: true,
  54. close: true,
  55. gravity: "top", // `top` or `bottom`
  56. position: "left", // `left`, `center` or `right`
  57. stopOnFocus: true, // Prevents dismissing of toast on hover
  58. style: {
  59. background: "linear-gradient(to right, #00b09b, #96c93d)",
  60. },
  61. onClick: function(){} // Callback after click
  62. }).showToast();
  63. ```
  64. > Toast messages will be centered on devices with screen width less than 360px.
  65. * See the [changelog](https://github.com/apvarun/toastify-js/blob/master/CHANGELOG.md)
  66. ### Add own custom classes
  67. If you want to use custom classes on the toast for customizing (like info or warning for example), you can do that as follows:
  68. ```javascript
  69. Toastify({
  70. text: "This is a toast",
  71. className: "info",
  72. style: {
  73. background: "linear-gradient(to right, #00b09b, #96c93d)",
  74. }
  75. }).showToast();
  76. ```
  77. Multiple classes also can be assigned as a string, with spaces between class names.
  78. ### Add some offset
  79. If you want to add offset to the toast, you can do that as follows:
  80. ```javascript
  81. Toastify({
  82. text: "This is a toast with offset",
  83. offset: {
  84. x: 50, // horizontal axis - can be a number or a string indicating unity. eg: '2em'
  85. y: 10 // vertical axis - can be a number or a string indicating unity. eg: '2em'
  86. },
  87. }).showToast();
  88. ```
  89. Toast will be pushed 50px from right in x axis and 10px from top in y axis.
  90. **Note:**
  91. If `position` is equals to `left`, it will be pushed from left.
  92. If `gravity` is equals to `bottom`, it will be pushed from bottom.
  93. ## API
  94. | Option Key | type | Usage | Defaults |
  95. |-----------------|----------------------|----------------------------------------------------------------------------|-------------|
  96. | text | string | Message to be displayed in the toast | "Hi there!" |
  97. | node | ELEMENT_NODE | Provide a node to be mounted inside the toast. `node` takes higher precedence over `text` | |
  98. | duration | number | Duration for which the toast should be displayed.<br>-1 for permanent toast | 3000 |
  99. | selector | string \| ELEMENT_NODE | ShadowRoot | CSS Selector or Element Node on which the toast should be added | body |
  100. | destination | URL string | URL to which the browser should be navigated on click of the toast | |
  101. | newWindow | boolean | Decides whether the `destination` should be opened in a new window or not | false |
  102. | close | boolean | To show the close icon or not | false |
  103. | gravity | "top" or "bottom" | To show the toast from top or bottom | "top" |
  104. | position | "left" or "right" | To show the toast on left or right | "right" |
  105. | backgroundColor | CSS background value | To be deprecated, use `style.background` option instead. Sets the background color of the toast | |
  106. | avatar | URL string | Image/icon to be shown before text | |
  107. | className | string | Ability to provide custom class name for further customization | |
  108. | stopOnFocus | boolean | To stop timer when hovered over the toast (Only if duration is set) | true |
  109. | callback | Function | Invoked when the toast is dismissed | |
  110. | onClick | Function | Invoked when the toast is clicked | |
  111. | offset | Object | Ability to add some offset to axis | |
  112. | escapeMarkup | boolean | Toggle the default behavior of escaping HTML markup | true |
  113. | style | object | Use the HTML DOM Style properties to add any style directly to toast | |
  114. | ariaLive | string | Announce the toast to screen readers, see https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/ARIA_Live_Regions for options | "polite" |
  115. | oldestFirst | boolean | Set the order in which toasts are stacked in page | true |
  116. > Deprecated properties: `backgroundColor` - use `style.background` option instead
  117. ## Browsers support
  118. | ![][ie]<br />IE / Edge | ![][firefox]<br />Firefox | ![][chrome]<br />Chrome | ![][safari]<br />Safari | ![][opera]<br />Opera |
  119. | ---------------------- | ------------------------- | ----------------------- | ----------------------- | --------------------- |
  120. | IE10, IE11, Edge | last 10 versions | last 10 versions | last 10 versions | last 10 versions |
  121. ## Contributors
  122. <!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
  123. <!-- prettier-ignore -->
  124. <table>
  125. <tr>
  126. <td align="center">
  127. <a href="https://github.com/AStoker">
  128. <img
  129. alt="AStoker"
  130. src="https://avatars.githubusercontent.com/u/2907279?v=4"
  131. width="117"
  132. />
  133. <br />
  134. AStoker
  135. </a>
  136. </td>
  137. <td align="center">
  138. <a href="https://github.com/caiomoura1994">
  139. <img
  140. alt="caiomoura1994"
  141. src="https://avatars.githubusercontent.com/u/9389139?v=4"
  142. width="117"
  143. />
  144. <br />
  145. caiomoura1994
  146. </a>
  147. </td>
  148. <td align="center">
  149. <a href="https://github.com/rndevfx">
  150. <img
  151. alt="rndevfx"
  152. src="https://avatars.githubusercontent.com/u/5052076?v=4"
  153. width="117"
  154. />
  155. <br />
  156. rndevfx
  157. </a>
  158. </td>
  159. <td align="center">
  160. <a href="https://github.com/1ess">
  161. <img
  162. alt="1ess"
  163. src="https://avatars.githubusercontent.com/u/36092926?v=4"
  164. width="117"
  165. />
  166. <br />
  167. 1ess
  168. </a>
  169. </td>
  170. <td align="center">
  171. <a href="https://github.com/d4rn0k">
  172. <img
  173. alt="d4rn0k"
  174. src="https://avatars.githubusercontent.com/u/2183269?v=4"
  175. width="117"
  176. />
  177. <br />
  178. d4rn0k
  179. </a>
  180. </td>
  181. <td align="center">
  182. <a href="https://github.com/danielkaiser80">
  183. <img
  184. alt="danielkaiser80"
  185. src="https://avatars.githubusercontent.com/u/33827555?v=4"
  186. width="117"
  187. />
  188. <br />
  189. danielkaiser80
  190. </a>
  191. </td>
  192. </tr>
  193. <tr>
  194. <td align="center">
  195. <a href="https://github.com/skjnldsv">
  196. <img
  197. alt="skjnldsv"
  198. src="https://avatars.githubusercontent.com/u/14975046?v=4"
  199. width="117"
  200. />
  201. <br />
  202. skjnldsv
  203. </a>
  204. </td>
  205. <td align="center">
  206. <a href="https://github.com/chasedeanda">
  207. <img
  208. alt="chasedeanda"
  209. src="https://avatars.githubusercontent.com/u/8203134?v=4"
  210. width="117"
  211. />
  212. <br />
  213. chasedeanda
  214. </a>
  215. </td>
  216. <td align="center">
  217. <a href="https://github.com/chrisgraham">
  218. <img
  219. alt="chrisgraham"
  220. src="https://avatars.githubusercontent.com/u/195389?v=4"
  221. width="117"
  222. />
  223. <br />
  224. chrisgraham
  225. </a>
  226. </td>
  227. <td align="center">
  228. <a href="https://github.com/Wachiwi">
  229. <img
  230. alt="Wachiwi"
  231. src="https://avatars.githubusercontent.com/u/4199845?v=4"
  232. width="117"
  233. />
  234. <br />
  235. Wachiwi
  236. </a>
  237. </td>
  238. <td align="center">
  239. <a href="https://github.com/FeixuRuins">
  240. <img
  241. alt="FeixuRuins"
  242. src="https://avatars.githubusercontent.com/u/66232834?v=4"
  243. width="117"
  244. />
  245. <br />
  246. FeixuRuins
  247. </a>
  248. </td>
  249. <td align="center">
  250. <a href="https://github.com/gavinhungry">
  251. <img
  252. alt="gavinhungry"
  253. src="https://avatars.githubusercontent.com/u/744538?v=4"
  254. width="117"
  255. />
  256. <br />
  257. gavinhungry
  258. </a>
  259. </td>
  260. </tr>
  261. <tr>
  262. <td align="center">
  263. <a href="https://github.com/haydster7">
  264. <img
  265. alt="haydster7"
  266. src="https://avatars.githubusercontent.com/u/4540595?v=4"
  267. width="117"
  268. />
  269. <br />
  270. haydster7
  271. </a>
  272. </td>
  273. <td align="center">
  274. <a href="https://github.com/joaquinwojcik">
  275. <img
  276. alt="joaquinwojcik"
  277. src="https://avatars.githubusercontent.com/u/3205737?v=4"
  278. width="117"
  279. />
  280. <br />
  281. joaquinwojcik
  282. </a>
  283. </td>
  284. <td align="center">
  285. <a href="https://github.com/juliushaertl">
  286. <img
  287. alt="juliushaertl"
  288. src="https://avatars.githubusercontent.com/u/3404133?v=4"
  289. width="117"
  290. />
  291. <br />
  292. juliushaertl
  293. </a>
  294. </td>
  295. <td align="center">
  296. <a href="https://github.com/mort3za">
  297. <img
  298. alt="mort3za"
  299. src="https://avatars.githubusercontent.com/u/510242?v=4"
  300. width="117"
  301. />
  302. <br />
  303. mort3za
  304. </a>
  305. </td>
  306. <td align="center">
  307. <a href="https://github.com/Sandip124">
  308. <img
  309. alt="Sandip124"
  310. src="https://avatars.githubusercontent.com/u/37034590?v=4"
  311. width="117"
  312. />
  313. <br />
  314. Sandip124
  315. </a>
  316. </td>
  317. <td align="center">
  318. <a href="https://github.com/Tadaz">
  319. <img
  320. alt="Tadaz"
  321. src="https://avatars.githubusercontent.com/u/10881931?v=4"
  322. width="117"
  323. />
  324. <br />
  325. Tadaz
  326. </a>
  327. </td>
  328. </tr>
  329. <tr>
  330. <td align="center">
  331. <a href="https://github.com/t12ung">
  332. <img
  333. alt="t12ung"
  334. src="https://avatars.githubusercontent.com/u/9781423?v=4"
  335. width="117"
  336. />
  337. <br />
  338. t12ung
  339. </a>
  340. </td>
  341. <td align="center">
  342. <a href="https://github.com/victorfeijo">
  343. <img
  344. alt="victorfeijo"
  345. src="https://avatars.githubusercontent.com/u/8865899?v=4"
  346. width="117"
  347. />
  348. <br />
  349. victorfeijo
  350. </a>
  351. </td>
  352. <td align="center">
  353. <a href="https://github.com/fiatjaf">
  354. <img
  355. alt="fiatjaf"
  356. src="https://avatars.githubusercontent.com/u/1653275?v=4"
  357. width="117"
  358. />
  359. <br />
  360. fiatjaf
  361. </a>
  362. </td>
  363. <td align="center">
  364. <a href="https://github.com/prousseau-korem">
  365. <img
  366. alt="prousseau-korem"
  367. src="https://avatars.githubusercontent.com/u/59747802?v=4"
  368. width="117"
  369. />
  370. <br />
  371. prousseau-korem
  372. </a>
  373. </td>
  374. </tr>
  375. </table>
  376. <!-- ALL-CONTRIBUTORS-LIST:END -->
  377. ## License
  378. MIT © [Varun A P](https://github.com/apvarun)
  379. <a href="https://www.buymeacoffee.com/apvarun" target="_blank" rel="noopener"><img src="https://cdn.buymeacoffee.com/buttons/v2/default-yellow.png" height="40" width="145" alt="Buy Me A Coffee"></a>
  380. [ie]: https://raw.githubusercontent.com/godban/browsers-support-badges/master/src/images/edge.png
  381. [firefox]: https://raw.githubusercontent.com/godban/browsers-support-badges/master/src/images/firefox.png
  382. [chrome]: https://raw.githubusercontent.com/godban/browsers-support-badges/master/src/images/chrome.png
  383. [safari]: https://raw.githubusercontent.com/godban/browsers-support-badges/master/src/images/safari.png
  384. [opera]: https://raw.githubusercontent.com/godban/browsers-support-badges/master/src/images/opera.png