ColorCheckbox.jsx 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // styling
  2. import styled from 'styled-components/macro';
  3. // utils
  4. import PropTypes from 'prop-types';
  5. const StyledColorCheckbox = styled.div`
  6. width: 32px;
  7. height: 32px;
  8. position: relative;
  9. border-radius: 8px;
  10. overflow: hidden;
  11. &:after {
  12. content: '';
  13. position: absolute;
  14. top: 0;
  15. left: 0;
  16. width: 100%;
  17. height: 100%;
  18. z-index: 1;
  19. background: ${({color1}) => `var(--${color1})`};
  20. }
  21. ${({isDuo, color2}) => isDuo && `
  22. &:before {
  23. content: '';
  24. position: absolute;
  25. top: 0;
  26. right: 0;
  27. width: 50%;
  28. height: 100%;
  29. z-index: 2;
  30. background: var(--${color2});
  31. }
  32. `}
  33. input {
  34. display: none;
  35. &:checked + label {
  36. opacity: 1;
  37. }
  38. }
  39. label {
  40. position: absolute;
  41. top: 0;
  42. left: 0;
  43. width: 100%;
  44. height: 100%;
  45. cursor: pointer;
  46. z-index: 10;
  47. opacity: 0;
  48. transition: opacity var(--transition);
  49. .icon {
  50. position: absolute;
  51. top: 50%;
  52. left: 50%;
  53. width: 20px;
  54. height: 20px;
  55. background: var(--widget);
  56. transform: translate(-50%, -50%);
  57. border-radius: 4px;
  58. display: flex;
  59. align-items: center;
  60. justify-content: center;
  61. font-size: 14px;
  62. color: ${({color1}) => `var(--${color1})`};
  63. }
  64. }
  65. `;
  66. const ColorCheckbox = ({id, color1, color2, checked, onChange, isDuo = false, type = 'checkbox', name}) => {
  67. return (
  68. <StyledColorCheckbox color1={color1} color2={color2} isDuo={isDuo}>
  69. <input
  70. type={type}
  71. checked={checked}
  72. onChange={onChange}
  73. id={id}
  74. name={name}
  75. />
  76. <label htmlFor={id}>
  77. <i className="icon icon-xmark"/>
  78. </label>
  79. </StyledColorCheckbox>
  80. )
  81. }
  82. ColorCheckbox.propTypes = {
  83. id: PropTypes.string.isRequired,
  84. color1: PropTypes.string.isRequired,
  85. color2: PropTypes.string,
  86. checked: PropTypes.bool,
  87. onChange: PropTypes.func,
  88. isDuo: PropTypes.bool,
  89. type: PropTypes.oneOf(['checkbox', 'radio'])
  90. }
  91. export default ColorCheckbox