diff --git a/src/content/index.css b/src/assets/styles/index.css similarity index 100% rename from src/content/index.css rename to src/assets/styles/index.css diff --git a/src/content/index.dev.tsx b/src/content/index.dev.tsx index 01b16c0..de1e294 100644 --- a/src/content/index.dev.tsx +++ b/src/content/index.dev.tsx @@ -14,7 +14,7 @@ import { createRoot } from 'react-dom/client'; import Content from './Content'; -import './index.css'; +import '@assets/styles/index.css'; const container = document.createElement('div'); diff --git a/src/content/index.prod.tsx b/src/content/index.prod.tsx index e5c6e8f..8e2df63 100644 --- a/src/content/index.prod.tsx +++ b/src/content/index.prod.tsx @@ -1,17 +1,9 @@ import React from 'react'; -import { createRoot } from 'react-dom/client'; +import styles from '@assets/styles/index.css?inline'; +import createShadowRoot from '@utils/createShadowRoot'; import Content from './Content'; -import styles from './index.css?inline'; -const container = document.createElement('div'); -const shadow = container.attachShadow({ mode: 'open' }); -const globalStyleSheet = new CSSStyleSheet(); -globalStyleSheet.replaceSync(styles); - -shadow.adoptedStyleSheets = [globalStyleSheet]; -document.body.appendChild(container); - -const root = createRoot(shadow); +const root = createShadowRoot(styles); root.render(); diff --git a/src/manifest.ts b/src/manifest.ts index 6deae5b..d3de10f 100644 --- a/src/manifest.ts +++ b/src/manifest.ts @@ -18,6 +18,7 @@ export default defineManifest({ }, options_page: 'src/options/index.html', action: { + default_popup: 'src/popup/index.html', default_icon: { 16: 'icon16.png', 32: 'icon32.png', diff --git a/src/options/Options.tsx b/src/options/Options.tsx index a57c937..371d5e0 100644 --- a/src/options/Options.tsx +++ b/src/options/Options.tsx @@ -1,5 +1,20 @@ import React, { JSX } from 'react'; export default function Options(): JSX.Element { - return
Options
; + return ( +
+ + + + +
+ ); } diff --git a/src/options/index.css b/src/options/index.css deleted file mode 100644 index e69de29..0000000 diff --git a/src/options/index.tsx b/src/options/index.tsx index 940c343..d70c7a2 100644 --- a/src/options/index.tsx +++ b/src/options/index.tsx @@ -1,10 +1,9 @@ import React from 'react'; -import { createRoot } from 'react-dom/client'; +import styles from '@assets/styles/index.css?inline'; +import createShadowRoot from '@utils/createShadowRoot'; import Options from './Options'; -import './index.css'; - -const root = createRoot(document.getElementById('my-ext-options-page')!); +const root = createShadowRoot(styles); root.render(); diff --git a/src/popup/Popup.tsx b/src/popup/Popup.tsx new file mode 100644 index 0000000..ea64081 --- /dev/null +++ b/src/popup/Popup.tsx @@ -0,0 +1,20 @@ +import React, { JSX } from 'react'; + +export default function Popup(): JSX.Element { + return ( +
+ + + + +
+ ); +} diff --git a/src/popup/index.html b/src/popup/index.html new file mode 100644 index 0000000..878bc91 --- /dev/null +++ b/src/popup/index.html @@ -0,0 +1,12 @@ + + + + + my ext popup page + + + +
+ + + diff --git a/src/popup/index.tsx b/src/popup/index.tsx new file mode 100644 index 0000000..f878930 --- /dev/null +++ b/src/popup/index.tsx @@ -0,0 +1,9 @@ +import React from 'react'; +import styles from '@assets/styles/index.css?inline'; +import createShadowRoot from '@utils/createShadowRoot'; + +import Popup from './Popup'; + +const root = createShadowRoot(styles); + +root.render(); diff --git a/src/utils/createShadowRoot.tsx b/src/utils/createShadowRoot.tsx new file mode 100644 index 0000000..aaf9b81 --- /dev/null +++ b/src/utils/createShadowRoot.tsx @@ -0,0 +1,27 @@ +import { createRoot } from 'react-dom/client'; + +/** + * Creates a shadow root with the specified styles and returns a React root in it. + * @param {string} styles - CSS styles to be applied to the shadow root. + * @returns {ReactRoot} - React root rendered inside the shadow root. + */ +export default function createShadowRoot(styles: string) { + // Create a container element to hold the shadow root + const container = document.createElement('div'); + + // Attach a shadow root to the container element + const shadow = container.attachShadow({ mode: 'open' }); + + // Create a new CSS style sheet and apply the specified styles + const globalStyleSheet = new CSSStyleSheet(); + globalStyleSheet.replaceSync(styles); + + // Apply the style sheet to the shadow root + shadow.adoptedStyleSheets = [globalStyleSheet]; + + // Append the container element to the document body + document.body.appendChild(container); + + // Return a React root created inside the shadow root + return createRoot(shadow); +} diff --git a/vite.config.ts b/vite.config.ts index bf6dac2..c3b0327 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -36,6 +36,8 @@ export default defineConfig({ resolve: { alias: { '@': resolve(__dirname, './src'), + '@utils': resolve(__dirname, './src/utils'), + '@assets': resolve(__dirname, './src/assets'), }, }, });