diff --git a/src/functions/createStyles/index.ts b/src/functions/createStyles/index.ts index d65390ab..115bb1bc 100644 --- a/src/functions/createStyles/index.ts +++ b/src/functions/createStyles/index.ts @@ -14,7 +14,7 @@ import type { Theme, ThemeAppearance, } from '@/types'; -import { isReactCssResult } from '@/utils'; +import { createCX, isReactCssResult } from '@/utils'; import { convertResponsiveStyleToString, useMediaQueryMap } from './response'; @@ -81,8 +81,7 @@ export const createStyles = // 由于使用了 reactCss 作为基础样式工具,因此在使用 cx 级联 className 时需要使用特殊处理的 cx // 要将 reactCss 的产出转为 css 产物 - const reactCx: ClassNamesUtil = (...classNames) => - cx(...(classNames.map((c) => (isReactCssResult(c) ? css(c) : c)) as any[])); + const cxUtil: ClassNamesUtil = createCX(css, cx); const styles = useMemo(() => { let tempStyles: ReturnStyleToUse; @@ -106,7 +105,7 @@ export const createStyles = isDarkMode, prefixCls, // 工具函数们 - cx: reactCx, + cx: cxUtil, css: reactCss, responsive, }, @@ -145,6 +144,6 @@ export const createStyles = return useMemo(() => { const { prefixCls, ...res } = theme; - return { styles, cx: reactCx, theme: res, prefixCls }; + return { styles, cx: cxUtil, theme: res, prefixCls }; }, [styles, theme]); }; diff --git a/src/functions/css.ts b/src/functions/css.ts index c0868dc3..4e335b65 100644 --- a/src/functions/css.ts +++ b/src/functions/css.ts @@ -1,3 +1,5 @@ +import { ClassNamesUtil } from '@/types'; +import { createCX } from '@/utils'; import createEmotion from '@emotion/css/create-instance'; export const emotion = createEmotion({ @@ -5,18 +7,15 @@ export const emotion = createEmotion({ speedy: false, }); -export const { - css, - cx, - injectGlobal, - keyframes, - sheet, - flush, - merge, - hydrate, - getRegisteredStyles, - cache, -} = emotion; +export const { injectGlobal, keyframes, sheet, flush, merge, hydrate, getRegisteredStyles, cache } = + emotion; export type { Emotion } from '@emotion/css/create-instance'; +export { css } from '@emotion/react'; export { createEmotion }; + +/** + * 用于将 css 生成的 styles 创建为 className + * @param classNames + */ +export const cx: ClassNamesUtil = createCX(emotion.css, emotion.cx); diff --git a/src/utils/css.ts b/src/utils/css.ts index 855cb7b5..954c98e7 100644 --- a/src/utils/css.ts +++ b/src/utils/css.ts @@ -1,6 +1,21 @@ +import { ClassNamesUtil } from '@/types'; +import { Emotion } from '@emotion/css/create-instance'; + /** * 判断是否是 ReactCss 的编译产物 * @param params */ export const isReactCssResult = (params: any) => typeof params === 'object' && 'styles' in params && 'name' in params && 'toString' in params; + +export const createCX = + (css: Emotion['css'], cx: Emotion['cx']): ClassNamesUtil => + (...classNames) => { + return cx( + ...(classNames.map((c) => + // 由于使用了 reactCss 作为基础样式工具,因此在使用 cx 级联 className 时需要使用特殊处理的 cx + // 要将 reactCss 的产出转为 css 产物 + isReactCssResult(c) ? css(c) : c, + ) as any[]), + ); + }; diff --git a/tests/containers/ThemeProvider.test.tsx b/tests/containers/ThemeProvider.test.tsx index 8c62ed01..12e5fe99 100644 --- a/tests/containers/ThemeProvider.test.tsx +++ b/tests/containers/ThemeProvider.test.tsx @@ -1,6 +1,6 @@ import { act, render, renderHook } from '@testing-library/react'; import { App, theme } from 'antd'; -import { css, GetCustomToken, ThemeProvider, useTheme, useThemeMode } from 'antd-style'; +import { css, cx, GetCustomToken, ThemeProvider, useTheme, useThemeMode } from 'antd-style'; import { MappingAlgorithm } from 'antd/es/config-provider/context'; import { MessageInstance } from 'antd/es/message/interface'; import { NotificationInstance } from 'antd/es/notification/interface'; @@ -100,7 +100,7 @@ describe('ThemeProvider', () => { it('自定义 Stylish', () => { const App = () => { const theme = useTheme(); - return
普通文本
; + return
普通文本
; }; const { container } = render( @@ -198,11 +198,11 @@ describe('ThemeProvider', () => { diff --git a/tests/functions/styled.test.tsx b/tests/functions/styled.test.tsx index c2582462..2c47f1fc 100644 --- a/tests/functions/styled.test.tsx +++ b/tests/functions/styled.test.tsx @@ -60,7 +60,7 @@ describe('styled', () => { expect(container).toMatchSnapshot(); - rerender(卡片); + rerender(卡片); expect(content).toHaveStyleRule('border-radius', '8px'); expect(content).toHaveStyleRule('padding', '24px');