feat(评论组件): 添加点赞功能及样式

- 将点赞按钮从简单的文本升级为包含图标和计数的组件
- 新增点赞按钮的悬停效果和样式
- 修改子元素检查逻辑,使用更通用的 Node 类型判断
This commit is contained in:
anghunk
2026-01-22 19:20:30 +08:00
parent 9c51339559
commit 74c7fa9e1c
3 changed files with 79 additions and 13 deletions

View File

@@ -105,26 +105,47 @@ export class CommentItem extends Component {
}, },
text: '回复' text: '回复'
}), }),
this.createElement('span', { this.createElement('div', {
className: 'cwd-comment-like', className: 'cwd-comment-like',
children: [ children: [
this.createElement('button', { this.createElement('button', {
className: 'cwd-comment-like-button', className: 'cwd-comment-like-button',
attributes: { attributes: {
type: 'button', type: 'button',
'aria-label': '点赞',
onClick: () => this.handleLikeComment() onClick: () => this.handleLikeComment()
}, },
text: '赞' children: [
}), this.createElement('span', {
this.createTextElement( className: 'cwd-comment-like-icon-wrapper',
'span', children: [
String( this.createElement('svg', {
typeof comment.likes === 'number' && Number.isFinite(comment.likes) && comment.likes >= 0 className: 'cwd-comment-like-icon',
? comment.likes attributes: {
: 0 viewBox: '0 0 24 24',
), 'aria-hidden': 'true'
'cwd-comment-like-count' },
) children: [
this.createElement('path', {
attributes: {
d: 'M12 21c-.4 0-.8-.1-1.1-.4L4.5 15C3 13.6 2 11.7 2 9.6 2 6.5 4.5 4 7.6 4c1.7 0 3.3.8 4.4 2.1C13.1 4.8 14.7 4 16.4 4 19.5 4 22 6.5 22 9.6c0 2.1-1 4-2.5 5.4l-6.4 5.6c-.3.3-.7.4-1.1.4z'
}
})
]
})
]
}),
this.createTextElement(
'span',
String(
typeof comment.likes === 'number' && Number.isFinite(comment.likes) && comment.likes >= 0
? comment.likes
: 0
),
'cwd-comment-like-count'
)
]
})
] ]
}), }),
this.createTextElement('span', formatRelativeTime(comment.created), 'cwd-comment-time') this.createTextElement('span', formatRelativeTime(comment.created), 'cwd-comment-time')

View File

@@ -134,7 +134,7 @@ export class Component {
if (options.children) { if (options.children) {
const children = Array.isArray(options.children) ? options.children : [options.children]; const children = Array.isArray(options.children) ? options.children : [options.children];
children.forEach(child => { children.forEach(child => {
if (child instanceof HTMLElement) { if (child instanceof Node) {
el.appendChild(child); el.appendChild(child);
} }
}); });

View File

@@ -586,6 +586,51 @@
gap: 12px; gap: 12px;
} }
.cwd-comment-like {
display: flex;
align-items: center;
display: none;
}
.cwd-comment-like-button {
display: inline-flex;
align-items: center;
gap: 4px;
padding: 2px 8px;
border-radius: 999px;
border: 1px solid var(--cwd-border-light, #eaeef2);
background: var(--cwd-bg-secondary, #f6f8fa);
color: var(--cwd-text-secondary, #6e7781);
cursor: pointer;
font-size: 12px;
font-family: inherit;
transition: background-color 0.2s ease, border-color 0.2s ease, color 0.2s ease, transform 0.15s ease;
}
.cwd-comment-like-button:hover {
background: rgba(9, 105, 218, 0.08);
border-color: var(--cwd-primary, #0969da);
color: var(--cwd-primary, #0969da);
}
.cwd-comment-like-icon-wrapper {
display: inline-flex;
align-items: center;
justify-content: center;
}
.cwd-comment-like-icon {
width: 14px;
height: 14px;
fill: currentColor;
}
.cwd-comment-like-count {
min-width: 1.5em;
text-align: right;
}
.cwd-action-btn { .cwd-action-btn {
display: inline-flex; display: inline-flex;
align-items: center; align-items: center;