feat(theme/SvgWrapper): icon field support svg and img icon in HomeLayout #3036
feat(theme/SvgWrapper): icon field support svg and img icon in HomeLayout #3036
Conversation
✅ Deploy Preview for rspress-v2 ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
Deploying rspress-v2 with
|
| Latest commit: |
7219d55
|
| Status: | ✅ Deploy successful! |
| Preview URL: | https://4e332e7c.rspress-v2.pages.dev |
| Branch Preview URL: | https://syt-vibe-kanban-94b0-shouldu.rspress-v2.pages.dev |
Rsdoctor Bundle Diff AnalysisFound 3 projects in monorepo, 3 projects with changes. 📊 Quick Summary
📋 Detailed Reports (Click to expand)📁 nodePath:
📦 Download Diff Report: node Bundle Diff 📁 webPath:
📦 Download Diff Report: web Bundle Diff 📁 node_mdPath:
📦 Download Diff Report: node_md Bundle Diff Generated by Rsdoctor GitHub Action |
There was a problem hiding this comment.
Pull request overview
This PR refactors the SvgWrapper component to use renderHtmlOrText as a fallback for handling various icon types, and updates the HomeFeature component to use SvgWrapper instead of directly using renderHtmlOrText for icons. This unifies icon handling and expands support for different icon formats.
Changes:
- Enhanced
SvgWrapperto handle multiple icon formats: React components, inline SVG strings, image URLs/paths, and emoji/text/HTML - Updated
HomeFeatureto useSvgWrapperfor icon rendering instead ofrenderHtmlOrText - Added CSS styling to size
imgandsvgelements within the feature icon container - Updated documentation to clarify supported icon formats with specific examples
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| packages/core/src/theme/components/SvgWrapper/index.tsx | Enhanced with logic to detect and handle SVG strings, URLs/paths, and fallback to renderHtmlOrText for other content types |
| packages/core/src/theme/components/HomeFeature/index.tsx | Changed icon rendering from direct renderHtmlOrText usage to SvgWrapper component |
| packages/core/src/theme/components/HomeFeature/index.scss | Added sizing constraints for img and svg elements within icon containers |
| website/docs/en/ui/components/home-feature.mdx | Updated documentation with detailed examples of supported icon formats |
| website/docs/zh/ui/components/home-feature.mdx | Updated Chinese documentation with detailed examples of supported icon formats |
| packages/shared/src/types/index.ts | Added comprehensive JSDoc comments documenting supported icon types |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
- 新增 `isUrlOrPath` 函数判断字符串是否是 URL/路径
- 扩展支持三种字符串类型:
- **SVG 内联字符串**(以 `<svg` 开头)→ 用 `dangerouslySetInnerHTML` 渲染
- **URL/路径** → 用 `<img>` 渲染
- **其他** → 返回 null(emoji/HTML 由外部处理)
- 新增 `shouldUseSvgWrapper` 函数判断 icon 是否应使用 SvgWrapper
- icon 渲染逻辑:
- SVG/URL 类型 → `SvgWrapper`
- 其他(emoji/HTML)→ `renderHtmlOrText`
- 为 `.rp-home-feature__icon` 添加 `img, svg` 尺寸样式
- 更新 `Feature.icon` 的 JSDoc 注释
- `website/docs/zh/ui/components/home-feature.mdx`
- `website/docs/en/ui/components/home-feature.mdx`
```yaml
features:
- title: '快速'
icon: '🚀' # emoji
- title: '强大'
icon: '<svg>...</svg>' # SVG 内联
- title: '灵活'
icon: '/icons/feature.svg' # 本地图片
- title: '现代'
icon: 'https://example.com/icon.png' # 远程图片
```
1. **SvgWrapper** (`packages/core/src/theme/components/SvgWrapper/index.tsx`):
- 导入了 `renderHtmlOrText`
- 对于非 SVG/URL 的字符串(如 emoji/text/HTML),现在使用 `renderHtmlOrText` 渲染,而不是返回 `null`
2. **HomeFeature** (`packages/core/src/theme/components/HomeFeature/index.tsx`):
- 删除了 `shouldUseSvgWrapper` 函数
- 删除了 `renderHtmlOrText` 的导入
- 简化 icon 渲染逻辑,直接使用 `<SvgWrapper icon={icon} />`
- `details` 也简化为直接渲染 `{details}`
…正确使用 `<img>` 渲染。
d2cc834 to
7219d55
Compare
Summary
feat(theme/SvgWrapper): icon field support svg and img icon in HomeLayout
Related Issue
close #2970
Checklist
AI Summary
Changes Made
This PR refactors the
SvgWrappercomponent to handle all icon types, eliminating the need for theshouldUseSvgWrapperhelper function inHomeFeature.SvgWrapper enhancements:
dangerouslySetInnerHTML)<img>)renderHtmlOrTextSvgWrapperPropstypeHomeFeature simplification:
shouldUseSvgWrapperconditional logicSvgWrapperdirectly for all icon renderingWhy
Previously,
HomeFeaturehad to manually check whether to useSvgWrapperorrenderHtmlOrTextbased on the icon string format. By enhancingSvgWrapperto handle all cases internally, we:SvgWrapperImplementation Details
The
SvgWrappernow checks icon strings in this order:<svg→ render as inline SVG/,./,../,http://,https://) → render as<img>renderHtmlOrText(handles emoji and HTML text)This PR was written using Vibe Kanban