Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(plugins): icons enhanced feature can be used for layout menu #12515

Merged
merged 6 commits into from
Jul 12, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/docs/docs/max/layout-menu.md
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,8 @@ icon: 'HomeFilled';
icon: 'HomeTwoTone';
```

兼容默认配置[icons](https://umijs.org/docs/api/config#icons),可以在使用图标集或者本地的图标。具体请参考配置的icons页面。

#### access

- Type: `string`
Expand Down
5 changes: 3 additions & 2 deletions examples/max/.umirc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ export default defineConfig({
{
title: 'site.title',
path: '/',
icon: 'PlaySquareFilled',
icon: 'ic:baseline-14mp',
component: 'index',
name: 'index',
},
{
path: '/users',
icon: 'SmileFilled',
icon: 'local:rice',
component: 'users',
name: 'users',
wrappers: ['@/wrappers/foo', '@/wrappers/bar'],
Expand Down Expand Up @@ -105,6 +105,7 @@ export default defineConfig({
jsStrategy: 'granularChunks',
},
icons: {
autoInstall: {},
include: ['local:rice', 'local:logo/umi', 'ant-design:fire-twotone'],
},
});
2 changes: 2 additions & 0 deletions examples/max/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
"react-dom": "18.3.1"
},
"devDependencies": {
"@iconify-json/ic": "1.1.17",
"@iconify-json/solar": "1.1.9",
"cross-env": "^7.0.3",
"cypress": "^12.0.0",
"start-server-and-test": "^1.15.2",
Expand Down
7 changes: 6 additions & 1 deletion examples/max/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,12 @@ export default function HomePage() {
tailwindcss
</h2>

<h2> Icons</h2>
<h2>Icon library icons</h2>
<Icon icon="ic:baseline-14mp" />
<Icon icon="ic:baseline-3p" />
<Icon icon="solar:4k-bold" />

<h2>Local Icons</h2>
<div>
{includedIcons.map((i) => {
return <Icon spin icon={i} className={i} key={i} />;
Expand Down
17 changes: 17 additions & 0 deletions packages/plugins/src/layout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -423,12 +423,19 @@ export default { ${icons.join(', ')} };
`,
});

// 是否启用了 icons 功能
const isIconsFeatureEnable = !!api.config.icons;
fz6m marked this conversation as resolved.
Show resolved Hide resolved
// runtime.tsx
api.writeTmpFile({
path: 'runtime.tsx',
content: `
import React from 'react';
import icons from './icons';
${
isIconsFeatureEnable
? `import { Icon, getIconComponent } from '@umijs/max';`
: ''
}

function formatIcon(name: string) {
return name
Expand All @@ -442,6 +449,16 @@ export function patchRoutes({ routes }) {
Object.keys(routes).forEach(key => {
const { icon } = routes[key];
if (icon && typeof icon === 'string') {
${
isIconsFeatureEnable
? `const Component = getIconComponent(icon)
if (Component) {
routes[key].icon = <Icon icon={icon} width={14} height={14} />;
fz6m marked this conversation as resolved.
Show resolved Hide resolved
return;
}`
: ''
}

const upperIcon = formatIcon(icon);
if (icons[upperIcon] || icons[upperIcon + 'Outlined']) {
routes[key].icon = React.createElement(icons[upperIcon] || icons[upperIcon + 'Outlined']);
Expand Down
8 changes: 6 additions & 2 deletions packages/preset-umi/src/features/icons/icons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -322,10 +322,14 @@ interface IUmiIconProps extends React.SVGAttributes<SVGElement> {
flip?: 'vertical' | 'horizontal' | 'horizontal,vertical' | 'vertical,horizontal';
}

export const getIconComponent = (icon: Pick<IUmiIconProps, 'icon'>) => {
const iconName = normalizeIconName(alias[icon] || icon);
return iconsMap[iconName];
}

export const Icon = React.forwardRef<HTMLSpanElement, IUmiIconProps>((props, ref) => {
const { icon, hover, style, className = '' , rotate, spin, flip, ...extraProps } = props;
const iconName = normalizeIconName(alias[icon] || icon);
const Component = iconsMap[iconName];
const Component = getIconComponent(icon);
if (!Component) {
// TODO: give a error icon when dev, to help developer find the error
return null;
Expand Down
18 changes: 18 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.