ReloadButton.tsx 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*
  2. Copyright (C) 2017 Cloudbase Solutions SRL
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Affero General Public License as
  5. published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Affero General Public License for more details.
  11. You should have received a copy of the GNU Affero General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. import React from 'react'
  15. import { observer } from 'mobx-react'
  16. import styled, { createGlobalStyle } from 'styled-components'
  17. import reloadImage from './images/reload.svg'
  18. const Wrapper = styled.div<any>`
  19. width: 16px;
  20. height: 16px;
  21. background: url('${reloadImage}') no-repeat center;
  22. cursor: pointer;
  23. `
  24. const GlobalStyle = createGlobalStyle`
  25. .reload-animation {
  26. transform: rotate(360deg);
  27. transition: transform 1s cubic-bezier(0, 1.4, 1, 1);
  28. }
  29. `
  30. type Props = {
  31. onClick: () => void,
  32. style?: React.CSSProperties
  33. }
  34. @observer
  35. class ReloadButton extends React.Component<Props> {
  36. wrapper: HTMLElement | undefined | null
  37. timeout: number | undefined | null
  38. onClick() {
  39. if (this.timeout) {
  40. return
  41. }
  42. if (this.props.onClick) {
  43. this.props.onClick()
  44. }
  45. const nonNullWrapper = this.wrapper
  46. if (!nonNullWrapper) {
  47. return
  48. }
  49. nonNullWrapper.className += ' reload-animation'
  50. this.timeout = window.setTimeout(() => {
  51. nonNullWrapper.className = nonNullWrapper.className.substr(0, nonNullWrapper.className.indexOf(' reload-animation'))
  52. this.timeout = null
  53. }, 1000)
  54. }
  55. render() {
  56. return (
  57. <>
  58. <GlobalStyle />
  59. <Wrapper
  60. ref={(div: HTMLElement | null | undefined) => { this.wrapper = div }}
  61. // eslint-disable-next-line react/jsx-props-no-spreading
  62. {...this.props}
  63. onClick={() => { this.onClick() }}
  64. />
  65. </>
  66. )
  67. }
  68. }
  69. export default ReloadButton