diff --git a/packages/amis-core/src/components/CustomStyle.tsx b/packages/amis-core/src/components/CustomStyle.tsx index 5b4e4246a..aa2a3ad73 100644 --- a/packages/amis-core/src/components/CustomStyle.tsx +++ b/packages/amis-core/src/components/CustomStyle.tsx @@ -15,6 +15,8 @@ interface CustomStyleProps { env: RendererEnv; } +export const styleIdCount = new Map(); + export default function (props: CustomStyleProps) { const {config, env} = props; const {themeCss, classNames, id, defaultData, wrapperCustomStyle} = config; @@ -22,6 +24,22 @@ export default function (props: CustomStyleProps) { return null; } + useEffect(() => { + if (styleIdCount.has(id)) { + styleIdCount.set(id, styleIdCount.get(id) + 1); + } else if (id) { + styleIdCount.set(id, 1); + } + return () => { + if (styleIdCount.has(id)) { + styleIdCount.set(id, styleIdCount.get(id) - 1); + if (styleIdCount.get(id) === 0) { + styleIdCount.delete(id); + } + } + }; + }, [id]); + useEffect(() => { if (themeCss && id) { insertCustomStyle( @@ -35,7 +53,7 @@ export default function (props: CustomStyleProps) { } return () => { - if (id) { + if (id && !styleIdCount.get(id)) { removeCustomStyle('', id, env.getModalContainer?.().ownerDocument); } }; @@ -51,7 +69,7 @@ export default function (props: CustomStyleProps) { } return () => { - if (id) { + if (id && !styleIdCount.get(id)) { removeCustomStyle( 'wrapperCustomStyle', id,