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

Overriding styles antd component on V5 #85

Open
meded90 opened this issue Jan 31, 2023 · 3 comments
Open

Overriding styles antd component on V5 #85

meded90 opened this issue Jan 31, 2023 · 3 comments

Comments

@meded90
Copy link

meded90 commented Jan 31, 2023

I want to customize the styles of some antd components written in cssinjs.

I created a HOC component to access the theme and override some styles and call it after defining the providers

import { useToken, useStyleRegister } from 'antd/es/theme/internal'
import { prefixCls } from 'Src/constants'
import { ReactNode } from 'react'
import { CSSObject } from '@ant-design/cssinjs'
import { GlobalToken } from 'antd/es/theme/interface'



function getStyleButton(token: GlobalToken): CSSObject {
  return {
    [`.${prefixCls}-btn`]: {
      ['&-default']: {
        backgroundColor: 'transparent '
      }
    }
  }
}



export const OverloadStyle = (props: { children: ReactNode }) => {
  const [theme, token, hashId] = useToken()

  useStyleRegister(
    {
      theme,
      token,
      hashId,
      path: ['OverloadStyle']
    },
    () => [
      getStyleButton(token),
    ]
  )
  return <>{props.children}</>
}

but there was a problem with style priority
image

calling !important is not the best way

Uploading image.png…

how to make those styles which I define were below?
Or are there other more convenient ways to extend the standard styles?

https://stackoverflow.com/questions/75286685/overriding-styles-antd-component-on-v5

@pooryak
Copy link

pooryak commented Feb 7, 2023

have you found any solution? @meded90

@meded90
Copy link
Author

meded90 commented Mar 17, 2023

You can call OverloadStyle at the bottom of react tree after app

<>
  <App/>
  <OverloadStyle/>
</>

but at the same time, it is important that the components you override must be initialized, if you have asynchronous loading of modules this may not work. so i just use !important
radix-ui – OverloadStyle 2023-03-17 17-22-15

@vipslord
Copy link

srsly? you just recommend to put !important?
better rename class to
css-dev-only-do-not-override-just-use-important-for-hashPriority

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

No branches or pull requests

3 participants