ant-design / ant-design

An enterprise-class UI design language and React UI library
https://ant.design
MIT License
92.5k stars 49.79k forks source link

ConfigProvider for library based on antd #42447

Closed stanlaktionov closed 1 year ago

stanlaktionov commented 1 year ago

What problem does this feature solve?

We have 5 projects in mono repository. All projects are using library of components, built on top of antd. After migration to antd v5 we are facing a problem with overriding font family. Previously we used styles, applied for body element, which was inherited by the rest of components. But now, after v5 was released, font family (-apple-system,BlinkMacSystemFont,segoe ui,Roboto,helvetica neue,Arial,noto sans,sans-serif,apple color emoji,segoe ui emoji,segoe ui symbol,noto color emoji;) is specified almost per each component. Ant Design provides an ability to override font family in Config Provider with theme tokens. But in our case we do not have root component, because it is library, and each component is exported separately. Is there any way to specify theme overrides during the build, and avoid overriding font family for each component separately (either with <ConfigProvider or with css). Thank you!

What does the proposed API look like?

Some kind of webpack plugin, that accepts props, that will be overriden for theme

MadCcc commented 1 year ago
<ConfigProvider
  theme={{
    token: {
      fontFamily: 'xxx'
    }
  }}
>
  {Your components}
</ConfigProvider>

Ref: https://ant.design/docs/react/customize-theme

May this help you.

MadCcc commented 1 year ago

Personally I recommend you to wrap ConfigProvider in your own lib, and use it in your apps. In this way, you can simply proxy ConfigProvider for all your apps which depend on your lib.

Or using ConfigProvider in each application would be a good idea.

futbolistua commented 1 year ago

In this case, we need to wrap all Antd components inside our components lib and it's not flexible and not works as expected for libraries

stanlaktionov commented 1 year ago

@MadCcc

We have following structure: components/componentA components/componentB

and index.ts files with the following content

export from "./components/componentA"; export from "./components/componentB";

In your suggestion I have to wrap each component with <ConfigProvider> but I want to avoid it Any suggestions in my case?

MadCcc commented 1 year ago

In this case, we need to wrap all Antd components inside our components lib and it's not flexible and not works as expected for libraries

Sorry for my unclear answer. You can provide your own XXXConfigProvider in your lib:

export const XXXConfigProvider = (props) => (
  <ConfigProvider {...props} theme={{ ...props.theme, token: { ...props.theme.token, fontFamily: 'xxx' }}}>
    {props.children}
  </ConfigProvider>
);

And use it in your apps.

It's better to use ConfigProvider in apps only once than in lib with each component.

stanlaktionov commented 1 year ago

@MadCcc It's better to use ConfigProvider in apps only once than in lib with each component.

But library is compiled with wrong theme tokens

MadCcc commented 1 year ago

But library is compiled with wrong theme tokens

That's ok, tokens could be change during runtime with cssinjs.

futbolistua commented 1 year ago

We do not use cssinjs

MadCcc commented 1 year ago

We do not use cssinjs

Just use ConfigProvider in your apps, and the tokens in it like fontFamily will work for all the antd components in your app. You DO NOT need to use cssinjs or configure any other bundle tools, antd would take over this.

zombieJ commented 1 year ago

Follow up of @MadCcc 's topic. In our development, we have shared lib base on the antd which follow the antd theme.useToken for customize style usage. And additional create theme token package for different business line. This help the shared components can dynamic use in different design style business system and maintain the theme separated.

rpatidar commented 6 months ago

are there any workarounds ? i am facing similar issues.

https://stackoverflow.com/questions/78437036/antd-theme-not-applying-consistently-to-all-components

afc163 commented 6 months ago

@rpatidar Could you provide a codesandbox reproduce?

JaLe29 commented 5 months ago

What is a solution of this problem?

theashishmaurya commented 4 months ago

In this case, we need to wrap all Antd components inside our components lib and it's not flexible and not works as expected for libraries

Sorry for my unclear answer. You can provide your own XXXConfigProvider in your lib:

export const XXXConfigProvider = (props) => (
  <ConfigProvider {...props} theme={{ ...props.theme, token: { ...props.theme.token, fontFamily: 'xxx' }}}>
    {props.children}
  </ConfigProvider>
);

And use it in your apps.

It's better to use ConfigProvider in apps only once than in lib with each component.

If I do this In my main app the components which uses and theme are not updating or using the tokens.

RafaZeero commented 1 month ago

Any update on this?

KhoiDAO2k2 commented 1 month ago

I am also encountering this issue while developing an antd-based library for my projects. Is there a way for the antd components in my packages to inherit the closest ConfigProvider context when they are used in different projects?