ant-design / antd-style

css-in-js library with antd v5 token system
https://ant-design.github.io/antd-style/
MIT License
187 stars 29 forks source link

🧐 Using with class components #85

Open dashqa opened 11 months ago

dashqa commented 11 months ago

Any chance of using this library with class components? I tried to make HOC that passes useStyle hook properties to the class component, but now it's not possible to pass classname like className={styles.name} because antd-style generates random class name.

arvinxx commented 10 months ago

maybe I can add a method like withStyle HOC to support class Component ?

dashqa commented 10 months ago

I think the HOC implementation would be helpful to other developers and is included in the antd-style package.

I have already implemented it as follows:

const withStyles = (WrappedComponent, useStyles) => {
  const WithProps = React.memo((props) => {
    return <WrappedComponent {...props} />;
  });

  function WithStyles(props) {
    const styleProps = useStyles(props);
    const themeModeProps = useThemeMode();
    const responsiveProps = useResponsive();

    const styles = useMemo(
      () => ({
        ...styleProps,
        theme: themeModeProps,
        responsive: responsiveProps,
      }),
      [styleProps, themeModeProps, responsiveProps]
    );

    return <WithProps {...props} _style={styles} />;
  }

  return WithStyles;
};

export default withStyles;