Environment
@forge/react12.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
- 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 />);
- Open the popup.
- Tick A, then tick B.
- 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)
-
In the Popup adapter in
RendererWithComponents, thetriggeris 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. -
ADS Popup stores the trigger element as
triggerRefstate, anduseFocusManager’s effect depends on it. Each remount therefore re-runs the effect. The cleanup callsfocusTrap.deactivate(), and withreturnFocusOnDeactivate: shouldReturnFocus(defaulttrue), 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. -
The UI Kit Popup adapter does not pass
shouldReturnFocusto the ADS Popup, althoughPopupProps(generated from the ADS props) accepts it. SettingshouldReturnFocus={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(andautoFocusif it isn’t already passed) through to the ADS Popup.
Workarounds we tried
shouldRenderToParentwithshouldDisableFocusLockremoves 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.