Revert "Replace useRefObjectAsForwardedRef with useMergedRefs internally"#7737
Revert "Replace useRefObjectAsForwardedRef with useMergedRefs internally"#7737liuliu-dev wants to merge 1 commit intomainfrom
useRefObjectAsForwardedRef with useMergedRefs internally"#7737Conversation
…ernally …" This reverts commit f04e85d.
|
|
There was a problem hiding this comment.
Pull request overview
Reverts #7638 by switching internal “combined refs” usage back from useMergedRefs to useRefObjectAsForwardedRef across multiple components in @primer/react.
Changes:
- Replace
useMergedRefsusage withuseRefObjectAsForwardedRefand pass local ref objects directly to underlying elements. - Update
useMergedRefsdocs/tests variable naming (mergedRef→combinedRef). - Remove the changeset that described the (now-reverted) internal ref-handling update.
Show a summary per file
| File | Description |
|---|---|
| packages/react/src/TextInputWithTokens/TextInputWithTokens.tsx | Reverts forwarded-ref wiring back to useRefObjectAsForwardedRef + local ref. |
| packages/react/src/PageLayout/PageLayout.tsx | Reverts Pane/Sidebar forwarded-ref wiring back to useRefObjectAsForwardedRef. |
| packages/react/src/Overlay/Overlay.tsx | Reverts overlay ref wiring back to useRefObjectAsForwardedRef. |
| packages/react/src/Link/Link.tsx | Reverts ref wiring and introduces a ref cast to satisfy polymorphic typing. |
| packages/react/src/hooks/useMergedRefs.ts | Renames variables in JSDoc examples (mergedRef → combinedRef). |
| packages/react/src/hooks/tests/useMergedRefs.test.tsx | Renames test variable usage accordingly. |
| packages/react/src/Heading/Heading.tsx | Reverts forwarded-ref wiring back to useRefObjectAsForwardedRef. |
| packages/react/src/Dialog/Dialog.tsx | Reverts forwarded-ref wiring back to useRefObjectAsForwardedRef. |
| packages/react/src/deprecated/DialogV1/Dialog.tsx | Reverts forwarded-ref wiring back to useRefObjectAsForwardedRef. |
| packages/react/src/Button/ButtonBase.tsx | Reverts forwarded-ref wiring and changes ref assignment on polymorphic component. |
| packages/react/src/Autocomplete/AutocompleteOverlay.tsx | Reverts combined ref handling for overlay scroll container. |
| packages/react/src/Autocomplete/AutocompleteInput.tsx | Reverts forwarded-ref wiring back to useRefObjectAsForwardedRef. |
| packages/react/src/Autocomplete/Autocomplete.test.tsx | Removes an explicit focus call in a menu-open test. |
| packages/react/src/ActionList/Heading.tsx | Reverts forwarded-ref wiring back to useRefObjectAsForwardedRef. |
| packages/react/src/ActionBar/ActionBar.tsx | Reverts forwarded-ref wiring back to useRefObjectAsForwardedRef for icon button. |
| .changeset/combined-refs-hook.md | Deletes the changeset introduced by the reverted PR. |
Copilot's findings
- Files reviewed: 16/16 changed files
- Comments generated: 4
| data-inline={inline} | ||
| data-hover-color={hoverColor} | ||
| {...restProps} | ||
| ref={mergedRef} | ||
| // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
| ref={innerRef as any} |
There was a problem hiding this comment.
ref={innerRef as any} (and disabling the lint rule) bypasses type-safety for the polymorphic as prop and can mask cases where the rendered component doesn’t support refs. Prefer keeping the ref fully typed (e.g., by using useMergedRefs again, or by expressing the polymorphic ref type explicitly) so TS can enforce that ref is valid for As without resorting to any.
| // @ts-ignore temporary disable as we migrate to css modules, until we remove PolymorphicForwardRefComponent | ||
| ref={innerRef} |
There was a problem hiding this comment.
Avoid introducing @ts-ignore on the ref assignment here; it hides a real typing mismatch between the polymorphic as element and innerRef’s declared element type. Consider typing innerRef to match the union of supported elements (e.g., button/anchor) or switching back to useMergedRefs so forwardedRef can be attached without suppressing TypeScript errors.
| // @ts-ignore temporary disable as we migrate to css modules, until we remove PolymorphicForwardRefComponent | |
| ref={innerRef} | |
| ref={ | |
| Component === 'a' | |
| ? (innerRef as React.Ref<HTMLAnchorElement>) | |
| : (innerRef as React.Ref<HTMLButtonElement>) | |
| } |
| const inputNode = getByLabelText(AUTOCOMPLETE_LABEL) | ||
|
|
||
| expect(inputNode.getAttribute('aria-expanded')).not.toBe('true') | ||
| inputNode.focus() | ||
| fireEvent.click(inputNode) | ||
| fireEvent.keyDown(inputNode, {key: 'ArrowDown'}) | ||
|
|
There was a problem hiding this comment.
Removing inputNode.focus() means this test now relies on fireEvent.click(inputNode) to focus/open the menu, which can make it pass even if the intended “ArrowDown when focused opens menu” behavior regresses (click may open the menu by itself). To keep the assertion targeted, focus the input (or assert the menu remains closed after click) before the ArrowDown keyDown.
| const featureFlagMaxHeightClampToViewport = useFeatureFlag('primer_react_overlay_max_height_clamp_to_viewport') | ||
| const overlayRef = useRef<HTMLDivElement>(null) | ||
| const mergedRef = useMergedRefs(forwardedRef, overlayRef) | ||
| useRefObjectAsForwardedRef(forwardedRef, overlayRef) | ||
| const slideAnimationDistance = 8 // var(--base-size-8), hardcoded to do some math |
There was a problem hiding this comment.
Switching back to useRefObjectAsForwardedRef reintroduces its known behavior of invoking callback refs on every re-render (see useRefObjectAsForwardedRef tests’ TODO), which can cause unnecessary work/side effects and is especially risky with React 19 callback refs that return cleanup functions. Prefer useMergedRefs here so refs are only updated on mount/unmount/target changes and React 19 cleanup refs are handled correctly.
|
👋 Hi from github/github-ui! Your integration PR is ready: https://github.com/github/github-ui/pull/18110 |
|
Integration test results from github/github-ui:
CI check runs linting, type checking, and unit tests. Check the workflow logs for specific failures. Need help? If you believe this failure is unrelated to your changes, please reach out to the Primer team for assistance. |
Reverts #7638