RawLayout.tsx 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import React, { ReactNode, useState } from 'react';
  2. import { ColorScheme } from '@growi/core';
  3. import Head from 'next/head';
  4. import { ToastContainer } from 'react-toastify';
  5. import { useIsomorphicLayoutEffect } from 'usehooks-ts';
  6. import { useNextThemes, NextThemesProvider } from '~/stores/use-next-themes';
  7. import loggerFactory from '~/utils/logger';
  8. const logger = loggerFactory('growi:cli:RawLayout');
  9. type Props = {
  10. className?: string,
  11. children?: ReactNode,
  12. }
  13. export const RawLayout = ({ children, className }: Props): JSX.Element => {
  14. const classNames: string[] = ['layout-root', 'growi'];
  15. if (className != null) {
  16. classNames.push(className);
  17. }
  18. // get color scheme from next-themes
  19. const { resolvedTheme, resolvedThemeByAttributes } = useNextThemes();
  20. const [colorScheme, setColorScheme] = useState<ColorScheme|undefined>(undefined);
  21. // set colorScheme in CSR
  22. useIsomorphicLayoutEffect(() => {
  23. setColorScheme(resolvedTheme ?? resolvedThemeByAttributes);
  24. }, [resolvedTheme, resolvedThemeByAttributes]);
  25. return (
  26. <>
  27. <Head>
  28. <meta charSet="utf-8" />
  29. <meta name="viewport" content="initial-scale=1.0, width=device-width" />
  30. </Head>
  31. <NextThemesProvider>
  32. <div className={classNames.join(' ')} data-color-scheme={colorScheme}>
  33. {children}
  34. <ToastContainer theme={colorScheme} />
  35. </div>
  36. </NextThemesProvider>
  37. </>
  38. );
  39. };