index.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. const { UIPlugin } = require('@uppy/core')
  2. const { h } = require('preact')
  3. /**
  4. * Informer
  5. * Shows rad message bubbles
  6. * used like this: `uppy.info('hello world', 'info', 5000)`
  7. * or for errors: `uppy.info('Error uploading img.jpg', 'error', 5000)`
  8. *
  9. */
  10. module.exports = class Informer extends UIPlugin {
  11. static VERSION = require('../package.json').version
  12. constructor (uppy, opts) {
  13. super(uppy, opts)
  14. this.type = 'progressindicator'
  15. this.id = this.opts.id || 'Informer'
  16. this.title = 'Informer'
  17. // set default options
  18. const defaultOptions = {}
  19. // merge default options with the ones set by user
  20. this.opts = { ...defaultOptions, ...opts }
  21. }
  22. render = (state) => {
  23. const { isHidden, message, details } = state.info
  24. function displayErrorAlert () {
  25. const errorMessage = `${message} \n\n ${details}`
  26. alert(errorMessage)
  27. }
  28. const handleMouseOver = () => {
  29. clearTimeout(this.uppy.infoTimeoutID)
  30. }
  31. const handleMouseLeave = () => {
  32. this.uppy.infoTimeoutID = setTimeout(this.uppy.hideInfo, 2000)
  33. }
  34. return (
  35. <div
  36. className="uppy uppy-Informer"
  37. aria-hidden={isHidden}
  38. >
  39. <p role="alert">
  40. {message}
  41. {' '}
  42. {details && (
  43. <span
  44. aria-label={details}
  45. data-microtip-position="top-left"
  46. data-microtip-size="medium"
  47. role="tooltip"
  48. onClick={displayErrorAlert}
  49. onMouseOver={handleMouseOver}
  50. onMouseLeave={handleMouseLeave}
  51. >
  52. ?
  53. </span>
  54. )}
  55. </p>
  56. </div>
  57. )
  58. }
  59. install () {
  60. const target = this.opts.target
  61. if (target) {
  62. this.mount(target, this)
  63. }
  64. }
  65. }