Presentation.tsx 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import { useEffect } from 'react';
  2. import Reveal from 'reveal.js';
  3. import type { PresentationOptions } from '../consts';
  4. import { Slides } from './Slides';
  5. import 'reveal.js/dist/reveal.css';
  6. import './Presentation.global.scss';
  7. import styles from './Presentation.module.scss';
  8. const baseRevealOptions: Reveal.Options = {
  9. // adjust size to the marp preset size
  10. width: 1280,
  11. height: 720,
  12. maxScale: 1.2,
  13. slideNumber: 'c/t',
  14. display: '',
  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. marp?: boolean,
  28. children?: string,
  29. }
  30. export const Presentation = (props: PresentationProps): JSX.Element => {
  31. const { options, marp, children } = props;
  32. const { revealOptions } = options;
  33. useEffect(() => {
  34. if (children == null) {
  35. return;
  36. }
  37. const deck = new Reveal({ ...baseRevealOptions, ...revealOptions });
  38. deck.initialize()
  39. .then(() => deck.slide(0)); // navigate to the first slide
  40. deck.on('ready', removeAllHiddenElements);
  41. deck.on('slidechanged', removeAllHiddenElements);
  42. return function cleanup() {
  43. deck.off('ready', removeAllHiddenElements);
  44. deck.off('slidechanged', removeAllHiddenElements);
  45. };
  46. }, [children, revealOptions]);
  47. return (
  48. <div className={`grw-presentation ${styles['grw-presentation']} reveal`}>
  49. <Slides options={options} hasMarpFlag={marp} presentation>{children}</Slides>
  50. </div>
  51. );
  52. };