Presentation.tsx 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import React, { useEffect } from 'react';
  2. import Reveal from 'reveal.js';
  3. import type { PresentationOptions } from '../consts';
  4. import { MARP_CONTAINER_CLASS_NAME, 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. };
  15. /**
  16. * Remove all [hidden] in order to activate transitions
  17. * cz: All of .past and .future elements are hidden by `display: none !important`
  18. * @see https://getbootstrap.com/docs/4.6/content/reboot/#html5-hidden-attribute
  19. */
  20. const removeAllHiddenElements = () => {
  21. const sections = document.querySelectorAll('.grw-presentation section');
  22. sections.forEach(section => section.removeAttribute('hidden'));
  23. };
  24. export type PresentationProps = {
  25. options: PresentationOptions,
  26. children?: string,
  27. }
  28. export const Presentation = (props: PresentationProps): JSX.Element => {
  29. const { options, children } = props;
  30. const { revealOptions } = options;
  31. useEffect(() => {
  32. let deck: Reveal.Api;
  33. if (children != null) {
  34. deck = new Reveal({ ...baseRevealOptions, ...revealOptions });
  35. deck.initialize()
  36. .then(() => deck.slide(0)); // navigate to the first slide
  37. deck.on('ready', removeAllHiddenElements);
  38. deck.on('slidechanged', removeAllHiddenElements);
  39. }
  40. return function cleanup() {
  41. deck?.off('ready', removeAllHiddenElements);
  42. deck?.off('slidechanged', removeAllHiddenElements);
  43. };
  44. }, [children, revealOptions]);
  45. return (
  46. <div className={`grw-presentation ${styles['grw-presentation']} reveal ${MARP_CONTAINER_CLASS_NAME}`}>
  47. <div className="slides">
  48. <Slides options={options}>{children}</Slides>
  49. </div>
  50. </div>
  51. );
  52. };