generated from arvinxx/npm-template
-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
143 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// =========== emotion css 方案 =========== // | ||
// 写成 useStyles 的方式 | ||
// 适用场景:单纯用于部分样式的,没必要抽成组件的话,使用这种写法 | ||
// 可支持将样式部分独立到 style.ts 文件中 | ||
import { useMemo } from 'react'; | ||
|
||
import type { AntdStylish } from './stylish'; | ||
import { useInternalStylish } from './stylish'; | ||
import { AntdToken } from './types'; | ||
import { useToken } from './useToken'; | ||
|
||
export interface CreateStylesTheme { | ||
token: AntdToken; | ||
stylish: AntdStylish; | ||
} | ||
|
||
/** | ||
* 业务应用中创建样式基础写法 | ||
*/ | ||
export function createStyles<Props, ReturnStyle>( | ||
createStyleFn: (theme: CreateStylesTheme, props?: Props) => ReturnStyle, | ||
) { | ||
return (props?: Props): ReturnStyle => { | ||
const token = useToken(); | ||
const stylish = useInternalStylish(); | ||
|
||
return useMemo(() => createStyleFn({ token, stylish }, props), [token, props]); | ||
}; | ||
} | ||
|
||
export { css, cx } from '@emotion/css'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from './createStyles'; | ||
export * from './styled'; | ||
export * from './types'; | ||
export * from './useToken'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
import { useToken } from '@/useToken'; | ||
import { css } from '@emotion/css'; | ||
import { transparentize } from 'polished'; | ||
import { useMemo } from 'react'; | ||
|
||
export interface AntdStylish { | ||
defaultButton: string; | ||
|
||
textInfo: string; | ||
textDefault: string; | ||
|
||
containerBgHover: string; | ||
containerBgL2: string; | ||
|
||
dataInputContainer: string; | ||
dataInputContainerFocused: string; | ||
backgroundBlur: string; | ||
} | ||
|
||
/** | ||
* 一组统一封装好的 antd 标准样式 | ||
*/ | ||
export const useInternalStylish = (): AntdStylish => { | ||
const token = useToken(); | ||
|
||
return useMemo(() => { | ||
const containerBgHover = css` | ||
cursor: pointer; | ||
transition: 150ms background-color ease-in-out; | ||
&:hover { | ||
background: ${token.colorFillQuaternary}; | ||
} | ||
`; | ||
const dataInputContainerHover = css` | ||
color: ${token.colorText}; | ||
background-color: ${token.colorFillTertiary}; | ||
border-color: transparent; | ||
`; | ||
const dataInputContainerFocused = css` | ||
color: ${token.colorText} !important; | ||
background-color: ${token.colorFillQuaternary} !important; | ||
border-color: ${token.colorPrimary} !important; | ||
box-shadow: none; | ||
`; | ||
|
||
const defaultButtonBase = css` | ||
color: ${token.colorTextSecondary}; | ||
background: ${token.colorFillQuaternary}; | ||
border-color: transparent; | ||
`; | ||
|
||
return { | ||
defaultButton: css` | ||
${defaultButtonBase}; | ||
&:hover { | ||
color: ${token.colorText} !important; | ||
background: ${token.colorFillSecondary} !important; | ||
border-color: transparent !important; | ||
} | ||
&:focus { | ||
${defaultButtonBase}; | ||
border-color: ${token.colorPrimary} !important; | ||
} | ||
`, | ||
|
||
textInfo: css` | ||
color: ${token.colorTextSecondary}; | ||
&:hover { | ||
color: ${token.colorText}; | ||
} | ||
`, | ||
textDefault: css` | ||
color: ${token.colorTextSecondary}; | ||
`, | ||
|
||
containerBgHover: css` | ||
cursor: pointer; | ||
transition: 150ms background-color ease-in-out; | ||
&:hover { | ||
background: ${token.colorFillQuaternary}; | ||
} | ||
`, | ||
containerBgL2: css` | ||
${containerBgHover}; | ||
border-radius: 4px; | ||
background: ${token.colorFillQuaternary}; | ||
&:hover { | ||
background: ${token.colorFillTertiary}; | ||
} | ||
`, | ||
dataInputContainerFocused, | ||
dataInputContainer: css` | ||
&:hover { | ||
${dataInputContainerHover} | ||
} | ||
&:focus { | ||
${dataInputContainerFocused} | ||
} | ||
`, | ||
|
||
backgroundBlur: css` | ||
background: ${transparentize(0.4)(token.colorBgElevated)}; | ||
backdrop-filter: blur(10px); | ||
`, | ||
}; | ||
}, [token]); | ||
}; |