Button.tsx 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. import React, { FC } from "react";
  2. import styled, { css } from "styled-components";
  3. import { switchProp, prop, ifProp } from "styled-tools";
  4. import { Flex, BoxProps } from "reflexbox/styled-components";
  5. // TODO: another solution for inline SVG
  6. import SVG from "react-inlinesvg";
  7. import { spin } from "../helpers/animations";
  8. interface Props extends BoxProps {
  9. color?: "purple" | "gray" | "blue" | "red";
  10. disabled?: boolean;
  11. icon?: string; // TODO: better typing
  12. isRound?: boolean;
  13. onClick?: any; // TODO: better typing
  14. type?: "button" | "submit" | "reset";
  15. }
  16. const Button = styled(Flex)<Props>`
  17. position: relative;
  18. align-items: center;
  19. justify-content: center;
  20. font-weight: normal;
  21. text-align: center;
  22. line-height: 1;
  23. word-break: keep-all;
  24. color: ${switchProp(prop("color", "blue"), {
  25. blue: "white",
  26. red: "white",
  27. purple: "white",
  28. gray: "#444"
  29. })};
  30. background: ${switchProp(prop("color", "blue"), {
  31. blue: "linear-gradient(to right, #42a5f5, #2979ff)",
  32. red: "linear-gradient(to right, #ee3b3b, #e11c1c)",
  33. purple: "linear-gradient(to right, #7e57c2, #6200ea)",
  34. gray: "linear-gradient(to right, #e0e0e0, #bdbdbd)"
  35. })};
  36. box-shadow: ${switchProp(prop("color", "blue"), {
  37. blue: "0 5px 6px rgba(66, 165, 245, 0.5)",
  38. red: "0 5px 6px rgba(168, 45, 45, 0.5)",
  39. purple: "0 5px 6px rgba(81, 45, 168, 0.5)",
  40. gray: "0 5px 6px rgba(160, 160, 160, 0.5)"
  41. })};
  42. border: none;
  43. border-radius: 100px;
  44. transition: all 0.4s ease-out;
  45. cursor: pointer;
  46. overflow: hidden;
  47. :hover,
  48. :focus {
  49. outline: none;
  50. box-shadow: ${switchProp(prop("color", "blue"), {
  51. blue: "0 6px 15px rgba(66, 165, 245, 0.5)",
  52. red: "0 6px 15px rgba(168, 45, 45, 0.5)",
  53. purple: "0 6px 15px rgba(81, 45, 168, 0.5)",
  54. gray: "0 6px 15px rgba(160, 160, 160, 0.5)"
  55. })};
  56. transform: translateY(-2px) scale(1.02, 1.02);
  57. }
  58. `;
  59. const Icon = styled(SVG)`
  60. svg {
  61. width: 16px;
  62. height: 16px;
  63. margin-right: 12px;
  64. stroke: ${ifProp({ color: "gray" }, "#444", "#fff")};
  65. ${ifProp(
  66. { icon: "loader" },
  67. css`
  68. width: 20px;
  69. height: 20px;
  70. margin: 0;
  71. animation: ${spin} 1s linear infinite;
  72. `
  73. )}
  74. ${ifProp(
  75. "isRound",
  76. css`
  77. width: 15px;
  78. height: 15px;
  79. margin: 0;
  80. `
  81. )}
  82. @media only screen and (max-width: 768px) {
  83. width: 12px;
  84. height: 12px;
  85. margin-right: 6px;
  86. }
  87. }
  88. `;
  89. Button.defaultProps = {
  90. as: "button",
  91. width: "auto",
  92. flex: "0 0 auto",
  93. height: [32, 40],
  94. py: 0,
  95. px: [24, 32],
  96. fontSize: [12, 13],
  97. color: "blue",
  98. icon: "",
  99. isRound: false
  100. };
  101. interface NavButtonProps extends BoxProps {
  102. disabled?: boolean;
  103. onClick?: any; // TODO: better typing
  104. type?: "button" | "submit" | "reset";
  105. key?: string;
  106. }
  107. export const NavButton = styled(Flex)<NavButtonProps>`
  108. display: flex;
  109. border: none;
  110. border-radius: 4px;
  111. box-shadow: 0 0px 10px rgba(100, 100, 100, 0.1);
  112. background-color: white;
  113. cursor: pointer;
  114. transition: all 0.2s ease-out;
  115. box-sizing: border-box;
  116. :hover {
  117. transform: translateY(-2px);
  118. }
  119. ${ifProp(
  120. "disabled",
  121. css`
  122. background-color: #f6f6f6;
  123. box-shadow: 0 0px 5px rgba(150, 150, 150, 0.1);
  124. cursor: default;
  125. :hover {
  126. transform: none;
  127. }
  128. `
  129. )}
  130. `;
  131. NavButton.defaultProps = {
  132. as: "button",
  133. type: "button",
  134. flex: "0 0 auto",
  135. alignItems: "center",
  136. justifyContent: "center",
  137. width: "auto",
  138. height: [26, 28],
  139. py: 0,
  140. px: ["6px", "8px"],
  141. fontSize: [12]
  142. };