Skip to content

Invoke is a leading creative engine that uses image and video generation models to empower professionals, artists, and enthusiasts to generate and create visual media using the latest AI-driven technologies. The solution offers an industry-leading application interface, and serves as the foundation for multiple commercial products.

refactor(preview): decompose widget into frame/footer zones with density model

joshistoast633c1303938a
diff --git a/invokeai/frontend/webv2/src/workbench/widgets/preview/PreviewFooter.tsx b/invokeai/frontend/webv2/src/workbench/widgets/preview/PreviewFooter.tsx
new file mode 100644
index 0000000000..9980b2f23d
--- /dev/null
+++ b/invokeai/frontend/webv2/src/workbench/widgets/preview/PreviewFooter.tsx
@@ -0,0 +1,83 @@
+import type { GeneratedImageContract } from '@workbench/types';
+
+import { Badge, HStack, Stack, Text } from '@chakra-ui/react';
+import { Button } from '@workbench/components/ui';
+import { ChevronLeftIcon, ChevronRightIcon } from 'lucide-react';
+import { useTranslation } from 'react-i18next';
+
+/**
+ * The preview's info card: board context ("N of M"), prev/next navigation and
+ * the image identity rows. Later phases add the action strip, step message and
+ * metadata rail here — never in the widget shell.
+ */
+export const PreviewFooter = ({
+ boardImageCount,
+ boardName,
+ image,
+ isLive,
+ isLoadingBoard,
+ onNext,
+ onPrevious,
+ selectedIndex,
+}: {
+ boardImageCount: number;
+ boardName: string;
+ image: GeneratedImageContract;
+ isLive: boolean;
+ isLoadingBoard: boolean;
+ onNext: () => void;
+ onPrevious: () => void;
+ selectedIndex: number;
+}) => {
+ const { t } = useTranslation();
+
+ return (
+ <Stack borderWidth="1px" borderColor="border.subtle" gap="2" p="3" rounded="lg">
+ <HStack align="center" justify="space-between">
+ <HStack gap="2" minW="0">
+ <Badge flexShrink={0} size="xs" variant="subtle">
+ {boardName}
+ </Badge>
+ <Text color="fg.subtle" fontSize="2xs" truncate>
+ {isLoadingBoard
+ ? t('widgets.preview.loadingBoard')
+ : selectedIndex === -1
+ ? t('widgets.preview.imageCount', { count: boardImageCount })
+ : t('common.countOfTotal', { count: selectedIndex + 1, total: boardImageCount })}
+ </Text>
+ </HStack>
+ <HStack flexShrink={0} gap="1">
+ <Button
+ aria-label={t('widgets.preview.previousImageInBoard')}
+ disabled={selectedIndex <= 0}
+ size="2xs"
+ variant="outline"
+ onClick={onPrevious}
+ >
+ <ChevronLeftIcon />
+ </Button>
+ <Button
+ aria-label={t('widgets.preview.nextImageInBoard')}
+ disabled={selectedIndex === -1 || selectedIndex >= boardImageCount - 1}
+ size="2xs"
+ variant="outline"
+ onClick={onNext}
+ >
+ <ChevronRightIcon />
+ </Button>
+ </HStack>
+ </HStack>
+ <Stack>
+ <Text fontSize="xs" fontWeight="800" truncate>
+ {image.imageName}
+ </Text>
+ <Text color="fg.subtle" fontSize="2xs">
+ {t('widgets.preview.sourceRun', { id: image.sourceQueueItemId })}
+ </Text>
+ <Text color="fg.subtle" flexShrink={0} fontSize="2xs">
+ {isLive ? t('common.generating') : `${image.width} x ${image.height}`}
+ </Text>
+ </Stack>
+ </Stack>
+ );
+};
diff --git a/invokeai/frontend/webv2/src/workbench/widgets/preview/PreviewFrame.tsx b/invokeai/frontend/webv2/src/workbench/widgets/preview/PreviewFrame.tsx
new file mode 100644
index 0000000000..0759e9c91a
--- /dev/null
+++ b/invokeai/frontend/webv2/src/workbench/widgets/preview/PreviewFrame.tsx
@@ -0,0 +1,150 @@
+import type { StreamingImageSource } from '@workbench/images/streamingImageSource';
+
+import { Badge, Box, Flex, type SystemStyleObject } from '@chakra-ui/react';
+import { useCallback, useMemo, type CSSProperties, type MouseEvent, type ReactNode } from 'react';
+
+/**
+ * The preview's image surface: a dot-grid backdrop with an aspect-fitted,
+ * shadowed frame. Owns everything drawn over the image (live badge, and later
+ * the loupe, progress hairline, and drop-to-compare affordance) so the widget
+ * shell never grows frame-specific rendering.
+ */
+
+export const previewGridCss = {
+ backgroundImage: 'radial-gradient(circle, currentColor 1px, transparent 1.5px)',
+ backgroundPosition: 'center',
+ backgroundRepeat: 'repeat',
+ backgroundSize: '24px 24px',
+} as const;
+
+export const getFittedFrameCss = (width: number, height: number): SystemStyleObject => ({
+ aspectRatio: `${width} / ${height}`,
+ height: 'auto',
+ maxHeight: '100%',
+ maxWidth: '100%',
+ width: `min(100cqw, calc(100cqh * ${width / height}))`,
+});
+
+export const PreviewFrame = ({
+ children,
+ frameHeight,
+ frameWidth,
+ isLive,
+ liveBadgeLabel,
+ onContextMenu,
+ padding,
+ shouldAntialiasLiveImage,
+ source,
+ variant,
+}: {
+ /** Rendered instead of the fitted frame when there is no image source (inset variant only). */
+ children?: ReactNode;
+ frameHeight: number;
+ frameWidth: number;
+ isLive: boolean;
+ liveBadgeLabel: string;
+ onContextMenu?: (x: number, y: number) => void;
+ padding?: string;
+ shouldAntialiasLiveImage: boolean;
+ source: StreamingImageSource | null;
+ /** `framed` = bordered surface for a selected image; `inset` = flush surface for the empty state. */
+ variant: 'framed' | 'inset';
+}) => {
+ const handleContextMenu = useCallback(
+ (event: MouseEvent<HTMLDivElement>) => {
+ if (onContextMenu) {
+ event.preventDefault();
+ onContextMenu(event.clientX, event.clientY);
+ }
+ },
+ [onContextMenu]
+ );
+ const imageStyle = useMemo<CSSProperties>(
+ () => ({
+ display: 'block',
+ height: 'auto',
+ imageRendering: isLive && !shouldAntialiasLiveImage ? 'pixelated' : 'auto',
+ width: '100%',
+ }),
+ [isLive, shouldAntialiasLiveImage]
+ );
+ const liveBadge = isLive ? (
+ <Badge left="2" pointerEvents="none" position="absolute" size="xs" top="2" variant="solid">
+ {liveBadgeLabel}
+ </Badge>
+ ) : null;
+
+ if (variant === 'inset') {
+ return (
+ <Flex
+ align="center"
+ backgroundColor="bg.inset"
+ color="fg.grid"
+ containerType="size"
+ css={previewGridCss}
+ h="full"
+ justify="center"
+ w="full"
+ >
+ {source ? (
+ <Box
+ borderColor={isLive ? 'accent.solid' : 'border.emphasized'}
+ borderWidth="1px"
+ boxShadow="0 24px 80px rgba(0,0,0,0.42)"
+ css={getFittedFrameCss(frameWidth, frameHeight)}
+ overflow="hidden"
+ position="relative"
+ rounded="lg"
+ >
+ <img
+ alt={source.alt}
+ draggable={false}
+ height={frameHeight}
+ src={source.src}
+ style={imageStyle}
+ width={frameWidth}
+ />
+ {liveBadge}
+ </Box>
+ ) : (
+ children
+ )}
+ </Flex>
+ );
+ }
+
+ return (
+ <Flex
+ align="center"
+ borderWidth="1px"
+ borderColor="border.subtle"
+ color="fg.grid"
+ containerType="size"
+ css={previewGridCss}
+ flex="1"
+ justify="center"
+ minH="0"
+ overflow="hidden"
+ p={padding}
+ rounded="lg"
+ w="full"
+ >
+ <Box
+ bg="transparent"
+ borderWidth="1px"
+ borderColor={isLive ? 'accent.solid' : 'border.emphasized'}
+ boxShadow="0 24px 80px rgba(0,0,0,0.42)"
+ css={getFittedFrameCss(frameWidth, frameHeight)}
+ overflow="hidden"
+ position="relative"
+ rounded="lg"
+ onContextMenu={onContextMenu ? handleContextMenu : undefined}
+ >
+ {source ? (
+ <img alt={source.alt} height={frameHeight} src={source.src} style={imageStyle} width={frameWidth} />
+ ) : null}
+ {liveBadge}
+ </Box>
+ </Flex>
+ );
+};
diff --git a/invokeai/frontend/webv2/src/workbench/widgets/preview/PreviewWidgetView.tsx b/invokeai/frontend/webv2/src/workbench/widgets/preview/PreviewWidgetView.tsx
index bb178bd7f8..ed07e40b9e 100644
--- a/invokeai/frontend/webv2/src/workbench/widgets/preview/PreviewWidgetView.tsx
+++ b/invokeai/frontend/webv2/src/workbench/widgets/preview/PreviewWidgetView.tsx
@@ -1,9 +1,8 @@
/* eslint-disable react/react-compiler */
import type { GeneratedImageContract, WidgetViewProps } from '@workbench/types';
-import { Badge, Box, Flex, HStack, Stack, Text, type SystemStyleObject } from '@chakra-ui/react';
+import { Box, Stack, Text } from '@chakra-ui/react';
import { useProgressImage, type ProgressImageSnapshot } from '@workbench/backend/progressImageStore';
-import { Button } from '@workbench/components/ui';
import {
listGalleryBoards,
listGalleryImages,
@@ -29,21 +28,13 @@ import {
useWidgetValuesSelector,
useWorkbenchDispatch,
} from '@workbench/WorkbenchContext';
-import { ChevronLeftIcon, ChevronRightIcon } from 'lucide-react';
-import {
- useCallback,
- useEffect,
- useEffectEvent,
- useMemo,
- useRef,
- useState,
- type KeyboardEvent,
- type MouseEvent,
- type CSSProperties,
-} from 'react';
+import { useCallback, useEffect, useEffectEvent, useMemo, useRef, useState, type KeyboardEvent } from 'react';
import { useTranslation } from 'react-i18next';
import { PreviewCompare } from './PreviewCompare';
+import { usePreviewDensity, type PreviewDensity } from './previewDensity';
+import { PreviewFooter } from './PreviewFooter';
+import { PreviewFrame } from './PreviewFrame';
import { getPreviewComparisonMode, type PreviewComparisonMode } from './previewSettings';
type PreviewImage = GeneratedImageContract & Partial<Pick<GalleryImage, 'boardId' | 'imageCategory' | 'starred'>>;
@@ -125,25 +116,9 @@ export const getPreviewBoardsRequestKey = ({
refreshToken: string;
}): string | null => (hasSelectedImage ? refreshToken : null);
-const previewGridCss = {
- backgroundImage: 'radial-gradient(circle, currentColor 1px, transparent 1.5px)',
- backgroundPosition: 'center',
- backgroundRepeat: 'repeat',
- backgroundSize: '24px 24px',
-} as const;
-
-const getFittedFrameCss = (width: number, height: number): SystemStyleObject => ({
- aspectRatio: `${width} / ${height}`,
- height: 'auto',
- maxHeight: '100%',
- maxWidth: '100%',
- width: `min(100cqw, calc(100cqh * ${width / height}))`,
-});
-
const selectGenerateRecallValues = createGenerateFormValuesSelector();
export const PreviewWidgetView = ({ region, runtime }: WidgetViewProps) => {
- const { t } = useTranslation();
const galleryValues = useActiveProjectSelector((project) => getProjectWidgetValues(project, 'gallery'));
const previewValues = useActiveProjectSelector((project) => getProjectWidgetValues(project, 'preview'));
const generateValues = useWidgetValuesSelector('generate', selectGenerateRecallValues);
@@ -152,6 +127,7 @@ export const PreviewWidgetView = ({ region, runtime }: WidgetViewProps) => {
);
const progressImage = useProgressImage();
const dispatch = useWorkbenchDispatch();
+ const { density, rootRef } = usePreviewDensity(region);
const selectedImage = useMemo(() => getSelectedImage(galleryValues), [galleryValues]);
const compareImage = getGalleryCompareImage(galleryValues);
const comparisonMode = getPreviewComparisonMode(previewValues);
@@ -169,7 +145,7 @@ export const PreviewWidgetView = ({ region, runtime }: WidgetViewProps) => {
const [boards, setBoards] = useState<GalleryBoard[]>(fallbackBoards);
const [boardImages, setBoardImages] = useState<PreviewImage[]>(localImages);
const [isLoadingBoard, setIsLoadingBoard] = useState(false);
- const isSidePanel = region === 'left' || region === 'right';
+ const { t } = useTranslation();
const selectedIndex = selectedImage
? boardImages.findIndex((image) => image.imageName === selectedImage.imageName)
: -1;
@@ -413,7 +389,7 @@ export const PreviewWidgetView = ({ region, runtime }: WidgetViewProps) => {
}, [runtime.commands, runtime.hotkeys, t]);
return (
- <Box p="2" h="full">
+ <Box ref={rootRef} p="2" h="full">
{selectedImage ? (
<>
{isComparing && compareImage ? (
@@ -430,8 +406,8 @@ export const PreviewWidgetView = ({ region, runtime }: WidgetViewProps) => {
<SelectedImagePreview
boardImageCount={boardImages.length}
boardName={boardName}
+ density={density}
image={selectedImage}
- isCompact={isSidePanel}
isLoadingBoard={isLoadingBoard}
progressImage={shouldShowProgressImage ? progressImage : null}
selectedIndex={selectedIndex}
@@ -461,8 +437,8 @@ export const PreviewWidgetView = ({ region, runtime }: WidgetViewProps) => {
const SelectedImagePreview = ({
boardImageCount,
boardName,
+ density,
image,
- isCompact,
isLoadingBoard,
progressImage,
selectedIndex,
@@ -473,8 +449,8 @@ const SelectedImagePreview = ({
}: {
boardImageCount: number;
boardName: string;
+ density: PreviewDensity;
image: GeneratedImageContract;
- isCompact: boolean;
isLoadingBoard: boolean;
progressImage: ProgressImageSnapshot | null;
selectedIndex: number;
@@ -511,114 +487,30 @@ const SelectedImagePreview = ({
},
[onNext, onPrevious]
);
- const handleContextMenu = useCallback(
- (event: MouseEvent<HTMLDivElement>) => {
- event.preventDefault();
- onContextMenu(event.clientX, event.clientY);
- },
- [onContextMenu]
- );
- const imageStyle = useMemo<CSSProperties>(
- () => ({
- display: 'block',
- height: 'auto',
- imageRendering: isProgressImage && !shouldAntialiasProgressImage ? 'pixelated' : 'auto',
- width: '100%',
- }),
- [isProgressImage, shouldAntialiasProgressImage]
- );
return (
<Stack gap="3" h="full" minH="0" outline="none" tabIndex={0} w="full" onKeyDown={handleKeyDown}>
- <Flex
- align="center"
- borderWidth="1px"
- borderColor="border.subtle"
- color="fg.grid"
- containerType="size"
- css={previewGridCss}
- flex="1"
- justify="center"
- minH="0"
- overflow="hidden"
- p={isCompact ? '3' : '6'}
- rounded="lg"
- w="full"
- >
- <Box
- bg="transparent"
- borderWidth="1px"
- borderColor={isProgressImage ? 'accent.solid' : 'border.emphasized'}
- boxShadow="0 24px 80px rgba(0,0,0,0.42)"
- css={getFittedFrameCss(previewWidth, previewHeight)}
- overflow="hidden"
- position="relative"
- rounded="lg"
- onContextMenu={handleContextMenu}
- >
- {previewImage ? (
- <img
- alt={previewImage.alt}
- height={previewHeight}
- src={previewImage.src}
- style={imageStyle}
- width={previewWidth}
- />
- ) : null}
- {isProgressImage ? (
- <Badge left="2" pointerEvents="none" position="absolute" size="xs" top="2" variant="solid">
- {t('common.generating')}
- </Badge>
- ) : null}
- </Box>
- </Flex>
- <Stack borderWidth="1px" borderColor="border.subtle" gap="2" p="3" rounded="lg">
- <HStack align="center" justify="space-between">
- <HStack gap="2" minW="0">
- <Badge flexShrink={0} size="xs" variant="subtle">
- {boardName}
- </Badge>
- <Text color="fg.subtle" fontSize="2xs" truncate>
- {isLoadingBoard
- ? t('widgets.preview.loadingBoard')
- : selectedIndex === -1
- ? t('widgets.preview.imageCount', { count: boardImageCount })
- : t('common.countOfTotal', { count: selectedIndex + 1, total: boardImageCount })}
- </Text>
- </HStack>
- <HStack flexShrink={0} gap="1">
- <Button
- aria-label={t('widgets.preview.previousImageInBoard')}
- disabled={selectedIndex <= 0}
- size="2xs"
- variant="outline"
- onClick={onPrevious}
- >
- <ChevronLeftIcon />
- </Button>
- <Button
- aria-label={t('widgets.preview.nextImageInBoard')}
- disabled={selectedIndex === -1 || selectedIndex >= boardImageCount - 1}
- size="2xs"
- variant="outline"
- onClick={onNext}
- >
- <ChevronRightIcon />
- </Button>
- </HStack>
- </HStack>
- <Stack>
- <Text fontSize="xs" fontWeight="800" truncate>
- {image.imageName}
- </Text>
- <Text color="fg.subtle" fontSize="2xs">
- {t('widgets.preview.sourceRun', { id: image.sourceQueueItemId })}
- </Text>
- <Text color="fg.subtle" flexShrink={0} fontSize="2xs">
- {isProgressImage ? t('common.generating') : `${image.width} x ${image.height}`}
- </Text>
- </Stack>
- </Stack>
+ <PreviewFrame
+ frameHeight={previewHeight}
+ frameWidth={previewWidth}
+ isLive={isProgressImage}
+ liveBadgeLabel={t('common.generating')}
+ padding={density === 'full' ? '6' : '3'}
+ shouldAntialiasLiveImage={shouldAntialiasProgressImage}
+ source={previewImage}
+ variant="framed"
+ onContextMenu={onContextMenu}
+ />
+ <PreviewFooter
+ boardImageCount={boardImageCount}
+ boardName={boardName}
+ image={image}
+ isLive={isProgressImage}
+ isLoadingBoard={isLoadingBoard}
+ selectedIndex={selectedIndex}
+ onNext={onNext}
+ onPrevious={onPrevious}
+ />
</Stack>
);
};
@@ -636,53 +528,23 @@ const EmptyPreview = ({
});
return (
- <Flex
- align="center"
- backgroundColor="bg.inset"
- color="fg.grid"
- containerType="size"
- css={previewGridCss}
- h="full"
- justify="center"
- w="full"
+ <PreviewFrame
+ frameHeight={previewImage?.height ?? 1}
+ frameWidth={previewImage?.width ?? 1}
+ isLive
+ liveBadgeLabel={t('common.generating')}
+ shouldAntialiasLiveImage={shouldAntialiasProgressImage}
+ source={previewImage}
+ variant="inset"
>
- {previewImage ? (
- <Box
- borderColor="accent.solid"
- borderWidth="1px"
- boxShadow="0 24px 80px rgba(0,0,0,0.42)"
- css={getFittedFrameCss(previewImage.width ?? 1, previewImage.height ?? 1)}
- overflow="hidden"
- position="relative"
- rounded="lg"
- >
- <img
- alt={previewImage.alt}
- draggable={false}
- height={previewImage.height}
- src={previewImage.src}
- style={{
- display: 'block',
- height: 'auto',
- imageRendering: shouldAntialiasProgressImage ? 'auto' : 'pixelated',
- width: '100%',
- }}
- width={previewImage.width}
- />
- <Badge left="2" pointerEvents="none" position="absolute" size="xs" top="2" variant="solid">
- {t('common.generating')}
- </Badge>
- </Box>
- ) : (
- <Stack align="center" color="fg" gap="2" maxW="18rem" textAlign="center">
- <Text fontSize="sm" fontWeight="800">
- {t('widgets.preview.noGallerySelection')}
- </Text>
- <Text color="fg.subtle" fontSize="2xs">
- {t('widgets.preview.emptyDescription')}
- </Text>
- </Stack>
- )}
- </Flex>
+ <Stack align="center" color="fg" gap="2" maxW="18rem" textAlign="center">
+ <Text fontSize="sm" fontWeight="800">
+ {t('widgets.preview.noGallerySelection')}
+ </Text>
+ <Text color="fg.subtle" fontSize="2xs">
+ {t('widgets.preview.emptyDescription')}
+ </Text>
+ </Stack>
+ </PreviewFrame>
);
};
diff --git a/invokeai/frontend/webv2/src/workbench/widgets/preview/previewDensity.test.ts b/invokeai/frontend/webv2/src/workbench/widgets/preview/previewDensity.test.ts
new file mode 100644
index 0000000000..cb2c2fe9f4
--- /dev/null
+++ b/invokeai/frontend/webv2/src/workbench/widgets/preview/previewDensity.test.ts
@@ -0,0 +1,24 @@
+import { describe, expect, it } from 'vitest';
+
+import { getPreviewDensity, PREVIEW_FULL_MIN_WIDTH_PX, PREVIEW_MINIMAL_MAX_WIDTH_PX } from './previewDensity';
+
+describe('getPreviewDensity', () => {
+ it('is minimal below the minimal threshold in any region', () => {
+ expect(getPreviewDensity({ region: 'center', widthPx: PREVIEW_MINIMAL_MAX_WIDTH_PX - 1 })).toBe('minimal');
+ expect(getPreviewDensity({ region: 'right', widthPx: 100 })).toBe('minimal');
+ });
+
+ it('is compact in side panels regardless of width', () => {
+ expect(getPreviewDensity({ region: 'right', widthPx: 520 })).toBe('compact');
+ expect(getPreviewDensity({ region: 'left', widthPx: 900 })).toBe('compact');
+ });
+
+ it('is full in the center region at or above the full threshold', () => {
+ expect(getPreviewDensity({ region: 'center', widthPx: PREVIEW_FULL_MIN_WIDTH_PX })).toBe('full');
+ expect(getPreviewDensity({ region: 'center', widthPx: 1200 })).toBe('full');
+ });
+
+ it('is compact in a narrow center region', () => {
+ expect(getPreviewDensity({ region: 'center', widthPx: PREVIEW_FULL_MIN_WIDTH_PX - 1 })).toBe('compact');
+ });
+});
diff --git a/invokeai/frontend/webv2/src/workbench/widgets/preview/previewDensity.ts b/invokeai/frontend/webv2/src/workbench/widgets/preview/previewDensity.ts
new file mode 100644
index 0000000000..0d6cefd246
--- /dev/null
+++ b/invokeai/frontend/webv2/src/workbench/widgets/preview/previewDensity.ts
@@ -0,0 +1,70 @@
+import type { WorkbenchRegion } from '@workbench/types';
+
+import { useEffectEvent, useState, type Ref } from 'react';
+
+/**
+ * Single source of truth for the preview widget's responsive behavior. Every
+ * zone component (frame, filmstrip, footer) branches on this value instead of
+ * defining its own breakpoints, so side-panel ergonomics are tuned in one place.
+ *
+ * - `full` — center region with room to breathe: full action strip,
+ * filmstrip, metadata rows, footer message line.
+ * - `compact` — side panels and narrow center: star + overflow menu,
+ * one-line summaries, no footer message line.
+ * - `minimal` — very narrow: frame + hairline progress + prev/next only.
+ */
+export type PreviewDensity = 'full' | 'compact' | 'minimal';
+
+export const PREVIEW_MINIMAL_MAX_WIDTH_PX = 280;
+export const PREVIEW_FULL_MIN_WIDTH_PX = 560;
+
+export const getPreviewDensity = ({
+ region,
+ widthPx,
+}: {
+ region: WorkbenchRegion;
+ widthPx: number;
+}): PreviewDensity => {
+ if (widthPx < PREVIEW_MINIMAL_MAX_WIDTH_PX) {
+ return 'minimal';
+ }
+
+ if (region === 'left' || region === 'right') {
+ return 'compact';
+ }
+
+ return widthPx >= PREVIEW_FULL_MIN_WIDTH_PX ? 'full' : 'compact';
+};
+
+/**
+ * Observes the widget root's width and returns the derived density. The
+ * ResizeObserver lives in a ref callback (with cleanup) so there is no effect
+ * to keep in sync; state only changes when the density bucket changes.
+ */
+export const usePreviewDensity = (
+ region: WorkbenchRegion
+): { density: PreviewDensity; rootRef: Ref<HTMLDivElement> } => {
+ const [density, setDensity] = useState<PreviewDensity>(region === 'left' || region === 'right' ? 'compact' : 'full');
+ const applyMeasuredWidth = useEffectEvent((widthPx: number) => {
+ setDensity(getPreviewDensity({ region, widthPx }));
+ });
+ const [rootRef] = useState(() => (node: HTMLDivElement | null) => {
+ if (!node) {
+ return;
+ }
+
+ const observer = new ResizeObserver((entries) => {
+ const widthPx = entries[0]?.contentRect.width;
+
+ if (typeof widthPx === 'number' && widthPx > 0) {
+ applyMeasuredWidth(widthPx);
+ }
+ });
+
+ observer.observe(node);
+
+ return () => observer.disconnect();
+ });
+
+ return { density, rootRef };
+};