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.
TypeScript ai-artartificial-intelligencegenerative-artimage-generationimg2imginpaintinglatent-diffusionlinuxmacosoutpaintingstable-diffusiontxt2imgwindows
fix(theme): hide each scrollbar for its own axis only, and share the phantom heal
Josh Corbett6232b832cef7
diff --git a/invokeai/frontend/webv2/src/platform/ui/JsonPreview.tsx b/invokeai/frontend/webv2/src/platform/ui/JsonPreview.tsxindex 96a68f32b2..0aaf7e5890 100644--- a/invokeai/frontend/webv2/src/platform/ui/JsonPreview.tsx+++ b/invokeai/frontend/webv2/src/platform/ui/JsonPreview.tsx@@ -4,6 +4,7 @@ import { useCallback, useEffect, useMemo, useRef, useState } from 'react';import { IconButton } from './Button';import { toaster } from './toaster';+import { useScrollAreaPhantomHeal } from './useScrollAreaPhantomHeal';/*** The workbench's standard JSON preview: a monospace block with a copy button@@ -29,6 +30,9 @@ export const JsonPreview = ({}) => {const [hasCopied, setHasCopied] = useState(false);const copyResetTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null);+ const viewportRef = useRef<HTMLDivElement | null>(null);++ useScrollAreaPhantomHeal(viewportRef);const json = useMemo(() => text ?? JSON.stringify(value, null, 2) ?? 'null', [text, value]);useEffect(@@ -84,7 +88,7 @@ export const JsonPreview = ({<Icon as={hasCopied ? CheckIcon : CopyIcon} boxSize="3" color={hasCopied ? 'green.solid' : undefined} /></IconButton><ScrollArea.Root flex="1" maxW="full" minH="0" minW="0" size="xs" variant="hover" w="full">- <ScrollArea.Viewport aria-label={label} h="full" maxH={maxH} minW="0" role="region" w="full">+ <ScrollArea.Viewport ref={viewportRef} aria-label={label} h="full" maxH={maxH} minW="0" role="region" w="full"><ScrollArea.Content w="full"><Codebg="transparent"diff --git a/invokeai/frontend/webv2/src/platform/ui/Scrollable.browser.test.tsx b/invokeai/frontend/webv2/src/platform/ui/Scrollable.browser.test.tsxindex acfe4f7a9a..4b1ac3051a 100644--- a/invokeai/frontend/webv2/src/platform/ui/Scrollable.browser.test.tsx+++ b/invokeai/frontend/webv2/src/platform/ui/Scrollable.browser.test.tsx@@ -30,6 +30,19 @@ afterEach(async () => {/** No spaces and no hyphens: nothing the layout can break on, so it contributes its whole width to min-content. */const UNBREAKABLE = 'AVeryLongUnbreakableWorkflowNameThatHasNowhereToWrap';+/** Zag's measure timing varies under load; poll instead of guessing a delay. */+const waitFor = async (predicate: () => boolean, timeoutMs = 3000): Promise<void> => {+ const startedAt = Date.now();++ await act(async () => {+ while (!predicate() && Date.now() - startedAt < timeoutMs) {+ await new Promise<void>((resolve) => {+ globalThis.setTimeout(resolve, 25);+ });+ }+ });+};+const renderScrollable = async (children: ReactNode, orientation: 'horizontal' | 'vertical'): Promise<HTMLElement> => {host = document.createElement('div');host.style.cssText = 'width:200px;height:80px;';@@ -57,7 +70,7 @@ describe('Scrollable', () => {expect(viewport.scrollWidth).toBeLessThanOrEqual(viewport.clientWidth + 1);});- it('still lets a horizontal strip outgrow its viewport', async () => {+ it('still lets a horizontal strip outgrow its viewport, scrollbar shown', async () => {const viewport = await renderScrollable(<div style={{ display: 'flex', whiteSpace: 'nowrap' }}>{UNBREAKABLE}@@ -67,6 +80,45 @@ describe('Scrollable', () => {);expect(viewport.scrollWidth).toBeGreaterThan(viewport.clientWidth);++ // The orientation guard must not over-hide: a bar whose own axis+ // overflows stays visible.+ const scrollbar = document.querySelector<HTMLElement>(+ '[data-scope="scroll-area"][data-part="scrollbar"][data-orientation="horizontal"]'+ )!;++ await waitFor(() => getComputedStyle(scrollbar).display !== 'none');+ expect(getComputedStyle(scrollbar).display).not.toBe('none');+ });++ it('hides the vertical scrollbar when only sideways spill exists', async () => {+ // Chakra's stock guard hides a scrollbar only when NEITHER axis+ // overflows, so nowrap rows spilling sideways pinned a min-size thumb on+ // the vertical bar (dynamic prompts, JSON previews). The theme's+ // orientation-scoped guard is what this pins.+ const viewport = await renderScrollable(+ <div style={{ whiteSpace: 'nowrap' }}>+ {UNBREAKABLE}+ {UNBREAKABLE}+ {UNBREAKABLE}+ </div>,+ 'vertical'+ );++ expect(viewport.scrollHeight).toBeLessThanOrEqual(viewport.clientHeight);++ const scrollbar = document.querySelector<HTMLElement>(+ '[data-scope="scroll-area"][data-part="scrollbar"][data-orientation="vertical"]'+ )!;++ await waitFor(() => getComputedStyle(scrollbar).display === 'none');++ expect(scrollbar).not.toBeNull();+ // The spill is real and zag records it; only the orientation guard keeps+ // the vertical bar out of it.+ expect(scrollbar.hasAttribute('data-overflow-x')).toBe(true);+ expect(viewport.scrollWidth).toBeGreaterThan(viewport.clientWidth);+ expect(getComputedStyle(scrollbar).display).toBe('none');});it('hides the scrollbar for non-overflowing content inside a popover', async () => {@@ -93,13 +145,14 @@ describe('Scrollable', () => {</ChakraProvider>);await new Promise<void>((resolve) => {- globalThis.setTimeout(resolve, 400);+ globalThis.setTimeout(resolve, 50);});});const scrollbar = document.querySelector<HTMLElement>('[data-scope="scroll-area"][data-part="scrollbar"]')!;expect(scrollbar).not.toBeNull();+ await waitFor(() => !scrollbar.hasAttribute('data-overflow-y'));expect(scrollbar.hasAttribute('data-overflow-y')).toBe(false);expect(getComputedStyle(scrollbar).display).toBe('none');});diff --git a/invokeai/frontend/webv2/src/platform/ui/Scrollable.tsx b/invokeai/frontend/webv2/src/platform/ui/Scrollable.tsxindex 1e0ec59e77..fefd507125 100644--- a/invokeai/frontend/webv2/src/platform/ui/Scrollable.tsx+++ b/invokeai/frontend/webv2/src/platform/ui/Scrollable.tsx@@ -1,10 +1,11 @@import type { ComponentProps, ReactNode, RefObject } from 'react';import { ScrollArea } from '@chakra-ui/react';-import { useMountEffect } from '@platform/react/useMountEffect';import { usePreservedScrollOffset } from '@platform/react/usePreservedScrollOffset';import { useRef } from 'react';+import { useScrollAreaPhantomHeal } from './useScrollAreaPhantomHeal';+type ScrollAreaRootProps = ComponentProps<typeof ScrollArea.Root>;type ScrollAreaContentProps = ComponentProps<typeof ScrollArea.Content>;type ScrollAreaViewportProps = ComponentProps<typeof ScrollArea.Viewport>;@@ -56,71 +57,7 @@ export const Scrollable = ({// container that stops being rendered loses its offset outright.usePreservedScrollOffset(resolvedViewportRef);- // Zag's initial measure can race a popover's zero-size mount (its resize- // observers can even end up watching pre-remount nodes), and its- // "has overflow" default then strands a phantom thumb on content that never- // overflowed. Whenever a check finds that contradiction on the live nodes,- // one synthetic scroll routes a re-measure through zag's own event path —- // and clears the contradiction, so the checks cannot loop. Scroll listeners- // on these viewports must tolerate a no-op scroll event.- useMountEffect(() => {- // Always through the ref: a popover's presence pass can remount the- // subtree, and a node captured at effect time goes dead (which is also- // how zag's own observers end up blind here).- const hasPhantomScrollbar = () => {- const viewport = resolvedViewportRef.current;- // `:scope >`: a nested Scrollable's scrollbar must not answer for ours.- const scrollbar = viewport?.parentElement?.querySelector(':scope > [data-part="scrollbar"]');-- if (!viewport || !scrollbar) {- return false;- }-- const phantomY = scrollbar.hasAttribute('data-overflow-y') && viewport.scrollHeight <= viewport.clientHeight;- const phantomX = scrollbar.hasAttribute('data-overflow-x') && viewport.scrollWidth <= viewport.clientWidth;-- return Boolean(phantomY || phantomX);- };-- let observed: HTMLElement | null = null;- const timers: ReturnType<typeof setTimeout>[] = [];- const observer = new ResizeObserver(() => check());-- const check = () => {- const viewport = resolvedViewportRef.current;-- if (!viewport) {- return;- }-- if (viewport !== observed) {- if (observed) {- observer.unobserve(observed);- }-- observer.observe(viewport);- observed = viewport;- }-- if (hasPhantomScrollbar()) {- viewport.dispatchEvent(new Event('scroll'));- }- };-- // The timed checks catch a presence remount the observer can no longer- // see; each re-syncs observation to the ref's current node, and the- // re-synced observer covers real resizes from then on.- for (const delay of [64, 250, 600]) {- timers.push(setTimeout(check, delay));- }-- check();-- return () => {- timers.forEach(clearTimeout);- observer.disconnect();- };- });+ useScrollAreaPhantomHeal(resolvedViewportRef);return (<ScrollArea.Root size="xs" variant="hover" {...rootProps}>diff --git a/invokeai/frontend/webv2/src/platform/ui/theme/recipes.ts b/invokeai/frontend/webv2/src/platform/ui/theme/recipes.tsindex 84e9a62676..8a4586615a 100644--- a/invokeai/frontend/webv2/src/platform/ui/theme/recipes.ts+++ b/invokeai/frontend/webv2/src/platform/ui/theme/recipes.ts@@ -686,6 +686,24 @@ export const dialogSlotRecipe = defineSlotRecipe({},});+/**+ * Chakra hides a scrollbar only when NEITHER axis overflows+ * (`&:not([data-overflow-x], [data-overflow-y])`), so a vertical-only bar+ * sticks around — thumb clamped to its minimum size — whenever content merely+ * spills sideways (nowrap rows, wide JSON). Each bar answers for its own axis.+ */+export const scrollAreaSlotRecipe = defineSlotRecipe({+ ...chakraSlotRecipes.scrollArea,+ base: {+ ...chakraSlotRecipes.scrollArea.base,+ scrollbar: {+ ...chakraSlotRecipes.scrollArea.base?.scrollbar,+ '&[data-orientation="vertical"]:not([data-overflow-y])': { display: 'none' },+ '&[data-orientation="horizontal"]:not([data-overflow-x])': { display: 'none' },+ },+ },+});+export const sliderSlotRecipe = defineSlotRecipe({...chakraSlotRecipes.slider,base: {diff --git a/invokeai/frontend/webv2/src/platform/ui/theme/system.ts b/invokeai/frontend/webv2/src/platform/ui/theme/system.tsindex 6dcd8f552c..49bcf1dcf8 100644--- a/invokeai/frontend/webv2/src/platform/ui/theme/system.ts+++ b/invokeai/frontend/webv2/src/platform/ui/theme/system.ts@@ -12,6 +12,7 @@ import {numberInputSlotRecipe,popoverSlotRecipe,progressCircleSlotRecipe,+ scrollAreaSlotRecipe,segmentGroupSlotRecipe,selectSlotRecipe,skeletonRecipe,@@ -365,6 +366,7 @@ const config = defineConfig({numberInput: numberInputSlotRecipe,popover: popoverSlotRecipe,progressCircle: progressCircleSlotRecipe,+ scrollArea: scrollAreaSlotRecipe,segmentGroup: segmentGroupSlotRecipe,select: selectSlotRecipe,slider: sliderSlotRecipe,diff --git a/invokeai/frontend/webv2/src/platform/ui/useScrollAreaPhantomHeal.ts b/invokeai/frontend/webv2/src/platform/ui/useScrollAreaPhantomHeal.tsnew file mode 100644index 0000000000..e40ee45bb8--- /dev/null+++ b/invokeai/frontend/webv2/src/platform/ui/useScrollAreaPhantomHeal.ts@@ -0,0 +1,78 @@+import type { RefObject } from 'react';++import { useMountEffect } from '@platform/react/useMountEffect';++/**+ * Zag's initial scroll-area measure can race a popover's or dialog's+ * zero-size mount (its resize observers can even end up watching pre-remount+ * nodes), and its "has overflow" default then strands a phantom thumb on+ * content that never overflowed. Whenever a check finds that contradiction on+ * the live nodes, one synthetic scroll routes a re-measure through zag's own+ * event path — and clears the contradiction, so the checks cannot loop.+ * Scroll listeners on these viewports must tolerate a no-op scroll event.+ *+ * Mount this next to any `ScrollArea.Viewport` (see `Scrollable`); the ref+ * must point at the viewport element.+ */+export const useScrollAreaPhantomHeal = (viewportRef: RefObject<HTMLElement | null>): void => {+ useMountEffect(() => {+ // Always through the ref: a presence pass can remount the subtree, and a+ // node captured at effect time goes dead (which is also how zag's own+ // observers end up blind here).+ const hasPhantomScrollbar = () => {+ const viewport = viewportRef.current;+ // `:scope >`: a nested scroll area's scrollbar must not answer for+ // ours. Every scrollbar carries both overflow attributes, so the first+ // sibling bar is authoritative.+ const scrollbar = viewport?.parentElement?.querySelector(':scope > [data-part="scrollbar"]');++ if (!viewport || !scrollbar) {+ return false;+ }++ const phantomY = scrollbar.hasAttribute('data-overflow-y') && viewport.scrollHeight <= viewport.clientHeight;+ const phantomX = scrollbar.hasAttribute('data-overflow-x') && viewport.scrollWidth <= viewport.clientWidth;++ return Boolean(phantomY || phantomX);+ };++ let observed: HTMLElement | null = null;+ const timers: ReturnType<typeof setTimeout>[] = [];+ const observer = new ResizeObserver(() => check());++ const check = () => {+ const viewport = viewportRef.current;++ if (!viewport) {+ return;+ }++ if (viewport !== observed) {+ if (observed) {+ observer.unobserve(observed);+ }++ observer.observe(viewport);+ observed = viewport;+ }++ if (hasPhantomScrollbar()) {+ viewport.dispatchEvent(new Event('scroll'));+ }+ };++ // The timed checks catch a presence remount the observer can no longer+ // see; each re-syncs observation to the ref's current node, and the+ // re-synced observer covers real resizes from then on.+ for (const delay of [64, 250, 600]) {+ timers.push(setTimeout(check, delay));+ }++ check();++ return () => {+ timers.forEach(clearTimeout);+ observer.disconnect();+ };+ });+};diff --git a/invokeai/frontend/webv2/src/workbench/widget-frame/WidgetFailureBoundary.tsx b/invokeai/frontend/webv2/src/workbench/widget-frame/WidgetFailureBoundary.tsxindex 01a8c04289..8912821e46 100644--- a/invokeai/frontend/webv2/src/workbench/widget-frame/WidgetFailureBoundary.tsx+++ b/invokeai/frontend/webv2/src/workbench/widget-frame/WidgetFailureBoundary.tsx@@ -7,10 +7,11 @@ import type {import { Box, Code, Flex, HStack, ScrollArea, Stack, Text, useRecipe } from '@chakra-ui/react';import { Button } from '@platform/ui/Button';+import { useScrollAreaPhantomHeal } from '@platform/ui/useScrollAreaPhantomHeal';import { chipRecipe } from '@theme/recipes';import { resolveWidgetInstanceLabel } from '@workbench/widgetLabels';import { TriangleAlertIcon } from 'lucide-react';-import { Component, type ErrorInfo, type ReactNode } from 'react';+import { Component, type ErrorInfo, type ReactNode, useRef } from 'react';import { useTranslation } from 'react-i18next';import { WidgetPanelFrame, WidgetTooltipFrame } from './WidgetFrames';@@ -56,6 +57,9 @@ const WidgetFailureCard = ({onRetry: () => void;}) => {const { t } = useTranslation();+ const viewportRef = useRef<HTMLDivElement | null>(null);++ useScrollAreaPhantomHeal(viewportRef);return (<Stack bg="bg.muted" borderColor="border.error" borderWidth="1px" gap="2" p="3" rounded="md">@@ -63,7 +67,7 @@ const WidgetFailureCard = ({{t('widgets.failure.title', { label })}</Text><ScrollArea.Root maxH="8rem" size="xs" variant="hover">- <ScrollArea.Viewport maxH="8rem">+ <ScrollArea.Viewport ref={viewportRef} maxH="8rem"><ScrollArea.Content><Code display="block" p="2" whiteSpace="pre-wrap">{details}