@tarve/react-icons 0.3.0 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tarve/react-icons",
3
- "version": "0.3.0",
3
+ "version": "0.5.0",
4
4
  "description": "React icon-library adapters for Tarve",
5
5
  "license": "Apache-2.0",
6
6
  "keywords": [
package/src/lucide.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { REACT_FORWARD_REF, finiteNumber, reactSvgElementToTarve, record, type ComponentAdapter, type SvgAttributes, type SvgNode } from "./react-svg";
1
+ import { REACT_FORWARD_REF, memoizeAdapter, finiteNumber, reactSvgElementToTarve, record, type ComponentAdapter, type SvgAttributes, type SvgNode } from "./react-svg";
2
2
 
3
3
  const SVG_ELEMENTS = new Set(["path", "circle", "ellipse", "g", "line", "polygon", "polyline", "rect"]);
4
4
  type LucideData = {
@@ -43,7 +43,7 @@ function lucideData(value: unknown): LucideData | undefined {
43
43
  };
44
44
  }
45
45
 
46
- export const lucideReactAdapter: ComponentAdapter = ({ type, props }) => {
46
+ export const lucideReactAdapter: ComponentAdapter = memoizeAdapter(({ type, props }) => {
47
47
  const component = record(type);
48
48
  if (component?.$$typeof !== REACT_FORWARD_REF || typeof component.render !== "function") return undefined;
49
49
 
@@ -94,7 +94,7 @@ export const lucideReactAdapter: ComponentAdapter = ({ type, props }) => {
94
94
  })),
95
95
  },
96
96
  });
97
- };
97
+ });
98
98
 
99
99
  function toReactLike(nodes: readonly SvgNode[]): unknown[] {
100
100
  return nodes.map(([tag, attrs, children]) => ({
package/src/phosphor.ts CHANGED
@@ -1,8 +1,8 @@
1
- import { REACT_FORWARD_REF, finiteNumber, reactSvgElementToTarve, record, type ComponentAdapter } from "./react-svg";
1
+ import { REACT_FORWARD_REF, memoizeAdapter, finiteNumber, reactSvgElementToTarve, record, type ComponentAdapter } from "./react-svg";
2
2
 
3
3
  const WEIGHTS = new Set(["thin", "light", "regular", "bold", "fill", "duotone"]);
4
4
 
5
- export const phosphorReactAdapter: ComponentAdapter = ({ type, props }) => {
5
+ export const phosphorReactAdapter: ComponentAdapter = memoizeAdapter(({ type, props }) => {
6
6
  const component = record(type);
7
7
  if (component?.$$typeof !== REACT_FORWARD_REF || typeof component.render !== "function") return undefined;
8
8
 
@@ -36,4 +36,4 @@ export const phosphorReactAdapter: ComponentAdapter = ({ type, props }) => {
36
36
  children: [renderedProps.children, weightNode],
37
37
  },
38
38
  });
39
- };
39
+ });
@@ -0,0 +1,17 @@
1
+ import { expect, test } from "bun:test";
2
+ import { memoizeAdapter, type ComponentAdapter } from "./react-svg";
3
+
4
+ test("memoizeAdapter runs an adapter once per component and primitive props", () => {
5
+ let calls = 0;
6
+ const adapter = memoizeAdapter((({ props }) => { calls++; return { type: "svg", props: { svg: String(props.size) } }; }) as ComponentAdapter);
7
+ const Icon = { $$typeof: Symbol.for("react.forward_ref"), render: () => null };
8
+ const first = adapter({ type: Icon, props: { size: 16, color: "red" } });
9
+ expect(adapter({ type: Icon, props: { size: 16, color: "red" } })).toBe(first!);
10
+ expect(calls).toBe(1);
11
+ adapter({ type: Icon, props: { size: 20, color: "red" } });
12
+ expect(calls).toBe(2);
13
+ // Objects (style, children, handlers) are not keyed: every call adapts again.
14
+ adapter({ type: Icon, props: { size: 16, style: { margin: 2 } } });
15
+ adapter({ type: Icon, props: { size: 16, style: { margin: 2 } } });
16
+ expect(calls).toBe(4);
17
+ });
package/src/react-svg.ts CHANGED
@@ -161,8 +161,45 @@ export function reactSvgElementToTarve(value: unknown): TarveVNode | undefined {
161
161
  });
162
162
  }
163
163
 
164
+ /**
165
+ * Remembers an adapter's result per component and props, so an icon drawn on
166
+ * every render (a sidebar, a toolbar, a list row) runs its React component and
167
+ * serializes its SVG once instead of on each update. Props other than strings,
168
+ * numbers and booleans (children, style objects, handlers) skip the cache.
169
+ */
170
+ export function memoizeAdapter(adapter: ComponentAdapter, limit = 256): ComponentAdapter {
171
+ const cache = new WeakMap<object, Map<string, TarveVNode | undefined>>();
172
+ return input => {
173
+ const type = input.type;
174
+ if (type === null || (typeof type !== "object" && typeof type !== "function")) return adapter(input);
175
+ const key = propsKey(input.props);
176
+ if (key === undefined) return adapter(input);
177
+ let entries = cache.get(type);
178
+ if (!entries) cache.set(type, entries = new Map());
179
+ if (entries.has(key)) return entries.get(key);
180
+ const adapted = adapter(input);
181
+ if (entries.size >= limit) entries.delete(entries.keys().next().value!);
182
+ entries.set(key, adapted);
183
+ return adapted;
184
+ };
185
+ }
186
+
187
+ function propsKey(props: Readonly<Record<string, unknown>>): string | undefined {
188
+ let key = "";
189
+ // Unsorted: one call site always lists its props in the same order, and a
190
+ // different order only costs a second cache entry.
191
+ for (const name in props) {
192
+ const value = props[name];
193
+ if (value === undefined) continue;
194
+ const kind = typeof value;
195
+ if (kind !== "string" && kind !== "number" && kind !== "boolean") return undefined;
196
+ key += name + "\u0000" + kind[0] + String(value) + "\u0000";
197
+ }
198
+ return key;
199
+ }
200
+
164
201
  /** Generic adapter for React icon components whose forwardRef renders a static <svg>. */
165
- export const reactSvgAdapter: ComponentAdapter = ({ type, props }) => {
202
+ export const reactSvgAdapter: ComponentAdapter = memoizeAdapter(({ type, props }) => {
166
203
  const component = record(type);
167
204
  if (component?.$$typeof !== REACT_FORWARD_REF || typeof component.render !== "function") return undefined;
168
205
  try {
@@ -172,4 +209,4 @@ export const reactSvgAdapter: ComponentAdapter = ({ type, props }) => {
172
209
  } catch {
173
210
  return undefined;
174
211
  }
175
- };
212
+ });