SaveButton.tsx 5.1 KB

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