Fab.tsx 3.7 KB

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