Fab.jsx 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import React, { useState, useCallback, useEffect } from 'react';
  2. import PropTypes from 'prop-types';
  3. import loggerFactory from '@alias/logger';
  4. import StickyEvents from 'sticky-events';
  5. import NavigationContainer from '../services/NavigationContainer';
  6. import { withUnstatedContainers } from './UnstatedUtils';
  7. const logger = loggerFactory('growi:cli:Fab');
  8. const Fab = (props) => {
  9. const { navigationContainer } = props;
  10. const [animateClasses, setAnimateClasses] = useState('invisible');
  11. const stickyChangeHandler = useCallback((event) => {
  12. logger.debug('StickyEvents.CHANGE detected');
  13. const classes = event.detail.isSticky ? 'animated fadeInUp faster' : 'animated fadeOut faster';
  14. setAnimateClasses(classes);
  15. }, []);
  16. // setup effect by sticky event
  17. useEffect(() => {
  18. // sticky
  19. // See: https://github.com/ryanwalters/sticky-events
  20. const stickyEvents = new StickyEvents({ stickySelector: '#grw-fav-sticky-trigger' });
  21. const { stickySelector } = stickyEvents;
  22. const elem = document.querySelector(stickySelector);
  23. elem.addEventListener(StickyEvents.CHANGE, stickyChangeHandler);
  24. // return clean up handler
  25. return () => {
  26. elem.removeEventListener(StickyEvents.CHANGE, stickyChangeHandler);
  27. };
  28. }, [stickyChangeHandler]);
  29. return (
  30. <div className="grw-fab d-none d-md-block">
  31. <div className={`rounded-circle position-absolute ${animateClasses}`} style={{ bottom: '2.3rem', right: '4rem' }}>
  32. <button
  33. type="button"
  34. className="btn btn-lg btn-create-page btn-primary rounded-circle p-0 waves-effect waves-light"
  35. onClick={navigationContainer.openPageCreateModal}
  36. >
  37. <i className="icon-pencil"></i>
  38. </button>
  39. </div>
  40. <div className={`rounded-circle position-absolute ${animateClasses}`} style={{ bottom: 0, right: 0 }}>
  41. <button type="button" className="btn btn-light btn-scroll-to-top rounded-circle p-0" onClick={() => navigationContainer.smoothScrollIntoView()}>
  42. <i className="icon-control-start"></i>
  43. </button>
  44. </div>
  45. </div>
  46. );
  47. };
  48. Fab.propTypes = {
  49. navigationContainer: PropTypes.instanceOf(NavigationContainer).isRequired,
  50. };
  51. export default withUnstatedContainers(Fab, [NavigationContainer]);