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

Support for Web Components #28

Closed
felixschorer opened this issue Apr 28, 2022 · 2 comments
Closed

Support for Web Components #28

felixschorer opened this issue Apr 28, 2022 · 2 comments
Labels
enhancement New feature or request

Comments

@felixschorer
Copy link

Hello,

will there be an option to customize where the styles will be injected at runtime by the CSS-in-JS implementation?

We are currently using Antd v4 within Web Components to author micro frontends. Each micro frontend is a separate Web Component which internally uses a number of Antd components. We're using shadow DOM to isolate the styles of the different micro frontends.

I briefly looked through the code in this repository. If I understood the code correctly, the styles will currently be injected into the head of the document? This would break our approach to micro frontends as the Antd styles wouldn't be able to penetrate into the shadow DOM of our Web Components.

From my very basic understanding, it may be simple to add that feature. However, I am unaware of any constraints related to SSR.

The updateCSS function from rc-util already provides a way to override the HTML element where the styles will be injected using the attachTo option. Also, the StyleProvider already checks whether there is a parent StyleContext present. By providing a parent StyleContext in the application code it could be possible to implement a mechanism to override the HTML element where the styles will be injected:

  export interface StyleContextProps {
    autoClear?: boolean;
    mock?: 'server' | 'client';
    cache: CacheEntity;
    defaultCache: boolean;
+   attachTo: Element;
  }

Since Antd v4 isn't compatible with React 18, we will have to upgrade Antd v5 eventually. We would greatly appreciate it if there could be a solution to this problem.

Kind regards,
Felix Schorer

@zombieJ
Copy link
Member

zombieJ commented May 3, 2022

Thanks for feedback. Since current lib is alpha version. We may make sure it works as expect for basic requirement. But yes, this function seems helpful which should be added in.

@zombieJ zombieJ added the enhancement New feature or request label May 3, 2022
@felixschorer
Copy link
Author

I was able to make it work by nesting the app inside a StyleProvider:

import { StyleProvider } from '@ant-design/cssinjs'

<StyleProvider container={someElement}>
  <App />
</StyleProvider>

Thank you so much for making this work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants