layout.js 1.74 KB
Newer Older
邓晓峰's avatar
邓晓峰 committed
1 2
import React, { useEffect, useState } from 'react';
import Layout from 'dumi-theme-default/src/layout';
dengxiaofeng's avatar
dengxiaofeng committed
3 4 5
import { ConfigProvider } from 'antd';
import zhCN from 'antd/es/locale/zh_CN';
import moment from 'moment';
邓晓峰's avatar
邓晓峰 committed
6 7 8
import Darkreader from 'react-darkreader';
import 'moment/locale/zh-cn';
import './layout.less';
dengxiaofeng's avatar
dengxiaofeng committed
9 10 11
moment.locale('zh-cn');

export default ({ children, ...props }) => {
邓晓峰's avatar
邓晓峰 committed
12 13 14
  const colorScheme = window.matchMedia('(prefers-color-scheme: dark)').matches && 'dark';
  const defaultDarken = localStorage.getItem('procomponents_dark_theme') || colorScheme;

dengxiaofeng's avatar
dengxiaofeng committed
15 16
  useEffect(() => {
    (function (h, o, t, j, a, r) {
邓晓峰's avatar
邓晓峰 committed
17
      // @ts-ignore
dengxiaofeng's avatar
dengxiaofeng committed
18
      h.hj =
邓晓峰's avatar
邓晓峰 committed
19
        // @ts-ignore
dengxiaofeng's avatar
dengxiaofeng committed
20 21
        h.hj ||
        function () {
邓晓峰's avatar
邓晓峰 committed
22
          // @ts-ignore
dengxiaofeng's avatar
dengxiaofeng committed
23 24
          (h.hj.q = h.hj.q || []).push(arguments);
        };
邓晓峰's avatar
邓晓峰 committed
25
      // @ts-ignore
dengxiaofeng's avatar
dengxiaofeng committed
26 27 28 29
      h._hjSettings = { hjid: 2036108, hjsv: 6 };
      a = o.getElementsByTagName('head')[0];
      r = o.createElement('script');
      r.async = 1;
邓晓峰's avatar
邓晓峰 committed
30
      // @ts-ignore
dengxiaofeng's avatar
dengxiaofeng committed
31 32 33 34 35
      r.src = t + h._hjSettings.hjid + j + h._hjSettings.hjsv;
      a.appendChild(r);
    })(window, document, 'https://static.hotjar.com/c/hotjar-', '.js?sv=');
  }, []);
  return (
邓晓峰's avatar
邓晓峰 committed
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
    <>
      <div
        style={{
          position: 'fixed',
          right: 8,
          top: 20,
          zIndex: 999,
        }}
      >
        <Darkreader
          defaultDarken={defaultDarken === 'dark'}
          onChange={(check) => {
            if (!check) {
              localStorage.setItem('procomponents_dark_theme', 'light');
              return;
            }
            localStorage.setItem('procomponents_dark_theme', 'dark');
          }}
        />
      </div>
      <ConfigProvider locale={zhCN}>
        <Layout {...props}>{children}</Layout>
      </ConfigProvider>
    </>
dengxiaofeng's avatar
dengxiaofeng committed
60
  );
邓晓峰's avatar
邓晓峰 committed
61
};