checkboxFancy.tsx 1.9 KB

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