UI Kit Popup: focus jumps to an earlier element on every re-render while the popup is open

Environment

  • @forge/react 12.3.0, UI Kit
  • Jira Cloud, Chrome
  • Module: jira:globalPage (the same happens in any UI Kit module)

Summary
While a UI Kit Popup is open, every re-render of the app moves keyboard focus to an element that was focused earlier. In a popup that holds a list of checkboxes, ticking a box moves focus to the box ticked before it. A button that changes state (for example a “Clear” button that becomes disabled) sends focus back to a checkbox. Apps cannot prevent this, because UI Kit does not pass shouldReturnFocus through to the ADS Popup.

Steps to reproduce

  1. Render this in a UI Kit module:
import React, { useState } from 'react';
import ForgeReconciler, { Box, Button, Checkbox, Popup, Stack } from '@forge/react';

const App = () => {
  const [open, setOpen] = useState(false);
  const [picked, setPicked] = useState<string[]>([]);
  const toggle = (v: string) => setPicked((p) => (p.includes(v) ? p.filter((x) => x !== v) : [...p, v]));

  return (
    <Popup
      isOpen={open}
      onClose={() => setOpen(false)}
      placement="bottom-start"
      trigger={() => <Button onClick={() => setOpen(!open)}>{`Fields (${picked.length})`}</Button>}
      content={() => (
        <Box padding="space.200">
          <Stack space="space.100">
            {['A', 'B', 'C'].map((v) => (
              <Checkbox key={v} id={`box-${v}`} label={v} isChecked={picked.includes(v)} onChange={() => toggle(v)} />
            ))}
            <Button onClick={() => setPicked([])} isDisabled={picked.length === 0}>Clear</Button>
          </Stack>
        </Box>
      )}
    />
  );
};

ForgeReconciler.render(<App />);

  1. Open the popup.
  2. Tick A, then tick B.
  3. Tick C, then press Clear.

Expected
Focus stays on the element that was clicked: B after step 3, Clear (or the popup) after step 4.

Actual

  • After step 3, focus moves from B back to A.
  • After step 4, Clear becomes disabled and focus jumps to one of the checkboxes ticked earlier.

The focus ring visibly jumps between rows, and a keyboard user loses their place in the list.

Cause (read in Jira’s production bundle)

  1. In the Popup adapter in RendererWithComponents, the trigger is rendered through a wrapper component defined inside the adapter’s render function:

    const _ = ({ triggerProps }) => <Box as="span"><Box as="span" {...triggerProps}>…</Box></Box>;
    return <Popup … trigger={(e) => <_ triggerProps={e} />} />;
    
    

    Because _ is a new component type on every render, React unmounts and remounts the trigger’s DOM on each re-render of the app. We confirmed it in the DOM: the trigger’s button and its two span wrappers are replaced on every state change, while the popup container is kept.

  2. ADS Popup stores the trigger element as triggerRef state, and useFocusManager’s effect depends on it. Each remount therefore re-runs the effect. The cleanup calls focusTrap.deactivate(), and with returnFocusOnDeactivate: shouldReturnFocus (default true), focus-trap returns focus to the element that was focused when that trap was activated. After the first pick, that is the previously ticked checkbox.

  3. The UI Kit Popup adapter does not pass shouldReturnFocus to the ADS Popup, although PopupProps (generated from the ADS props) accepts it. Setting shouldReturnFocus={false} has no effect; we tested it on a development install.

Suggested fix

  • Define the trigger wrapper outside the adapter’s render function (or memoize it), so the trigger is not remounted on every render.
  • Pass shouldReturnFocus (and autoFocus if it isn’t already passed) through to the ADS Popup.

Workarounds we tried

  • shouldRenderToParent with shouldDisableFocusLock removes the trap, but Tab then closes the popup, so a keyboard user cannot reach the checkboxes.
  • What we ship now: while the popup is open, nothing re-renders. Ticks go into a ref, the checkboxes are uncontrolled (defaultChecked), and the choice is applied when the popup closes. This works, but it rules out a popup whose content updates live.

Hi @DruvisCukursLineset,

I can reproduce this. I ran your code in a scratch UI Kit global page on @forge/react 12.3.0 and 12.3.1, CLI 14.1.0, Chromium. Ticking B sent focus back to A in all three mouse runs per version, and the trigger button and its two spans were new nodes after each render. shouldReturnFocus={false} did nothing. Neither did a module-level trigger.

The prop is also dropped in your own bundle. The Popup wrapper in @forge/react (out/components/popup.js) forwards a fixed list of props, and shouldReturnFocus isn’t on it. 12.4.0-next.2 is the same.

Passing it through needs @forge/react fixed as well as the host adapter. Your remount fix is host-side only.

I couldn’t find an ECO ticket. The nearest is ECO-1799, where the Modal’s focus restore can’t be turned off and makes the issue view jump. I’d raise it at Developer and Marketplace support.

Thanks for running it on both versions, and for finding the prop missing in @forge/react’s popup.js. That means the fix has to land on both sides. I’ve raised it with developer support: Jira Service Management.

Hi @DruvisCukursLineset and @Mihai_leanzero , thanks for raising. I have raised a ticket for your tracking https://jira.atlassian.com/browse/ECO-1892 . A fix for this will come soon. The shouldReturnFocus prop already defaults to true on the underlying component and is not required. This is a bug within the pop up state