import React, { Component } from "react"; import styled from "styled-components"; import loading from "assets/loading.gif"; type Props = { text?: string; onClick: () => void; disabled?: boolean; status?: string | null; color?: string; rounded?: boolean; helper?: string | null; saveText?: string | null; // Makes flush with corner if not within a modal makeFlush?: boolean; clearPosition?: boolean; statusPosition?: "right" | "left"; // Provide the classname to modify styles from other components className?: string; successText?: string; absoluteSave?: boolean; }; const SaveButton: React.FC = (props) => { const renderStatus = () => { if (props.status) { if (props.status === "successful") { return ( done {props?.successText || "Successfully updated"} ); } else if (props.status === "loading") { return ( {props.saveText || "Updating . . ."} ); } else if (props.status === "error") { return ( error_outline Could not update ); } else { return ( error_outline {props.status} ); } } else if (props.helper) { return ( {props.helper} ); } }; return ( {props.statusPosition !== "right" &&
{renderStatus()}
} {props.statusPosition === "right" &&
{renderStatus()}
}
); }; export default SaveButton; const LoadingGif = styled.img` width: 15px; height: 15px; margin-right: 9px; margin-bottom: 0px; `; const StatusTextWrapper = styled.p` display: -webkit-box; line-clamp: 2; -webkit-line-clamp: 2; -webkit-box-orient: vertical; line-height: 19px; margin: 0; `; // TODO: prevent status re-render on form refresh to allow animation // animation: statusFloatIn 0.5s; const StatusWrapper = styled.div<{ successful: boolean; position: "right" | "left"; }>` display: flex; align-items: center; font-family: "Work Sans", sans-serif; font-size: 13px; color: #ffffff55; ${(props) => { if (props.position !== "right") { return "margin-right: 25px;"; } return "margin-left: 25px;"; }} max-width: 500px; overflow: hidden; text-overflow: ellipsis; > i { font-size: 18px; margin-right: 10px; float: left; color: ${(props) => (props.successful ? "#4797ff" : "#fcba03")}; } animation-fill-mode: forwards; @keyframes statusFloatIn { from { opacity: 0; transform: translateY(10px); } to { opacity: 1; transform: translateY(0px); } } `; const ButtonWrapper = styled.div` ${(props: { makeFlush: boolean; clearPosition?: boolean; absoluteSave: boolean }) => { const baseStyles = ` display: flex; position: ${props.absoluteSave ? "absolute" : ""}; bottom: ${props.absoluteSave ? "5px" : ""}; align-items: center; z-index: 99; `; if (props.clearPosition) { return baseStyles; } if (!props.makeFlush) { return ` ${baseStyles} position: absolute; justify-content: flex-end; bottom: 25px; right: 27px; left: 27px; `; } return ` ${baseStyles} position: absolute; justify-content: flex-end; bottom: 5px; right: 0; `; }} `; const Button = styled.button<{ disabled: boolean; color: string; rounded: boolean; }>` height: 35px; font-size: 13px; font-weight: 500; font-family: "Work Sans", sans-serif; color: white; display: flex; align-items: center; padding: 6px 20px 7px 20px; text-align: left; border: 0; border-radius: ${(props) => (props.rounded ? "100px" : "5px")}; background: ${(props) => (!props.disabled ? (props.color || props.theme.button) : "#aaaabb")}; cursor: ${(props) => (props.disabled ? "not-allowed" : "pointer")}; user-select: none; :focus { outline: 0; } :hover { filter: ${(props) => (!props.disabled ? "brightness(120%)" : "")}; } > i { color: white; width: 18px; height: 18px; font-weight: 600; font-size: 14px; border-radius: 20px; display: flex; align-items: center; margin-right: 10px; margin-left: -5px; justify-content: center; } `;