From a6cdf3c48562918296f149d8378e60d147291800 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?kiner-tang=28=E6=96=87=E8=BE=89=29?= <1127031143@qq.com> Date: Mon, 13 Nov 2023 14:43:28 +0800 Subject: [PATCH] feat: mentions support cssVar (#45796) * feat: mentions support cssVar * feat: optimize code --- components/mentions/index.tsx | 11 +++++++--- components/mentions/style/cssVar.ts | 4 ++++ components/mentions/style/index.ts | 34 ++++++++++++++++------------- 3 files changed, 31 insertions(+), 18 deletions(-) create mode 100644 components/mentions/style/cssVar.ts diff --git a/components/mentions/index.tsx b/components/mentions/index.tsx index aadf4a8906..becd4ee4e3 100644 --- a/components/mentions/index.tsx +++ b/components/mentions/index.tsx @@ -18,6 +18,8 @@ import DefaultRenderEmpty from '../config-provider/defaultRenderEmpty'; import { FormItemInputContext } from '../form/context'; import Spin from '../spin'; import useStyle from './style'; +import useCSSVar from './style/cssVar'; +import useCSSVarCls from '../config-provider/hooks/useCSSVarCls'; export const { Option } = RcMentions; @@ -154,7 +156,9 @@ const InternalMentions: React.ForwardRefRenderFunction ); - return wrapSSR(mentions); + return wrapCSSVar(mentions); }; const Mentions = React.forwardRef( diff --git a/components/mentions/style/cssVar.ts b/components/mentions/style/cssVar.ts new file mode 100644 index 0000000000..49139e863f --- /dev/null +++ b/components/mentions/style/cssVar.ts @@ -0,0 +1,4 @@ +import { genCSSVarRegister } from '../../theme/internal'; +import { prepareComponentToken } from '.'; + +export default genCSSVarRegister('Mentions', prepareComponentToken); diff --git a/components/mentions/style/index.ts b/components/mentions/style/index.ts index 711557826a..60d341ecbc 100644 --- a/components/mentions/style/index.ts +++ b/components/mentions/style/index.ts @@ -8,8 +8,9 @@ import { initInputToken, } from '../../input/style'; import { resetComponent, textEllipsis } from '../../style'; -import type { FullToken, GenerateStyle } from '../../theme/internal'; +import type { FullToken, GenerateStyle, GetDefaultToken } from '../../theme/internal'; import { genComponentStyleHook, mergeToken } from '../../theme/internal'; +import { unit } from '@ant-design/cssinjs'; export interface ComponentToken extends SharedComponentToken { /** @@ -29,7 +30,10 @@ export interface ComponentToken extends SharedComponentToken { controlItemWidth: number; } -type MentionsToken = FullToken<'Mentions'> & SharedInputToken; +type MentionsToken = FullToken<'Mentions'> & + SharedInputToken & { + itemPaddingVertical: string | number; + }; const genMentionsStyle: GenerateStyle = (token) => { const { @@ -49,12 +53,9 @@ const genMentionsStyle: GenerateStyle = (token) => { borderRadius, borderRadiusLG, boxShadowSecondary, + itemPaddingVertical, } = token; - const itemPaddingVertical = Math.round( - (token.controlHeight - token.fontSize * token.lineHeight) / 2, - ); - return { [componentCls]: { ...resetComponent(token), @@ -92,9 +93,9 @@ const genMentionsStyle: GenerateStyle = (token) => { [`> textarea, ${componentCls}-measure`]: { color: colorText, boxSizing: 'border-box', - minHeight: controlHeight - 2, + minHeight: token.calc(controlHeight).sub(2), margin: 0, - padding: `${paddingBlock}px ${paddingInline}px`, + padding: `${unit(paddingBlock)} ${unit(paddingInline)}`, overflow: 'inherit', overflowX: 'hidden', overflowY: 'auto', @@ -176,7 +177,7 @@ const genMentionsStyle: GenerateStyle = (token) => { position: 'relative', display: 'block', minWidth: token.controlItemWidth, - padding: `${itemPaddingVertical}px ${controlPaddingHorizontal}px`, + padding: `${unit(itemPaddingVertical)} ${unit(controlPaddingHorizontal)}`, color: colorText, borderRadius, fontWeight: 'normal', @@ -215,6 +216,14 @@ const genMentionsStyle: GenerateStyle = (token) => { }; }; +export const prepareComponentToken: GetDefaultToken<'Mentions'> = (token) => ({ + ...initComponentToken(token), + dropdownHeight: 250, + controlItemWidth: 100, + zIndexPopup: token.zIndexPopupBase + 50, + itemPaddingVertical: token.controlHeight - token.fontHeight, +}); + // ============================== Export ============================== export default genComponentStyleHook( 'Mentions', @@ -222,10 +231,5 @@ export default genComponentStyleHook( const mentionsToken = mergeToken(token, initInputToken(token)); return [genMentionsStyle(mentionsToken)]; }, - (token) => ({ - ...initComponentToken(token), - dropdownHeight: 250, - controlItemWidth: 100, - zIndexPopup: token.zIndexPopupBase + 50, - }), + prepareComponentToken, );