Fab.tsx 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import React, {
  2. useState, useCallback, useEffect, useRef,
  3. } from 'react';
  4. import { useRipple } from 'react-use-ripple';
  5. import StickyEvents from 'sticky-events';
  6. import { smoothScrollIntoView } from '~/client/util/smooth-scroll';
  7. import { useCurrentPagePath, useCurrentUser } from '~/stores/context';
  8. import { usePageCreateModal } from '~/stores/modal';
  9. import loggerFactory from '~/utils/logger';
  10. import { CreatePageIcon } from './Icons/CreatePageIcon';
  11. import { ReturnTopIcon } from './Icons/ReturnTopIcon';
  12. import styles from './Fab.module.scss';
  13. const logger = loggerFactory('growi:cli:Fab');
  14. export const Fab = (): JSX.Element => {
  15. const { data: currentUser } = useCurrentUser();
  16. const { data: currentPath = '' } = useCurrentPagePath();
  17. const { open: openCreateModal } = usePageCreateModal();
  18. const [animateClasses, setAnimateClasses] = useState('invisible');
  19. const [buttonClasses, setButtonClasses] = useState('');
  20. // ripple
  21. const createBtnRef = useRef(null);
  22. useRipple(createBtnRef, { rippleColor: 'rgba(255, 255, 255, 0.3)' });
  23. /*
  24. * TODO: Comment out to prevent err >>> TypeError: Cannot read properties of null (reading 'bottom')
  25. * We need add style={{ position: 'relative }} to child elements if disable StickyEvents. see: use grep = "<Fab".
  26. */
  27. // const stickyChangeHandler = useCallback((event) => {
  28. // logger.debug('StickyEvents.CHANGE detected');
  29. // const newAnimateClasses = event.detail.isSticky ? 'animated fadeInUp faster' : 'animated fadeOut faster';
  30. // const newButtonClasses = event.detail.isSticky ? '' : 'disabled grw-pointer-events-none';
  31. // setAnimateClasses(newAnimateClasses);
  32. // setButtonClasses(newButtonClasses);
  33. // }, []);
  34. // // setup effect by sticky event
  35. // useEffect(() => {
  36. // // sticky
  37. // // See: https://github.com/ryanwalters/sticky-events
  38. // const stickyEvents = new StickyEvents({ stickySelector: '#grw-fav-sticky-trigger' });
  39. // const { stickySelector } = stickyEvents;
  40. // const elem = document.querySelector(stickySelector);
  41. // elem.addEventListener(StickyEvents.CHANGE, stickyChangeHandler);
  42. // // return clean up handler
  43. // return () => {
  44. // elem.removeEventListener(StickyEvents.CHANGE, stickyChangeHandler);
  45. // };
  46. // }, [stickyChangeHandler]);
  47. if (currentPath == null) {
  48. return <></>;
  49. }
  50. const renderPageCreateButton = () => {
  51. return (
  52. <>
  53. <div className={`rounded-circle position-absolute ${animateClasses}`} style={{ bottom: '2.3rem', right: '4rem' }}>
  54. <button
  55. type="button"
  56. className={`btn btn-lg btn-create-page btn-primary rounded-circle p-0 ${buttonClasses}`}
  57. ref={createBtnRef}
  58. onClick={() => openCreateModal(currentPath)}
  59. >
  60. <CreatePageIcon />
  61. </button>
  62. </div>
  63. </>
  64. );
  65. };
  66. return (
  67. <div className={`${styles['grw-fab']} grw-fab d-none d-md-block d-edit-none`} data-testid="grw-fab">
  68. {currentUser != null && renderPageCreateButton()}
  69. <div className={`rounded-circle position-absolute ${animateClasses}`} style={{ bottom: 0, right: 0 }} data-testid="grw-fab-return-to-top">
  70. <button
  71. type="button"
  72. className={`btn btn-light btn-scroll-to-top rounded-circle p-0 ${buttonClasses}`}
  73. onClick={() => smoothScrollIntoView()}
  74. >
  75. <ReturnTopIcon />
  76. </button>
  77. </div>
  78. </div>
  79. );
  80. };