wxt-dev / wxt

⚡ Next-gen Web Extension Framework
https://wxt.dev
MIT License
3.04k stars 95 forks source link

Can you provide an example of integrating Antd? #601

Closed ggymzz closed 1 month ago

ggymzz commented 1 month ago

Describe the bug

A clear and concise description of what the bug is.

To Reproduce

Share minimal reproduction. Examples of a minimal reproduction:

⚠️ If you don't upload a minimal reproduction, your issue will be closed until a reproduction is added.

Why? https://antfu.me/posts/why-reproductions-are-required

Steps to reproduce the bug using the reproduction:

  1. Install dependencies: pnpm i
  2. Start dev mode: pnpm dev
  3. Click this...
  4. Do that...
  5. Etc...

Expected behavior

A clear and concise description of what you expected to happen.

Screenshots

If applicable, add screenshots to help explain your problem.

Environment

Paste output here

Additional context

Add any other context about the problem here, otherwise you can delete this section.

aklinker1 commented 1 month ago

No, just use it how you would in a regular project.

https://ant.design/docs/react/introduce

sedationh commented 1 month ago

@aklinker1 I try to use Antd 5 in content ui {createShadowRootUi} bt the style seems not work

image

except for that, in the shell output a lot of error log image

i provide the mvp here https://github.com/sedationh/awesome-toc/tree/question

ples attention the branch is question

aklinker1 commented 1 month ago

@sedationh Looks like by default, Ant adds component styles to the webpage's head element, not the shadow root. So basically, the styles are applied to the page, but the shadow root is isolated style-wise from the page, so they don't get applied to your UI.

They have docs for this exact case: https://ant.design/docs/react/compatible-style#shadow-dom-usage

As for WXT, createShadowRootUi handles creating the shadow root and the container. Just modify the onMount to look like this:

-     onMount: (container) => {
+     onMount: (container, shadow) => {
        const app = document.createElement('div');
        container.append(app);

        const root = ReactDOM.createRoot(app);
-       root.render(<App />);
+       root.render(
+        <StyleProvider container="shadow" />
+          <App />
+        <StyleProvider />
+       );
        return root;
      },

Here it is working:

Screenshot 2024-04-15 at 12 37 58 PM

As for the warnings, no idea whats going on there...

sedationh commented 1 month ago

@aklinker1 LGTM thank you~