checkboxFancy.tsx 1.9 KB

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