feat: mentions support cssVar (#45796)

* feat: mentions support cssVar

* feat: optimize code
This commit is contained in:
kiner-tang(文辉) 2023-11-13 14:43:28 +08:00 committed by GitHub
parent fc62994a1e
commit a6cdf3c485
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 31 additions and 18 deletions

View File

@ -18,6 +18,8 @@ import DefaultRenderEmpty from '../config-provider/defaultRenderEmpty';
import { FormItemInputContext } from '../form/context'; import { FormItemInputContext } from '../form/context';
import Spin from '../spin'; import Spin from '../spin';
import useStyle from './style'; import useStyle from './style';
import useCSSVar from './style/cssVar';
import useCSSVarCls from '../config-provider/hooks/useCSSVarCls';
export const { Option } = RcMentions; export const { Option } = RcMentions;
@ -154,7 +156,9 @@ const InternalMentions: React.ForwardRefRenderFunction<MentionsRef, MentionProps
const prefixCls = getPrefixCls('mentions', customizePrefixCls); const prefixCls = getPrefixCls('mentions', customizePrefixCls);
// Style // Style
const [wrapSSR, hashId] = useStyle(prefixCls); const [, hashId] = useStyle(prefixCls);
const rootCls = useCSSVarCls(prefixCls);
const wrapCSSVar = useCSSVar(rootCls);
const mergedClassName = classNames( const mergedClassName = classNames(
{ {
@ -167,6 +171,7 @@ const InternalMentions: React.ForwardRefRenderFunction<MentionsRef, MentionProps
!hasFeedback && className, !hasFeedback && className,
rootClassName, rootClassName,
hashId, hashId,
rootCls,
); );
const mentions = ( const mentions = (
@ -181,7 +186,7 @@ const InternalMentions: React.ForwardRefRenderFunction<MentionsRef, MentionProps
filterOption={mentionsfilterOption} filterOption={mentionsfilterOption}
onFocus={onFocus} onFocus={onFocus}
onBlur={onBlur} onBlur={onBlur}
dropdownClassName={classNames(popupClassName, rootClassName, hashId)} dropdownClassName={classNames(popupClassName, rootClassName, hashId, rootCls)}
ref={mergedRef} ref={mergedRef}
options={mergedOptions} options={mergedOptions}
suffix={hasFeedback && feedbackIcon} suffix={hasFeedback && feedbackIcon}
@ -191,7 +196,7 @@ const InternalMentions: React.ForwardRefRenderFunction<MentionsRef, MentionProps
</RcMentions> </RcMentions>
); );
return wrapSSR(mentions); return wrapCSSVar(mentions);
}; };
const Mentions = React.forwardRef<MentionsRef, MentionProps>( const Mentions = React.forwardRef<MentionsRef, MentionProps>(

View File

@ -0,0 +1,4 @@
import { genCSSVarRegister } from '../../theme/internal';
import { prepareComponentToken } from '.';
export default genCSSVarRegister('Mentions', prepareComponentToken);

View File

@ -8,8 +8,9 @@ import {
initInputToken, initInputToken,
} from '../../input/style'; } from '../../input/style';
import { resetComponent, textEllipsis } from '../../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 { genComponentStyleHook, mergeToken } from '../../theme/internal';
import { unit } from '@ant-design/cssinjs';
export interface ComponentToken extends SharedComponentToken { export interface ComponentToken extends SharedComponentToken {
/** /**
@ -29,7 +30,10 @@ export interface ComponentToken extends SharedComponentToken {
controlItemWidth: number; controlItemWidth: number;
} }
type MentionsToken = FullToken<'Mentions'> & SharedInputToken; type MentionsToken = FullToken<'Mentions'> &
SharedInputToken & {
itemPaddingVertical: string | number;
};
const genMentionsStyle: GenerateStyle<MentionsToken> = (token) => { const genMentionsStyle: GenerateStyle<MentionsToken> = (token) => {
const { const {
@ -49,12 +53,9 @@ const genMentionsStyle: GenerateStyle<MentionsToken> = (token) => {
borderRadius, borderRadius,
borderRadiusLG, borderRadiusLG,
boxShadowSecondary, boxShadowSecondary,
itemPaddingVertical,
} = token; } = token;
const itemPaddingVertical = Math.round(
(token.controlHeight - token.fontSize * token.lineHeight) / 2,
);
return { return {
[componentCls]: { [componentCls]: {
...resetComponent(token), ...resetComponent(token),
@ -92,9 +93,9 @@ const genMentionsStyle: GenerateStyle<MentionsToken> = (token) => {
[`> textarea, ${componentCls}-measure`]: { [`> textarea, ${componentCls}-measure`]: {
color: colorText, color: colorText,
boxSizing: 'border-box', boxSizing: 'border-box',
minHeight: controlHeight - 2, minHeight: token.calc(controlHeight).sub(2),
margin: 0, margin: 0,
padding: `${paddingBlock}px ${paddingInline}px`, padding: `${unit(paddingBlock)} ${unit(paddingInline)}`,
overflow: 'inherit', overflow: 'inherit',
overflowX: 'hidden', overflowX: 'hidden',
overflowY: 'auto', overflowY: 'auto',
@ -176,7 +177,7 @@ const genMentionsStyle: GenerateStyle<MentionsToken> = (token) => {
position: 'relative', position: 'relative',
display: 'block', display: 'block',
minWidth: token.controlItemWidth, minWidth: token.controlItemWidth,
padding: `${itemPaddingVertical}px ${controlPaddingHorizontal}px`, padding: `${unit(itemPaddingVertical)} ${unit(controlPaddingHorizontal)}`,
color: colorText, color: colorText,
borderRadius, borderRadius,
fontWeight: 'normal', fontWeight: 'normal',
@ -215,6 +216,14 @@ const genMentionsStyle: GenerateStyle<MentionsToken> = (token) => {
}; };
}; };
export const prepareComponentToken: GetDefaultToken<'Mentions'> = (token) => ({
...initComponentToken(token),
dropdownHeight: 250,
controlItemWidth: 100,
zIndexPopup: token.zIndexPopupBase + 50,
itemPaddingVertical: token.controlHeight - token.fontHeight,
});
// ============================== Export ============================== // ============================== Export ==============================
export default genComponentStyleHook( export default genComponentStyleHook(
'Mentions', 'Mentions',
@ -222,10 +231,5 @@ export default genComponentStyleHook(
const mentionsToken = mergeToken<MentionsToken>(token, initInputToken(token)); const mentionsToken = mergeToken<MentionsToken>(token, initInputToken(token));
return [genMentionsStyle(mentionsToken)]; return [genMentionsStyle(mentionsToken)];
}, },
(token) => ({ prepareComponentToken,
...initComponentToken(token),
dropdownHeight: 250,
controlItemWidth: 100,
zIndexPopup: token.zIndexPopupBase + 50,
}),
); );