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(webv2): give the seed randomize switch an id of its own
Lincoln Steine479c7517cbf
diff --git a/invokeai/frontend/webv2/src/features/upscale/ui/UpscaleWidgetView.tsx b/invokeai/frontend/webv2/src/features/upscale/ui/UpscaleWidgetView.tsxindex f53f959cc2..e1ae5ebed8 100644--- a/invokeai/frontend/webv2/src/features/upscale/ui/UpscaleWidgetView.tsx+++ b/invokeai/frontend/webv2/src/features/upscale/ui/UpscaleWidgetView.tsx@@ -52,7 +52,7 @@ import { Combobox, Field, IconButton, Select, Tooltip } from '@platform/ui';import { SliderNumberField } from '@platform/ui/SliderNumberField';import { toaster } from '@platform/ui/toaster';import { DicesIcon } from 'lucide-react';-import { memo, useCallback, useMemo } from 'react';+import { memo, useCallback, useId, useMemo } from 'react';import { useTranslation } from 'react-i18next';import { areInputImagesEquivalent, valuesAreEqual } from './upscaleComparators';@@ -284,6 +284,13 @@ export const UpscaleWidgetView = () => {[t, values]);const patch = useCallback((next: Partial<UpscaleWidgetValues>) => patchValues(next), [patchValues]);+ // Chakra's `Field.Root` hands its single `ids.control` to EVERY control+ // inside it, and the seed Field holds the NumberInput, the shuffle button+ // and this switch. Without an id of its own the switch's hidden input+ // collides with the seed input, so the `<label>` Switch.Root renders points+ // at the seed field and clicking the toggle only moved focus.+ const seedSwitchId = useId();+ const seedSwitchIds = useMemo(() => ({ hiddenInput: `${seedSwitchId}-randomize-seed` }), [seedSwitchId]);const patchPromptDraft = useCallback((next: ProjectPromptDraftPatch) => patchDraft(next), [patchDraft]);useMountEffect(() => {@@ -639,7 +646,12 @@ export const UpscaleWidgetView = () => {<DicesIcon /></IconButton></Tooltip>- <Switch.Root checked={values.shouldRandomizeSeed} size="sm" onCheckedChange={set.randomizeSeed}>+ <Switch.Root+ checked={values.shouldRandomizeSeed}+ ids={seedSwitchIds}+ size="sm"+ onCheckedChange={set.randomizeSeed}+ ><Switch.HiddenInput /><Switch.Control _checked={SWITCH_CHECKED_PROPS}><Switch.Thumb />diff --git a/invokeai/frontend/webv2/src/features/video/ui/VideoWidgetView.tsx b/invokeai/frontend/webv2/src/features/video/ui/VideoWidgetView.tsxindex 483196894e..ba6d790bbe 100644--- a/invokeai/frontend/webv2/src/features/video/ui/VideoWidgetView.tsx+++ b/invokeai/frontend/webv2/src/features/video/ui/VideoWidgetView.tsx@@ -31,7 +31,7 @@ import { Button } from '@platform/ui/Button';import { SliderNumberField } from '@platform/ui/SliderNumberField';import { toaster } from '@platform/ui/toaster';import { ArrowLeftRightIcon, DicesIcon } from 'lucide-react';-import { useCallback, useMemo } from 'react';+import { useCallback, useId, useMemo } from 'react';import { useTranslation } from 'react-i18next';import { areVideoValuesEqual } from './videoComparators';@@ -150,6 +150,13 @@ export const VideoWidgetView = () => {);const patch = useCallback((next: Partial<VideoWidgetValues>) => patchValues(next), [patchValues]);+ // Chakra's `Field.Root` hands its single `ids.control` to EVERY control+ // inside it, and this Field holds three. Without an id of its own the+ // switch's hidden input collides with the seed NumberInput, so the+ // `<label>` Switch.Root renders points at the seed field: clicking the+ // toggle focused the seed input and never toggled anything.+ const seedSwitchId = useId();+ const seedSwitchIds = useMemo(() => ({ hiddenInput: `${seedSwitchId}-randomize-seed` }), [seedSwitchId]);useMountEffect(() => {void ensureModelsLoaded();@@ -679,15 +686,22 @@ export const VideoWidgetView = () => {<DicesIcon /></IconButton><HStack gap="1">- <Switch.Root checked={values.shouldRandomizeSeed} size="sm" onCheckedChange={set.randomizeSeed}>+ <Switch.Root+ checked={values.shouldRandomizeSeed}+ ids={seedSwitchIds}+ size="sm"+ onCheckedChange={set.randomizeSeed}+ ><Switch.HiddenInput /><Switch.Control _checked={SWITCH_CHECKED_PROPS}><Switch.Thumb /></Switch.Control>+ {/* Inside Switch.Root, so the words are part of the control+ (they were an inert sibling <Text> before). */}+ <Switch.Label color="fg.muted" fontSize="2xs">+ {t('widgets.video.randomizeSeed')}+ </Switch.Label></Switch.Root>- <Text color="fg.muted" fontSize="2xs">- {t('widgets.video.randomizeSeed')}- </Text></HStack></HStack></Field>diff --git a/invokeai/frontend/webv2/src/features/video/ui/seedToggle.browser.test.tsx b/invokeai/frontend/webv2/src/features/video/ui/seedToggle.browser.test.tsxnew file mode 100644index 0000000000..a998c18175--- /dev/null+++ b/invokeai/frontend/webv2/src/features/video/ui/seedToggle.browser.test.tsx@@ -0,0 +1,98 @@+import { ChakraProvider, NumberInput, Switch } from '@chakra-ui/react';+import { Field } from '@platform/ui/Field';+import { system } from '@theme/system';+import { act, useId, useMemo } from 'react';+import { createRoot, type Root } from 'react-dom/client';+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';+import { userEvent } from 'vitest/browser';++/**+ * The seed row of the Video and Upscale panels puts three controls -- a seed+ * NumberInput, a shuffle button and a randomize Switch -- inside ONE `Field`.+ * Chakra's `Field.Root` hands its single `ids.control` to every control it+ * contains, so without an id of its own the switch's hidden input collides+ * with the seed input and the `<label>` that `Switch.Root` renders resolves to+ * the seed field instead. Clicking the toggle then just moved focus.+ */+let container: HTMLDivElement;+let root: Root;++beforeEach(() => {+ container = document.createElement('div');+ document.body.append(container);+ root = createRoot(container);+});++afterEach(() => {+ act(() => root.unmount());+ container.remove();+});++const SeedRow = ({ onCheckedChange }: { onCheckedChange: (details: { checked: boolean }) => void }) => {+ const seedSwitchId = useId();+ const seedSwitchIds = useMemo(() => ({ hiddenInput: `${seedSwitchId}-randomize-seed` }), [seedSwitchId]);++ return (+ <Field hint="seed" label="Seed">+ <NumberInput.Root size="xs" value="42">+ <NumberInput.Input aria-label="Seed" />+ </NumberInput.Root>+ <Switch.Root checked={false} ids={seedSwitchIds} size="sm" onCheckedChange={onCheckedChange}>+ <Switch.HiddenInput data-testid="switch-input" />+ <Switch.Control data-testid="switch-control">+ <Switch.Thumb />+ </Switch.Control>+ <Switch.Label>Randomize seed</Switch.Label>+ </Switch.Root>+ </Field>+ );+};++const render = (onCheckedChange: (details: { checked: boolean }) => void) =>+ act(() => {+ root.render(+ <ChakraProvider value={system}>+ <SeedRow onCheckedChange={onCheckedChange} />+ </ChakraProvider>+ );+ });++describe('seed row randomize switch', () => {+ it('does not share the Field control id with the seed input', async () => {+ await render(vi.fn());++ const seed = container.querySelector<HTMLInputElement>('input[aria-label="Seed"]')!;+ const toggle = container.querySelector<HTMLInputElement>('[data-testid="switch-input"]')!;++ expect(toggle.id).not.toBe('');+ expect(toggle.id).not.toBe(seed.id);+ // The label Switch.Root renders must point at the switch, not the seed field.+ expect(toggle.closest('label')?.getAttribute('for')).toBe(toggle.id);+ });++ it('toggles when the control is clicked, leaving the seed input unfocused', async () => {+ const onCheckedChange = vi.fn();++ await render(onCheckedChange);+ const seed = container.querySelector<HTMLInputElement>('input[aria-label="Seed"]')!;++ await userEvent.click(container.querySelector('[data-testid="switch-control"]')!);++ expect(onCheckedChange).toHaveBeenCalledWith(expect.objectContaining({ checked: true }));+ expect(document.activeElement).not.toBe(seed);+ });++ it('toggles when the label text is clicked', async () => {+ const onCheckedChange = vi.fn();++ await render(onCheckedChange);++ const label = [...container.querySelectorAll('*')].find(+ (node) => node.textContent === 'Randomize seed' && node.children.length === 0+ )!;++ await userEvent.click(label);++ expect(onCheckedChange).toHaveBeenCalledWith(expect.objectContaining({ checked: true }));+ });+});