Test memoize svg icon

This commit is contained in:
Lim Chee Aun 2024-01-03 09:49:48 +08:00
parent 1adcca5666
commit 92c0a8b4f0

View file

@ -1,3 +1,4 @@
import moize from 'moize';
import { useEffect, useRef, useState } from 'preact/hooks'; import { useEffect, useRef, useState } from 'preact/hooks';
const SIZES = { const SIZES = {
@ -110,6 +111,29 @@ export const ICONS = {
const ICONDATA = {}; const ICONDATA = {};
// Memoize the dangerouslySetInnerHTML of the SVGs
const SVGICon = moize(
function ({ size, width, height, body, rotate, flip }) {
return (
<svg
width={size}
height={size}
viewBox={`0 0 ${width} ${height}`}
dangerouslySetInnerHTML={{ __html: body }}
style={{
transform: `${rotate ? `rotate(${rotate})` : ''} ${
flip ? `scaleX(-1)` : ''
}`,
}}
/>
);
},
{
isShallowEqual: true,
maxSize: Object.keys(ICONS).length,
},
);
function Icon({ function Icon({
icon, icon,
size = 'm', size = 'm',
@ -150,16 +174,24 @@ function Icon({
}} }}
> >
{iconData && ( {iconData && (
<svg // <svg
width={iconSize} // width={iconSize}
height={iconSize} // height={iconSize}
viewBox={`0 0 ${iconData.width} ${iconData.height}`} // viewBox={`0 0 ${iconData.width} ${iconData.height}`}
dangerouslySetInnerHTML={{ __html: iconData.body }} // dangerouslySetInnerHTML={{ __html: iconData.body }}
style={{ // style={{
transform: `${rotate ? `rotate(${rotate})` : ''} ${ // transform: `${rotate ? `rotate(${rotate})` : ''} ${
flip ? `scaleX(-1)` : '' // flip ? `scaleX(-1)` : ''
}`, // }`,
}} // }}
// />
<SVGICon
size={iconSize}
width={iconData.width}
height={iconData.height}
body={iconData.body}
rotate={rotate}
flip={flip}
/> />
)} )}
</span> </span>