PagePathNavTitle.tsx 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import { type JSX, useState } from 'react';
  2. import dynamic from 'next/dynamic';
  3. import withLoadingProps from 'next-dynamic-loading-props';
  4. import { useIsomorphicLayoutEffect } from 'usehooks-ts';
  5. import type { PagePathNavLayoutProps } from '../PagePathNav';
  6. import { PagePathNav } from '../PagePathNav';
  7. import styles from './PagePathNavTitle.module.scss';
  8. const moduleClass = styles['grw-page-path-nav-title'] ?? '';
  9. const PagePathNavSticky = withLoadingProps<PagePathNavLayoutProps>(
  10. (useLoadingProps) =>
  11. dynamic(
  12. () =>
  13. import('~/client/components/PagePathNavSticky').then(
  14. (mod) => mod.PagePathNavSticky,
  15. ),
  16. {
  17. ssr: false,
  18. loading: () => {
  19. // eslint-disable-next-line react-hooks/rules-of-hooks
  20. const props = useLoadingProps();
  21. return <PagePathNav {...props} />;
  22. },
  23. },
  24. ),
  25. );
  26. /**
  27. * Switch PagePathNav and PagePathNavSticky
  28. * @returns
  29. */
  30. export const PagePathNavTitle = (
  31. props: PagePathNavLayoutProps,
  32. ): JSX.Element => {
  33. const [isClient, setClient] = useState(false);
  34. useIsomorphicLayoutEffect(() => {
  35. setClient(true);
  36. }, []);
  37. const className = `${moduleClass} mb-4`;
  38. return isClient ? (
  39. <PagePathNavSticky
  40. {...props}
  41. className={className}
  42. latterLinkClassName="fs-2"
  43. />
  44. ) : (
  45. <PagePathNav {...props} className={className} latterLinkClassName="fs-2" />
  46. );
  47. };