Skip to content

Commit a6c945c

Browse files
committed
fix: support shadow dom, close #6912
1 parent d0f7c34 commit a6c945c

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

Diff for: components/_util/cssinjs/StyleContext.tsx

+11-5
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import CacheEntity from './Cache';
44
import type { Linter } from './linters/interface';
55
import type { Transformer } from './transformers/interface';
66
import { arrayType, booleanType, objectType, someType, stringType, withInstall } from '../type';
7-
import initDefaultProps from '../props-util/initDefaultProps';
87
export const ATTR_TOKEN = 'data-token-hash';
98
export const ATTR_MARK = 'data-css-hash';
109
export const ATTR_CACHE_PATH = 'data-cache-path';
@@ -25,7 +24,10 @@ export function createCache() {
2524
(style as any)[CSS_IN_JS_INSTANCE] = (style as any)[CSS_IN_JS_INSTANCE] || cssinjsInstanceId;
2625

2726
// Not force move if no head
28-
document.head.insertBefore(style, firstChild);
27+
// Not force move if no head
28+
if ((style as any)[CSS_IN_JS_INSTANCE] === cssinjsInstanceId) {
29+
document.head.insertBefore(style, firstChild);
30+
}
2931
});
3032

3133
// Deduplicate of moved styles
@@ -83,12 +85,16 @@ const defaultStyleContext: StyleContextProps = {
8385
defaultCache: true,
8486
hashPriority: 'low',
8587
};
88+
// fix: https://github.com/vueComponent/ant-design-vue/issues/6912
8689
export const useStyleInject = () => {
87-
return inject(StyleContextKey, shallowRef({ ...defaultStyleContext }));
90+
return inject(StyleContextKey, shallowRef({ ...defaultStyleContext, cache: createCache() }));
8891
};
8992
export const useStyleProvider = (props: UseStyleProviderProps) => {
9093
const parentContext = useStyleInject();
91-
const context = shallowRef<Partial<StyleContextProps>>({ ...defaultStyleContext });
94+
const context = shallowRef<Partial<StyleContextProps>>({
95+
...defaultStyleContext,
96+
cache: createCache(),
97+
});
9298
watch(
9399
[() => unref(props), parentContext],
94100
() => {
@@ -144,7 +150,7 @@ export const StyleProvider = withInstall(
144150
defineComponent({
145151
name: 'AStyleProvider',
146152
inheritAttrs: false,
147-
props: initDefaultProps(styleProviderProps(), defaultStyleContext),
153+
props: styleProviderProps(),
148154
setup(props, { slots }) {
149155
useStyleProvider(props);
150156
return () => slots.default?.();

0 commit comments

Comments
 (0)