SwitchingButtonGroup.tsx 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import type { ButtonHTMLAttributes, DetailedHTMLProps, JSX } 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<
  7. ButtonHTMLAttributes<HTMLButtonElement>,
  8. HTMLButtonElement
  9. > & {
  10. active?: boolean;
  11. };
  12. const SwitchingButton = memo((props: SwitchingButtonProps) => {
  13. const { active, className, children, onClick, ...rest } = props;
  14. return (
  15. <button
  16. type="button"
  17. className={`btn btn-sm py-1 d-flex align-items-center justify-content-center
  18. ${className}
  19. ${active ? 'active' : ''}
  20. `}
  21. onClick={onClick}
  22. {...rest}
  23. >
  24. {children}
  25. </button>
  26. );
  27. });
  28. type Props = {
  29. showPreview: boolean;
  30. onSelected?: (showPreview: boolean) => void;
  31. };
  32. export const SwitchingButtonGroup = (props: Props): JSX.Element => {
  33. const { t } = useTranslation();
  34. const { showPreview, onSelected } = props;
  35. return (
  36. <fieldset className={`btn-group ${moduleClass}`} aria-label="Comment view">
  37. <SwitchingButton
  38. active={showPreview}
  39. className="ps-2 pe-3"
  40. onClick={() => onSelected?.(true)}
  41. >
  42. <span className="material-symbols-outlined me-0">play_arrow</span>
  43. <span className="d-none d-sm-inline">{t('page_comment.preview')}</span>
  44. </SwitchingButton>
  45. <SwitchingButton
  46. active={!showPreview}
  47. className="px-2"
  48. onClick={() => onSelected?.(false)}
  49. >
  50. <span className="material-symbols-outlined me-1">edit_square</span>
  51. <span className="d-none d-sm-inline">{t('page_comment.write')}</span>
  52. </SwitchingButton>
  53. </fieldset>
  54. );
  55. };