SwitchingButtonGroup.tsx 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import type { ButtonHTMLAttributes, DetailedHTMLProps } from 'react';
  2. import { memo } from 'react';
  3. import { useTranslation } from 'next-i18next';
  4. import styles from './SwitchingButtonGroup.module.scss';
  5. const moduleClass = styles['btn-group-switching'] ?? '';
  6. type SwitchingButtonProps = DetailedHTMLProps<ButtonHTMLAttributes<HTMLButtonElement>, HTMLButtonElement> & {
  7. active?: boolean,
  8. }
  9. const SwitchingButton = memo((props: SwitchingButtonProps) => {
  10. const {
  11. active, className, children, onClick, ...rest
  12. } = props;
  13. return (
  14. <button
  15. type="button"
  16. className={`btn btn-sm py-1 d-flex align-items-center justify-content-center
  17. ${className}
  18. ${active ? 'active' : ''}
  19. `}
  20. onClick={onClick}
  21. {...rest}
  22. >
  23. {children}
  24. </button>
  25. );
  26. });
  27. type Props = {
  28. showPreview: boolean,
  29. onSelected?: (showPreview: boolean) => void,
  30. };
  31. export const SwitchingButtonGroup = (props: Props): JSX.Element => {
  32. const { t } = useTranslation();
  33. const {
  34. showPreview, onSelected,
  35. } = props;
  36. return (
  37. <div
  38. className={`btn-group ${moduleClass}`}
  39. role="group"
  40. >
  41. <SwitchingButton
  42. active={showPreview}
  43. className="ps-2 pe-3"
  44. onClick={() => onSelected?.(true)}
  45. >
  46. <span className="material-symbols-outlined me-0">play_arrow</span>
  47. <span className="d-none d-sm-inline">{t('Preview')}</span>
  48. </SwitchingButton>
  49. <SwitchingButton
  50. active={!showPreview}
  51. className="px-2"
  52. onClick={() => onSelected?.(false)}
  53. >
  54. <span className="material-symbols-outlined me-1">edit_square</span>
  55. <span className="d-none d-sm-inline">{t('Write')}</span>
  56. </SwitchingButton>
  57. </div>
  58. );
  59. };