Skip to content

Commit

Permalink
✨ feat: 优化 StyledProvider 实现逻辑并新增 useEmotion 的 hook
Browse files Browse the repository at this point in the history
  • Loading branch information
arvinxx committed Jan 21, 2023
1 parent c4feb3b commit d9d8014
Show file tree
Hide file tree
Showing 9 changed files with 30 additions and 46 deletions.
11 changes: 4 additions & 7 deletions src/containers/StyleProvider/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { StyleUtilsContext } from '@/context/StyleUtilsContext';
import { EmotionContext } from '@/context/EmotionContext';
import { createEmotion } from '@/functions';
import { StylisPlugin } from '@emotion/cache';
import { Emotion } from '@emotion/css/create-instance';
Expand Down Expand Up @@ -36,7 +36,8 @@ export const StyleProvider: FC<StyleProviderProps> = memo(
...emotionOptions
}) => {
const emotion = useMemo(() => {
const defaultSpeedy = process.env.NODE_ENV !== 'development';
const defaultSpeedy = process.env.NODE_ENV === 'development';

return createEmotion({
speedy: speedy ?? defaultSpeedy,
key: prefix,
Expand All @@ -48,10 +49,6 @@ export const StyleProvider: FC<StyleProviderProps> = memo(
getEmotionInstance?.(emotion);
}, [emotion]);

return (
<StyleUtilsContext.Provider value={{ css: emotion.css, cx: emotion.cx }}>
{children}
</StyleUtilsContext.Provider>
);
return <EmotionContext.Provider value={emotion}>{children}</EmotionContext.Provider>;
},
);
10 changes: 10 additions & 0 deletions src/context/EmotionContext.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { createContext } from 'react';

import { emotion, type Emotion } from '@/functions';

export interface CommonStyleUtils {
cx: Emotion['cx'];
css: Emotion['css'];
}

export const EmotionContext = createContext<Emotion>(emotion);
13 changes: 0 additions & 13 deletions src/context/StyleUtilsContext.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/context/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export * from './StyleUtilsContext';
export * from './EmotionContext';
export * from './ThemeModeContext';
5 changes: 2 additions & 3 deletions src/functions/createStyles.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { useMemo } from 'react';

import { CommonStyleUtils } from '@/context';
import { useTheme } from '@/hooks';
import { useStyleUtils } from '@/hooks/useStyleUtils';
import { useEmotion, useTheme } from '@/hooks';
import {
FullStylish,
FullToken,
Expand Down Expand Up @@ -52,7 +51,7 @@ export const createStyles =
) =>
(props?: Props): ReturnStyles<Input> => {
const theme = useTheme();
const { css, cx } = useStyleUtils();
const { css, cx } = useEmotion();

const styles = useMemo(() => {
let tempStyles: ReturnStyleToUse<Input>;
Expand Down
23 changes: 6 additions & 17 deletions src/functions/css.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,11 @@
import createEmotion from '@emotion/css/create-instance';

const {
css,
cx,
injectGlobal,
keyframes,
sheet,
flush,
merge,
hydrate,
getRegisteredStyles,
cache,
} = createEmotion({
export const emotion = createEmotion({
key: 'ant-css',
speedy: false,
});

export { type CSSObject } from '@emotion/css';
export type { Emotion } from '@emotion/css/create-instance';
export {
export const {
css,
cx,
injectGlobal,
Expand All @@ -28,6 +15,8 @@ export {
merge,
hydrate,
getRegisteredStyles,
createEmotion,
cache,
};
} = emotion;
export { type CSSObject } from '@emotion/css';
export type { Emotion } from '@emotion/css/create-instance';
export { createEmotion };
1 change: 1 addition & 0 deletions src/hooks/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export * from './useAntdStylish';
export * from './useAntdTheme';
export * from './useAntdToken';
export * from './useEmotion';
export * from './useTheme';
export * from './useThemeMode';
6 changes: 6 additions & 0 deletions src/hooks/useEmotion.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { useContext } from 'react';

import { EmotionContext } from '@/context';
import { Emotion } from '../functions';

export const useEmotion = (): Emotion => useContext(EmotionContext);
5 changes: 0 additions & 5 deletions src/hooks/useStyleUtils.ts

This file was deleted.

0 comments on commit d9d8014

Please sign in to comment.