CalloutViewer.tsx 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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_TITLE: CALLOUT_TO = {
  11. note: 'Note',
  12. tip: 'Tip',
  13. important: 'Important',
  14. warning: 'Warning',
  15. caution: 'Caution',
  16. };
  17. const CALLOUT_TO_ICON: CALLOUT_TO = {
  18. note: 'info',
  19. tip: 'lightbulb',
  20. important: 'feedback',
  21. warning: 'warning',
  22. caution: 'report',
  23. };
  24. type CalloutViewerProps = {
  25. children: ReactNode,
  26. node: Element,
  27. name: string
  28. }
  29. export const CalloutViewer = React.memo((props: CalloutViewerProps): JSX.Element => {
  30. const { node, name, children } = props;
  31. if (node == null) {
  32. return <></>;
  33. }
  34. return (
  35. <div className={`${moduleClass} callout-viewer`}>
  36. <div className={`callout callout-${CALLOUT_TO_TITLE[name].toLowerCase()}`}>
  37. <div className="callout-indicator">
  38. <div className="callout-hint">
  39. <span className="material-symbols-outlined">{CALLOUT_TO_ICON[name]}</span>
  40. </div>
  41. <div className="callout-title">
  42. {CALLOUT_TO_TITLE[name]}
  43. </div>
  44. </div>
  45. <div className="callout-content">
  46. {children}
  47. </div>
  48. </div>
  49. </div>
  50. );
  51. });
  52. CalloutViewer.displayName = 'CalloutViewer';