user8106148
user8106148

Reputation:

How to use css-in-js in ant-design components?

Actually, A ReactJS web application inherited to me and it was initialized by CRA and Ant-Design. I just worked with Material-UI and had no experience with And Design.

The way Material-UI handles the styles is awesome but AntD uses less to handle CSS. it's very hard for me to work like ancients.

I searched a lot but didn't find a way to use JSS[CSS in JS] in a project with Ant Design.

I mean like below:

import { AntDCompo } from 'antd';
import { Styled , injectStyled } from 'react-jss';

const MyCompo = ({ classes }) => (
  <AntDCompo className={classes.container} />
);

const styles = Styled({
  container: {
    color: '#f00',
  },
});

export default injectStyled(styles)(MyCompo);

But in the Ant Design docs just declare like ancient stuffs:

import { AntDCompo } from 'antd';
import styles from 'myCompoStyles.less';

const MyCompo = () => (
  <AntDCompo className={styles.container} />
);

How I can do it? or Is there any way to use JSS for AndD components?

Upvotes: 5

Views: 6379

Answers (1)

Dennis Vash
Dennis Vash

Reputation: 53874

As every CSS-in-JS, you can target the normal CSS-properties:

import { Icon } from 'antd';
const DarkHoverStyle = styled(Icon)`
  color: gray;
  :hover {
    color: palevioletred;
  }
`;

In some occasions you may need to target a specific antd CSS class.

const GlobalStyle = createGlobalStyle`
  .ant-tooltip-inner {
    background-color: palevioletred;
    color: black;
  }
`;

In the next example, we styled the hover color of an Icon and the default styling of the Tooltip component (changed the default black background):

export default function App() {
  return (
    <FlexBox>
      <GlobalStyle />
      <Tooltip title="Github Icon">
        <DarkHoverStyle type="github" style={{ fontSize: 100 }} />
      </Tooltip>
    </FlexBox>
  );
}

Edit react-antd-styled-template

Upvotes: 1

Related Questions