Blame view

天文台pc/tianwentai-ui/node_modules/@mui/system/esm/ThemeProvider/ThemeProvider.js 4.4 KB
bc518174   王天杨   提交两个项目文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
  'use client';
  
  import * as React from 'react';
  import PropTypes from 'prop-types';
  import { ThemeProvider as MuiThemeProvider, useTheme as usePrivateTheme } from '@mui/private-theming';
  import exactProp from '@mui/utils/exactProp';
  import { ThemeContext as StyledEngineThemeContext } from '@mui/styled-engine';
  import useThemeWithoutDefault from "../useThemeWithoutDefault/index.js";
  import RtlProvider from "../RtlProvider/index.js";
  import DefaultPropsProvider from "../DefaultPropsProvider/index.js";
  import useLayerOrder from "./useLayerOrder.js";
  import { jsxs as _jsxs, jsx as _jsx } from "react/jsx-runtime";
  const EMPTY_THEME = {};
  function useThemeScoping(themeId, upperTheme, localTheme, isPrivate = false) {
    return React.useMemo(() => {
      const resolvedTheme = themeId ? upperTheme[themeId] || upperTheme : upperTheme;
      if (typeof localTheme === 'function') {
        const mergedTheme = localTheme(resolvedTheme);
        const result = themeId ? {
          ...upperTheme,
          [themeId]: mergedTheme
        } : mergedTheme;
        // must return a function for the private theme to NOT merge with the upper theme.
        // see the test case "use provided theme from a callback" in ThemeProvider.test.js
        if (isPrivate) {
          return () => result;
        }
        return result;
      }
      return themeId ? {
        ...upperTheme,
        [themeId]: localTheme
      } : {
        ...upperTheme,
        ...localTheme
      };
    }, [themeId, upperTheme, localTheme, isPrivate]);
  }
  
  /**
   * This component makes the `theme` available down the React tree.
   * It should preferably be used at **the root of your component tree**.
   *
   * <ThemeProvider theme={theme}> // existing use case
   * <ThemeProvider theme={{ id: theme }}> // theme scoping
   */
  function ThemeProvider(props) {
    const {
      children,
      theme: localTheme,
      themeId
    } = props;
    const upperTheme = useThemeWithoutDefault(EMPTY_THEME);
    const upperPrivateTheme = usePrivateTheme() || EMPTY_THEME;
    if (process.env.NODE_ENV !== 'production') {
      if (upperTheme === null && typeof localTheme === 'function' || themeId && upperTheme && !upperTheme[themeId] && typeof localTheme === 'function') {
        console.error(['MUI: You are providing a theme function prop to the ThemeProvider component:', '<ThemeProvider theme={outerTheme => outerTheme} />', '', 'However, no outer theme is present.', 'Make sure a theme is already injected higher in the React tree ' + 'or provide a theme object.'].join('\n'));
      }
    }
    const engineTheme = useThemeScoping(themeId, upperTheme, localTheme);
    const privateTheme = useThemeScoping(themeId, upperPrivateTheme, localTheme, true);
    const rtlValue = (themeId ? engineTheme[themeId] : engineTheme).direction === 'rtl';
    const layerOrder = useLayerOrder(engineTheme);
    return /*#__PURE__*/_jsx(MuiThemeProvider, {
      theme: privateTheme,
      children: /*#__PURE__*/_jsx(StyledEngineThemeContext.Provider, {
        value: engineTheme,
        children: /*#__PURE__*/_jsx(RtlProvider, {
          value: rtlValue,
          children: /*#__PURE__*/_jsxs(DefaultPropsProvider, {
            value: themeId ? engineTheme[themeId].components : engineTheme.components,
            children: [layerOrder, children]
          })
        })
      })
    });
  }
  process.env.NODE_ENV !== "production" ? ThemeProvider.propTypes /* remove-proptypes */ = {
    // ┌────────────────────────────── Warning ──────────────────────────────┐
    // │ These PropTypes are generated from the TypeScript type definitions. │
    // │    To update them, edit the d.ts file and run `pnpm proptypes`.     │
    // └─────────────────────────────────────────────────────────────────────┘
    /**
     * Your component tree.
     */
    children: PropTypes.node,
    /**
     * A theme object. You can provide a function to extend the outer theme.
     */
    theme: PropTypes.oneOfType([PropTypes.func, PropTypes.object]).isRequired,
    /**
     * The design system's unique id for getting the corresponded theme when there are multiple design systems.
     */
    themeId: PropTypes.string
  } : void 0;
  if (process.env.NODE_ENV !== 'production') {
    process.env.NODE_ENV !== "production" ? ThemeProvider.propTypes = exactProp(ThemeProvider.propTypes) : void 0;
  }
  export default ThemeProvider;