Blame view

天文台pc/tianwentai-ui/node_modules/@mui/system/esm/ThemeProvider/useLayerOrder.js 1.9 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
  import useEnhancedEffect from '@mui/utils/useEnhancedEffect';
  import useId from '@mui/utils/useId';
  import GlobalStyles from "../GlobalStyles/index.js";
  import useThemeWithoutDefault from "../useThemeWithoutDefault/index.js";
  
  /**
   * This hook returns a `GlobalStyles` component that sets the CSS layer order (for server-side rendering).
   * Then on client-side, it injects the CSS layer order into the document head to ensure that the layer order is always present first before other Emotion styles.
   */
  import { jsx as _jsx } from "react/jsx-runtime";
  export default function useLayerOrder(theme) {
    const upperTheme = useThemeWithoutDefault();
    const id = useId() || '';
    const {
      modularCssLayers
    } = theme;
    let layerOrder = 'mui.global, mui.components, mui.theme, mui.custom, mui.sx';
    if (!modularCssLayers || upperTheme !== null) {
      // skip this hook if upper theme exists.
      layerOrder = '';
    } else if (typeof modularCssLayers === 'string') {
      layerOrder = modularCssLayers.replace(/mui(?!\.)/g, layerOrder);
    } else {
      layerOrder = `@layer ${layerOrder};`;
    }
    useEnhancedEffect(() => {
      const head = document.querySelector('head');
      if (!head) {
        return;
      }
      const firstChild = head.firstChild;
      if (layerOrder) {
        // Only insert if first child doesn't have data-mui-layer-order attribute
        if (firstChild && firstChild.hasAttribute?.('data-mui-layer-order') && firstChild.getAttribute('data-mui-layer-order') === id) {
          return;
        }
        const styleElement = document.createElement('style');
        styleElement.setAttribute('data-mui-layer-order', id);
        styleElement.textContent = layerOrder;
        head.prepend(styleElement);
      } else {
        head.querySelector(`style[data-mui-layer-order="${id}"]`)?.remove();
      }
    }, [layerOrder, id]);
    if (!layerOrder) {
      return null;
    }
    return /*#__PURE__*/_jsx(GlobalStyles, {
      styles: layerOrder
    });
  }