generated from arvinxx/npm-template
-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathindex.tsx
126 lines (106 loc) · 3.12 KB
/
index.tsx
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import { createEmotion, Emotion } from '@/core/createEmotion';
import { StyleManager } from '@/types';
import {
StyleProvider as AntdStyleProvider,
StyleProviderProps as AntdStyleProviderProps,
} from '@ant-design/cssinjs';
import { StylisPlugin } from '@emotion/cache';
import { Context, FC, memo, ReactNode, useContext, useEffect, useMemo } from 'react';
export interface StyleProviderProps
extends Partial<
Pick<
AntdStyleProviderProps,
'autoClear' | 'cache' | 'hashPriority' | 'ssrInline' | 'transformers' | 'linters'
>
> {
/**
* emotion 样式前缀,默认值为 acss
*/
prefix?: string;
/**
* 随机数,用于 CSP
*/
nonce?: string;
/**
* Stylis 插件
*/
stylisPlugins?: StylisPlugin[];
/**
* 渲染样式的容器
*/
container?: Element | ShadowRoot | null;
/**
* 开启极速模式,极速模式下不会插入真实的样式 style
* @default false
*/
speedy?: boolean;
/**
* 样式插入点,用于控制第一个 style 标签插入的位置
*/
insertionPoint?: HTMLElement;
/**
* 获取到 styleManager 实例的回调函数
* @param styleManager - StyleManager 实例
*/
getStyleManager?: (styleManager: StyleManager) => void;
/**
* 子组件
*/
children: ReactNode;
/**
* 启用 layer 属性
*/
layer?: boolean;
}
export const createStyleProvider = (EmotionContext: Context<Emotion>): FC<StyleProviderProps> =>
memo(
({
children,
prefix: outerPrefix,
speedy: outSpeedy,
getStyleManager,
container: outerContainer,
nonce,
insertionPoint,
stylisPlugins,
linters,
...antdStyleProviderProps
}) => {
const defaultEmotion = useContext(EmotionContext);
const prefix = outerPrefix ?? defaultEmotion.sheet.key;
const container = outerContainer ?? defaultEmotion.sheet.container;
const speedy = outSpeedy ?? defaultEmotion.sheet.isSpeedy;
const emotion = useMemo(() => {
const defaultSpeedy = process.env.NODE_ENV === 'development';
const instance = createEmotion({
speedy: speedy ?? defaultSpeedy,
key: prefix,
container: container as Node,
nonce,
insertionPoint,
stylisPlugins,
});
if (typeof global !== 'undefined') {
const cacheManager = global.__ANTD_STYLE_CACHE_MANAGER_FOR_SSR__;
if (cacheManager) {
// add 方法有幂等
instance.cache = cacheManager.add(instance.cache);
}
}
return instance;
}, [prefix, speedy, container, nonce, insertionPoint, stylisPlugins]);
useEffect(() => {
getStyleManager?.(emotion);
}, [emotion]);
const content = <EmotionContext.Provider value={emotion}>{children}</EmotionContext.Provider>;
if (Boolean(Object.keys(antdStyleProviderProps).length) || container) {
return (
// @ts-ignore
<AntdStyleProvider linters={linters} container={container} {...antdStyleProviderProps}>
{content}
</AntdStyleProvider>
);
}
return content;
},
);