Skip to content

fix: resolve the react-hooks/refs and set-state-in-effect warnings - #3323

Merged
interactivellama merged 22 commits into
masterfrom
fix/react-hooks-refs
Nov 24, 2026
Merged

interactivellama merged 22 commits into
masterfrom
fix/react-hooks-refs

Conversation

@interactivellama

@interactivellama interactivellama commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

Part of the v11 merge train, stack 1 "clean CI": react-hooks-refs → deprecated-dom-apis (#3327) → deprecated-prop lint (#3311) → private exports. ci-warnings (#3322) has merged, so this PR now targets master and merges first.

Additional description

This fixes the 32 react-hooks/refs and 3 react-hooks/set-state-in-effect warnings and removes the lint cap from the previous PR, so lint warnings are back to the tracked backlog.

Root cause Fix
Generated ids kept in useRef(generateId()) and read during render (22: color-picker, combobox, date-picker, tooltip) React's useId(). A passed id still takes precedence. Since React 19.2, useId returns ids such as _R_1_, which are valid CSS identifiers
Tooltip anchored nubbin measured the trigger during render measure it in a layout effect, re-measured after every render while the nubbin is open, so a trigger that resizes stays anchored
menu-dropdown wrote handleCloseRef during render, and memoized triggerRef.current useLatestRef. It also reports one onClick per keyboard open from the trigger
carousel kept its measured width only in a ref derive the stage translation and keep the initial width in state
portal container node in a ref keep it in state. The one intentional setState in an effect is documented
combobox clamped the active pill and re-synced the highlighted option in effects do both during render

Dialog fixes found while testing the stories

Symptom Root cause Fix
A controlled Popover (and so ColorPicker) never closed on a click outside the rewrite dropped 0.10.x's react-onclickoutside, so onRequestClose never fired for an outside click Popover uses useClickOutside and calls onRequestClose(event, { trigger: 'clickOutside' }). When controlled, it only requests the close and stays open until the parent closes it
A popover that mounts open (ColorPicker "Menu Open", Popover "walkthrough on anchor") wasn't positioned, so its buttons couldn't be clicked unused inputRendered state recreated the container callback ref on mount, so the trigger ref was detached while Dialog asked for its target remove the state
Escape didn't close a Filter popover Portal called onOpen before its children mounted, so Dialog couldn't move focus inside call onOpen from an effect after the portal node renders
Tooltip with an anchored nubbin showed two nubbins for some ids the #<id>:after rule is invalid for an id that isn't a CSS identifier (leading digit, space, quote) use an [id="…"] attribute selector
ColorPicker stories didn't close on OK, Cancel or a click outside the stories didn't control isOpen, and action('onChange')(event, data) threw on the circular React event before the close request the story wrapper and __examples__/default.jsx own isOpen and close from onRequestOpen / onRequestClose. The story logs only data
Modal stories' Cancel/Save buttons did nothing the footers had no handlers the stories own isOpen and close from the footer buttons. Prettier formatting is in a separate style(modal) commit
SimpleDataTable interactive-elements checkboxes didn't toggle checked with no handler make them controlled

Each fix has a regression test where the behavior is observable, for example:

  • the tooltip nubbin with a stubbed getBoundingClientRect
  • the menu-dropdown onClick count
  • a popover browser test that clicks a footer button of a popover mounted open
  • a controlled close on OK, Cancel and an outside click
  • Escape from a portal popover
  • a fixed id reaching the DOM for Combobox, DatePicker and Tooltip

Snapshots

17 story snapshots change their generated ids from slds-snapshot-id-N to _R_…_. These are stable: each story renders as its own renderToStaticMarkup root, so its ids depend only on its own tree. Full and filtered test:snapshots runs both pass with --update=none.

Breaking changes

This PR has no breaking API changes. Behavior changes to be aware of:

  • Popover now calls onRequestClose with { trigger: 'clickOutside' }, as 0.10.x did.
  • Portal's onOpen fires once the children have mounted, one render later than before.
  • Generated ids now look like _R_1_ instead of slds-…. Pass id if you need a fixed one.

CONTRIBUTOR checklist (do not remove)

Please complete for every pull request.

Green gate — all of these must pass locally (CI runs the same):

  • npm run lint passes with 0 errors (npm run lint:fix to auto-fix). Warnings are the tracked gradual-migration backlog; do not add new errors.
  • npm run typecheck is clean (tsc --noEmit).
  • npm test -- --run passes — this runs both Vitest projects (jsdom unit + real-browser browser) and the story HTML snapshots. If you changed rendered markup on purpose, update snapshots with npm test -- --run -u and review the diff.
  • npm run build succeeds (type-check + Vite library build + .d.ts + entry shims).

Behavior & docs:

  • First-time contributors: sign the CLA (a bot will comment with instructions).
  • Added/updated Vitest + React Testing Library tests for the change (template: components/button/__tests__). New props and bug fixes should get a regression test. Tests that genuinely need a real browser (focus traversal, layout/measurement, canvas) go in a *.browser.test.jsx file (the browser project); everything else is jsdom.
  • Reviewed the component's Storybook stories: npm run storybook → http://localhost:6007/. Confirm the story actually renders and behaves (deprecated Storyshots/Karma are gone; there is no separate browser-tests server).
  • Story/example code uses the component's real prop API (e.g. slots like content / visualPickerFigure, render props like renderItem, icon props, events), not assumptions — a common source of "component looks broken" reports has been stories authored against the wrong API. Cross-check against components/<name>/__examples__.
  • Storybook Accessibility (a11y) panel shows no new violations for the affected stories.
  • If you added or changed a type, remember runtime PropTypes were removed in the TS migration — types are the contract. Ship accurate .d.ts (they're generated by the build); JS consumers rely on them via editor type-acquisition.

REVIEWER checklist (do not remove)

  • CI is green (GitHub Actions CI workflow: lint, typecheck, test, build). Chromatic visual review, if configured, has been checked.
  • Change has adequate test coverage; bug fixes include a regression test.
  • Reviewed the affected Storybook stories and their a11y panels.
  • Rendered markup still conforms to SLDS — check the story HTML snapshot diff (components/__tests__/__snapshots__/story-snapshots.test.jsx.snap).
  • Public API / package surface: if exports changed, the deep-import contract still holds (@salesforce/design-system-react/components/<name> and nested paths resolve for ESM, CJS, and types). Removing/renaming a public component or export is a breaking change → feat!:/BREAKING CHANGE: and a major version bump.

@interactivellama
interactivellama removed this pull request from stack #3324 November 17, 2026 17:47
@interactivellama
interactivellama removed this pull request from stack #3334 November 17, 2026 22:47
@interactivellama
interactivellama removed this pull request from stack #3335 November 17, 2026 23:49
@interactivellama
interactivellama removed this pull request from stack #3336 November 18, 2026 00:38
@interactivellama interactivellama added this to the 11.0.0-rc.0 milestone Nov 18, 2026
@interactivellama
interactivellama removed this pull request from stack #3337 November 18, 2026 00:49
@interactivellama
interactivellama removed this pull request from stack #3339 November 18, 2026 23:30
@interactivellama
interactivellama removed this pull request from stack #3344 November 19, 2026 04:42
The refs rule follows ref reads through functions called during
render, so Combobox and Tooltip still reported after the id change:

- Combobox built input onChange handlers with withInputOnChange(...)
  in each sub-render. They are now two memoized handlers:
  handleInnerInputChange, and handleInnerInputChangeWhileUnselected for
  the single-selection variants that ignore typing once an option is
  selected.
- Combobox called getCustomPopoverProps(body) in renderPopover and
  read the merged props' id. The props are now memoized as
  customPopoverProps, and the popover id comes from the popover prop.
- Tooltip passed the trigger props (and their handlers) to
  renderTrigger from its own render. The trigger slot now renders
  through a small TooltipTrigger component.

Rendered output and handler behaviour are unchanged.
handleCloseRef was assigned during render so the open-dropdown
registration could call the latest handleClose. It now uses
useLatestRef, which updates the ref in an insertion effect. Its only
reader is another dropdown's open handler, which runs after commit.
Prettier output only: wrap the keyboardNavigate declaration and drop the trailing blank lines.
…gger

componentContext captured triggerRef.current in a useMemo, so the
value was frozen when the memo ran: null on the first render, and only
refreshed when handleClick changed identity. keyboardNavigate's
"Enter/Down on the closed trigger also clicks it" branch therefore ran
or not depending on timing. Pressing Enter on a closed trigger called
onClick 0 times the first time; the second time it called onClick
once and onRequestOpen twice, because handleClick's stale getIsOpen
opened the menu again after toggleOpen had.

componentContext now exposes getTrigger(), read at keydown time, and
its handleClick only reports onClick: toggleOpen has already opened
the menu. Each keyboard open from the trigger now reports onClick,
onRequestOpen and onOpen exactly once. 0.10.x (componentContext was
the component instance, so the trigger was always current) also
called onClick once, but went through handleOpen twice.

The new test presses Enter on the closed trigger over two open/close
rounds and fails without this change.
… width in state

The width measured on mount was kept only in initialStageWidthRef and
read during render for the item width. It is now state, set in the
mount effect next to stageWidth, so both updates batch into one
render.

That change made the effect that copied the panel offset into
translateX state report set-state-in-effect. translateX is now derived
during render from the width and currentPanel, which also drops a
render per panel change. Until the stage is measured it keeps the old
off-screen value (-1000000), so first paint is unchanged.
Portal kept its container node in portalNodeRef next to an isMounted
flag, and render read the ref to call createPortal. The attached node
is now state: the mount effect sets it where it used to set isMounted,
and render portals into it. The effects still use the ref. It is the
same single extra render as before, and under StrictMode the double
mount still removes the first node before creating the second.

The setState in the mount effect still reports set-state-in-effect;
the next commit documents why it stays.
The portal node has to be appended to its parent before the children
mount, because Dialog measures its target in its own effects and child
effects run before parent effects. Creating the node in render would
put a DOM side effect in render, and creating it later would mount
the children into a detached node. This is the "sync with an external
system" case the rule allows but cannot detect, so it gets an inline
disable with the reason, as docs/codebase-overview.md asks.
…ffect

An effect kept activeSelectedOption and its index inside selection
after a controlled parent shrank it (#3211). The clamp ran after
commit, so for one render no pill had tabindex=0. The index is now
clamped while rendering, and the active pill is read from selection
by index, so it is correct on the first render. The separate
activeSelectedOption state and its setters are gone; SelectedListbox
only reads the index.

The new test empties a controlled selection and fills it again, and
checks the first pill is tabbable. It passes before and after this
change and guards the refactor, as does the existing #3211 test.
…ons change

An effect moved the highlight (activeOptionIndex) to the highlighted
option's new position when options changed, or cleared it when the
option was gone. Until it ran, aria-activedescendant could point at
the wrong option or at one that no longer existed. The check now runs
during render and only sets state when the stored index is stale, so
it settles after one extra render and never commits a stale index.

It compares the option's position rather than the getOptions identity:
optionsSearchEntity and optionsAddItem default to new arrays on each
render, so getOptions changes every render.

The new test reorders the options under a highlighted option, then
removes it, and checks aria-activedescendant follows and then clears.
With the react-hooks/refs and set-state-in-effect warnings fixed, lint
is at 0 warnings, so npm run lint now runs with --max-warnings=0. The
ROADMAP P3 backlog entry is closed and its status row updated, and
AGENTS.md and the PR template no longer describe warnings as an
accepted backlog.
A popover that mounted open was never positioned, so it kept Dialog's
fallback `pointer-events: none` and nothing inside it could be clicked
(the ColorPicker "Menu Open" story, `walkthrough-on-anchor`).

Root cause: the container ref callback depended on an `inputRendered`
state that nothing read. Its first call set that state, which recreated
the callback, so React detached and re-attached the trigger ref in the
next commit. Dialog asks for its target element in that same commit,
got null, and skipped `createPopper`. Removing the unused state keeps
the callback stable.
Nothing called onRequestClose with { trigger: 'clickOutside' }. The
rewrite dropped the react-onclickoutside HOC, and the unused
handleClickOutside was later deleted, so a controlling parent (such as
ColorPicker) never learned about outside clicks. Popover now listens with
useClickOutside while open, treating its container and any portal
content carrying ignore-click-<id> as inside.
The stories and the doc-site example left isOpen to ColorPicker, so they
did not show the controlled model: the parent receives onRequestOpen and
onRequestClose and sets isOpen. Menu Open now starts with a controlled
isOpen instead of defaultIsOpen. onChange no longer passes the React event
to action(), which threw while serializing it and stopped OK before its
close request.
… Filter popover

A popover in a portal (position="overflowBoundaryElement", as Filter
uses) never received focus on open, so Escape, which the popover handles
on its own keydown, did nothing until the user clicked inside it. Portal
called onOpen in the effect that creates its node, before the children
had rendered into it; Dialog's handleOpen then found no dialog content
and skipped moving focus. Portal now calls onOpen in the commit after the
node exists, when the children are in the document.
The stories' footer buttons (Cancel, Save, OK, Back, Next, and so on)
had no onClick, so only Escape and the close button closed the modal.
ModalWithTrigger now passes its close handler to a footer render
function, and each story's buttons call it.
…xes controlled

Checkbox is controlled, and the example passed neither `checked` nor `onChange`,
so clicking a checkbox did nothing. Keep each cell's checkbox state and update
it from `data.checked`.
12c615e moved the trigger measurement into a layout effect keyed on
open state and align, so a trigger that resized while the tooltip stayed
open kept its old size and the nubbin sat off-centre. The old in-render
measurement ran every render; the layout effect now does too, and its
updater keeps the same state when the size is unchanged, so it settles.
The anchored nubbin hides the SLDS nubbin with a `#<id>:after` rule. An
id passed in that is not a CSS identifier (one that starts with a digit,
or holds a space or a quote) made the rule invalid, so the browser
dropped it and both nubbins showed. The rule now uses an `[id="…"]`
attribute selector, which matches any id.
@interactivellama
interactivellama marked this pull request as ready for review November 21, 2026 00:26
@interactivellama
interactivellama removed this pull request from stack #3359 November 23, 2026 07:57
@interactivellama
interactivellama removed this pull request from stack #3360 November 23, 2026 23:53
@interactivellama
interactivellama merged commit 456b1ce into master Nov 24, 2026
7 checks passed
@interactivellama
interactivellama deleted the fix/react-hooks-refs branch November 24, 2026 21:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants