Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Oct 23, 2025

Summary

Addresses a performance issue in the Popover.Content component where creating a new empty array on every render causes unnecessary re-renders of the useOnOutsideClick hook.

Problem

In PR #7063, the PopoverContent component was updated to support click-outside detection. However, the implementation created a new empty array on every render when ignoreClickRefs was undefined:

useOnOutsideClick({
  onClickOutside: onClickOutside ?? (() => null),
  containerRef: divRef,
  ignoreClickRefs: ignoreClickRefs ?? [],  // ❌ New array on every render
})

Since useOnOutsideClick includes ignoreClickRefs in its dependency array, this caused the callback to be recreated on every render, even when there were no actual changes.

Solution

Created a stable empty array reference at module level that is reused across renders:

// Stable empty array reference to avoid unnecessary re-renders
const EMPTY_IGNORE_CLICK_REFS: React.RefObject<HTMLElement>[] = []

// In the component:
useOnOutsideClick({
  onClickOutside: onClickOutside ?? (() => null),
  containerRef: divRef,
  ignoreClickRefs: ignoreClickRefs ?? EMPTY_IGNORE_CLICK_REFS,  // ✅ Stable reference
})

This ensures the same array reference is used when ignoreClickRefs is undefined, preventing unnecessary re-renders and callback recreations.

Additional Changes

  • Fixed a pre-existing lint error in Popover.features.stories.tsx (removed unused StoryFn import)

Related


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

@changeset-bot
Copy link

changeset-bot bot commented Oct 23, 2025

⚠️ No Changeset found

Latest commit: 2f2f7db

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

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

Copilot AI changed the title [WIP] WIP address feedback on popover click outside implementation fix(Popover): use stable empty array reference to prevent unnecessary re-renders Oct 23, 2025
Copilot AI requested a review from francinelucca October 23, 2025 14:55
@francinelucca francinelucca marked this pull request as ready for review October 23, 2025 15:06
@francinelucca francinelucca requested a review from a team as a code owner October 23, 2025 15:06
@francinelucca francinelucca merged commit b6be2d2 into feat/popover-implement-click-outside Oct 23, 2025
@francinelucca francinelucca deleted the copilot/stack-pr-7063 branch October 23, 2025 15:06
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