Skip to content

Commit 774b902

Browse files
authored
[core] fix(Icon): use loaded paths in initial state if available (#6273)
1 parent eb6a7b7 commit 774b902

File tree

2 files changed

+5
-27
lines changed

2 files changed

+5
-27
lines changed

packages/core/src/components/icon/icon.tsx

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -69,26 +69,16 @@ export interface IconProps extends IntentProps, Props, SVGIconProps, IconHTMLAtt
6969
* @see https://blueprintjs.com/docs/#core/components/icon
7070
*/
7171
export const Icon: React.FC<IconProps> = React.forwardRef<any, IconProps>((props, ref) => {
72-
const { icon } = props;
73-
74-
const {
75-
autoLoad,
76-
className,
77-
color,
78-
icon: _icon,
79-
intent,
80-
tagName,
81-
svgProps,
82-
title,
83-
htmlTitle,
84-
...htmlProps
85-
} = props;
86-
const [iconPaths, setIconPaths] = React.useState<IconPaths>();
72+
const { autoLoad, className, color, icon, intent, tagName, svgProps, title, htmlTitle, ...htmlProps } = props;
8773

8874
// Preserve Blueprint v4.x behavior: iconSize prop takes predecence, then size prop, then fall back to default value
8975
// eslint-disable-next-line deprecation/deprecation
9076
const size = props.iconSize ?? props.size ?? IconSize.STANDARD;
9177

78+
const [iconPaths, setIconPaths] = React.useState<IconPaths | undefined>(() =>
79+
typeof icon === "string" ? Icons.getPaths(icon, size) : undefined,
80+
);
81+
9282
React.useEffect(() => {
9383
let shouldCancelIconLoading = false;
9484
if (typeof icon === "string") {

packages/core/test/icon/iconTests.tsx

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,6 @@ describe("<Icon>", () => {
102102

103103
it("title sets content of <title> element", async () => {
104104
const wrapper = mount(<Icon icon="airplane" title="bird" />);
105-
await waitUntilSpyCalledOnce(iconLoader);
106105
wrapper.update();
107106
assert.equal(wrapper.find("title").text(), "bird");
108107
});
@@ -128,7 +127,6 @@ describe("<Icon>", () => {
128127
/** Asserts that rendered icon has width/height equal to size. */
129128
async function assertIconSize(icon: React.ReactElement<IconProps>, size: number) {
130129
const wrapper = mount(icon);
131-
await waitUntilSpyCalledOnce(iconLoader);
132130
wrapper.update();
133131
const svg = wrapper.find("svg");
134132
assert.strictEqual(svg.prop("width"), size);
@@ -138,18 +136,8 @@ describe("<Icon>", () => {
138136
/** Asserts that rendered icon has color equal to color. */
139137
async function assertIconColor(icon: React.ReactElement<IconProps>, color?: string) {
140138
const wrapper = mount(icon);
141-
await waitUntilSpyCalledOnce(iconLoader);
142139
wrapper.update();
143140
const svg = wrapper.find("svg");
144141
assert.deepEqual(svg.prop("fill"), color);
145142
}
146143
});
147-
148-
async function waitUntilSpyCalledOnce(spy: Sinon.SinonSpy, timeout = 1000, interval = 50): Promise<void> {
149-
await new Promise(resolve => setTimeout(resolve, interval));
150-
if (spy.calledOnce) {
151-
return;
152-
} else {
153-
return waitUntilSpyCalledOnce(spy, timeout - interval, interval);
154-
}
155-
}

0 commit comments

Comments
 (0)