Skip to content

Commit 87b3919

Browse files
committed
fix: firefox content script styling bug
1 parent 245f478 commit 87b3919

File tree

3 files changed

+30
-11
lines changed

3 files changed

+30
-11
lines changed

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,15 @@
2424
"dependencies": {
2525
"@eduardoac-skimlinks/webext-redux": "3.0.1-release-candidate",
2626
"@reduxjs/toolkit": "^1.9.5",
27+
"@twind/core": "^1.1.3",
28+
"@twind/preset-autoprefix": "^1.0.7",
29+
"@twind/preset-tailwind": "^1.1.4",
30+
"lodash-es": "^4.17.21",
2731
"react": "18.2.0",
2832
"react-dom": "18.2.0",
2933
"react-redux": "^8.1.0",
3034
"redux-persist-webextension-storage": "^1.0.2",
3135
"reduxjs-toolkit-persist": "^7.2.1",
32-
"@twind/core": "^1.1.3",
33-
"@twind/preset-autoprefix": "^1.0.7",
34-
"@twind/preset-tailwind": "^1.1.4",
3536
"webextension-polyfill": "^0.10.0"
3637
},
3738
"devDependencies": {
@@ -61,7 +62,6 @@
6162
"jest": "^29.5.0",
6263
"jest-chrome": "^0.8.0",
6364
"jest-environment-jsdom": "^29.5.0",
64-
"lodash-es": "^4.17.21",
6565
"nano-staged": "^0.8.0",
6666
"npm-run-all2": "^6.0.5",
6767
"postcss": "^8.4.24",

src/content/index.tsx

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ import 'construct-style-sheets-polyfill';
33
import React from 'react';
44
import { createRoot } from 'react-dom/client';
55
import { Provider } from 'react-redux';
6-
import { twind, config, cssom, observe } from './twind';
6+
import { debounce } from 'lodash-es';
7+
import { twind, config, cssom, observe, stringify } from './twind';
78
import { proxyStore } from '../app/proxyStore';
89
import Content from './Content';
910

@@ -14,16 +15,34 @@ proxyStore.ready().then(() => {
1415
document.body.append(contentRoot);
1516

1617
const shadowRoot = contentRoot.attachShadow({ mode: 'open' });
18+
const sheet = cssom(new CSSStyleSheet());
19+
20+
// shadowRoot.adoptedStyleSheet bug in firefox
21+
// see: https://bugzilla.mozilla.org/show_bug.cgi?id=1827104
22+
if (navigator?.userAgent.includes('Firefox')) {
23+
const style = document.createElement('style');
24+
const debouncedSyncCss = debounce(() => {
25+
style.textContent += stringify(sheet.target);
26+
}, 100);
27+
28+
const originalSheetInsert = sheet.insert;
29+
(sheet.insert as typeof originalSheetInsert) = (...params) => {
30+
originalSheetInsert(...params);
31+
debouncedSyncCss();
32+
};
33+
shadowRoot.appendChild(style);
34+
} else {
35+
shadowRoot.adoptedStyleSheets = [sheet.target];
36+
}
37+
38+
const tw = twind(config, sheet);
39+
observe(tw, shadowRoot);
40+
1741
const shadowWrapper = document.createElement('div');
1842
shadowWrapper.id = 'root';
1943
shadowWrapper.style.display = 'contents';
2044
shadowRoot.appendChild(shadowWrapper);
2145

22-
const sheet = cssom(new CSSStyleSheet());
23-
const tw = twind(config, sheet);
24-
shadowRoot.adoptedStyleSheets = [sheet.target];
25-
observe(tw, shadowRoot);
26-
2746
createRoot(shadowWrapper).render(
2847
<React.StrictMode>
2948
<Provider store={proxyStore}>

src/popup/Popup.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Counter } from '../app/features/counter';
22

33
const Popup = () => {
4-
document.body.className = 'w-[30rem] h-[10rem]';
4+
document.body.className = 'w-[30rem] h-[15rem]';
55

66
return (
77
<>

0 commit comments

Comments
 (0)