Button.tsx 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 { ButtonProps } from './types';
  5. import React, { useCallback } from 'react';
  6. import styled from 'styled-components';
  7. import Icon from '../Icon';
  8. import Spinner from '../Spinner';
  9. // isPrimary, isPosition, isNegative - here for old compat, check, remove
  10. function Button ({ children, className = '', icon, isBasic, isBusy, isCircular, isDisabled, isFull, isIcon, isNegative, isPositive, isPrimary, label, onClick, onMouseEnter, onMouseLeave, tabIndex }: ButtonProps): React.ReactElement<ButtonProps> {
  11. const _onClick = useCallback(
  12. () => !(isBusy || isDisabled) && onClick && onClick(),
  13. [isBusy, isDisabled, onClick]
  14. );
  15. return (
  16. <button
  17. className={`ui--Button${label ? ' hasLabel' : ''}${isBasic ? ' isBasic' : ''}${isCircular ? ' isCircular' : ''}${isFull ? ' isFull' : ''}${isIcon ? ' isIcon' : ''}${isNegative ? ' isNegative' : ''}${isPositive ? ' isPositive' : ''}${isPrimary ? ' isPrimary' : ''}${(isBusy || isDisabled) ? ' isDisabled' : ''}${isBusy ? ' isBusy' : ''}${!onClick ? ' isReadOnly' : ''} ${className}`}
  18. onClick={_onClick}
  19. onMouseEnter={onMouseEnter}
  20. onMouseLeave={onMouseLeave}
  21. tabIndex={tabIndex}
  22. >
  23. <Icon icon={icon} />
  24. {label}
  25. {children}
  26. <Spinner
  27. className='ui--Button-spinner'
  28. variant='cover'
  29. />
  30. </button>
  31. );
  32. }
  33. export default React.memo(styled(Button)`
  34. background: transparent; // #e9e8e7; // similar to rgba(0, 0, 0, 0.05);
  35. border: none;
  36. color: #4e4e4e;
  37. cursor: pointer;
  38. // font-size: 0.92857142857rem; // 13/14px
  39. margin: 0;
  40. position: relative;
  41. text-align: center;
  42. &:not(.hasLabel) {
  43. padding: 0.7em 0.4rem;
  44. }
  45. &:not(.isCircular) {
  46. border-radius: 0.25rem;
  47. }
  48. &:focus {
  49. outline:0;
  50. }
  51. &.hasLabel {
  52. padding: 0.7rem 1.15rem 0.7rem ${1.15 - 0.425}rem;
  53. .ui--Icon {
  54. margin-right: 0.425rem !important;
  55. }
  56. }
  57. &.isBasic {
  58. background: #fff;
  59. }
  60. &.isCircular {
  61. border-radius: 10rem;
  62. }
  63. &.isDisabled, &.isReadOnly {
  64. background: none;
  65. box-shadow: none;
  66. cursor: not-allowed;
  67. }
  68. &.isBusy {
  69. cursor: wait;
  70. }
  71. &.isFull {
  72. display: block;
  73. width: 100%;
  74. }
  75. &.isIcon {
  76. background: transparent;
  77. }
  78. .ui--Button-spinner {
  79. visibility: hidden;
  80. }
  81. .ui--Button-overlay {
  82. background: rgba(253, 252, 251, 0.75);
  83. bottom: 0;
  84. left: 0;
  85. position: absolute;
  86. right: 0;
  87. top: 0;
  88. visibility: hidden;
  89. }
  90. .ui--Icon {
  91. border-radius: 1rem;
  92. box-sizing: content-box;
  93. height: 1rem;
  94. margin: -0.425rem 0;
  95. padding: 0.425rem;
  96. width: 1rem;
  97. }
  98. &.isBusy {
  99. .ui--Button-spinner {
  100. visibility: visible;
  101. }
  102. }
  103. &.isDisabled {
  104. color: #bcbbba;
  105. }
  106. `);