From 47421286aa8a7a99e51ddfce32702ceec39b2afb Mon Sep 17 00:00:00 2001 From: Cody Olsen Date: Fri, 13 Dec 2024 19:31:28 +0100 Subject: [PATCH] fix: improve `SanityDefaultPreview` memoization --- .../components/SanityDefaultPreview.tsx | 34 +++++++++++-------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/packages/sanity/src/core/preview/components/SanityDefaultPreview.tsx b/packages/sanity/src/core/preview/components/SanityDefaultPreview.tsx index 247e059e0838..7d9c3fa3126c 100644 --- a/packages/sanity/src/core/preview/components/SanityDefaultPreview.tsx +++ b/packages/sanity/src/core/preview/components/SanityDefaultPreview.tsx @@ -5,9 +5,9 @@ import {type SanityImageSource} from '@sanity/image-url/lib/types/types' import {type ImageUrlFitMode} from '@sanity/types' import { type ComponentType, - createElement, type ElementType, isValidElement, + memo, type ReactElement, type ReactNode, useCallback, @@ -37,8 +37,10 @@ export interface SanityDefaultPreviewProps extends Omit imageUrlBuilder(client), [client]) @@ -50,6 +52,8 @@ export function SanityDefaultPreview(props: SanityDefaultPreviewProps): ReactEle dimensions: {width?: number; height?: number; fit: ImageUrlFitMode; dpr?: number} }) => { const {dimensions} = options + const width = dimensions.width || 100 + const height = dimensions.height || 100 // Handle sanity image return ( @@ -61,8 +65,8 @@ export function SanityDefaultPreview(props: SanityDefaultPreviewProps): ReactEle .image( mediaProp as SanityImageSource /*will only enter this code path if it's compatible*/, ) - .width(dimensions.width || 100) - .height(dimensions.height || 100) + .width(width) + .height(height) .fit(dimensions.fit) .dpr(dimensions.dpr || 1) .url() || '' @@ -74,11 +78,11 @@ export function SanityDefaultPreview(props: SanityDefaultPreviewProps): ReactEle ) const renderIcon = useCallback(() => { - return createElement(icon || FallbackIcon) - }, [icon]) + return Icon ? : + }, [Icon]) const media = useMemo(() => { - if (icon === false) { + if (Icon === false) { // Explicitly disabled return false } @@ -108,7 +112,7 @@ export function SanityDefaultPreview(props: SanityDefaultPreviewProps): ReactEle // Render fallback icon return renderIcon - }, [icon, imageUrl, mediaProp, renderIcon, renderMedia, title]) + }, [Icon, imageUrl, mediaProp, renderIcon, renderMedia, title]) const previewProps: Omit = useMemo( () => ({ @@ -120,12 +124,11 @@ export function SanityDefaultPreview(props: SanityDefaultPreviewProps): ReactEle [media, restProps, title], ) - const layoutComponent = _previewComponents[layout || 'default'] + const LayoutComponent = _previewComponents[layout || 'default'] as ComponentType< + Omit + > - const children = createElement( - layoutComponent as ComponentType>, - previewProps, - ) + const children = if (tooltip) { return ( @@ -142,4 +145,5 @@ export function SanityDefaultPreview(props: SanityDefaultPreviewProps): ReactEle } return children -} +}) +SanityDefaultPreview.displayName = 'Memo(SanityDefaultPreview)'