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(webv2): label the dynamic prompts settings with real fields

joshistoast14ae3f7c6d41
diff --git a/invokeai/frontend/webv2/src/features/generation/ui/promptFields/DynamicPromptsControls.browser.test.tsx b/invokeai/frontend/webv2/src/features/generation/ui/promptFields/DynamicPromptsControls.browser.test.tsx
index a626b23b9b..abab281daa 100644
--- a/invokeai/frontend/webv2/src/features/generation/ui/promptFields/DynamicPromptsControls.browser.test.tsx
+++ b/invokeai/frontend/webv2/src/features/generation/ui/promptFields/DynamicPromptsControls.browser.test.tsx
@@ -178,16 +178,68 @@ describe('dynamic prompts popover controls', () => {
await act(async () => {
await userEvent.click(document.querySelector<HTMLElement>('[data-scope="menu"][data-part="trigger"]')!);
});
+ // A dispatched click rather than userEvent: the menu animates open, and
+ // userEvent waits for the element to be "stable" before it will click.
await act(async () => {
- await userEvent.click(
- [...document.querySelectorAll('[data-scope="menu"][data-part="item"]')].at(-1) as HTMLElement
- );
+ ([...document.querySelectorAll('[data-scope="menu"][data-part="item"]')].at(-1) as HTMLElement).click();
+ await Promise.resolve();
});
expect(onChange).toHaveBeenCalledWith({ combinatorial: false });
expect(row().getBoundingClientRect().height).toBe(combinatorialHeight);
});
+ it('labels both settings controls with a real, associated label', async () => {
+ await render();
+ await openPopover();
+
+ const popover = document.querySelector('[data-scope="popover"][data-part="content"]')!;
+ const numberInput = popover.querySelector<HTMLInputElement>('[data-scope="number-input"][data-part="input"]')!;
+ const numberLabel = popover.querySelector<HTMLLabelElement>(`label[for="${numberInput.id}"]`);
+
+ // The visible label is wired to the input rather than a detached bit of
+ // text with an aria-label duplicating it.
+ expect(numberLabel, 'number input should have an associated label').toBeTruthy();
+ expect(numberInput.getAttribute('aria-label')).toBeNull();
+
+ // A <label for> cannot name a button, so the trigger is named by the field
+ // label plus its own value.
+ const trigger = popover.querySelector<HTMLElement>('[data-scope="menu"][data-part="trigger"]')!;
+ const namedBy = (trigger.getAttribute('aria-labelledby') ?? '')
+ .split(' ')
+ .map((id) => document.getElementById(id)?.textContent ?? '');
+
+ expect(namedBy.filter(Boolean)).toHaveLength(2);
+ });
+
+ it('anchors the mode menu to its trigger', async () => {
+ // Regression: setting an explicit id on the trigger left zag unable to find
+ // the element, so the positioner had nothing to anchor to and the menu
+ // rendered at the top-left of the screen.
+ await render();
+ await openPopover();
+
+ const trigger = document.querySelector<HTMLElement>('[data-scope="menu"][data-part="trigger"]')!;
+
+ await act(async () => {
+ trigger.click();
+ await Promise.resolve();
+ });
+ await act(async () => {
+ await new Promise((resolve) => {
+ window.setTimeout(resolve, 300);
+ });
+ });
+
+ const content = document.querySelector<HTMLElement>('[data-scope="menu"][data-part="content"]')!;
+ const triggerBox = trigger.getBoundingClientRect();
+ const contentBox = content.getBoundingClientRect();
+
+ expect(contentBox.width).toBeGreaterThan(0);
+ expect(Math.abs(contentBox.left - triggerBox.left)).toBeLessThan(40);
+ expect(contentBox.top).toBeGreaterThan(triggerBox.top - 40);
+ });
+
it('gives both tabs a header control of the same height', async () => {
await render();
await openPopover();
diff --git a/invokeai/frontend/webv2/src/features/generation/ui/promptFields/DynamicPromptsPanel.tsx b/invokeai/frontend/webv2/src/features/generation/ui/promptFields/DynamicPromptsPanel.tsx
index abac822120..70d380b6c6 100644
--- a/invokeai/frontend/webv2/src/features/generation/ui/promptFields/DynamicPromptsPanel.tsx
+++ b/invokeai/frontend/webv2/src/features/generation/ui/promptFields/DynamicPromptsPanel.tsx
@@ -11,10 +11,11 @@ import {
import { HighlightedPrompt } from '@features/generation/ui/promptFields/PromptHighlight';
import { PANEL_HEADER_CONTROL_HEIGHT, PromptPanelHeader } from '@features/generation/ui/promptFields/PromptPanelHeader';
import { Button, IconButton } from '@platform/ui/Button';
+import { Field } from '@platform/ui/Field';
import { MenuContent } from '@platform/ui/Menu';
import { Scrollable } from '@platform/ui/Scrollable';
import { Tooltip } from '@platform/ui/Tooltip';
-import { CheckIcon, ChevronDownIcon, ShuffleIcon } from 'lucide-react';
+import { ChevronDownIcon, ShuffleIcon } from 'lucide-react';
import { useCallback, useId, useMemo } from 'react';
import { useTranslation } from 'react-i18next';
@@ -47,6 +48,12 @@ export const DynamicPromptsPanel = ({
// collide with the number input beside it and send label clicks to the wrong
// control. Explicit ids keep them apart.
const seedSwitchId = useId();
+ const modeFieldId = useId();
+ const modeTriggerId = useId();
+ // A <label for> cannot name a button, so the trigger is named by the field's
+ // label plus its own text: "Mode" + "All combinations".
+ const modeLabelledBy = `${modeFieldId}-label ${modeTriggerId}`;
+ const modeMenuIds = useMemo(() => ({ trigger: modeTriggerId }), [modeTriggerId]);
const seedSwitchIds = useMemo(() => ({ hiddenInput: seedSwitchId, label: `${seedSwitchId}-label` }), [seedSwitchId]);
const visiblePrompts = expansion.prompts.slice(0, MAX_PREVIEW_ROWS);
const hiddenPromptCount = expansion.prompts.length - visiblePrompts.length;
@@ -110,15 +117,19 @@ export const DynamicPromptsPanel = ({
</PromptPanelHeader>
<HStack align="end" gap="2">
- <Stack flex="1" gap="1" minW="0">
- <Text color="fg.subtle" fontSize="2xs" truncate>
- {t('widgets.generate.dynamicPrompts.mode')}
- </Text>
+ <Field id={modeFieldId} label={t('widgets.generate.dynamicPrompts.mode')}>
{/* A menu rather than a Select: Chakra's Select renders a hidden native
select whose sync throws inside this popover. */}
- <Menu.Root positioning={MENU_POSITIONING}>
+ <Menu.Root ids={modeMenuIds} positioning={MENU_POSITIONING}>
<Menu.Trigger asChild>
- <Button justifyContent="space-between" minW="0" size="xs" variant="outline" w="full">
+ <Button
+ aria-labelledby={modeLabelledBy}
+ justifyContent="space-between"
+ minW="0"
+ size="xs"
+ variant="outline"
+ w="full"
+ >
<Text as="span" truncate>
{modeLabel}
</Text>
@@ -132,9 +143,7 @@ export const DynamicPromptsPanel = ({
{modeItems.map((item) => (
<Menu.RadioItem key={item.value} value={item.value}>
<Menu.ItemText>{item.label}</Menu.ItemText>
- <Menu.ItemIndicator>
- <CheckIcon />
- </Menu.ItemIndicator>
+ <Menu.ItemIndicator />
</Menu.RadioItem>
))}
</Menu.RadioItemGroup>
@@ -142,11 +151,9 @@ export const DynamicPromptsPanel = ({
</Menu.Positioner>
</Portal>
</Menu.Root>
- </Stack>
- <Stack gap="1" w="6.5rem">
- <Text color="fg.subtle" fontSize="2xs" truncate>
- {maxPromptsLabel}
- </Text>
+ </Field>
+ {/* Fixed width and no shrink: the mode field takes the slack instead. */}
+ <Field flex="0 0 auto" label={maxPromptsLabel} w="6.5rem">
<NumberInput.Root
allowMouseWheel
max={DYNAMIC_PROMPTS_MAX_PROMPTS}
@@ -156,9 +163,9 @@ export const DynamicPromptsPanel = ({
onValueChange={handleMaxPromptsChange}
>
<NumberInput.Control />
- <NumberInput.Input aria-label={maxPromptsLabel} paddingStart="2" />
+ <NumberInput.Input paddingStart="2" />
</NumberInput.Root>
- </Stack>
+ </Field>
{/* Always rendered, merely hidden, so switching modes cannot reflow the row. */}
<Tooltip content={t('widgets.generate.dynamicPrompts.shuffle')}>
<IconButton