Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: 修复styled-components兼容性问题 #2606

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions src/sandbox/patchers/dynamicAppend/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,37 @@ export function recordStyledComponentsCSSRules(styleElements: HTMLStyleElement[]
*/
if (styleElement instanceof HTMLStyleElement && isStyledComponentsLike(styleElement)) {
if (styleElement.sheet) {
if (!styledComponentCSSRulesMap.has(styleElement)) {
const originSheet = styleElement.sheet;

(['insertRule', 'deleteRule'] as const).forEach((key) => {
const originSheetFn = originSheet[key];

originSheet[key] = function resetSheetFn(...args: any[]) {
if (styleElement.sheet?.[key] && styleElement.sheet[key] !== resetSheetFn) {
// @ts-ignore
return styleElement.sheet[key].call(styleElement.sheet, ...args);
} else {
// @ts-ignore
return originSheetFn.call(originSheet, ...args);
}
};
});

(['cssRules'] as const).forEach((key) => {
const originSheetElement = originSheet[key];

Object.defineProperty(originSheet, key, {
get() {
if (styleElement.sheet && styleElement.sheet !== originSheet) {
return styleElement.sheet[key];
} else {
return originSheetElement;
}
},
});
});
}
// record the original css rules of the style element for restore
styledComponentCSSRulesMap.set(styleElement, (styleElement.sheet as CSSStyleSheet).cssRules);
}
Expand Down