checkboxFancy.tsx 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import {css, Theme} from '@emotion/react';
  2. import styled from '@emotion/styled';
  3. import {IconCheckmark, IconSubtract} from 'sentry/icons';
  4. interface Props extends React.HTMLAttributes<HTMLDivElement> {
  5. isChecked?: boolean;
  6. isDisabled?: boolean;
  7. isIndeterminate?: boolean;
  8. size?: string;
  9. }
  10. const disabledStyles = (p: Props & {theme: Theme}) =>
  11. p.isDisabled &&
  12. css`
  13. background: ${p.isChecked || p.isIndeterminate
  14. ? p.theme.gray200
  15. : p.theme.backgroundSecondary};
  16. border-color: ${p.theme.border};
  17. `;
  18. const hoverStyles = (p: Props & {theme: Theme}) =>
  19. !p.isDisabled &&
  20. css`
  21. border: 2px solid
  22. ${p.isChecked || p.isIndeterminate ? p.theme.active : p.theme.textColor};
  23. `;
  24. const CheckboxFancy = styled(
  25. ({isDisabled, isChecked, isIndeterminate, size: _size, ...props}: Props) => (
  26. <div
  27. data-test-id="checkbox-fancy"
  28. role="checkbox"
  29. aria-disabled={isDisabled}
  30. aria-checked={isIndeterminate ? 'mixed' : isChecked}
  31. {...props}
  32. >
  33. {isIndeterminate && <IconSubtract legacySize="70%" color="white" />}
  34. {!isIndeterminate && isChecked && <IconCheckmark legacySize="70%" color="white" />}
  35. </div>
  36. )
  37. )`
  38. display: flex;
  39. align-items: center;
  40. justify-content: center;
  41. box-shadow: 1px 1px 5px 0px rgba(0, 0, 0, 0.05) inset;
  42. width: ${p => p.size};
  43. height: ${p => p.size};
  44. border-radius: 5px;
  45. background: ${p => (p.isChecked || p.isIndeterminate ? p.theme.active : 'transparent')};
  46. border: 2px solid
  47. ${p => (p.isChecked || p.isIndeterminate ? p.theme.active : p.theme.gray200)};
  48. cursor: ${p => (p.isDisabled ? 'not-allowed' : 'pointer')};
  49. ${p => (!p.isChecked || !p.isIndeterminate) && 'transition: 500ms border ease-out'};
  50. &:hover {
  51. ${hoverStyles}
  52. }
  53. ${disabledStyles}
  54. `;
  55. CheckboxFancy.defaultProps = {
  56. size: '16px',
  57. isChecked: false,
  58. isIndeterminate: false,
  59. };
  60. export default CheckboxFancy;