SaveButton.tsx 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. import React, { Component } from "react";
  2. import styled from "styled-components";
  3. import loading from "assets/loading.gif";
  4. type PropsType = {
  5. text?: string;
  6. onClick: () => void;
  7. disabled?: boolean;
  8. status?: string | null;
  9. color?: string;
  10. rounded?: boolean;
  11. helper?: string | null;
  12. // Makes flush with corner if not within a modal
  13. makeFlush?: boolean;
  14. clearPosition?: boolean;
  15. statusPosition?: "right" | "left";
  16. };
  17. type StateType = {};
  18. export default class SaveButton extends Component<PropsType, StateType> {
  19. renderStatus = () => {
  20. if (this.props.status) {
  21. if (this.props.status === "successful") {
  22. return (
  23. <StatusWrapper position={this.props.statusPosition} successful={true}>
  24. <i className="material-icons">done</i>
  25. <StatusTextWrapper>Successfully updated</StatusTextWrapper>
  26. </StatusWrapper>
  27. );
  28. } else if (this.props.status === "loading") {
  29. return (
  30. <StatusWrapper
  31. position={this.props.statusPosition}
  32. successful={false}
  33. >
  34. <LoadingGif src={loading} />
  35. <StatusTextWrapper>Updating . . .</StatusTextWrapper>
  36. </StatusWrapper>
  37. );
  38. } else if (this.props.status === "error") {
  39. return (
  40. <StatusWrapper
  41. position={this.props.statusPosition}
  42. successful={false}
  43. >
  44. <i className="material-icons">error_outline</i>
  45. <StatusTextWrapper>Could not update</StatusTextWrapper>
  46. </StatusWrapper>
  47. );
  48. } else {
  49. return (
  50. <StatusWrapper
  51. position={this.props.statusPosition}
  52. successful={false}
  53. >
  54. <i className="material-icons">error_outline</i>
  55. <StatusTextWrapper>{this.props.status}</StatusTextWrapper>
  56. </StatusWrapper>
  57. );
  58. }
  59. } else if (this.props.helper) {
  60. return (
  61. <StatusWrapper position={this.props.statusPosition} successful={true}>
  62. {this.props.helper}
  63. </StatusWrapper>
  64. );
  65. }
  66. };
  67. render() {
  68. return (
  69. <ButtonWrapper
  70. makeFlush={this.props.makeFlush}
  71. clearPosition={this.props.clearPosition}
  72. >
  73. {this.props.statusPosition !== "right" && (
  74. <div>{this.renderStatus()}</div>
  75. )}
  76. <Button
  77. rounded={this.props.rounded}
  78. disabled={this.props.disabled}
  79. onClick={this.props.onClick}
  80. color={this.props.color || "#616FEEcc"}
  81. >
  82. {this.props.children || this.props.text}
  83. </Button>
  84. {this.props.statusPosition === "right" && (
  85. <div>{this.renderStatus()}</div>
  86. )}
  87. </ButtonWrapper>
  88. );
  89. }
  90. }
  91. const LoadingGif = styled.img`
  92. width: 15px;
  93. height: 15px;
  94. margin-right: 9px;
  95. margin-bottom: 0px;
  96. `;
  97. const StatusTextWrapper = styled.p`
  98. display: -webkit-box;
  99. line-clamp: 2;
  100. -webkit-line-clamp: 2;
  101. -webkit-box-orient: vertical;
  102. line-height: 19px;
  103. margin: 0;
  104. `;
  105. const StatusWrapper = styled.div<{
  106. successful: boolean;
  107. position: "right" | "left";
  108. }>`
  109. display: flex;
  110. align-items: center;
  111. font-family: "Work Sans", sans-serif;
  112. font-size: 13px;
  113. color: #ffffff55;
  114. ${(props) => {
  115. if (props.position !== "right") {
  116. return "margin-right: 25px;";
  117. }
  118. return "margin-left: 25px;";
  119. }}
  120. max-width: 500px;
  121. overflow: hidden;
  122. text-overflow: ellipsis;
  123. > i {
  124. font-size: 18px;
  125. margin-right: 10px;
  126. float: left;
  127. color: ${(props) => (props.successful ? "#4797ff" : "#fcba03")};
  128. }
  129. animation: statusFloatIn 0.5s;
  130. animation-fill-mode: forwards;
  131. @keyframes statusFloatIn {
  132. from {
  133. opacity: 0;
  134. transform: translateY(10px);
  135. }
  136. to {
  137. opacity: 1;
  138. transform: translateY(0px);
  139. }
  140. }
  141. `;
  142. const ButtonWrapper = styled.div`
  143. ${(props: { makeFlush: boolean; clearPosition?: boolean }) => {
  144. const baseStyles = `
  145. display: flex;
  146. align-items: center;
  147. `;
  148. if (props.clearPosition) {
  149. return baseStyles;
  150. }
  151. if (!props.makeFlush) {
  152. return `
  153. ${baseStyles}
  154. position: absolute;
  155. justify-content: flex-end;
  156. bottom: 25px;
  157. right: 27px;
  158. left: 27px;
  159. `;
  160. }
  161. return `
  162. ${baseStyles}
  163. position: absolute;
  164. justify-content: flex-end;
  165. bottom: 5px;
  166. right: 0;
  167. `;
  168. }}
  169. `;
  170. const Button = styled.button<{
  171. disabled: boolean;
  172. color: string;
  173. rounded: boolean;
  174. }>`
  175. height: 35px;
  176. font-size: 13px;
  177. font-weight: 500;
  178. font-family: "Work Sans", sans-serif;
  179. color: white;
  180. display: flex;
  181. align-items: center;
  182. padding: 6px 20px 7px 20px;
  183. text-align: left;
  184. border: 0;
  185. border-radius: ${(props) => (props.rounded ? "100px" : "5px")};
  186. background: ${(props) => (!props.disabled ? props.color : "#aaaabb")};
  187. box-shadow: ${(props) =>
  188. !props.disabled ? "0 2px 5px 0 #00000030" : "none"};
  189. cursor: ${(props) => (!props.disabled ? "pointer" : "default")};
  190. user-select: none;
  191. :focus {
  192. outline: 0;
  193. }
  194. :hover {
  195. filter: ${(props) => (!props.disabled ? "brightness(120%)" : "")};
  196. }
  197. > i {
  198. color: white;
  199. width: 18px;
  200. height: 18px;
  201. font-weight: 600;
  202. font-size: 14px;
  203. border-radius: 20px;
  204. display: flex;
  205. align-items: center;
  206. margin-right: 10px;
  207. margin-left: -5px;
  208. justify-content: center;
  209. }
  210. `;