Button.tsx 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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";
  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 StyledButton = 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: white;
  25. background: ${switchProp(prop("color", "blue"), {
  26. blue: "linear-gradient(to right, #42a5f5, #2979ff)",
  27. purple: "linear-gradient(to right, #7e57c2, #6200ea)",
  28. gray: "linear-gradient(to right, #e0e0e0, #bdbdbd)"
  29. })};
  30. box-shadow: ${switchProp(prop("color", "blue"), {
  31. blue: "0 5px 6px rgba(66, 165, 245, 0.5)",
  32. purple: "0 5px 6px rgba(81, 45, 168, 0.5)",
  33. gray: "0 5px 6px rgba(160, 160, 160, 0.5)"
  34. })};
  35. border: none;
  36. border-radius: 100px;
  37. transition: all 0.4s ease-out;
  38. cursor: pointer;
  39. overflow: hidden;
  40. :hover,
  41. :focus {
  42. outline: none;
  43. box-shadow: ${switchProp(prop("color", "blue"), {
  44. blue: "0 6px 15px rgba(66, 165, 245, 0.5)",
  45. purple: "0 6px 15px rgba(81, 45, 168, 0.5)",
  46. gray: "0 6px 15px rgba(160, 160, 160, 0.5)"
  47. })};
  48. transform: translateY(-2px) scale(1.02, 1.02);
  49. }
  50. a & {
  51. text-decoration: none;
  52. border: none;
  53. }
  54. ${ifProp(
  55. { size: "big" },
  56. css`
  57. height: 56px;
  58. @media only screen and (max-width: 448px) {
  59. height: 40px;
  60. }
  61. `
  62. )}
  63. `;
  64. const Icon = styled(SVG)`
  65. svg {
  66. width: 16px;
  67. height: 16px;
  68. margin-right: 12px;
  69. stroke: ${ifProp({ color: "gray" }, "#444", "#fff")};
  70. ${ifProp(
  71. { icon: "loader" },
  72. css`
  73. width: 20px;
  74. height: 20px;
  75. margin: 0;
  76. animation: ${spin} 1s linear infinite;
  77. `
  78. )}
  79. ${ifProp(
  80. "isRound",
  81. css`
  82. width: 15px;
  83. height: 15px;
  84. margin: 0;
  85. `
  86. )}
  87. @media only screen and (max-width: 768px) {
  88. width: 12px;
  89. height: 12px;
  90. margin-right: 6px;
  91. }
  92. }
  93. `;
  94. const Button: FC<Props> = props => {
  95. const SVGIcon = props.icon ? (
  96. <Icon
  97. icon={props.icon}
  98. isRound={props.isRound}
  99. color={props.color}
  100. src={`/images/${props.icon}.svg`}
  101. />
  102. ) : (
  103. ""
  104. );
  105. return (
  106. <StyledButton {...props}>
  107. {SVGIcon}
  108. {props.icon !== "loader" && props.children}
  109. </StyledButton>
  110. );
  111. };
  112. Button.defaultProps = {
  113. as: "button",
  114. width: "auto",
  115. flex: "0 0 auto",
  116. height: [32, 40],
  117. py: 0,
  118. px: [24, 32],
  119. fontSize: [12, 13],
  120. color: "blue",
  121. icon: "",
  122. isRound: false
  123. };
  124. export default Button;