Skip to content

Commit 740f448

Browse files
committed
use a lazy icon
1 parent d002c4f commit 740f448

File tree

2 files changed

+7
-8
lines changed

2 files changed

+7
-8
lines changed

packages/dev/s2-docs/src/IconPicker.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import {PressResponder} from '@react-aria/interactions';
1010
import {useGlobalListeners} from '@react-aria/utils';
1111

1212
const iconList = Object.keys(icons).map(name => ({id: name.replace(/^S2_Icon_(.*?)(Size\d+)?_2.*/, '$1'), icon: icons[name].default}));
13+
const iconMap = Object.fromEntries(iconList.map(item => [item.id, item.icon]));
14+
export const Icon = ({icon}) => createElement(iconMap[icon]);
1315

1416
const itemStyle = style({
1517
...focusRing(),

packages/dev/s2-docs/src/VisualExampleClient.tsx

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,9 @@ import React, {createContext, createElement, Fragment, isValidElement, lazy, Rea
1616
import RemoveCircle from '@react-spectrum/s2/icons/RemoveCircle';
1717
import {TabLink} from './FileTabs';
1818
import {useLocale} from 'react-aria';
19-
// eslint-disable-next-line
20-
import icons from '/packages/@react-spectrum/s2/s2wf-icons/*.svg';
2119

2220
export const IconPicker = lazy(() => import('./IconPicker').then(({IconPicker}) => ({default: IconPicker})));
23-
24-
// Create icon map for use in Output component
25-
const iconList = Object.keys(icons).map(name => ({id: name.replace(/^S2_Icon_(.*?)(Size\d+)?_2.*/, '$1'), icon: icons[name].default}));
26-
const iconMap = Object.fromEntries(iconList.map(item => [item.id, item.icon]));
21+
let LazyIcon = lazy(() => import('./IconPicker').then(({Icon}) => ({default: Icon})));
2722

2823
type Props = {[name: string]: any};
2924
type Controls = {[name: string]: PropControl};
@@ -142,8 +137,10 @@ export function Output({align = 'center', acceptOrientation}: {align?: 'center'
142137
let iconElement: ReactNode | null = null;
143138
if (children.avatar) {
144139
iconElement = <Avatar src="https://i.imgur.com/xIe7Wlb.png" />;
145-
} else if (children.icon && iconMap[children.icon]) {
146-
iconElement = createElement(iconMap[children.icon]);
140+
} else if (children.icon) {
141+
iconElement = (<Suspense fallback={''}>
142+
<LazyIcon icon={children.icon} />
143+
</Suspense>);
147144
}
148145

149146
children = (

0 commit comments

Comments
 (0)