Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

Switch.tsx 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import React, { useCallback } from 'react';
  2. import { makeStyles } from 'tss-react/mui';
  3. import { isMobileBrowser } from '../../../environment/utils';
  4. import { ISwitchProps } from '../types';
  5. interface IProps extends ISwitchProps {
  6. className?: string;
  7. /**
  8. * Id of the toggle.
  9. */
  10. id?: string;
  11. }
  12. const useStyles = makeStyles()(theme => {
  13. return {
  14. container: {
  15. position: 'relative',
  16. backgroundColor: theme.palette.ui05,
  17. borderRadius: '12px',
  18. width: '40px',
  19. height: '24px',
  20. border: 0,
  21. outline: 0,
  22. cursor: 'pointer',
  23. transition: '.3s',
  24. display: 'inline-block',
  25. '&.disabled': {
  26. backgroundColor: theme.palette.ui05,
  27. cursor: 'default',
  28. '& .toggle': {
  29. backgroundColor: theme.palette.ui03
  30. }
  31. },
  32. '&.is-mobile': {
  33. height: '32px',
  34. width: '50px',
  35. borderRadius: '32px'
  36. }
  37. },
  38. containerOn: {
  39. backgroundColor: theme.palette.action01
  40. },
  41. toggle: {
  42. width: '16px',
  43. height: '16px',
  44. position: 'absolute',
  45. top: '4px',
  46. left: '4px',
  47. backgroundColor: theme.palette.ui10,
  48. borderRadius: '100%',
  49. transition: '.3s',
  50. '&.is-mobile': {
  51. width: '24px',
  52. height: '24px'
  53. }
  54. },
  55. toggleOn: {
  56. left: '20px',
  57. '&.is-mobile': {
  58. left: '22px'
  59. }
  60. },
  61. checkbox: {
  62. height: 0,
  63. width: 0
  64. }
  65. };
  66. });
  67. const Switch = ({ className, id, checked, disabled, onChange }: IProps) => {
  68. const { classes: styles, cx } = useStyles();
  69. const isMobile = isMobileBrowser();
  70. const change = useCallback((e: React.ChangeEvent<HTMLInputElement>) => {
  71. onChange(e.target.checked);
  72. }, []);
  73. return (
  74. <label
  75. className = { cx('toggle-container', styles.container, checked && styles.containerOn,
  76. isMobile && 'is-mobile', disabled && 'disabled', className) }>
  77. <input
  78. type = 'checkbox'
  79. { ...(id ? { id } : {}) }
  80. checked = { checked }
  81. className = { styles.checkbox }
  82. disabled = { disabled }
  83. onChange = { change } />
  84. <div className = { cx('toggle', styles.toggle, checked && styles.toggleOn, isMobile && 'is-mobile') } />
  85. </label>
  86. );
  87. };
  88. export default Switch;