Presentation.tsx 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import React, { useEffect } from 'react';
  2. import Reveal from 'reveal.js';
  3. import type { PresentationOptions } from '../consts';
  4. import { parseSlideFrontmatterInMarkdown } from '../services/parse-slide-frontmatter';
  5. import { MARP_CONTAINER_CLASS_NAME, Slides } from './Slides';
  6. import 'reveal.js/dist/reveal.css';
  7. import './Presentation.global.scss';
  8. import styles from './Presentation.module.scss';
  9. const baseRevealOptions: Reveal.Options = {
  10. // adjust size to the marp preset size
  11. width: 1280,
  12. height: 720,
  13. maxScale: 1.2,
  14. slideNumber: 'c/t',
  15. };
  16. /**
  17. * Remove all [hidden] in order to activate transitions
  18. * cz: All of .past and .future elements are hidden by `display: none !important`
  19. * @see https://getbootstrap.com/docs/4.6/content/reboot/#html5-hidden-attribute
  20. */
  21. const removeAllHiddenElements = () => {
  22. const sections = document.querySelectorAll('.grw-presentation section');
  23. sections.forEach(section => section.removeAttribute('hidden'));
  24. };
  25. export type PresentationProps = {
  26. options: PresentationOptions,
  27. isEnabledMarp: boolean,
  28. children?: string,
  29. }
  30. export const Presentation = (props: PresentationProps): JSX.Element => {
  31. const { options, isEnabledMarp, children } = props;
  32. const { revealOptions } = options;
  33. let marp = false;
  34. if (isEnabledMarp) {
  35. [marp] = parseSlideFrontmatterInMarkdown(children);
  36. }
  37. useEffect(() => {
  38. let deck: Reveal.Api;
  39. if (children != null) {
  40. deck = new Reveal({ ...baseRevealOptions, ...revealOptions });
  41. deck.initialize()
  42. .then(() => deck.slide(0)); // navigate to the first slide
  43. deck.on('ready', removeAllHiddenElements);
  44. deck.on('slidechanged', removeAllHiddenElements);
  45. }
  46. return function cleanup() {
  47. deck?.off('ready', removeAllHiddenElements);
  48. deck?.off('slidechanged', removeAllHiddenElements);
  49. };
  50. }, [children, revealOptions]);
  51. return (
  52. <div className={`grw-presentation ${styles['grw-presentation']} reveal ${MARP_CONTAINER_CLASS_NAME}`}>
  53. <div className="slides">
  54. <Slides options={options} hasMarpFlag={marp}>{children}</Slides>
  55. </div>
  56. </div>
  57. );
  58. };