Fab.tsx 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import React, {
  2. useState, useCallback, useRef, useEffect,
  3. } from 'react';
  4. import { animateScroll } from 'react-scroll';
  5. import { useRipple } from 'react-use-ripple';
  6. import { usePageCreateModal } from '~/stores/modal';
  7. import { useCurrentPagePath } from '~/stores/page';
  8. import { useIsAbleToChangeEditorMode } from '~/stores/ui';
  9. import { useSticky } from '~/stores/use-sticky';
  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: isAbleToChangeEditorMode } = useIsAbleToChangeEditorMode();
  17. const { data: currentPath = '' } = useCurrentPagePath();
  18. const { open: openCreateModal } = usePageCreateModal();
  19. const [animateClasses, setAnimateClasses] = useState<string>('invisible');
  20. const [buttonClasses, setButtonClasses] = useState<string>('');
  21. // ripple
  22. const createBtnRef = useRef(null);
  23. useRipple(createBtnRef, { rippleColor: 'rgba(255, 255, 255, 0.3)' });
  24. // Get sticky status
  25. const isSticky = useSticky('#grw-fav-sticky-trigger');
  26. /**
  27. * After the fade animation is finished, fix the button display status.
  28. * Prevents the fade animation occurred each time by button components rendered.
  29. * Check Fab.module.scss for fade animation time.
  30. */
  31. useEffect(() => {
  32. const timer = setTimeout(() => {
  33. if (isSticky) {
  34. setAnimateClasses('visible');
  35. setButtonClasses('');
  36. }
  37. else {
  38. setAnimateClasses('invisible');
  39. }
  40. }, 500);
  41. const newAnimateClasses = isSticky ? 'animated fadeInUp faster' : 'animated fadeOut faster';
  42. const newButtonClasses = isSticky ? '' : 'disabled grw-pointer-events-none';
  43. setAnimateClasses(newAnimateClasses);
  44. setButtonClasses(newButtonClasses);
  45. return () => clearTimeout(timer);
  46. }, [isSticky]);
  47. const PageCreateButton = useCallback(() => {
  48. return (
  49. <div className={`rounded-circle position-absolute ${animateClasses}`} style={{ bottom: '2.3rem', right: '4rem' }}>
  50. <button
  51. type="button"
  52. className={`btn btn-lg btn-create-page btn-primary rounded-circle p-0 ${buttonClasses}`}
  53. ref={createBtnRef}
  54. onClick={currentPath != null
  55. ? () => openCreateModal(currentPath)
  56. : undefined}
  57. >
  58. <CreatePageIcon />
  59. </button>
  60. </div>
  61. );
  62. }, [animateClasses, buttonClasses, currentPath, openCreateModal]);
  63. const ScrollToTopButton = useCallback(() => {
  64. const clickHandler = () => {
  65. animateScroll.scrollToTop({ duration: 200 });
  66. };
  67. return (
  68. <div className={`rounded-circle position-absolute ${animateClasses}`} style={{ bottom: 0, right: 0 }} data-testid="grw-fab-return-to-top">
  69. <button
  70. type="button"
  71. className={`btn btn-light btn-scroll-to-top rounded-circle p-0 ${buttonClasses}`}
  72. onClick={clickHandler}
  73. >
  74. <ReturnTopIcon />
  75. </button>
  76. </div>
  77. );
  78. }, [animateClasses, buttonClasses]);
  79. if (currentPath == null) {
  80. return <></>;
  81. }
  82. return (
  83. <div className={`${styles['grw-fab']} grw-fab d-none d-md-block d-edit-none`} data-testid="grw-fab-container">
  84. {isAbleToChangeEditorMode && <PageCreateButton />}
  85. <ScrollToTopButton />
  86. </div>
  87. );
  88. };