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

🐛[BUG] StyleProvider 的 prefix 和 speedy 属性存在问题 #177

Open
pokerboard opened this issue Nov 6, 2024 · 0 comments
Open

Comments

@pokerboard
Copy link

💻 复现代码

1、StyleProvider 设置 speedy 为 true 时,不符合预期

import { StyleProvider, createGlobalStyle, createStyles } from 'antd-style';

const useStyles = createStyles({
  content: {
    color: 'red',
  },
});

const GlobalStyles = createGlobalStyle({
  '*': {
    fontSize: 40,
  },
});

const Demo: React.FC = () => {
  const { styles } = useStyles();

  return (
    <>
      <GlobalStyles />
      <div className={styles.content}>content</div>
    </>
  );
};

const Wrapper: React.FC = () => (
  <StyleProvider speedy>
    <Demo />
  </StyleProvider>
);

export default Wrapper;

image

2、StyleProvider 设置 prefix 为 demo,speedy 为 true 时,局部样式符合预期,全局样式前缀错误且不符合预期

import { StyleProvider, createGlobalStyle, createStyles } from 'antd-style';

const useStyles = createStyles({
  content: {
    color: 'red',
  },
});

const GlobalStyles = createGlobalStyle({
  '*': {
    fontSize: 40,
  },
});

const Demo: React.FC = () => {
  const { styles } = useStyles();

  return (
    <>
      <GlobalStyles />
      <div className={styles.content}>content</div>
    </>
  );
};

const Wrapper: React.FC = () => (
  <StyleProvider prefix="demo" speedy>
    <Demo />
  </StyleProvider>
);

export default Wrapper;

image

🏞 期望结果

image

临时解决方案

// @/utils/styles.tsx

import { CacheProvider, Global } from '@emotion/react';
import { serializeStyles } from '@emotion/serialize';
import type { CSSStyle } from 'antd-style';
import { createInstance } from 'antd-style';
import type { GlobalTheme } from 'antd-style/es/factories/createGlobalStyle';
import { memo } from 'react';

const instance = createInstance({
  key: 'demo',
  speedy: true,
});

export const { createStyles, useTheme, ThemeProvider } = instance;

export const createGlobalStyle = (
  ...styles: CSSStyle<GlobalTheme>
): React.FC<any> =>
  memo((props) => {
    const theme = useTheme();

    return (
      <Global
        styles={serializeStyles(styles, undefined, { ...props, theme })}
      />
    );
  });

export const StyleProvider: React.FC<React.PropsWithChildren> = ({
  children,
}) => (
  <CacheProvider value={instance.styleManager.cache}>{children}</CacheProvider>
);
import { StyleProvider, createGlobalStyle, createStyles } from '@/utils/styles';

const useStyles = createStyles({
  content: {
    color: 'red',
  },
});

const GlobalStyles = createGlobalStyle({
  '*': {
    fontSize: 40,
  },
});

const Demo: React.FC = () => {
  const { styles } = useStyles();

  return (
    <>
      <GlobalStyles />
      <div className={styles.content}>content</div>
    </>
  );
};

const Wrapper: React.FC = () => (
  <StyleProvider>
    <Demo />
  </StyleProvider>
);

export default Wrapper;
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

1 participant