index.tsx 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import React from "react";
  2. import styled, { DefaultTheme } from "styled-components";
  3. enum ButtonType {
  4. PRIMARY = "PRIMARY",
  5. SECONDARY = "BLURPLE",
  6. DANGER = "DANGER",
  7. SUCCESS = "SEAGREEN",
  8. WARNING = "ORANGE",
  9. }
  10. export interface ButtonProps
  11. extends React.ButtonHTMLAttributes<HTMLButtonElement> {
  12. status?: keyof typeof ButtonType;
  13. block?: boolean;
  14. }
  15. function getButtonStatus(status: keyof typeof ButtonType, theme: DefaultTheme) {
  16. return theme[ButtonType[status]];
  17. }
  18. const StyledButton = styled.button<{
  19. status: keyof typeof ButtonType;
  20. block: boolean;
  21. }>`
  22. display: block;
  23. background: ${({ status, theme }) => getButtonStatus(status, theme)};
  24. color: #ffffff;
  25. padding: 8px 16px;
  26. min-width: 60px;
  27. width: ${({ block }) => (block ? "100%" : "fit-content")};
  28. height: 40px;
  29. :disabled {
  30. cursor: not-allowed;
  31. opacity: 0.5;
  32. }
  33. @media only screen and (max-width: 768px) {
  34. font-size: 18px;
  35. }
  36. `;
  37. const StyledButtonContent = styled.div`
  38. display: flex;
  39. justify-content: center;
  40. align-items: center;
  41. gap: 8px;
  42. white-space: nowrap;
  43. text-overflow: ellipsis;
  44. `;
  45. export const Button: React.FC<ButtonProps> = ({
  46. children,
  47. status,
  48. block = false,
  49. ...props
  50. }) => {
  51. return (
  52. <StyledButton
  53. type="button"
  54. status={status ?? "PRIMARY"}
  55. block={block}
  56. {...props}
  57. >
  58. <StyledButtonContent>{children}</StyledButtonContent>
  59. </StyledButton>
  60. );
  61. };