Checkbox.tsx 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import React, { FC } from "react";
  2. import styled, { css } from "styled-components";
  3. import { ifProp } from "styled-tools";
  4. import { Flex, BoxProps } from "reflexbox/styled-components";
  5. import Text, { Span } from "./Text";
  6. interface InputProps {
  7. checked: boolean;
  8. id?: string;
  9. name: string;
  10. }
  11. const Input = styled(Flex).attrs({
  12. as: "input",
  13. type: "checkbox",
  14. m: 0,
  15. p: 0,
  16. width: 0,
  17. height: 0,
  18. opacity: 0
  19. })<InputProps>`
  20. position: relative;
  21. opacity: 0;
  22. `;
  23. const Box = styled(Flex).attrs({
  24. alignItems: "center",
  25. justifyContent: "center"
  26. })<{ checked: boolean }>`
  27. position: relative;
  28. transition: color 0.3s ease-out;
  29. border-radius: 4px;
  30. background-color: white;
  31. box-shadow: 0 2px 4px rgba(50, 50, 50, 0.2);
  32. cursor: pointer;
  33. input:focus + & {
  34. outline: 3px solid rgba(65, 164, 245, 0.5);
  35. }
  36. ${ifProp(
  37. "checked",
  38. css`
  39. box-shadow: 0 3px 5px rgba(50, 50, 50, 0.4);
  40. :after {
  41. content: "";
  42. position: absolute;
  43. width: 80%;
  44. height: 80%;
  45. display: block;
  46. border-radius: 2px;
  47. background-color: #9575cd;
  48. box-shadow: 0 2px 4px rgba(50, 50, 50, 0.2);
  49. cursor: pointer;
  50. }
  51. `
  52. )}
  53. `;
  54. interface Props extends InputProps, BoxProps {
  55. label: string;
  56. }
  57. const Checkbox: FC<Props> = ({
  58. checked,
  59. height,
  60. id,
  61. label,
  62. name,
  63. width,
  64. ...rest
  65. }) => {
  66. return (
  67. <Flex
  68. flex="0 0 auto"
  69. as="label"
  70. alignItems="center"
  71. style={{ cursor: "pointer" }}
  72. {...(rest as any)}
  73. >
  74. <Input name={name} id={id} checked={checked} />
  75. <Box checked={checked} width={width} height={height} />
  76. <Span ml={12} color="#555">
  77. {label}
  78. </Span>
  79. </Flex>
  80. );
  81. };
  82. Checkbox.defaultProps = {
  83. width: [18],
  84. height: [18]
  85. };
  86. export default Checkbox;