Skip to content

Commit fee7c04

Browse files
authored
refactor:comment (#6238)
* refactor:comment * fix inheritAttrs: false & attrs.class
1 parent 8658806 commit fee7c04

File tree

6 files changed

+178
-162
lines changed

6 files changed

+178
-162
lines changed

components/comment/index.tsx

+15-3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ import { flattenChildren } from '../_util/props-util';
55
import type { VueNode } from '../_util/type';
66
import { withInstall } from '../_util/type';
77
import useConfigInject from '../config-provider/hooks/useConfigInject';
8+
9+
// CSSINJS
10+
import useStyle from './style';
11+
812
export const commentProps = () => ({
913
actions: Array,
1014
/** The element to display as the comment author. */
@@ -24,10 +28,15 @@ export type CommentProps = Partial<ExtractPropTypes<ReturnType<typeof commentPro
2428
const Comment = defineComponent({
2529
compatConfig: { MODE: 3 },
2630
name: 'AComment',
31+
inheritAttrs: false,
2732
props: commentProps(),
2833
slots: ['actions', 'author', 'avatar', 'content', 'datetime'],
29-
setup(props, { slots }) {
34+
setup(props, { slots, attrs }) {
3035
const { prefixCls, direction } = useConfigInject('comment', props);
36+
37+
// style
38+
const [wrapSSR, hashId] = useStyle(prefixCls);
39+
3140
const renderNested = (prefixCls: string, children: VueNode) => {
3241
return <div class={`${prefixCls}-nested`}>{children}</div>;
3342
};
@@ -79,18 +88,21 @@ const Comment = defineComponent({
7988
</div>
8089
);
8190
const children = flattenChildren(slots.default?.());
82-
return (
91+
return wrapSSR(
8392
<div
93+
{...attrs}
8494
class={[
8595
pre,
8696
{
8797
[`${pre}-rtl`]: direction.value === 'rtl',
8898
},
99+
attrs.class,
100+
hashId.value,
89101
]}
90102
>
91103
{comment}
92104
{children && children.length ? renderNested(pre, children) : null}
93-
</div>
105+
</div>,
94106
);
95107
};
96108
},

components/comment/style/index.less

-105
This file was deleted.

components/comment/style/index.tsx

+161-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,161 @@
1-
import '../../style/index.less';
2-
import './index.less';
1+
import type { FullToken, GenerateStyle } from '../../theme/internal';
2+
import { genComponentStyleHook, mergeToken } from '../../theme/internal';
3+
4+
export interface ComponentToken {}
5+
6+
type CommentToken = FullToken<'Comment'> & {
7+
commentBg: string;
8+
commentPaddingBase: string;
9+
commentNestIndent: string;
10+
commentFontSizeBase: number;
11+
commentFontSizeSm: number;
12+
commentAuthorNameColor: string;
13+
commentAuthorTimeColor: string;
14+
commentActionColor: string;
15+
commentActionHoverColor: string;
16+
commentActionsMarginBottom: string;
17+
commentActionsMarginTop: number;
18+
commentContentDetailPMarginBottom: string;
19+
};
20+
21+
const genBaseStyle: GenerateStyle<CommentToken> = token => {
22+
const {
23+
componentCls,
24+
commentBg,
25+
commentPaddingBase,
26+
commentNestIndent,
27+
commentFontSizeBase,
28+
commentFontSizeSm,
29+
commentAuthorNameColor,
30+
commentAuthorTimeColor,
31+
commentActionColor,
32+
commentActionHoverColor,
33+
commentActionsMarginBottom,
34+
commentActionsMarginTop,
35+
commentContentDetailPMarginBottom,
36+
} = token;
37+
38+
return {
39+
[componentCls]: {
40+
position: 'relative',
41+
backgroundColor: commentBg,
42+
43+
[`${componentCls}-inner`]: {
44+
display: 'flex',
45+
padding: commentPaddingBase,
46+
},
47+
48+
[`${componentCls}-avatar`]: {
49+
position: 'relative',
50+
flexShrink: 0,
51+
marginRight: token.marginSM,
52+
cursor: 'pointer',
53+
54+
[`img`]: {
55+
width: '32px',
56+
height: '32px',
57+
borderRadius: '50%',
58+
},
59+
},
60+
61+
[`${componentCls}-content`]: {
62+
position: 'relative',
63+
flex: `1 1 auto`,
64+
minWidth: `1px`,
65+
fontSize: commentFontSizeBase,
66+
wordWrap: 'break-word',
67+
68+
[`&-author`]: {
69+
display: 'flex',
70+
flexWrap: 'wrap',
71+
justifyContent: 'flex-start',
72+
marginBottom: token.marginXXS,
73+
fontSize: commentFontSizeBase,
74+
75+
[`& > a,& > span`]: {
76+
paddingRight: token.paddingXS,
77+
fontSize: commentFontSizeSm,
78+
lineHeight: `18px`,
79+
},
80+
81+
[`&-name`]: {
82+
color: commentAuthorNameColor,
83+
fontSize: commentFontSizeBase,
84+
transition: `color ${token.motionDurationSlow}`,
85+
86+
[`> *`]: {
87+
color: commentAuthorNameColor,
88+
89+
[`&:hover`]: {
90+
color: commentAuthorNameColor,
91+
},
92+
},
93+
},
94+
95+
[`&-time`]: {
96+
color: commentAuthorTimeColor,
97+
whiteSpace: 'nowrap',
98+
cursor: 'auto',
99+
},
100+
},
101+
102+
[`&-detail p`]: {
103+
marginBottom: commentContentDetailPMarginBottom,
104+
whiteSpace: 'pre-wrap',
105+
},
106+
},
107+
108+
[`${componentCls}-actions`]: {
109+
marginTop: commentActionsMarginTop,
110+
marginBottom: commentActionsMarginBottom,
111+
paddingLeft: 0,
112+
113+
[`> li`]: {
114+
display: 'inline-block',
115+
color: commentActionColor,
116+
117+
[`> span`]: {
118+
marginRight: '10px',
119+
color: commentActionColor,
120+
fontSize: commentFontSizeSm,
121+
cursor: 'pointer',
122+
transition: `color ${token.motionDurationSlow}`,
123+
userSelect: 'none',
124+
125+
[`&:hover`]: {
126+
color: commentActionHoverColor,
127+
},
128+
},
129+
},
130+
},
131+
132+
[`${componentCls}-nested`]: {
133+
marginLeft: commentNestIndent,
134+
},
135+
136+
'&-rtl': {
137+
direction: 'rtl',
138+
},
139+
},
140+
};
141+
};
142+
143+
export default genComponentStyleHook('Comment', token => {
144+
console.log(token);
145+
const commentToken = mergeToken<CommentToken>(token, {
146+
commentBg: 'inherit',
147+
commentPaddingBase: `${token.paddingMD}px 0`,
148+
commentNestIndent: `44px`,
149+
commentFontSizeBase: token.fontSize,
150+
commentFontSizeSm: token.fontSizeSM,
151+
commentAuthorNameColor: token.colorTextTertiary,
152+
commentAuthorTimeColor: token.colorTextPlaceholder,
153+
commentActionColor: token.colorTextTertiary,
154+
commentActionHoverColor: token.colorTextSecondary,
155+
commentActionsMarginBottom: 'inherit',
156+
commentActionsMarginTop: token.marginSM,
157+
commentContentDetailPMarginBottom: 'inherit',
158+
});
159+
160+
return [genBaseStyle(commentToken)];
161+
});

components/comment/style/rtl.less

-51
This file was deleted.

components/style.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ import './list/style';
5050
import './tree-select/style';
5151
import './drawer/style';
5252
// import './skeleton/style';
53-
import './comment/style';
53+
// import './comment/style';
5454
// import './config-provider/style';
5555
import './empty/style';
5656
import './statistic/style';

components/theme/interface/components.ts

+1
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ export interface ComponentTokenMap {
6464
// Cascader?: CascaderComponentToken;
6565
// Checkbox?: CheckboxComponentToken;
6666
// Collapse?: CollapseComponentToken;
67+
Comment?: {};
6768
// DatePicker?: DatePickerComponentToken;
6869
Descriptions?: {};
6970
Divider?: DividerComponentToken;

0 commit comments

Comments
 (0)