Skip to content

Follow up #8001 by deriving Autocomplete and LabelGroup state during render#8005

Open
Copilot wants to merge 7 commits into
copilot/derive-state-from-effectsfrom
copilot/copilotderive-state-from-effects
Open

Follow up #8001 by deriving Autocomplete and LabelGroup state during render#8005
Copilot wants to merge 7 commits into
copilot/derive-state-from-effectsfrom
copilot/copilotderive-state-from-effects

Conversation

Copilot AI commented Jun 17, 2026

Copy link
Copy Markdown
Contributor

Follow-up to #8001 for the two explicitly deferred “splittable later” sites only. This narrows two mixed-responsibility effects so derivable state updates happen during render, while real side effects remain in effects.

Changelog

New

  • None.

Changed

  • AutocompleteMenu

    • Split close-time reordering from onOpenChange.
    • Re-sort now happens during render on the showMenu: true -> false transition using the same previous-value pattern already used in SelectPanel.
    • onOpenChange remains in useEffect as the actual side effect.
  • LabelGroup

    • Numeric visibleChildCount truncation now derives visibilityMap from child indices instead of reading committed DOM state.
    • 'auto' truncation remains effect-driven via IntersectionObserver.
    • Added a shallow equality guard and memoized derived map to avoid redundant render-phase updates.
  • Tests

    • Tightened Autocomplete coverage to assert onOpenChange(false -> true -> false) across open/close.
    • Added LabelGroup rerender coverage for numeric truncation updates.

Example of the render-time transition pattern used here:

const [prevShowMenu, setPrevShowMenu] = useState(showMenu)

if (prevShowMenu !== showMenu) {
  setPrevShowMenu(showMenu)

  if (prevShowMenu && showMenu === false && !sortResultMatchesState) {
    setSortedItemIds(itemIdSortResult)
  }
}

Removed

  • Redundant effect-driven numeric truncation update path in LabelGroup.
  • The now-unneeded set-state-in-effect / derived-state suppression around Autocomplete close-time sorting.

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

Behavior-preserving internal refactor only; no API or visual change intended.

Testing & Reviewing

Review by change site:

  • Autocomplete

    • Confirm close-time sorting still happens only on open→closed transition.
    • Confirm onOpenChange still fires with the current open state and remains effect-driven.
  • LabelGroup

    • Confirm numeric visibleChildCount truncation updates correctly across rerenders.
    • Confirm 'auto' truncation, inline expand/collapse, and overlay overflow behavior are unchanged.

Validated with:

  • npm test -- --run packages/react/src/Autocomplete/Autocomplete.test.tsx packages/react/src/LabelGroup/LabelGroup.test.tsx
  • npx eslint --max-warnings=0 packages/react/src/Autocomplete/AutocompleteMenu.tsx packages/react/src/LabelGroup/LabelGroup.tsx packages/react/src/Autocomplete/Autocomplete.test.tsx packages/react/src/LabelGroup/LabelGroup.test.tsx
  • npm run type-check -w @primer/react
  • npm run build
  • npm test
  • npm run type-check
  • npm run lint
  • npm run lint:css
  • npm run format:diff

Merge checklist

@changeset-bot

changeset-bot Bot commented Jun 17, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 87741d5

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

Copilot AI and others added 2 commits June 17, 2026 12:43
Co-authored-by: mattcosta7 <8616962+mattcosta7@users.noreply.github.com>
Co-authored-by: mattcosta7 <8616962+mattcosta7@users.noreply.github.com>
Copilot AI changed the title [WIP] Refactor flagged suppressions to derive state during render Follow up #8001 by deriving Autocomplete and LabelGroup state during render Jun 17, 2026
Copilot AI requested a review from mattcosta7 June 17, 2026 12:52
@mattcosta7 mattcosta7 marked this pull request as ready for review June 17, 2026 15:24
Copilot AI review requested due to automatic review settings June 17, 2026 15:24
@mattcosta7 mattcosta7 requested a review from a team as a code owner June 17, 2026 15:24
@mattcosta7 mattcosta7 requested a review from llastflowers June 17, 2026 15:24
@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 Jun 17, 2026
@github-actions

Copy link
Copy Markdown
Contributor

⚠️ 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. Check the integration testing docs for step-by-step instructions. Or, apply the integration-tests: skipped manually label to skip these checks.

To publish a canary release for integration testing, apply the Canary Release label to this PR.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

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 is a follow-up refactor to #8001 that narrows two previously “splittable later” effect sites by deriving state during render for derivable transitions (while keeping real side effects in effects). The focus is Autocomplete close-time reordering and LabelGroup numeric truncation.

Changes:

  • AutocompleteMenu: Track showMenu transitions during render and trigger close-time re-sorting on the open→closed transition; keep onOpenChange in an effect.
  • LabelGroup: For numeric visibleChildCount, derive the truncation visibilityMap from child indices (not committed DOM) and add a shallow equality guard; keep 'auto' truncation effect-driven via IntersectionObserver.
  • Tests: Strengthen coverage for onOpenChange(false → true → false) and add rerender coverage for numeric LabelGroup truncation updates.
Show a summary per file
File Description
packages/react/src/LabelGroup/LabelGroup.tsx Derives numeric truncation visibility from child indices and updates render path to use a cached childArray.
packages/react/src/LabelGroup/LabelGroup.test.tsx Adds rerender assertions to ensure numeric truncation updates correctly when visibleChildCount changes.
packages/react/src/Autocomplete/AutocompleteMenu.tsx Moves close-time sorting to a render-time transition check while keeping onOpenChange effect-driven.
packages/react/src/Autocomplete/Autocomplete.test.tsx Tightens onOpenChange assertions to validate open/close sequencing.

Copilot's findings

  • Files reviewed: 4/4 changed files
  • Comments generated: 2

Comment thread packages/react/src/Autocomplete/AutocompleteMenu.tsx Outdated
Comment thread packages/react/src/LabelGroup/LabelGroup.tsx
@mattcosta7 mattcosta7 added the Canary Release Apply this label when you want CI to create a canary release of the current PR label Jun 23, 2026
@github-actions github-actions Bot temporarily deployed to storybook-preview-8005 June 23, 2026 15:03 Inactive
…; address review feedback

- AutocompleteMenu remounts on open/close (Overlay vs VisuallyHidden parent),
  so the prevShowMenu render transition never fired and the re-sort was dead
  code. Derive the sorted order during render while the menu is closed instead.
- LabelGroup: align numeric visibility guard with existing truthiness checks so
  visibleChildCount={0} no longer causes redundant render-phase updates.
- Add changeset for the Autocomplete/LabelGroup render-time derivation.
@github-actions github-actions Bot temporarily deployed to storybook-preview-8005 June 23, 2026 15:34 Inactive
@mattcosta7 mattcosta7 marked this pull request as ready for review June 23, 2026 15:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Canary Release Apply this label when you want CI to create a canary release of the current PR 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.

4 participants