An open source design system that's fully customizable and agent ready
TypeScript
fix(useImageMode): depend on region values, not object identity (#10183)
Cindy Zhange9a5ca6e0dee
diff --git a/.changeset/use-image-mode-inline-region.md b/.changeset/use-image-mode-inline-region.mdnew file mode 100644index 0000000000..009a4651c7--- /dev/null+++ b/.changeset/use-image-mode-inline-region.md@@ -0,0 +1,7 @@+---+'@astryxdesign/core': patch+---++[fix] `useImageMode` samples the image once for a `region` written inline. The effect depended on the region object's identity, so the documented call shape (a fresh object literal each render) re-fetched and re-decoded the image on every render, and each result re-rendered, without end. The effect now depends on the region's four numbers.++@thecodebasedotdiff --git a/packages/core/src/hooks/useImageMode.test.ts b/packages/core/src/hooks/useImageMode.test.tsindex ddad57ce02..2d08389e96 100644--- a/packages/core/src/hooks/useImageMode.test.ts+++ b/packages/core/src/hooks/useImageMode.test.ts@@ -74,6 +74,28 @@ describe('useImageMode', () => {vi.unstubAllGlobals();});+ it('samples once for an inline region object across re-renders', async () => {+ stubSamplingPipeline([250, 250, 250]);+ const fetchSpy = vi.mocked(globalThis.fetch);++ // The documented call shape writes the region inline, so every render+ // hands the hook a new object. That must not re-fetch and re-sample.+ const {result, rerender} = renderHook(() =>+ useImageMode('https://example.com/hero.jpg', {+ region: {x: 0, y: 0.7, width: 1, height: 0.3},+ }),+ );++ await waitFor(() => expect(result.current).toBe('light'));+ expect(fetchSpy).toHaveBeenCalledTimes(1);++ rerender();+ rerender();+ await act(async () => {});++ expect(fetchSpy).toHaveBeenCalledTimes(1);+ });+it('returns the fallback while no src is provided', () => {const {result} = renderHook(() => useImageMode(null, {fallback: 'dark'}));expect(result.current).toBe('dark');diff --git a/packages/core/src/hooks/useImageMode.ts b/packages/core/src/hooks/useImageMode.tsindex e2f2ed6a2b..d893553d62 100644--- a/packages/core/src/hooks/useImageMode.ts+++ b/packages/core/src/hooks/useImageMode.ts@@ -68,6 +68,13 @@ export function useImageMode(options: UseImageModeOptions = {},): 'dark' | 'light' | null {const {region, threshold = 0.5, fallback = null} = options;+ // Depend on the region's numbers, not its identity: callers write the+ // region inline, and a new object each render would re-fetch and re-sample+ // the image on every render — forever, since the result itself re-renders.+ const regionX = region?.x;+ const regionY = region?.y;+ const regionWidth = region?.width;+ const regionHeight = region?.height;const [detectedResult, setDetectedResult] = useState<{src: string;mode: 'dark' | 'light' | null;@@ -93,13 +100,18 @@ export function useImageMode(return;}- const sx = region ? Math.round(region.x * bitmap.width) : 0;- const sy = region ? Math.round(region.y * bitmap.height) : 0;- const sw = region- ? Math.round(region.width * bitmap.width)+ const hasRegion =+ regionX !== undefined &&+ regionY !== undefined &&+ regionWidth !== undefined &&+ regionHeight !== undefined;+ const sx = hasRegion ? Math.round(regionX * bitmap.width) : 0;+ const sy = hasRegion ? Math.round(regionY * bitmap.height) : 0;+ const sw = hasRegion+ ? Math.round(regionWidth * bitmap.width): bitmap.width;- const sh = region- ? Math.round(region.height * bitmap.height)+ const sh = hasRegion+ ? Math.round(regionHeight * bitmap.height): bitmap.height;// Sample at a small size and average the pixels here. A 1×1@@ -150,7 +162,7 @@ export function useImageMode(return () => {cancelled = true;};- }, [src, region, threshold, fallback]);+ }, [src, regionX, regionY, regionWidth, regionHeight, threshold, fallback]);if (!src || detectedResult?.src !== src) {return fallback;