Skip to content

perf(SelectPanel): memoize overlay props and callbacks#7621

Open
hectahertz wants to merge 2 commits intomainfrom
hectahertz/perf-select-panel-memo-context
Open

perf(SelectPanel): memoize overlay props and callbacks#7621
hectahertz wants to merge 2 commits intomainfrom
hectahertz/perf-select-panel-memo-context

Conversation

@hectahertz
Copy link
Contributor

Closes #

SelectPanel was creating several new object/function references on every render that got passed as props to AnchoredOverlay:

  • overlayProps: complex nested object with style, role, aria attributes, created inline every render
  • focusTrapSettings: new object every render
  • preventBubbling: curried function creating a new closure every render
  • closeButtonProps: inline object literal every render

Memoized all of these. Also refactored preventBubbling from a curried function to a direct useCallback, passing the overlay's onKeyDown through the dependency array instead of the closure.

Changelog

New

N/A

Changed

  • Memoized overlayProps, focusTrapSettings, and preventBubbling to stabilize references across renders
  • Hoisted closeButtonProps to a module-level constant

Removed

N/A

Rollout strategy

  • Patch release
  • Minor release
  • Major release; if selected, include a written rollout or migration plan
  • None; if selected, include a brief description as to why

Testing & Reviewing

Pure memoization refactor, no behavioral changes. The preventBubbling function was refactored from curried to direct callback, but the logic is identical.

Merge checklist

  • Added/updated tests
  • Added/updated documentation
  • Added/updated previews (Storybook)
  • Changes are SSR compatible
  • Tested in Chrome
  • Tested in Firefox
  • Tested in Safari
  • Tested in Edge
  • (GitHub staff only) Integration tests pass at github/github-ui

@hectahertz hectahertz requested a review from a team as a code owner March 4, 2026 12:31
@hectahertz hectahertz requested review from Copilot and jonrohan March 4, 2026 12:31
@changeset-bot
Copy link

changeset-bot bot commented Mar 4, 2026

🦋 Changeset detected

Latest commit: 82bab6d

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@primer/react Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@github-actions github-actions bot added the integration-tests: recommended This change needs to be tested for breaking changes. See https://arc.net/l/quote/tdmpakpm label Mar 4, 2026
@github-actions
Copy link
Contributor

github-actions bot commented Mar 4, 2026

⚠️ Action required

👋 Hi, this pull request contains changes to the source code that github/github-ui depends on. If you are GitHub staff, test these changes with github/github-ui using the integration workflow. Or, apply the integration-tests: skipped manually label to skip these checks.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR refactors SelectPanel to reduce unnecessary allocations by memoizing several props/callbacks passed into AnchoredOverlay, helping stabilize references across renders.

Changes:

  • Memoizes focusTrapSettings and consolidated overlayProps into a mergedOverlayProps useMemo.
  • Refactors preventBubbling from a curried function to a direct useCallback.
  • Hoists closeButtonProps to a module-level constant and adds a patch changeset.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
packages/react/src/SelectPanel/SelectPanel.tsx Memoizes overlay-related props/callbacks to reduce re-renders/allocations when passing props to AnchoredOverlay.
.changeset/perf-select-panel-memo-context.md Adds a patch changeset describing the perf-focused memoization refactor.
Comments suppressed due to low confidence (1)

packages/react/src/SelectPanel/SelectPanel.tsx:803

  • The double cast in overlayProps?.onKeyDown?.(event as unknown as React.KeyboardEvent<HTMLDivElement>) looks unnecessary because event is already a React.KeyboardEvent<HTMLDivElement>. If there’s a typing mismatch, it would be clearer to type/narrow overlayProps.onKeyDown (or the extracted overlayOnKeyDown) rather than casting the event to unknown.
    (event: React.KeyboardEvent<HTMLDivElement>) => {
      overlayProps?.onKeyDown?.(event as unknown as React.KeyboardEvent<HTMLDivElement>)

Comment on lines 800 to +821
@@ -817,7 +816,35 @@ function Panel({

// if this is a typeahead event, don't propagate outside of menu
event.stopPropagation()
}
},
[overlayProps],
)
Copy link

Copilot AI Mar 4, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

preventBubbling closes over the entire overlayProps object and lists it in the dependency array. Since only overlayProps.onKeyDown is used, this will recreate the callback whenever any other overlay prop changes (defeating the memoization goal). Consider extracting const overlayOnKeyDown = overlayProps?.onKeyDown and depending on that instead (and call that inside the handler).

This issue also appears on line 801 of the same file.

Copilot uses AI. Check for mistakes.
} as React.CSSProperties,
onKeyDown: preventBubbling,
}),
[titleId, subtitle, subtitleId, overlayProps, variant, isKeyboardVisible, availablePanelHeight, preventBubbling],
Copy link

Copilot AI Mar 4, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mergedOverlayProps only uses subtitle for a truthiness check to decide whether to set aria-describedby. Depending on the full subtitle value can cause recomputation when callers pass a new ReactElement each render even though the boolean presence hasn’t changed. Consider using Boolean(subtitle) (or subtitle != null) in the deps instead of subtitle to keep the memo effective.

Suggested change
[titleId, subtitle, subtitleId, overlayProps, variant, isKeyboardVisible, availablePanelHeight, preventBubbling],
[titleId, Boolean(subtitle), subtitleId, overlayProps, variant, isKeyboardVisible, availablePanelHeight, preventBubbling],

Copilot uses AI. Check for mistakes.
Copy link
Member

@jonrohan jonrohan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems good, would be worth checking out integration tests also

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

integration-tests: recommended This change needs to be tested for breaking changes. See https://arc.net/l/quote/tdmpakpm

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants