chore: 当页面中没有对应id元素时才删除改id的样式 (#8213)

Co-authored-by: qinhaoyan <30946345+qinhaoyan@users.noreply.github.com>
This commit is contained in:
qkiroc 2023-09-22 17:09:05 +08:00 committed by GitHub
parent 921ea09052
commit 07fb301da7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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,