CalloutViewer.tsx 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. // Ref: https://github.com/Microflash/remark-callout-directives/blob/fabe4d8adc7738469f253836f0da346591ea2a2b/README.md
  2. import type { ReactNode } from 'react';
  3. import React from 'react';
  4. import { type Callout } from '../services/consts';
  5. import styles from './CalloutViewer.module.scss';
  6. const moduleClass = styles['callout-viewer'];
  7. type CALLOUT_TO = {
  8. [key in Callout]: string;
  9. }
  10. const CALLOUT_TO_TYPE: CALLOUT_TO = {
  11. note: 'Note',
  12. tip: 'Tip',
  13. important: 'Important',
  14. info: 'Important',
  15. warning: 'Warning',
  16. caution: 'Caution',
  17. danger: 'Caution',
  18. };
  19. const CALLOUT_TO_ICON: CALLOUT_TO = {
  20. note: 'info',
  21. tip: 'lightbulb',
  22. important: 'feedback',
  23. info: 'feedback',
  24. warning: 'warning',
  25. caution: 'report',
  26. danger: 'report',
  27. };
  28. type CalloutViewerProps = {
  29. children: ReactNode,
  30. node: Element,
  31. type: string,
  32. label?: string,
  33. }
  34. export const CalloutViewer = React.memo((props: CalloutViewerProps): JSX.Element => {
  35. const {
  36. node, type, label, children,
  37. } = props;
  38. if (node == null) {
  39. return <></>;
  40. }
  41. return (
  42. <div className={`${moduleClass} callout-viewer`}>
  43. <div className={`callout callout-${CALLOUT_TO_TYPE[type].toLowerCase()}`}>
  44. <div className="callout-indicator">
  45. <div className="callout-hint">
  46. <span className="material-symbols-outlined">{CALLOUT_TO_ICON[type]}</span>
  47. </div>
  48. <div className="callout-title">
  49. {label ?? CALLOUT_TO_TYPE[type]}
  50. </div>
  51. </div>
  52. <div className="callout-content">
  53. {children}
  54. </div>
  55. </div>
  56. </div>
  57. );
  58. });
  59. CalloutViewer.displayName = 'CalloutViewer';