Progress.tsx 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. // Copyright 2017-2020 @polkadot/react-components authors & contributors
  2. // This software may be modified and distributed under the terms
  3. // of the Apache-2.0 license. See the LICENSE file for details.
  4. import BN from 'bn.js';
  5. import React from 'react';
  6. import styled from 'styled-components';
  7. import { UInt } from '@polkadot/types';
  8. import { bnToBn } from '@polkadot/util';
  9. interface Props {
  10. className?: string;
  11. isDisabled?: boolean;
  12. total?: UInt | BN | number | null;
  13. value?: UInt | BN | number | null;
  14. }
  15. interface RotateProps {
  16. angle: string;
  17. type: 'first' | 'second';
  18. }
  19. function DivClip ({ angle, type }: RotateProps): React.ReactElement<RotateProps> {
  20. return (
  21. <div className={`clip ${type}`}>
  22. <div
  23. className='ui--highlight--bg'
  24. style={{ transform: `rotate(${angle}deg)` }}
  25. />
  26. </div>
  27. );
  28. }
  29. const Clip = React.memo(DivClip);
  30. function Progress ({ className = '', isDisabled, total, value }: Props): React.ReactElement<Props> | null {
  31. const _total = bnToBn(total || 0);
  32. const angle = _total.gtn(0)
  33. ? (bnToBn(value || 0).muln(36000).div(_total).toNumber() / 100)
  34. : 0;
  35. if (angle < 0) {
  36. return null;
  37. }
  38. return (
  39. <div className={`ui--Progress${isDisabled ? ' isDisabled' : ''} ${className}`}>
  40. <div className='background ui--highlight--bg' />
  41. <Clip
  42. angle={
  43. angle <= 180
  44. ? angle.toFixed(1)
  45. : '180'
  46. }
  47. type='first'
  48. />
  49. <Clip
  50. angle={
  51. angle <= 180
  52. ? '0'
  53. : (angle - 180).toFixed(1)
  54. }
  55. type='second'
  56. />
  57. <div className='inner'>
  58. <div>{Math.floor(angle * 100 / 360)}%</div>
  59. </div>
  60. </div>
  61. );
  62. }
  63. export default React.memo(styled(Progress)`
  64. border-radius: 100%;
  65. clip-path: circle(50%);
  66. height: 4.5rem;
  67. position: relative;
  68. width: 4.5rem;
  69. &.isDisabled {
  70. filter: grayscale(100%);
  71. opacity: 0.25;
  72. }
  73. .background,
  74. .clip {
  75. bottom: 0;
  76. left: 0;
  77. position: absolute;
  78. right: 0;
  79. top: 0;
  80. }
  81. .background {
  82. opacity: 0.125;
  83. }
  84. .clip {
  85. div {
  86. border-radius: 100%;
  87. bottom: 0;
  88. left: 0;
  89. position: absolute;
  90. right: 0;
  91. transform: rotate(0);
  92. top: 0;
  93. zoom: 1;
  94. }
  95. }
  96. .clip.first {
  97. clip-path: polygon(50% 0, 100% 0, 100% 100%, 50% 100%);
  98. div {
  99. clip-path: polygon(0 0, 50% 0, 50% 100%, 0 100%);
  100. }
  101. }
  102. .clip.second {
  103. clip-path: polygon(0 0, 50% 0, 50% 100%, 0 100%);
  104. div {
  105. clip-path: polygon(50% 0, 100% 0, 100% 100%, 50% 100%);
  106. }
  107. }
  108. .inner {
  109. align-items: center;
  110. background: rgba(245, 244, 243, 87.5%);
  111. border-radius: 100%;
  112. bottom: 0.375rem;
  113. display: flex;
  114. justify-content: center;
  115. left: 0.375rem;
  116. position: absolute;
  117. right: 0.375rem;
  118. top: 0.375rem;
  119. div {
  120. line-height: 1;
  121. font-size: 1.25rem;
  122. text-shadow: 0 0 2px #f5f4f3;
  123. }
  124. }
  125. `);