index.jsx 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. // @flow
  15. import React from 'react'
  16. import { observer } from 'mobx-react'
  17. import styled, { injectGlobal } from 'styled-components'
  18. import reloadImage from './images/reload.svg'
  19. const Wrapper = styled.div`
  20. width: 16px;
  21. height: 16px;
  22. background: url('${reloadImage}') no-repeat center;
  23. cursor: pointer;
  24. `
  25. injectGlobal`
  26. .reload-animation {
  27. transform: rotate(360deg);
  28. transition: transform 1s cubic-bezier(0, 1.4, 1, 1);
  29. }
  30. `
  31. type Props = {
  32. onClick: () => void,
  33. }
  34. @observer
  35. class ReloadButton extends React.Component<Props> {
  36. wrapper: HTMLElement
  37. timeout: ?TimeoutID
  38. onClick() {
  39. if (this.timeout) {
  40. return
  41. }
  42. if (this.props.onClick) {
  43. this.props.onClick()
  44. }
  45. if (!this.wrapper) {
  46. return
  47. }
  48. this.wrapper.className += ' reload-animation'
  49. this.timeout = setTimeout(() => {
  50. this.wrapper.className = this.wrapper.className.substr(0, this.wrapper.className.indexOf(' reload-animation'))
  51. this.timeout = null
  52. }, 1000)
  53. }
  54. render() {
  55. return (
  56. <Wrapper innerRef={div => { this.wrapper = div }} {...this.props} onClick={() => { this.onClick() }} />
  57. )
  58. }
  59. }
  60. export default ReloadButton